@@ -22,60 +22,53 @@ class TestMetrics(unittest.TestCase):
22
22
def test_metrics (self ):
23
23
"""Test that metrics example produces expected values"""
24
24
# Run the metrics example
25
- dirpath = os .path .dirname (os .path .realpath (__file__ ))
26
- test_script = f"{ dirpath } /../metrics_example.py"
27
-
25
+ test_script = f"{ os .path .dirname (os .path .realpath (__file__ ))} /../metrics_example.py"
26
+
28
27
result = subprocess .run (
29
28
[sys .executable , test_script ],
30
29
capture_output = True ,
31
30
text = True ,
32
- timeout = 10
31
+ timeout = 10 ,
32
+ check = True ,
33
33
)
34
-
34
+
35
35
# Script should run successfully
36
36
self .assertEqual (result .returncode , 0 )
37
-
37
+
38
38
# Parse the JSON output
39
39
output_data = json .loads (result .stdout )
40
-
40
+
41
41
# Get the metrics from the JSON structure
42
42
metrics = output_data ["resource_metrics" ][0 ]["scope_metrics" ][0 ]["metrics" ]
43
-
43
+
44
44
# Create a lookup dict for easier testing
45
45
metrics_by_name = {metric ["name" ]: metric for metric in metrics }
46
-
46
+
47
47
# Test Counter: should be 1 (called counter.add(1))
48
- counter_metric = metrics_by_name ["counter" ]
49
- counter_value = counter_metric ["data" ]["data_points" ][0 ]["value" ]
48
+ counter_value = metrics_by_name ["counter" ]["data" ]["data_points" ][0 ]["value" ]
50
49
self .assertEqual (counter_value , 1 , "Counter should have value 1" )
51
-
50
+
52
51
# Test UpDownCounter: should be -4 (1 + (-5) = -4)
53
- updown_metric = metrics_by_name ["updown_counter" ]
54
- updown_value = updown_metric ["data" ]["data_points" ][0 ]["value" ]
52
+ updown_value = metrics_by_name ["updown_counter" ]["data" ]["data_points" ][0 ]["value" ]
55
53
self .assertEqual (updown_value , - 4 , "UpDownCounter should have value -4" )
56
-
54
+
57
55
# Test Histogram: should have count=1, sum=99.9
58
- histogram_metric = metrics_by_name ["histogram" ]
59
- histogram_data = histogram_metric ["data" ]["data_points" ][0 ]
56
+ histogram_data = metrics_by_name ["histogram" ]["data" ]["data_points" ][0 ]
60
57
self .assertEqual (histogram_data ["count" ], 1 , "Histogram should have count 1" )
61
58
self .assertEqual (histogram_data ["sum" ], 99.9 , "Histogram should have sum 99.9" )
62
-
59
+
63
60
# Test Gauge: should be 1 (last value set)
64
- gauge_metric = metrics_by_name ["gauge" ]
65
- gauge_value = gauge_metric ["data" ]["data_points" ][0 ]["value" ]
61
+ gauge_value = metrics_by_name ["gauge" ]["data" ]["data_points" ][0 ]["value" ]
66
62
self .assertEqual (gauge_value , 1 , "Gauge should have value 1" )
67
-
63
+
68
64
# Test Observable Counter: should be 1 (from callback)
69
- obs_counter_metric = metrics_by_name ["observable_counter" ]
70
- obs_counter_value = obs_counter_metric ["data" ]["data_points" ][0 ]["value" ]
65
+ obs_counter_value = metrics_by_name ["observable_counter" ]["data" ]["data_points" ][0 ]["value" ]
71
66
self .assertEqual (obs_counter_value , 1 , "Observable counter should have value 1" )
72
-
67
+
73
68
# Test Observable UpDownCounter: should be -10 (from callback)
74
- obs_updown_metric = metrics_by_name ["observable_updown_counter" ]
75
- obs_updown_value = obs_updown_metric ["data" ]["data_points" ][0 ]["value" ]
69
+ obs_updown_value = metrics_by_name ["observable_updown_counter" ]["data" ]["data_points" ][0 ]["value" ]
76
70
self .assertEqual (obs_updown_value , - 10 , "Observable updown counter should have value -10" )
77
-
71
+
78
72
# Test Observable Gauge: should be 9 (from callback)
79
- obs_gauge_metric = metrics_by_name ["observable_gauge" ]
80
- obs_gauge_value = obs_gauge_metric ["data" ]["data_points" ][0 ]["value" ]
73
+ obs_gauge_value = metrics_by_name ["observable_gauge" ]["data" ]["data_points" ][0 ]["value" ]
81
74
self .assertEqual (obs_gauge_value , 9 , "Observable gauge should have value 9" )
0 commit comments