@@ -22,60 +22,53 @@ class TestMetrics(unittest.TestCase):
2222 def test_metrics (self ):
2323 """Test that metrics example produces expected values"""
2424 # 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+
2827 result = subprocess .run (
2928 [sys .executable , test_script ],
3029 capture_output = True ,
3130 text = True ,
32- timeout = 10
31+ timeout = 10 ,
32+ check = True ,
3333 )
34-
34+
3535 # Script should run successfully
3636 self .assertEqual (result .returncode , 0 )
37-
37+
3838 # Parse the JSON output
3939 output_data = json .loads (result .stdout )
40-
40+
4141 # Get the metrics from the JSON structure
4242 metrics = output_data ["resource_metrics" ][0 ]["scope_metrics" ][0 ]["metrics" ]
43-
43+
4444 # Create a lookup dict for easier testing
4545 metrics_by_name = {metric ["name" ]: metric for metric in metrics }
46-
46+
4747 # 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" ]
5049 self .assertEqual (counter_value , 1 , "Counter should have value 1" )
51-
50+
5251 # 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" ]
5553 self .assertEqual (updown_value , - 4 , "UpDownCounter should have value -4" )
56-
54+
5755 # 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 ]
6057 self .assertEqual (histogram_data ["count" ], 1 , "Histogram should have count 1" )
6158 self .assertEqual (histogram_data ["sum" ], 99.9 , "Histogram should have sum 99.9" )
62-
59+
6360 # 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" ]
6662 self .assertEqual (gauge_value , 1 , "Gauge should have value 1" )
67-
63+
6864 # 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" ]
7166 self .assertEqual (obs_counter_value , 1 , "Observable counter should have value 1" )
72-
67+
7368 # 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" ]
7670 self .assertEqual (obs_updown_value , - 10 , "Observable updown counter should have value -10" )
77-
71+
7872 # 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" ]
8174 self .assertEqual (obs_gauge_value , 9 , "Observable gauge should have value 9" )
0 commit comments