|
1 | 1 | #!/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 | +""" |
6 | 6 | import json |
7 | 7 | import urllib.parse |
8 | 8 | from urllib.request import urlopen |
9 | 9 |
|
10 | 10 |
|
11 | | -def get(url): |
12 | | - global response, res |
| 11 | +def get_json(url): |
13 | 12 | 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")) |
19 | 14 |
|
20 | 15 |
|
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") |
22 | 19 |
|
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) |
26 | 23 |
|
27 | | -values = list( |
28 | | - { |
| 24 | + instance_ids = { |
29 | 25 | r["metric"]["instance"] |
30 | 26 | for r in res["data"]["result"] |
31 | | - if not r["metric"]["service_name"] == "otelcol-contrib" |
| 27 | + if r["metric"].get("service_name") != "otelcol-contrib" |
32 | 28 | } |
33 | | -) |
34 | | -print(values) |
| 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" |
| 38 | + |
| 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}") |
35 | 42 |
|
36 | | -# both the agent and the exporter should report the same instance id |
37 | | -assert len(values) == 1 |
| 43 | + infos = res["data"]["result"] |
| 44 | + print(infos) |
38 | 45 |
|
39 | | -path = f'target_info{{instance="{values[0]}"}}' |
40 | | -path = urllib.parse.quote_plus(path) |
41 | | -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" |
42 | 48 |
|
43 | | -infos = res["data"]["result"] |
44 | | -print(infos) |
45 | 49 |
|
46 | | -# they should not have the same target info |
47 | | -# e.g. only the agent has telemetry_distro_name |
48 | | -assert len(infos) == 2 |
| 50 | +if __name__ == "__main__": |
| 51 | + main() |
0 commit comments