]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: update metrics related regression tests
authorEnsar Sarajčić <dev@ensarsarajcic.com>
Mon, 16 Dec 2024 10:10:32 +0000 (11:10 +0100)
committerEnsar Sarajčić <dev@ensarsarajcic.com>
Mon, 16 Dec 2024 10:10:32 +0000 (11:10 +0100)
pdns/dnsdistdist/dnsdist-web.cc
regression-tests.dnsdist/test_API.py
regression-tests.dnsdist/test_Prometheus.py

index 772af649028655b1d060c561288c22e84cf2c79c..55cbc1588dd389018db7f28fe65322ac2ba81daf 100644 (file)
@@ -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<pdns::stat_t*>(&entry.d_value)) {
       obj.emplace(entry.d_name, (double)(*val)->load());
     }
index dd0e9fde85a72d27f194c7a63dc12e98ce2e6eba..dfdda817f8a3313beb91f5867c33922325dba714 100644 (file)
@@ -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)
index f6d334038b0fae8f564d690d133528797d31bb8c..c4559f5d9b4ebbc23315eb379c41944560b9af36 100644 (file)
@@ -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"}')