forked from jupyterhub/mybinder.org-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-grafana-annotation.py
67 lines (57 loc) · 1.55 KB
/
post-grafana-annotation.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
#!/usr/bin/env python3
"""
Script to post [Grafana Annotations](http://docs.grafana.org/reference/annotations/)
This is primarily used to annotate deployments in Grafana,
which can be very useful when displayed alongside various graphs.
This script requires:
- An environment variable GRAFANA_API_KEY with a grafana
[API Key](http://docs.grafana.org/http_api/auth/#create-api-token)
with at least Editor permissions
- The requests library
"""
import argparse
import requests
import os
import time
def create_annotation(grafana_url, grafana_api_key, tags, text):
"""
Create annotation in a grafana instance.
"""
return requests.post(
grafana_url + "/api/annotations",
json={
'tags': tags,
'text': text,
'time': int(time.time() * 1000),
'isRegion': False
},
headers={
'Authorization': f'Bearer {grafana_api_key}'
}
).text
def main():
argparser = argparse.ArgumentParser()
argparser.add_argument(
'--grafana-url',
help='URL of the grafana instance to use'
)
argparser.add_argument(
'--tag',
help='Tags to add to the annotation',
default=[],
action='append',
dest='tags'
)
argparser.add_argument(
'text',
help='Text to use for the annotation'
)
args = argparser.parse_args()
print(create_annotation(
args.grafana_url,
os.environ['GRAFANA_API_KEY'],
args.tags,
args.text
))
if __name__ == '__main__':
main()