From: Jess Bees Date: Mon, 27 Oct 2025 14:44:39 +0000 (-0400) Subject: Limit dynamic metric types to a set of valid strings X-Git-Tag: rec-5.4.0-alpha1~40^2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b4eec9b004f26fc1fe2f638c6a480820fa6ad9ad;p=thirdparty%2Fpdns.git Limit dynamic metric types to a set of valid strings Basing this on code from dnsdist, it's limited to "counter" and "gauge" types. Signed-off-by: Jess Bees --- diff --git a/pdns/recursordist/rec_channel.hh b/pdns/recursordist/rec_channel.hh index 78ecd9d06d..3ca29eb54f 100644 --- a/pdns/recursordist/rec_channel.hh +++ b/pdns/recursordist/rec_channel.hh @@ -31,6 +31,7 @@ #include "iputils.hh" #include "dnsname.hh" #include "sholder.hh" +#include "rec_metrics.hh" #include extern GlobalStateHolder g_dontThrottleNames; @@ -104,7 +105,7 @@ struct StatsMapEntry { std::string d_prometheusName; std::string d_value; - std::optional d_prometheusTypeName = std::nullopt; + std::optional d_prometheusType = std::nullopt; std::optional d_prometheusDescr = std::nullopt; }; diff --git a/pdns/recursordist/rec_channel_rec.cc b/pdns/recursordist/rec_channel_rec.cc index 2610db82a3..70474fb5f9 100644 --- a/pdns/recursordist/rec_channel_rec.cc +++ b/pdns/recursordist/rec_channel_rec.cc @@ -118,7 +118,7 @@ struct dynmetrics { std::atomic* d_ptr; std::string d_prometheusName; - std::optional d_prometheusTypeName; + std::optional d_prometheusType; std::optional d_prometheusDescr; }; @@ -199,16 +199,23 @@ std::atomic* initDynMetric(const std::string& str, const std::str name = getPrometheusName(name); } - std::optional typeName; + std::optional metricType; if (!prometheusTypeName.empty()) { - typeName = std::optional(std::move(prometheusTypeName)); + static const std::map namesToTypes = { + {"counter", PrometheusMetricType::counter}, + {"gauge", PrometheusMetricType::gauge}, + }; + auto realtype = namesToTypes.find(prometheusTypeName); + if (realtype != namesToTypes.end()) { + metricType = std::optional(realtype->second); + } } std::optional descr; if (!prometheusDescr.empty()) { descr = std::optional(std::move(prometheusDescr)); } - auto ret = dynmetrics{new std::atomic(), std::move(name), typeName, descr}; + auto ret = dynmetrics{new std::atomic(), std::move(name), metricType, descr}; (*locked)[str] = ret; return ret.d_ptr; } @@ -274,7 +281,7 @@ StatsMap getAllStatsMap(StatComponent component) { for (const auto& value : *(d_dynmetrics.lock())) { if (disabledlistMap.count(value.first) == 0) { - ret.emplace(value.first, StatsMapEntry{value.second.d_prometheusName, std::to_string(*value.second.d_ptr), value.second.d_prometheusTypeName, value.second.d_prometheusDescr}); + ret.emplace(value.first, StatsMapEntry{value.second.d_prometheusName, std::to_string(*value.second.d_ptr), value.second.d_prometheusType, value.second.d_prometheusDescr}); } } } diff --git a/pdns/recursordist/ws-recursor.cc b/pdns/recursordist/ws-recursor.cc index f2fa083fa0..43aa30dbaf 100644 --- a/pdns/recursordist/ws-recursor.cc +++ b/pdns/recursordist/ws-recursor.cc @@ -577,8 +577,9 @@ static void prometheusMetrics(HttpRequest* /* req */, HttpResponse* resp) helpname = helpname.substr(0, helpname.length() - 6); } } else { - if (tup.second.d_prometheusTypeName) { - prometheusTypeName = *tup.second.d_prometheusTypeName; + if (tup.second.d_prometheusType) { + prometheusTypeName = MetricDefinitionStorage::getPrometheusStringMetricType( + *tup.second.d_prometheusType); } if (tup.second.d_prometheusDescr) { prometheusDescr = *tup.second.d_prometheusDescr;