From: Otto Date: Thu, 19 Aug 2021 07:08:53 +0000 (+0200) Subject: Prometheus help texts and general cleanup. Example output: X-Git-Tag: dnsdist-1.7.0-alpha1~57^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c8bbc5d45b6c89e370acc57421cde596a5e86bc2;p=thirdparty%2Fpdns.git Prometheus help texts and general cleanup. Example output: 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 --- diff --git a/pdns/rec_channel_rec.cc b/pdns/rec_channel_rec.cc index 8b19663913..eb562f5c2e 100644 --- a/pdns/rec_channel_rec.cc +++ b/pdns/rec_channel_rec.cc @@ -1158,6 +1158,7 @@ static StatsMap toRPZStatsMap(const string& name, const std::unordered_map` 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 ^^^^ diff --git a/pdns/recursordist/docs/metrics.rst b/pdns/recursordist/docs/metrics.rst index 4f4a29f9b6..c1f7255483 100644 --- a/pdns/recursordist/docs/metrics.rst +++ b/pdns/recursordist/docs/metrics.rst @@ -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 diff --git a/pdns/recursordist/rec_metrics.hh b/pdns/recursordist/rec_metrics.hh index 4ece7b49fe..1e6d875f28 100644 --- a/pdns/recursordist/rec_metrics.hh +++ b/pdns/recursordist/rec_metrics.hh @@ -23,36 +23,31 @@ #pragma once #include -#include -#include -#include -#include -#include +#include // 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 metrics; + static const std::map d_metrics; }; - -extern MetricDefinitionStorage g_metricDefinitions; diff --git a/pdns/ws-recursor.cc b/pdns/ws-recursor.cc index 8d53a13217..9df78ec358 100644 --- a/pdns/ws-recursor.cc +++ b/pdns/ws-recursor.cc @@ -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 MetricDefinitionStorage::metrics = { +const std::map MetricDefinitionStorage::d_metrics = { {"all-outqueries", MetricDefinition(PrometheusMetricType::counter, "Number of outgoing UDP queries since starting")}, @@ -1089,6 +1084,11 @@ const std::map 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