]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: add options to `declareMetric` to skip initialization if labels are used
authorEnsar Sarajčić <dev@ensarsarajcic.com>
Thu, 12 Dec 2024 15:01:14 +0000 (16:01 +0100)
committerEnsar Sarajčić <dev@ensarsarajcic.com>
Thu, 12 Dec 2024 15:01:14 +0000 (16:01 +0100)
pdns/dnsdistdist/dnsdist-lua-ffi.cc
pdns/dnsdistdist/dnsdist-lua.cc
pdns/dnsdistdist/dnsdist-metrics.cc
pdns/dnsdistdist/dnsdist-metrics.hh

index c7a0c055f6825b0abfc1d773ed5a762678031ce8..d6991323b090c82aa23875ba85bb202e952b2b01 100644 (file)
@@ -1805,7 +1805,8 @@ bool dnsdist_ffi_metric_declare(const char* name, size_t nameLen, const char* ty
   if (name == nullptr || nameLen == 0 || type == nullptr || description == nullptr) {
     return false;
   }
-  auto result = dnsdist::metrics::declareCustomMetric(name, type, description, customName != nullptr ? std::optional<std::string>(customName) : std::nullopt);
+  // TODO: add labels options?
+  auto result = dnsdist::metrics::declareCustomMetric(name, type, description, customName != nullptr ? std::optional<std::string>(customName) : std::nullopt, false);
   return !result;
 }
 
index 53e1038c135c6cf5cfa1824349dacadb2a47f3e7..cf366cb69987a1a6d6e3024363ca203d39d8872d 100644 (file)
@@ -85,7 +85,8 @@
 
 using std::thread;
 
-using update_metric_opts_t = LuaAssociativeTable<boost::variant<uint64_t, double, LuaAssociativeTable<std::string>>>;
+using update_metric_opts_t = LuaAssociativeTable<boost::variant<uint64_t, LuaAssociativeTable<std::string>>>;
+using declare_metric_opts_t = LuaAssociativeTable<boost::variant<bool, std::string>>;
 
 static boost::tribool s_noLuaSideEffect;
 
@@ -3412,8 +3413,22 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck)
     newThread.detach();
   });
 
-  luaCtx.writeFunction("declareMetric", [](const std::string& name, const std::string& type, const std::string& description, boost::optional<std::string> customName) {
-    auto result = dnsdist::metrics::declareCustomMetric(name, type, description, customName ? std::optional<std::string>(*customName) : std::nullopt);
+  luaCtx.writeFunction("declareMetric", [](const std::string& name, const std::string& type, const std::string& description, boost::optional<boost::variant<std::string, boost::optional<declare_metric_opts_t>>> opts) {
+    bool withLabels = false;
+    std::optional<std::string> customName = std::nullopt;
+    if (opts) {
+      auto* optCustomName = boost::get<std::string>(&opts.get());
+      if (optCustomName) {
+        customName = std::optional(*optCustomName);
+      }
+      if (!customName) {
+        boost::optional<declare_metric_opts_t> vars = boost::get<boost::optional<declare_metric_opts_t>>(opts.get());
+        getOptionalValue<std::string>(vars, "customName", customName);
+        getOptionalValue<bool>(vars, "withLabels", withLabels);
+        checkAllParametersConsumed("declareMetric", vars);
+      }
+    }
+    auto result = dnsdist::metrics::declareCustomMetric(name, type, description, customName, withLabels);
     if (result) {
       g_outputBuffer += *result + "\n";
       errlog("Error in declareMetric: %s", *result);
index 039b1ffc89249d49ccecf387beb3bbee3dfb71d8..18ac63aab2263bba69f677917bdd11811b4a6ca0 100644 (file)
@@ -169,7 +169,7 @@ Stats::Stats() :
 
 struct Stats g_stats;
 
-std::optional<std::string> declareCustomMetric(const std::string& name, const std::string& type, const std::string& description, std::optional<std::string> customName)
+std::optional<std::string> declareCustomMetric(const std::string& name, const std::string& type, const std::string& description, std::optional<std::string> customName, bool withLabels)
 {
   if (!std::regex_match(name, std::regex("^[a-z0-9-]+$"))) {
     return std::string("Unable to declare metric '") + std::string(name) + std::string("': invalid name\n");
@@ -180,6 +180,10 @@ std::optional<std::string> declareCustomMetric(const std::string& name, const st
     auto customCounters = s_customCounters.write_lock();
     auto itp = customCounters->emplace(name, std::map<std::string, MutableCounter>());
     if (itp.second) {
+      if (!withLabels) {
+        auto counter = itp.first->second.emplace("", MutableCounter());
+        g_stats.entries.write_lock()->emplace_back(Stats::EntryTriple{name, "", &counter.first->second.d_value});
+      }
       dnsdist::prometheus::PrometheusMetricDefinition def{name, type, description, finalCustomName};
       dnsdist::webserver::addMetricDefinition(def);
     }
@@ -188,6 +192,10 @@ std::optional<std::string> declareCustomMetric(const std::string& name, const st
     auto customGauges = s_customGauges.write_lock();
     auto itp = customGauges->emplace(name, std::map<std::string, MutableGauge>());
     if (itp.second) {
+      if (!withLabels) {
+        auto gauge = itp.first->second.emplace("", MutableGauge());
+        g_stats.entries.write_lock()->emplace_back(Stats::EntryTriple{name, "", &gauge.first->second.d_value});
+      }
       dnsdist::prometheus::PrometheusMetricDefinition def{name, type, description, finalCustomName};
       dnsdist::webserver::addMetricDefinition(def);
     }
index 8afb67509d20a56b9f932727d07601e1d0559555..6cab90527284c2080ccf08887a54a05efb4c3a0a 100644 (file)
@@ -35,7 +35,7 @@ namespace dnsdist::metrics
 {
 using Error = std::string;
 
-[[nodiscard]] std::optional<Error> declareCustomMetric(const std::string& name, const std::string& type, const std::string& description, std::optional<std::string> customName);
+[[nodiscard]] std::optional<Error> declareCustomMetric(const std::string& name, const std::string& type, const std::string& description, std::optional<std::string> customName, bool withLabels);
 [[nodiscard]] std::variant<uint64_t, Error> incrementCustomCounter(const std::string_view& name, uint64_t step, const std::unordered_map<std::string, std::string>& labels);
 [[nodiscard]] std::variant<uint64_t, Error> decrementCustomCounter(const std::string_view& name, uint64_t step, const std::unordered_map<std::string, std::string>& labels);
 [[nodiscard]] std::variant<double, Error> setCustomGauge(const std::string_view& name, const double value, const std::unordered_map<std::string, std::string>& labels);