From: Ensar Sarajčić Date: Mon, 16 Dec 2024 10:10:32 +0000 (+0100) Subject: dnsdist: update metrics related regression tests X-Git-Tag: dnsdist-2.0.0-alpha0^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ce95774ffbba6053a2208a60c758f604142aa84f;p=thirdparty%2Fpdns.git dnsdist: update metrics related regression tests --- diff --git a/pdns/dnsdistdist/dnsdist-web.cc b/pdns/dnsdistdist/dnsdist-web.cc index 772af64902..55cbc1588d 100644 --- a/pdns/dnsdistdist/dnsdist-web.cc +++ b/pdns/dnsdistdist/dnsdist-web.cc @@ -931,6 +931,9 @@ static void addStatsToJSONObject(Json::object& obj) if (entry.d_name == "special-memory-usage") { continue; // Too expensive for get-all } + if (entry.d_labels != "") { + continue; // Skip labeled metrics to prevent duplicates + } if (const auto& val = std::get_if(&entry.d_value)) { obj.emplace(entry.d_name, (double)(*val)->load()); } diff --git a/regression-tests.dnsdist/test_API.py b/regression-tests.dnsdist/test_API.py index dd0e9fde85..dfdda817f8 100644 --- a/regression-tests.dnsdist/test_API.py +++ b/regression-tests.dnsdist/test_API.py @@ -878,6 +878,10 @@ class TestAPICustomStatistics(APITestsBase): declareMetric("my-custom-metric", "counter", "Number of statistics") declareMetric("my-other-metric", "counter", "Another number of statistics") declareMetric("my-gauge", "gauge", "Current memory usage") + declareMetric("my-labeled-gauge", "gauge", "Custom gauge with labels", { withLabels = true }) + setMetric("my-labeled-gauge", 123, { labels = { foo = "bar" } }) + declareMetric("my-labeled-counter", "counter", "Custom counter with labels", { withLabels = true }) + incMetric("my-labeled-counter", { labels = { foo = "bar" } }) setWebserverConfig({password="%s", apiKey="%s"}) """ @@ -899,3 +903,8 @@ class TestAPICustomStatistics(APITestsBase): for key in expected: self.assertIn(key, content) self.assertTrue(content[key] >= 0) + + unexpected = ['my-labeled-gauge', 'my-labeled-counter'] + + for key in unexpected: + self.assertNotIn(key, content) diff --git a/regression-tests.dnsdist/test_Prometheus.py b/regression-tests.dnsdist/test_Prometheus.py index f6d334038b..c4559f5d9b 100644 --- a/regression-tests.dnsdist/test_Prometheus.py +++ b/regression-tests.dnsdist/test_Prometheus.py @@ -30,8 +30,9 @@ class TestPrometheus(DNSDistTest): 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"}') + declareMetric('custom-metric-foo', 'counter', 'Custom counter with labels', { withLabels = true }) + incMetric('custom-metric-foo', { labels = { x = 'bar', y = 'xyz' } }) + incMetric('custom-metric-foo', { labels = { x = 'baz', y = 'abc' } }) """ def checkPrometheusContentBasic(self, content): @@ -47,12 +48,16 @@ class TestPrometheus(DNSDistTest): tokens = line.split(' ') self.assertEqual(len(tokens), 2) if not line.startswith('dnsdist_') and not line.startswith('custom_'): - raise AssertionError('Expecting prometheus metric to be prefixed by \'dnsdist_\', got: "%s"' % (line)) + raise AssertionError( + 'Expecting prometheus metric to be prefixed by \'dnsdist_\', got: "%s"' % (line)) - def checkMetric(self, content, name, expectedType, expectedValue): + def checkMetric(self, content, name, expectedType, expectedValue, expectedLabels=""): typeFound = False helpFound = False valueFound = False + labelsFound = False + if expectedLabels == "": + labelsFound = True for line in content.splitlines(): if name in str(line): tokens = line.split(' ') @@ -70,10 +75,15 @@ class TestPrometheus(DNSDistTest): if tokens[0] == name: valueFound = True self.assertEqual(int(tokens[1]), expectedValue) + elif tokens[0] == name + expectedLabels: + valueFound = True + labelsFound = True + self.assertEqual(int(tokens[1]), expectedValue) self.assertTrue(typeFound) self.assertTrue(helpFound) self.assertTrue(valueFound) + self.assertTrue(labelsFound) def checkPrometheusContentPromtool(self, content): output = None @@ -106,3 +116,5 @@ class TestPrometheus(DNSDistTest): self.checkMetric(r.text, 'dnsdist_custom_metric1', 'counter', 1) self.checkMetric(r.text, 'dnsdist_custom_metric2', 'gauge', 0) self.checkMetric(r.text, 'custom_prometheus_name', 'counter', 0) + self.checkMetric(r.text, 'dnsdist_custom_metric_foo', 'counter', 1, '{x="bar",y="xyz"}') + self.checkMetric(r.text, 'dnsdist_custom_metric_foo', 'counter', 1, '{x="baz",y="abc"}')