]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Limit dynamic metric types to a set of valid strings
authorJess Bees <jesse@toomanybees.com>
Mon, 27 Oct 2025 14:44:39 +0000 (10:44 -0400)
committerJess Bees <jesse@toomanybees.com>
Mon, 27 Oct 2025 14:49:56 +0000 (10:49 -0400)
Basing this on code from dnsdist, it's limited to "counter" and "gauge"
types.

Signed-off-by: Jess Bees <jesse@toomanybees.com>
pdns/recursordist/rec_channel.hh
pdns/recursordist/rec_channel_rec.cc
pdns/recursordist/ws-recursor.cc

index 78ecd9d06d9f0b39b1bae89b74713d46c737241a..3ca29eb54f8143e498669a606d4167f182f90a95 100644 (file)
@@ -31,6 +31,7 @@
 #include "iputils.hh"
 #include "dnsname.hh"
 #include "sholder.hh"
+#include "rec_metrics.hh"
 #include <atomic>
 
 extern GlobalStateHolder<SuffixMatchNode> g_dontThrottleNames;
@@ -104,7 +105,7 @@ struct StatsMapEntry
 {
   std::string d_prometheusName;
   std::string d_value;
-  std::optional<std::string> d_prometheusTypeName = std::nullopt;
+  std::optional<PrometheusMetricType> d_prometheusType = std::nullopt;
   std::optional<std::string> d_prometheusDescr = std::nullopt;
 };
 
index 2610db82a3f619238c6f14588e47db9e9108118e..70474fb5f9627d05c541de8a2aa93f6bd51c3afe 100644 (file)
@@ -118,7 +118,7 @@ struct dynmetrics
 {
   std::atomic<unsigned long>* d_ptr;
   std::string d_prometheusName;
-  std::optional<string> d_prometheusTypeName;
+  std::optional<PrometheusMetricType> d_prometheusType;
   std::optional<string> d_prometheusDescr;
 };
 
@@ -199,16 +199,23 @@ std::atomic<unsigned long>* initDynMetric(const std::string& str, const std::str
     name = getPrometheusName(name);
   }
 
-  std::optional<std::string> typeName;
+  std::optional<PrometheusMetricType> metricType;
   if (!prometheusTypeName.empty()) {
-    typeName = std::optional(std::move(prometheusTypeName));
+    static const std::map<std::string, PrometheusMetricType> namesToTypes = {
+      {"counter", PrometheusMetricType::counter},
+      {"gauge",   PrometheusMetricType::gauge},
+    };
+    auto realtype = namesToTypes.find(prometheusTypeName);
+    if (realtype != namesToTypes.end()) {
+      metricType = std::optional(realtype->second);
+    }
   }
   std::optional<std::string> descr;
   if (!prometheusDescr.empty()) {
     descr = std::optional(std::move(prometheusDescr));
   }
 
-  auto ret = dynmetrics{new std::atomic<unsigned long>(), std::move(name), typeName, descr};
+  auto ret = dynmetrics{new std::atomic<unsigned long>(), 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});
       }
     }
   }
index f2fa083fa0303d78208e337beffb4e7efa12fae5..43aa30dbafc45711c2a66e7998c6ef0371888ef2 100644 (file)
@@ -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;