]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Prometheus help texts and general cleanup. Example output:
authorOtto <otto.moerbeek@open-xchange.com>
Thu, 19 Aug 2021 07:08:53 +0000 (09:08 +0200)
committerOtto <otto.moerbeek@open-xchange.com>
Thu, 19 Aug 2021 07:30:52 +0000 (09:30 +0200)
pdns_recursor_policy_hits 10
pdns_recursor_policy_hits{type="filter"} 3
pdns_recursor_policy_hits{type="rpz",policyname="rpz.local"} 5
pdns_recursor_policy_hits{type="rpz",policyname="rpzFile"} 2

pdns/rec_channel_rec.cc
pdns/recursordist/docs/lua-config/rpz.rst
pdns/recursordist/docs/metrics.rst
pdns/recursordist/rec_metrics.hh
pdns/ws-recursor.cc

index 8b19663913ad0ee2efdd062526addfac10ed14a2..eb562f5c2e0f109908bf7d9cae16d566326d775d 100644 (file)
@@ -1158,6 +1158,7 @@ static StatsMap toRPZStatsMap(const string& name, const std::unordered_map<std::
   const string pbasename = getPrometheusName(name);
   StatsMap entries;
 
+  uint64_t total = 0;
   for (const auto& entry: map) {
     auto &key = entry.first;
     auto count = entry.second.load();
@@ -1167,10 +1168,12 @@ static StatsMap toRPZStatsMap(const string& name, const std::unordered_map<std::
       pname = pbasename + "{type=\"filter\"}";
     } else {
       sname = name + "-rpz-" + key;
-      pname = pbasename + "{type=\"rpz\",zone=\"" + entry.first + "\"}";
+      pname = pbasename + "{type=\"rpz\",policyname=\"" + entry.first + "\"}";
     }
-    entries.emplace(make_pair(sname, StatsMapEntry{pname, std::to_string(count)}));
+    entries.emplace(sname, StatsMapEntry{pname, std::to_string(count)});
+    total += count;
   }
+  entries.emplace(name, StatsMapEntry{pbasename, std::to_string(total)});
   return entries;
 }
 
index e340394a8f6dc00d7aa5b19ed774268a8f2ee2c6..11450e8884e612b0828ee83921aee371cc14e81b 100644 (file)
@@ -141,6 +141,7 @@ The maximum TTL value of the synthesized records, overriding a higher value from
 policyName
 ^^^^^^^^^^
 The name logged as 'appliedPolicy' in :doc:`protobuf <protobuf>` messages when this policy is applied.
+Defaults to ``rpzFile`` for RPZs loaded by :func:`rpzFile` or the name of the zone for RPZs loaded by :func:`rpzPrimary`.
 
 tags
 ^^^^
index 4f4a29f9b606942e0a314ef6dc70dbe93a3ab427..c1f7255483b595d5b8aa3fbfa6ad5ca6796be70d 100644 (file)
@@ -594,6 +594,11 @@ policy-drops
 ^^^^^^^^^^^^
 packets dropped because of (Lua) policy decision
 
+policy-hits
+^^^^^^^^^^^
+Number of policy decisions based on Lua (``type = "filter"``), or RPZ (``type = "rpz"``). RPZ hits include the :ref:`rpz-policyName`.
+These metrics are useful for Prometheus and not listed other outputs by default.
+
 policy-result-noaction
 ^^^^^^^^^^^^^^^^^^^^^^
 packets that were not acted upon by   the RPZ/filter engine
index 4ece7b49fead668942c994fae944d6b0639f67b2..1e6d875f284d0cba03f3db3a6c94f27f0804c406 100644 (file)
 #pragma once
 
 #include <string>
-#include <unordered_map>
-#include <vector>
-#include <inttypes.h>
-#include <unistd.h>
-#include <atomic>
+#include <map>
 
 // Metric types for Prometheus
-enum class PrometheusMetricType : int
+enum class PrometheusMetricType
 {
-  counter = 1,
-  gauge = 2,
-  histogram = 3,
-  multicounter = 4
+  counter,
+  gauge,
+  histogram,
+  multicounter
 };
 
 // Keeps additional information about metrics
 struct MetricDefinition
 {
-  MetricDefinition(const PrometheusMetricType& prometheusType_, const std::string& description_)
+  MetricDefinition(const PrometheusMetricType prometheusType, const std::string& description) :
+    d_description(description), d_prometheusType(prometheusType)
   {
-    prometheusType = prometheusType_;
-    description = description_;
   }
 
   MetricDefinition() = default;
 
   // Metric description
-  std::string description;
+  std::string d_description;
   // Metric type for Prometheus
-  PrometheusMetricType prometheusType;
+  PrometheusMetricType d_prometheusType;
 };
 
 class MetricDefinitionStorage
@@ -61,9 +56,9 @@ public:
   // Return metric definition by name
   bool getMetricDetails(const std::string& metricName, MetricDefinition& metric)
   {
-    auto metricDetailsIter = metrics.find(metricName);
+    auto metricDetailsIter = d_metrics.find(metricName);
 
-    if (metricDetailsIter == metrics.end()) {
+    if (metricDetailsIter == d_metrics.end()) {
       return false;
     }
 
@@ -72,7 +67,7 @@ public:
   };
 
   // Return string representation of Prometheus metric type
-  std::string getPrometheusStringMetricType(const PrometheusMetricType& metricType)
+  static std::string getPrometheusStringMetricType(const PrometheusMetricType metricType)
   {
     switch (metricType) {
     case PrometheusMetricType::counter:
@@ -85,7 +80,8 @@ public:
       return "histogram";
       break;
     case PrometheusMetricType::multicounter:
-      return "multicounter";
+      // A multicounter produces multiple values of type "counter"
+      return "counter";
       break;
     default:
       return "";
@@ -95,7 +91,5 @@ public:
 
 private:
   // Description and types for prometheus output of stats
-  static const std::map<std::string, MetricDefinition> metrics;
+  static const std::map<std::string, MetricDefinition> d_metrics;
 };
-
-extern MetricDefinitionStorage g_metricDefinitions;
index 8d53a13217adc7aa720b21b893943c92a1cfd667..9df78ec3581398979b1796de0fdb304691fd53ac 100644 (file)
@@ -446,30 +446,25 @@ static void prometheusMetrics(HttpRequest *req, HttpResponse *resp) {
     for (const auto& tup : varmap) {
         std::string metricName = tup.first;
         std::string prometheusMetricName = tup.second.d_prometheusName;
+        std::string helpname = tup.second.d_prometheusName;
         MetricDefinition metricDetails;
 
         if (s_metricDefinitions.getMetricDetails(metricName, metricDetails)) {
           std::string prometheusTypeName = s_metricDefinitions.getPrometheusStringMetricType(
-                  metricDetails.prometheusType);
+                  metricDetails.d_prometheusType);
 
           if (prometheusTypeName.empty()) {
             continue;
           }
-          if (metricDetails.prometheusType == PrometheusMetricType::multicounter) {
-            string shortname = prometheusMetricName.substr(0, prometheusMetricName.find('{'));
-            output << "# HELP " << shortname << " " << metricDetails.description << "\n";
-            output << "# TYPE " << shortname << " " << "counter" << "\n";
+          if (metricDetails.d_prometheusType == PrometheusMetricType::multicounter) {
+            helpname = prometheusMetricName.substr(0, prometheusMetricName.find('{'));
           }
-          else if (metricDetails.prometheusType == PrometheusMetricType::histogram) {
+          else if (metricDetails.d_prometheusType == PrometheusMetricType::histogram) {
             // name is XXX_count, strip the _count part
-            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";
+            helpname = prometheusMetricName.substr(0, prometheusMetricName.length() - 6);
           }
+          output << "# TYPE " << helpname << " " << prometheusTypeName << "\n";
+          output << "# HELP " << helpname << " " << metricDetails.d_description << "\n";
         }
         output << prometheusMetricName << " " << tup.second.d_value << "\n";
     }
@@ -518,7 +513,7 @@ static void serveStuff(HttpRequest* req, HttpResponse* resp)
   }
 }
 
-const std::map<std::string, MetricDefinition> MetricDefinitionStorage::metrics = {
+const std::map<std::string, MetricDefinition> MetricDefinitionStorage::d_metrics = {
   {"all-outqueries",
    MetricDefinition(PrometheusMetricType::counter,
                     "Number of outgoing UDP queries since starting")},
@@ -1089,6 +1084,11 @@ const std::map<std::string, MetricDefinition> MetricDefinitionStorage::metrics =
   { "almost-expired-exceptions",
     MetricDefinition(PrometheusMetricType::counter,
                      "number of almost-expired tasks that caused an exception")},
+
+  // For multicounters, state the first
+  { "policy-hits",
+    MetricDefinition(PrometheusMetricType::multicounter,
+                     "Number of filter or RPZ policy hits")},
 };
 
 #define CHECK_PROMETHEUS_METRICS 0