This repository has been archived by the owner on Aug 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
send_to_datadog.py
executable file
·108 lines (89 loc) · 3.37 KB
/
send_to_datadog.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import json
# from pprint import pprint
import os
import sys
from datadog import api, initialize, statsd
options = {
"api_key": os.getenv("DATADOG_API_KEY"),
"app_key": os.getenv("DATADOG_APP_KEY"),
"statsd_host": "localhost",
"statsd_port": "8125",
}
def main(path):
initialize(**options)
dbl_name = "WebPageTest"
dbls = api.DashboardList.get_all()["dashboard_lists"]
# pprint(dbls)
try:
dbl = next(dbl for dbl in dbls if dbl["name"] == dbl_name)
print(f"Using existing {dbl_name} dashboard list")
except StopIteration:
print(f"Creating {dbl_name} dashboard list")
dbl = api.DashboardList.create(name=dbl_name)
tbdata = {}
tbs = api.Timeboard.get_all()["dashes"]
# pprint(tbs)
with open(path) as f:
data = json.load(f)
with open("metrics.json") as f:
metrics = json.load(f)
for test in data:
target_url = test["data"]["testUrl"]
tb = tbdata.setdefault(target_url, {})
tb["title"] = target_url
tb["description"] = f"WebPageTest results for {target_url}"
graphs = tb.setdefault("graphs", [])
sample = test["data"]["median"]["firstView"]
browser_name = sample["browser_name"]
browser_version = sample["browser_version"]
label = test["data"]["label"].replace("-", "_")
print(f"{target_url} - {browser_name} ({browser_version})")
requests = []
for metric in metrics:
name = metric["name"]
title = f"{metric['description']} ({metric['unit']})"
query = f"avg:wpt.batch.{label}.median.firstView.{name}{{*}}"
try:
graph = next(g for g in graphs if g["title"] == title)
requests = graph["definition"]["requests"]
if query not in requests:
requests.append({"q": query})
except StopIteration:
graphs.append(
{
"title": title,
"definition": {"requests": [{"q": query}], "viz": "timeseries"},
}
)
value = test["data"]["median"]["firstView"][name]
print(f"- {name}: {value} ({metric['unit']})")
statsd.gauge(f"wpt.batch.{label}.median.firstView.{name}", value)
summary = test["data"]["summary"]
print(summary)
print("=====================")
# pprint(tb)
for item in tbdata.values():
title = item["title"]
description = item["description"]
graphs = item["graphs"]
try:
tb = next(tb for tb in tbs if tb["title"] == title)
print(f"Updating {title} timeboard")
tb = api.Timeboard.update(
tb["id"], title=title, description=description, graphs=graphs
)
except StopIteration:
print(f"Creating {title} timeboard")
tb = api.Timeboard.create(
title=title, description=description, graphs=graphs
)
# pprint(tb)
print(f"Adding {title} timeboard to {dbl_name} dashboard list")
api.DashboardList.add_items(
dbl["id"], dashboards=[{"type": "custom_timeboard", "id": tb["dash"]["id"]}]
)
if __name__ == "__main__":
if not len(sys.argv) == 2:
print("Usage: python send_to_datadog.py path")
exit()
main(*sys.argv[1:])