From: Otto Date: Wed, 30 Jun 2021 08:27:04 +0000 (+0200) Subject: Modify per-thread cpu usage stats to be Prometheus-friendly. X-Git-Tag: dnsdist-1.7.0-alpha1~59^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F10554%2Fhead;p=thirdparty%2Fpdns.git Modify per-thread cpu usage stats to be Prometheus-friendly. 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 --- diff --git a/pdns/rec_channel_rec.cc b/pdns/rec_channel_rec.cc index 0c8928836f..53887cbd4d 100644 --- a/pdns/rec_channel_rec.cc +++ b/pdns/rec_channel_rec.cc @@ -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()); }); diff --git a/pdns/recursordist/rec_metrics.hh b/pdns/recursordist/rec_metrics.hh index 59921825a2..4ece7b49fe 100644 --- a/pdns/recursordist/rec_metrics.hh +++ b/pdns/recursordist/rec_metrics.hh @@ -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; diff --git a/pdns/ws-recursor.cc b/pdns/ws-recursor.cc index 181a0f318e..8d53a13217 100644 --- a/pdns/ws-recursor.cc +++ b/pdns/ws-recursor.cc @@ -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 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 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;