Skip to content

Commit 3da34f0

Browse files
committed
docs: add missing tests for getting started examples
Add test_metrics.py and test_otlpcollector.py to validate the metrics_example.py and otlpcollector_example.py scripts. - test_metrics.py: Validates all 7 metric types with specific expected values - test_otlpcollector.py: Tests OTLP collector example with graceful timeout handling Fixes #4001
1 parent 36ac612 commit 3da34f0

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import json
16+
import os
17+
import subprocess
18+
import sys
19+
import unittest
20+
21+
class TestMetrics(unittest.TestCase):
22+
def test_metrics(self):
23+
"""Test that metrics example produces expected values"""
24+
# Run the metrics example
25+
dirpath = os.path.dirname(os.path.realpath(__file__))
26+
test_script = f"{dirpath}/../metrics_example.py"
27+
28+
result = subprocess.run(
29+
[sys.executable, test_script],
30+
capture_output=True,
31+
text=True,
32+
timeout=10
33+
)
34+
35+
# Script should run successfully
36+
self.assertEqual(result.returncode, 0)
37+
38+
# Parse the JSON output
39+
output_data = json.loads(result.stdout)
40+
41+
# Get the metrics from the JSON structure
42+
metrics = output_data["resource_metrics"][0]["scope_metrics"][0]["metrics"]
43+
44+
# Create a lookup dict for easier testing
45+
metrics_by_name = {metric["name"]: metric for metric in metrics}
46+
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"]
50+
self.assertEqual(counter_value, 1, "Counter should have value 1")
51+
52+
# 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"]
55+
self.assertEqual(updown_value, -4, "UpDownCounter should have value -4")
56+
57+
# 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]
60+
self.assertEqual(histogram_data["count"], 1, "Histogram should have count 1")
61+
self.assertEqual(histogram_data["sum"], 99.9, "Histogram should have sum 99.9")
62+
63+
# 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"]
66+
self.assertEqual(gauge_value, 1, "Gauge should have value 1")
67+
68+
# 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"]
71+
self.assertEqual(obs_counter_value, 1, "Observable counter should have value 1")
72+
73+
# 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"]
76+
self.assertEqual(obs_updown_value, -10, "Observable updown counter should have value -10")
77+
78+
# 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"]
81+
self.assertEqual(obs_gauge_value, 9, "Observable gauge should have value 9")
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# oltpcollector_example.py
16+
import os
17+
import subprocess
18+
import sys
19+
import unittest
20+
21+
class TestOTLPCollector(unittest.TestCase):
22+
def test_otlpcollector(self):
23+
"""Test that OTLP collector example outputs 'Hello world!'"""
24+
dirpath = os.path.dirname(os.path.realpath(__file__))
25+
test_script = f"{dirpath}/../otlpcollector_example.py"
26+
27+
# Run the script with a short timeout since it will retry forever
28+
process = subprocess.Popen(
29+
[sys.executable, test_script],
30+
stdout=subprocess.PIPE,
31+
stderr=subprocess.PIPE,
32+
text=True
33+
)
34+
35+
# Wait 2 seconds then kill it (enough time to print "Hello world!")
36+
try:
37+
stdout, stderr = process.communicate(timeout=2)
38+
except subprocess.TimeoutExpired:
39+
process.kill()
40+
stdout, stderr = process.communicate()
41+
42+
# Check that it printed the expected message
43+
self.assertIn("Hello world!", stdout)

0 commit comments

Comments
 (0)