forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_metrics.py
More file actions
103 lines (88 loc) · 3.5 KB
/
Copy pathtest_metrics.py
File metadata and controls
103 lines (88 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import os
import subprocess
import sys
import unittest
class TestMetrics(unittest.TestCase):
def test_metrics(self):
"""Test that metrics example produces expected values"""
# Run the metrics example
test_script = f"{os.path.dirname(os.path.realpath(__file__))}/../metrics_example.py"
result = subprocess.run(
[sys.executable, test_script],
capture_output=True,
text=True,
timeout=10,
check=True,
)
# Script should run successfully
self.assertEqual(result.returncode, 0)
# Parse the JSON output
output_data = json.loads(result.stdout)
# Get the metrics from the JSON structure
metrics = output_data["resource_metrics"][0]["scope_metrics"][0][
"metrics"
]
# Create a lookup dict for easier testing
metrics_by_name = {metric["name"]: metric for metric in metrics}
# Test Counter: should be 1 (called counter.add(1))
counter_value = metrics_by_name["counter"]["data"]["data_points"][0][
"value"
]
self.assertEqual(counter_value, 1, "Counter should have value 1")
# Test UpDownCounter: should be -4 (1 + (-5) = -4)
updown_value = metrics_by_name["updown_counter"]["data"][
"data_points"
][0]["value"]
self.assertEqual(
updown_value, -4, "UpDownCounter should have value -4"
)
# Test Histogram: should have count=1, sum=99.9
histogram_data = metrics_by_name["histogram"]["data"]["data_points"][0]
self.assertEqual(
histogram_data["count"], 1, "Histogram should have count 1"
)
self.assertEqual(
histogram_data["sum"], 99.9, "Histogram should have sum 99.9"
)
# Test Gauge: should be 1 (last value set)
gauge_value = metrics_by_name["gauge"]["data"]["data_points"][0][
"value"
]
self.assertEqual(gauge_value, 1, "Gauge should have value 1")
# Test Observable Counter: should be 1 (from callback)
obs_counter_value = metrics_by_name["observable_counter"]["data"][
"data_points"
][0]["value"]
self.assertEqual(
obs_counter_value, 1, "Observable counter should have value 1"
)
# Test Observable UpDownCounter: should be -10 (from callback)
obs_updown_value = metrics_by_name["observable_updown_counter"][
"data"
]["data_points"][0]["value"]
self.assertEqual(
obs_updown_value,
-10,
"Observable updown counter should have value -10",
)
# Test Observable Gauge: should be 9 (from callback)
obs_gauge_value = metrics_by_name["observable_gauge"]["data"][
"data_points"
][0]["value"]
self.assertEqual(
obs_gauge_value, 9, "Observable gauge should have value 9"
)