Skip to content

Commit

Permalink
dnsdist: Dedup Prometheus help and type lines for custom metrics with…
Browse files Browse the repository at this point in the history
… labels

(cherry picked from commit 4781d5b)
  • Loading branch information
rgacogne committed Jul 8, 2024
1 parent d274ee0 commit f10c940
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 6 additions & 2 deletions pdns/dnsdist-web.cc
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ static void handlePrometheus(const YaHTTP::Request& req, YaHTTP::Response& resp)
static const std::set<std::string> metricBlacklist = { "special-memory-usage", "latency-count", "latency-sum" };
{
auto entries = dnsdist::metrics::g_stats.entries.read_lock();
std::unordered_set<std::string> helpAndTypeSent;
for (const auto& entry : *entries) {
const auto& metricName = entry.d_name;

Expand Down Expand Up @@ -505,8 +506,11 @@ static void handlePrometheus(const YaHTTP::Request& req, YaHTTP::Response& resp)
// for these we have the help and types encoded in the sources
// but we need to be careful about labels in custom metrics
std::string helpName = prometheusMetricName.substr(0, prometheusMetricName.find('{'));
output << "# HELP " << helpName << " " << metricDetails.description << "\n";
output << "# TYPE " << helpName << " " << prometheusTypeName << "\n";
if (helpAndTypeSent.count(helpName) == 0) {
helpAndTypeSent.insert(helpName);
output << "# HELP " << helpName << " " << metricDetails.description << "\n";
output << "# TYPE " << helpName << " " << prometheusTypeName << "\n";
}
output << prometheusMetricName << " ";

if (const auto& val = std::get_if<pdns::stat_t*>(&entry.d_value)) {
Expand Down
6 changes: 5 additions & 1 deletion regression-tests.dnsdist/test_Prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class TestPrometheus(DNSDistTest):
declareMetric('custom-metric2', 'gauge', 'Custom gauge')
-- and custom names
declareMetric('custom-metric3', 'counter', 'Custom counter', 'custom_prometheus_name')
-- test prometheus labels in custom metrics
declareMetric('custom-metric-foo-x-bar-y-xyz', 'counter', 'Custom counter with labels', 'custom_metric_foo{x="bar",y="xyz"}')
declareMetric('custom-metric-foo-x-baz-y-abc', 'counter', 'Custom counter with labels', 'custom_metric_foo{x="baz",y="abc"}')
"""

def checkPrometheusContentBasic(self, content):
Expand All @@ -42,7 +46,7 @@ def checkPrometheusContentBasic(self, content):
elif not line.startswith('#'):
tokens = line.split(' ')
self.assertEqual(len(tokens), 2)
if not line.startswith('dnsdist_') and not line.startswith('custom_prometheus_name'):
if not line.startswith('dnsdist_') and not line.startswith('custom_'):
raise AssertionError('Expecting prometheus metric to be prefixed by \'dnsdist_\', got: "%s"' % (line))

def checkMetric(self, content, name, expectedType, expectedValue):
Expand Down

0 comments on commit f10c940

Please sign in to comment.