-
Notifications
You must be signed in to change notification settings - Fork 130
/
api_limits_as_custom_metrics.py
95 lines (64 loc) · 1.77 KB
/
api_limits_as_custom_metrics.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
#-*- coding: utf-8 -*-
# stdlib
import time
# 3rd party
import requests
import datadog
api_key = '<API_KEY>'
app_key = '<APPLICATION KEY>'
options = {
'api_key': api_key,
'application_key': app_key
}
datadog.initialize(**options)
BASE_URL = 'https://api.datadoghq.com/api/v1/'
ENDPOINTS = [
'query', # https://docs.datadoghq.com/api/?lang=bash#query-time-series-points
'graph/snapshot', # https://docs.datadoghq.com/api/?lang=bash#graph-snapshot
'metrics', # https://docs.datadoghq.com/api/?lang=bash#get-list-of-active-metrics
]
PARAMS = {
'query': {
'from': int(time.time() - 3600),
'to': int(time.time()),
'query': 'system.cpu.idle{*}by{host}'
},
'graph/snapshot': {
'metric_query': 'system.load.1{*}',
'start': int(time.time() - 3600),
'end': int(time.time()),
},
'metrics': {
'from': int(time.time()) - 60
},
}
API_QUANTITIES = [
'X-RateLimit-Limit',
'X-RateLimit-Period',
'X-RateLimit-Remaining',
'X-RateLimit-Reset',
]
def merge_two_dicts(x, y):
z = x.copy() # start with x's keys and values
z.update(y) # modifies z with y's keys and values & returns None
return z
###### Get the data
payload = []
for endpoint in ENDPOINTS:
url = BASE_URL + endpoint
params = merge_two_dicts(options,PARAMS[endpoint])
# print "endpoint: %s" %endpoint
# print "url: %s" %url
# print "params"
# print params
res = requests.get(url, params=params)
# print "res.headers"
# print res.headers
for qu in API_QUANTITIES:
payload.append({
'metric': qu,
'points': int(res.headers[qu]),
'tags': ["endpoint:%s"%endpoint]
})
# Post metrics
datadog.api.Metric.send(payload)