Skip to content

Commit 6c8e2ab

Browse files
sginjixrmx
andauthored
docs: add missing tests for getting started examples (#4662)
* 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 * Fix lint errors * Apply pre-commit fixes --------- Co-authored-by: Riccardo Magliocchetti <[email protected]>
1 parent ade1058 commit 6c8e2ab

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
22+
class TestMetrics(unittest.TestCase):
23+
def test_metrics(self):
24+
"""Test that metrics example produces expected values"""
25+
# Run the metrics example
26+
test_script = f"{os.path.dirname(os.path.realpath(__file__))}/../metrics_example.py"
27+
28+
result = subprocess.run(
29+
[sys.executable, test_script],
30+
capture_output=True,
31+
text=True,
32+
timeout=10,
33+
check=True,
34+
)
35+
36+
# Script should run successfully
37+
self.assertEqual(result.returncode, 0)
38+
39+
# Parse the JSON output
40+
output_data = json.loads(result.stdout)
41+
42+
# Get the metrics from the JSON structure
43+
metrics = output_data["resource_metrics"][0]["scope_metrics"][0][
44+
"metrics"
45+
]
46+
47+
# Create a lookup dict for easier testing
48+
metrics_by_name = {metric["name"]: metric for metric in metrics}
49+
50+
# Test Counter: should be 1 (called counter.add(1))
51+
counter_value = metrics_by_name["counter"]["data"]["data_points"][0][
52+
"value"
53+
]
54+
self.assertEqual(counter_value, 1, "Counter should have value 1")
55+
56+
# Test UpDownCounter: should be -4 (1 + (-5) = -4)
57+
updown_value = metrics_by_name["updown_counter"]["data"][
58+
"data_points"
59+
][0]["value"]
60+
self.assertEqual(
61+
updown_value, -4, "UpDownCounter should have value -4"
62+
)
63+
64+
# Test Histogram: should have count=1, sum=99.9
65+
histogram_data = metrics_by_name["histogram"]["data"]["data_points"][0]
66+
self.assertEqual(
67+
histogram_data["count"], 1, "Histogram should have count 1"
68+
)
69+
self.assertEqual(
70+
histogram_data["sum"], 99.9, "Histogram should have sum 99.9"
71+
)
72+
73+
# Test Gauge: should be 1 (last value set)
74+
gauge_value = metrics_by_name["gauge"]["data"]["data_points"][0][
75+
"value"
76+
]
77+
self.assertEqual(gauge_value, 1, "Gauge should have value 1")
78+
79+
# Test Observable Counter: should be 1 (from callback)
80+
obs_counter_value = metrics_by_name["observable_counter"]["data"][
81+
"data_points"
82+
][0]["value"]
83+
self.assertEqual(
84+
obs_counter_value, 1, "Observable counter should have value 1"
85+
)
86+
87+
# Test Observable UpDownCounter: should be -10 (from callback)
88+
obs_updown_value = metrics_by_name["observable_updown_counter"][
89+
"data"
90+
]["data_points"][0]["value"]
91+
self.assertEqual(
92+
obs_updown_value,
93+
-10,
94+
"Observable updown counter should have value -10",
95+
)
96+
97+
# Test Observable Gauge: should be 9 (from callback)
98+
obs_gauge_value = metrics_by_name["observable_gauge"]["data"][
99+
"data_points"
100+
][0]["value"]
101+
self.assertEqual(
102+
obs_gauge_value, 9, "Observable gauge should have value 9"
103+
)
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+
22+
class TestOTLPCollector(unittest.TestCase):
23+
def test_otlpcollector(self):
24+
"""Test that OTLP collector example outputs 'Hello world!'"""
25+
dirpath = os.path.dirname(os.path.realpath(__file__))
26+
test_script = f"{dirpath}/../otlpcollector_example.py"
27+
28+
# Run the script with a short timeout since it will retry forever
29+
with subprocess.Popen(
30+
[sys.executable, test_script],
31+
stdout=subprocess.PIPE,
32+
stderr=subprocess.PIPE,
33+
text=True,
34+
) as process:
35+
# Wait 2 seconds then kill it (enough time to print "Hello world!")
36+
try:
37+
stdout, _ = process.communicate(timeout=2)
38+
except subprocess.TimeoutExpired:
39+
process.kill()
40+
stdout, _ = process.communicate()
41+
42+
# Check that it printed the expected message
43+
self.assertIn("Hello world!", stdout)

0 commit comments

Comments
 (0)