]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Modify per-thread cpu usage stats to be Prometheus-friendly. 10554/head
authorOtto <otto.moerbeek@open-xchange.com>
Wed, 30 Jun 2021 08:27:04 +0000 (10:27 +0200)
committerOtto <otto.moerbeek@open-xchange.com>
Wed, 18 Aug 2021 07:47:43 +0000 (09:47 +0200)
Plus fix a few small issues wrt HELP texts.

Example output:

pdns_recursor_cpu_msec{thread=0} 10
pdns_recursor_cpu_msec{thread=1} 0
pdns_recursor_cpu_msec{thread=2} 0

pdns/rec_channel_rec.cc
pdns/recursordist/rec_metrics.hh
pdns/ws-recursor.cc

index 0c8928836ff952a117781f4e21e1019c64968917..53887cbd4d7924b0df1ed50014eb5f7f3a22265f 100644 (file)
@@ -1132,6 +1132,19 @@ static StatsMap toStatsMap(const string& name, const pdns::AtomicHistogram& hist
   return entries;
 }
 
+static StatsMap toCPUStatsMap(const string& name)
+{
+  const string pbasename = getPrometheusName(name);
+  StatsMap entries;
+
+  for (unsigned int n = 0; n < g_numThreads; ++n) {
+    uint64_t tm = doGetThreadCPUMsec(n);
+    std::string pname = pbasename + "{thread=" + std::to_string(n) + '}';
+    entries.emplace(make_pair(name + "-thread-" + std::to_string(n), StatsMapEntry{pname, std::to_string(tm)}));
+  }
+  return entries;
+}
+
 extern ResponseStats g_rs;
 
 static void registerAllStats1()
@@ -1277,9 +1290,7 @@ static void registerAllStats1()
   addGetStat("cpu-steal", []{ return getCPUSteal(string()); });
 #endif
 
-  for (unsigned int n = 0; n < g_numThreads; ++n) {
-    addGetStat("cpu-msec-thread-" + std::to_string(n), [n]{ return doGetThreadCPUMsec(n);});
-  }
+  addGetStat("cpu-msec", []() { return toCPUStatsMap("cpu-msec"); });
 
 #ifdef MALLOC_TRACE
   addGetStat("memory-allocs", []{ return g_mtracer->getAllocs(string()); });
index 59921825a25a53fb207b1216bdbf1e41bf79d7ca..4ece7b49fead668942c994fae944d6b0639f67b2 100644 (file)
@@ -34,7 +34,8 @@ enum class PrometheusMetricType : int
 {
   counter = 1,
   gauge = 2,
-  histogram = 3
+  histogram = 3,
+  multicounter = 4
 };
 
 // Keeps additional information about metrics
@@ -83,6 +84,9 @@ public:
     case PrometheusMetricType::histogram:
       return "histogram";
       break;
+    case PrometheusMetricType::multicounter:
+      return "multicounter";
+      break;
     default:
       return "";
       break;
index 181a0f318ebcbd2cc306fe8fc6b84127b9908af9..8d53a13217adc7aa720b21b893943c92a1cfd667 100644 (file)
@@ -455,16 +455,21 @@ static void prometheusMetrics(HttpRequest *req, HttpResponse *resp) {
           if (prometheusTypeName.empty()) {
             continue;
           }
-          if (metricDetails.prometheusType == PrometheusMetricType::histogram) {
+          if (metricDetails.prometheusType == PrometheusMetricType::multicounter) {
+            string shortname = prometheusMetricName.substr(0, prometheusMetricName.find('{'));
+            output << "# HELP " << shortname << " " << metricDetails.description << "\n";
+            output << "# TYPE " << shortname << " " << "counter" << "\n";
+          }
+          else if (metricDetails.prometheusType == PrometheusMetricType::histogram) {
             // name is XXX_count, strip the _count part
-            prometheusMetricName = prometheusMetricName.substr(0, prometheusMetricName.length() - 6);
+            string shortname = prometheusMetricName.substr(0, prometheusMetricName.length() - 6);
+            output << "# HELP " << shortname << " " << metricDetails.description << "\n";
+            output << "# TYPE " << shortname << " " << prometheusTypeName << "\n";
+          } else {
+            // for these we have the help and types encoded in the sources:
             output << "# HELP " << prometheusMetricName << " " << metricDetails.description << "\n";
             output << "# TYPE " << prometheusMetricName << " " << prometheusTypeName << "\n";
-            continue;
           }
-          // for these we have the help and types encoded in the sources:
-          output << "# HELP " << prometheusMetricName << " " << metricDetails.description << "\n";
-          output << "# TYPE " << prometheusMetricName << " " << prometheusTypeName << "\n";
         }
         output << prometheusMetricName << " " << tup.second.d_value << "\n";
     }
@@ -593,9 +598,12 @@ const std::map<std::string, MetricDefinition> MetricDefinitionStorage::metrics =
   {"concurrent-queries",
    MetricDefinition(PrometheusMetricType::gauge,
                     "Number of MThreads currently running")},
+
+  // For multicounters, state the first
   {"cpu-msec-thread-0",
-   MetricDefinition(PrometheusMetricType::counter,
+   MetricDefinition(PrometheusMetricType::multicounter,
                     "Number of milliseconds spent in thread n")},
+
   {"dnssec-authentic-data-queries",
    MetricDefinition(PrometheusMetricType::counter,
                     "Number of queries received with the AD bit set")},
@@ -1037,6 +1045,25 @@ const std::map<std::string, MetricDefinition> MetricDefinitionStorage::metrics =
   { "dns64-prefix-answers",
     MetricDefinition(PrometheusMetricType::counter,
                      "Number of AAAA and PTR generated by a matching dns64-prefix")},
+  { "aggressive-nsec-cache-entries",
+    MetricDefinition(PrometheusMetricType::counter,
+                     "Number of entries in the aggressive NSEC cache")},
+
+  { "aggressive-nsec-cache-nsec-hits",
+    MetricDefinition(PrometheusMetricType::counter,
+                     "Number of NSEC-related hits from the aggressive NSEC cache")},
+
+  { "aggressive-nsec-cache-nsec-wc-hits",
+    MetricDefinition(PrometheusMetricType::counter,
+                     "Number of answers synthesized from the NSEC aggressive cache")},
+
+  { "aggressive-nsec-cache-nsec3-hits",
+    MetricDefinition(PrometheusMetricType::counter,
+                     "Number of NSEC3-related hits from the aggressive NSEC cache")},
+
+  { "aggressive-nsec-cache-nsec3-wc-hits",
+    MetricDefinition(PrometheusMetricType::counter,
+                     "Number of answers synthesized from the NSEC3 aggressive cache")},
 
   // For cumulative histogram, state the xxx_count name where xxx matches the name in rec_channel_rec
   { "cumul-answers-count",
@@ -1074,7 +1101,10 @@ static void validatePrometheusMetrics()
   auto varmap = getAllStatsMap(StatComponent::API);
   for (const auto& tup : varmap) {
     std::string metricName = tup.first;
-    if  (metricName.find("cpu-msec-") == 0) {
+    if (metricName.find("cpu-msec-") == 0) {
+      continue;
+    }
+    if (metricName.find("cumul-") == 0) {
       continue;
     }
     MetricDefinition metricDetails;