-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathjson_to_prometheus.py
More file actions
36 lines (34 loc) · 996 Bytes
/
json_to_prometheus.py
File metadata and controls
36 lines (34 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import sys
import json
with open(sys.argv[1]) as metrics_raw:
metrics_json = json.load(metrics_raw)
# metrics is a json list e.g.:
# [
# {
# "metric": "total_classes",
# "labels": {
# "project": "guava",
# "fuzzing_ratio": 0.1
# },
# "value": 20
# },
# {
# "metric": "testcases_generated",
# "labels": {
# "project": "guava",
# "fuzzing_ratio": 0.1
# },
# "value": 1042
# }
# ]
#
# the loop below iterates over each list item and constructs metrics set
metrics_set_str = ""
for metric in metrics_json:
labels_set_str = ""
comma = ""
for label, value in metric['labels'].items():
labels_set_str = f'{labels_set_str}{comma}{label}=\"{value}\"'
comma = ","
metrics_set_str += f'{metric["metric"]}{{{labels_set_str}}} {metric["value"]}\n'
print(metrics_set_str)