This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate.py
65 lines (48 loc) · 1.85 KB
/
aggregate.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import sys
import json
data = json.load(sys.stdin)
# Tags that propagate from a segment to individual spans
PROPAGATION_TAGS = ["release", "environment", "transaction"]
# Tags that should be indexed
INDEXED_TAGS = ["db.operation", "db.statement"] + PROPAGATION_TAGS
# Measurement that get indexed
INDEXED_MEASUREMENTS = ["db.rows_returned", "duration"]
aggregator = {}
def clean(description):
return description
for batch_id, batch in data["batches"].items():
if batch_id:
segment = batch[-1]
assert segment["is_segment"]
else:
segment = None
for span in batch:
tags = {}
span.setdefault("measurements", {})["duration"] = (
span["end_time"] - span["start_time"]
)
tags["op"] = span["tags"]["op"]
tags["cleaned_description"] = clean(span["description"])
merged_tags = dict(span["tags"])
if segment is not None:
counter_key = span["tags"]["op"] + ".counter"
segment_measurements = segment.setdefault("measurements", {})
segment_measurements[counter_key] = (
segment_measurements.get(counter_key) or 0
) + 1
for tag in PROPAGATION_TAGS:
if tag in segment["tags"]:
merged_tags.setdefault(tag, segment["tags"][tag])
for tag in INDEXED_TAGS:
if tag in merged_tags:
tags[tag] = merged_tags[tag]
for key in INDEXED_MEASUREMENTS:
if key not in span["measurements"]:
continue
aggregator_key = (key, tuple(sorted(tags.items())))
aggregator.setdefault(aggregator_key, []).append(span["measurements"][key])
data["metrics"] = [
{"metric": x[0][0], "tags": dict(sorted(x[0][1])), "values": x[1]}
for x in sorted(aggregator.items())
]
print(json.dumps(data, indent=2))