Skip to content

Commit 08e052a

Browse files
committed
fix(metrics): add additional tests
1 parent 5592d5e commit 08e052a

File tree

3 files changed

+164
-1
lines changed

3 files changed

+164
-1
lines changed

src/deep/api/plugin/metric/prometheus_metrics.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ def summary(self, name: str, labels: Dict[str, str], namespace: str, help_string
144144
deep.logging.exception(f"Error registering metric summary {namespace}_{name}")
145145
pass
146146

147+
@property
148+
def _cache(self):
149+
return self.__cache
150+
147151
def shutdown(self):
148152
"""Clean up and shutdown the plugin."""
149153
self.clear()
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Copyright (C) 2024 Intergral GmbH
2+
#
3+
# This program is free software: you can redistribute it and/or modify
4+
# it under the terms of the GNU Affero General Public License as published by
5+
# the Free Software Foundation, either version 3 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU Affero General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU Affero General Public License
14+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
import unittest
16+
17+
from mockito import when, mock, verify
18+
from opentelemetry.metrics import set_meter_provider, MeterProvider
19+
20+
import deep.logging
21+
from deep.api.plugin.metric.otel_metrics import OTelMetrics
22+
23+
24+
class TestOtelMetrics(unittest.TestCase):
25+
mock_provider = mock(MeterProvider)
26+
27+
@classmethod
28+
def setUpClass(cls):
29+
deep.logging.init()
30+
31+
set_meter_provider(cls.mock_provider)
32+
33+
def setUp(self):
34+
self.deep_provider = mock()
35+
when(self.mock_provider).get_meter('deep', '').thenReturn(self.deep_provider)
36+
when(self.mock_provider).get_meter('deep', '', None).thenReturn(self.deep_provider)
37+
38+
def test_post_counter_twice(self):
39+
metric = mock()
40+
when(self.deep_provider).create_counter('test_counter', "unit", "help").thenReturn(metric)
41+
metrics = OTelMetrics(None)
42+
metrics.counter("counter", {}, "test", "help", "unit", 1)
43+
metrics.counter("counter", {}, "test", "help", "unit", 1)
44+
45+
verify(self.deep_provider, 1).create_counter('test_counter', "unit", "help")
46+
verify(metric, 2).add(1, {})
47+
48+
def test_post_counter(self):
49+
metric = mock()
50+
when(self.deep_provider).create_counter('test_counter', "unit", "help").thenReturn(metric)
51+
metrics = OTelMetrics(None)
52+
metrics.counter("counter", {}, "test", "help", "unit", 1)
53+
54+
verify(self.deep_provider, 1).create_counter('test_counter', "unit", "help")
55+
56+
verify(metric).add(1, {})
57+
58+
def test_post_histogram(self):
59+
metric = mock()
60+
when(self.deep_provider).create_histogram('test_histogram', "unit", "help").thenReturn(metric)
61+
metrics = OTelMetrics(None)
62+
metrics.histogram("histogram", {}, "test", "help", "unit", 1)
63+
64+
verify(metric).record(1, {})
65+
66+
def test_post_gauge(self):
67+
metric = mock()
68+
when(self.deep_provider).create_up_down_counter('test_gauge', "unit", "help").thenReturn(metric)
69+
metrics = OTelMetrics(None)
70+
metrics.gauge("gauge", {}, "test", "help", "unit", 1)
71+
72+
verify(metric).add(1, {})
73+
74+
def test_post_summary(self):
75+
metric = mock()
76+
when(self.deep_provider).create_histogram('test_summary', "unit", "help").thenReturn(metric)
77+
metrics = OTelMetrics(None)
78+
metrics.summary("summary", {}, "test", "help", "unit", 1)
79+
80+
verify(metric).record(1, {})
81+
82+
def test_post_counter_error(self):
83+
metric = mock()
84+
when(self.deep_provider).create_counter('test_counter', "unit", "help").thenRaise(Exception("test otel error"))
85+
metrics = OTelMetrics(None)
86+
metrics.counter("counter", {}, "test", "help", "unit", 1)
87+
88+
verify(self.deep_provider, 1).create_counter('test_counter', "unit", "help")
89+
90+
verify(metric, 0).add(1, {})
91+
92+
def test_post_histogram_error(self):
93+
metric = mock()
94+
when(self.deep_provider).create_histogram('test_histogram', "unit", "help").thenRaise(
95+
Exception("test otel error"))
96+
metrics = OTelMetrics(None)
97+
metrics.histogram("histogram", {}, "test", "help", "unit", 1)
98+
99+
verify(metric, 0).record(1, {})
100+
101+
def test_post_gauge_error(self):
102+
metric = mock()
103+
when(self.deep_provider).create_up_down_counter('test_gauge', "unit", "help").thenRaise(
104+
Exception("test otel error"))
105+
metrics = OTelMetrics(None)
106+
metrics.gauge("gauge", {}, "test", "help", "unit", 1)
107+
108+
verify(metric, 0).add(1, {})
109+
110+
def test_post_summary_error(self):
111+
metric = mock()
112+
when(self.deep_provider).create_histogram('test_summary', "unit", "help").thenRaise(
113+
Exception("test otel error"))
114+
metrics = OTelMetrics(None)
115+
metrics.summary("summary", {}, "test", "help", "unit", 1)
116+
117+
verify(metric, 0).record(1, {})

tests/unit_tests/api/plugin/metrics/test_prometheus_metrics.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,52 @@ def tearDown(self):
2929

3030
def test_counter(self):
3131
self.plugin.counter("test", {}, "deep", "", "", 123)
32+
self.assertEqual(1, len(self.plugin._cache))
3233

3334
def test_counter_with_labels(self):
3435
self.plugin.counter("test_other", {'value': 'name'}, "deep", "", "", 123)
36+
self.assertEqual(1, len(self.plugin._cache))
3537

36-
def test_duplicate_registration(self):
38+
def test_duplicate_counter_registration(self):
3739
self.plugin.counter("test_other", {}, "deep", "", "", 123)
3840
self.plugin.counter("test_other", {'value': 'name'}, "deep", "", "", 123)
41+
self.assertEqual(1, len(self.plugin._cache))
42+
43+
def test_gauge(self):
44+
self.plugin.gauge("test", {}, "deep", "", "", 123)
45+
self.assertEqual(1, len(self.plugin._cache))
46+
47+
def test_gauge_with_labels(self):
48+
self.plugin.gauge("test_other", {'value': 'name'}, "deep", "", "", 123)
49+
self.assertEqual(1, len(self.plugin._cache))
50+
51+
def test_duplicate_gauge_registration(self):
52+
self.plugin.gauge("test_other", {}, "deep", "", "", 123)
53+
self.plugin.gauge("test_other", {'value': 'name'}, "deep", "", "", 123)
54+
self.assertEqual(1, len(self.plugin._cache))
55+
56+
def test_histogram(self):
57+
self.plugin.histogram("test", {}, "deep", "", "", 123)
58+
self.assertEqual(1, len(self.plugin._cache))
59+
60+
def test_histogram_with_labels(self):
61+
self.plugin.histogram("test_other", {'value': 'name'}, "deep", "", "", 123)
62+
self.assertEqual(1, len(self.plugin._cache))
63+
64+
def test_duplicate_histogram_registration(self):
65+
self.plugin.histogram("test_other", {}, "deep", "", "", 123)
66+
self.plugin.histogram("test_other", {'value': 'name'}, "deep", "", "", 123)
67+
self.assertEqual(1, len(self.plugin._cache))
68+
69+
def test_summary(self):
70+
self.plugin.summary("test", {}, "deep", "", "", 123)
71+
self.assertEqual(1, len(self.plugin._cache))
72+
73+
def test_summary_with_labels(self):
74+
self.plugin.summary("test_other", {'value': 'name'}, "deep", "", "", 123)
75+
self.assertEqual(1, len(self.plugin._cache))
76+
77+
def test_duplicate_summary_registration(self):
78+
self.plugin.summary("test_other", {}, "deep", "", "", 123)
79+
self.plugin.summary("test_other", {'value': 'name'}, "deep", "", "", 123)
80+
self.assertEqual(1, len(self.plugin._cache))

0 commit comments

Comments
 (0)