Skip to content

Commit 2f06678

Browse files
committed
clean up
Signed-off-by: Gregor Zeitlinger <[email protected]>
1 parent 3b5df68 commit 2f06678

File tree

1 file changed

+32
-34
lines changed

1 file changed

+32
-34
lines changed
Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,51 @@
11
#!/usr/bin/env python3
2-
3-
"""This script is used to check if the service instance id is present in the exported data
4-
The script will return 0 if the service instance id is present in the exported data"""
5-
2+
"""
3+
Check if the service instance id is present in the exported data.
4+
Returns 0 if the service instance id is present in the exported data.
5+
"""
66
import json
77
import urllib.parse
88
from urllib.request import urlopen
99

1010

11-
def get(url):
12-
global response, res
11+
def get_json(url):
1312
with urlopen(url) as response:
14-
# read the response
15-
res = response.read()
16-
# decode the response
17-
res = json.loads(res.decode("utf-8"))
18-
return res
13+
return json.loads(response.read().decode("utf-8"))
1914

2015

21-
res = get(" http://localhost:9090/api/v1/query?query=target_info")
16+
def main():
17+
# Query Prometheus for target_info
18+
res = get_json("http://localhost:9090/api/v1/query?query=target_info")
2219

23-
# uncomment the following line to use the local file instead of the url - for debugging
24-
# with open('example_target_info.json') as f:
25-
# res = json.load(f)
20+
# Uncomment for local debugging
21+
# with open('example_target_info.json') as f:
22+
# res = json.load(f)
2623

27-
values = list(
28-
{
24+
instance_ids = {
2925
r["metric"]["instance"]
3026
for r in res["data"]["result"]
31-
if not r["metric"]["service_name"] == "otelcol-contrib"
27+
if r["metric"].get("service_name") != "otelcol-contrib"
3228
}
33-
)
29+
instance_ids = list(instance_ids)
30+
31+
print(f'Instance ids found:{instance_ids}')
32+
if len(instance_ids) > 1:
33+
print("More than one instance id found")
34+
print(res)
35+
36+
# Both the agent and the exporter should report the same instance id
37+
assert len(instance_ids) == 1, "Expected exactly one instance id"
3438

35-
print("Instance ids found:")
36-
print(values)
37-
if len(values) > 1:
38-
print("More than one instance id found")
39-
print(res)
39+
query = f'target_info{{instance="{instance_ids[0]}"}}'
40+
encoded_query = urllib.parse.quote_plus(query)
41+
res = get_json(f"http://localhost:9090/api/v1/query?query={encoded_query}")
4042

41-
# both the agent and the exporter should report the same instance id
42-
assert len(values) == 1
43+
infos = res["data"]["result"]
44+
print(infos)
4345

44-
path = f'target_info{{instance="{values[0]}"}}'
45-
path = urllib.parse.quote_plus(path)
46-
res = get(f"http://localhost:9090/api/v1/query?query={path}")
46+
# They should not have the same target info (e.g. only the agent has telemetry_distro_name)
47+
assert len(infos) == 2, "Expected two target info results"
4748

48-
infos = res["data"]["result"]
49-
print(infos)
5049

51-
# they should not have the same target info
52-
# e.g. only the agent has telemetry_distro_name
53-
assert len(infos) == 2
50+
if __name__ == "__main__":
51+
main()

0 commit comments

Comments
 (0)