From: Ensar Sarajčić Date: Thu, 12 Dec 2024 15:01:14 +0000 (+0100) Subject: dnsdist: add options to `declareMetric` to skip initialization if labels are used X-Git-Tag: dnsdist-2.0.0-alpha0^2~14 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0dff6be4145aadd48cfd9f20f6ac48ac8b3d08df;p=thirdparty%2Fpdns.git dnsdist: add options to `declareMetric` to skip initialization if labels are used --- diff --git a/pdns/dnsdistdist/dnsdist-lua-ffi.cc b/pdns/dnsdistdist/dnsdist-lua-ffi.cc index c7a0c055f6..d6991323b0 100644 --- a/pdns/dnsdistdist/dnsdist-lua-ffi.cc +++ b/pdns/dnsdistdist/dnsdist-lua-ffi.cc @@ -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(customName) : std::nullopt); + // TODO: add labels options? + auto result = dnsdist::metrics::declareCustomMetric(name, type, description, customName != nullptr ? std::optional(customName) : std::nullopt, false); return !result; } diff --git a/pdns/dnsdistdist/dnsdist-lua.cc b/pdns/dnsdistdist/dnsdist-lua.cc index 53e1038c13..cf366cb699 100644 --- a/pdns/dnsdistdist/dnsdist-lua.cc +++ b/pdns/dnsdistdist/dnsdist-lua.cc @@ -85,7 +85,8 @@ using std::thread; -using update_metric_opts_t = LuaAssociativeTable>>; +using update_metric_opts_t = LuaAssociativeTable>>; +using declare_metric_opts_t = LuaAssociativeTable>; 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 customName) { - auto result = dnsdist::metrics::declareCustomMetric(name, type, description, customName ? std::optional(*customName) : std::nullopt); + luaCtx.writeFunction("declareMetric", [](const std::string& name, const std::string& type, const std::string& description, boost::optional>> opts) { + bool withLabels = false; + std::optional customName = std::nullopt; + if (opts) { + auto* optCustomName = boost::get(&opts.get()); + if (optCustomName) { + customName = std::optional(*optCustomName); + } + if (!customName) { + boost::optional vars = boost::get>(opts.get()); + getOptionalValue(vars, "customName", customName); + getOptionalValue(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); diff --git a/pdns/dnsdistdist/dnsdist-metrics.cc b/pdns/dnsdistdist/dnsdist-metrics.cc index 039b1ffc89..18ac63aab2 100644 --- a/pdns/dnsdistdist/dnsdist-metrics.cc +++ b/pdns/dnsdistdist/dnsdist-metrics.cc @@ -169,7 +169,7 @@ Stats::Stats() : struct Stats g_stats; -std::optional declareCustomMetric(const std::string& name, const std::string& type, const std::string& description, std::optional customName) +std::optional declareCustomMetric(const std::string& name, const std::string& type, const std::string& description, std::optional 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 declareCustomMetric(const std::string& name, const st auto customCounters = s_customCounters.write_lock(); auto itp = customCounters->emplace(name, std::map()); 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 declareCustomMetric(const std::string& name, const st auto customGauges = s_customGauges.write_lock(); auto itp = customGauges->emplace(name, std::map()); 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); } diff --git a/pdns/dnsdistdist/dnsdist-metrics.hh b/pdns/dnsdistdist/dnsdist-metrics.hh index 8afb67509d..6cab905272 100644 --- a/pdns/dnsdistdist/dnsdist-metrics.hh +++ b/pdns/dnsdistdist/dnsdist-metrics.hh @@ -35,7 +35,7 @@ namespace dnsdist::metrics { using Error = std::string; -[[nodiscard]] std::optional declareCustomMetric(const std::string& name, const std::string& type, const std::string& description, std::optional customName); +[[nodiscard]] std::optional declareCustomMetric(const std::string& name, const std::string& type, const std::string& description, std::optional customName, bool withLabels); [[nodiscard]] std::variant incrementCustomCounter(const std::string_view& name, uint64_t step, const std::unordered_map& labels); [[nodiscard]] std::variant decrementCustomCounter(const std::string_view& name, uint64_t step, const std::unordered_map& labels); [[nodiscard]] std::variant setCustomGauge(const std::string_view& name, const double value, const std::unordered_map& labels);