]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Use a variant of string/unordered map as argument
authorJess Bees <jesse@toomanybees.com>
Tue, 28 Oct 2025 20:19:04 +0000 (16:19 -0400)
committerJess Bees <jesse@toomanybees.com>
Wed, 29 Oct 2025 17:47:42 +0000 (13:47 -0400)
Using a Lua table for an argument is more intuitive than having an
optional 2nd argument, and now `initMetric`'s signature is actually
compatible with `getMetric`'s, rather than just being inspired by it.

Signed-off-by: Jess Bees <jesse@toomanybees.com>
pdns/recursordist/lua-recursor4.cc

index b8a030fcad8157a47775827670ff39aa1a6d0d62..2c0f801a889a8eee195dfa07c763189ed908ab49 100644 (file)
@@ -430,20 +430,21 @@ void RecursorLua4::postPrepareContext() // NOLINT(readability-function-cognitive
 
   d_pd.emplace_back("now", &g_now);
 
-  d_lw->writeFunction("initMetric", [](const std::string& str, const std::string& maybePrometheusName, const std::string& maybePrometheusTypeName, boost::optional<std::string> maybeDescr){
-    // Shift arguments, because it's actually the second `prometheusName` argument which is optional
-    // to match the optional `prometheusName` in `getMetric`.
+  d_lw->writeFunction("initMetric", [](const std::string& str, boost::optional<boost::variant<std::string, std::unordered_map<std::string, std::string>>> opts){
     std::string prometheusName;
     std::string prometheusTypeName;
     std::string prometheusDescr;
-    if (maybeDescr) {
-      prometheusName = maybePrometheusName;
-      prometheusTypeName = maybePrometheusTypeName;
-      prometheusDescr = *maybeDescr;
-    } else {
-      prometheusDescr = prometheusTypeName;
-      prometheusTypeName = prometheusName;
-      prometheusName = "";
+
+    if (opts) {
+      auto* optPrometheusName = boost::get<std::string>(&opts.get());
+      if (optPrometheusName != nullptr) {
+        prometheusName = *optPrometheusName;
+      } else {
+        boost::optional<std::unordered_map<std::string, std::string>> vars = {boost::get<std::unordered_map<std::string, std::string>>(opts.get())};
+        prometheusName = (*vars)["prometheusName"];
+        prometheusTypeName = (*vars)["type"];
+        prometheusDescr = (*vars)["description"];
+      }
     }
 
     return DynMetric{initDynMetric(str, prometheusName, prometheusTypeName, prometheusDescr)};