Created
June 8, 2020 15:50
-
-
Save posilva/40ed2488095fe2a34f70d81621e61e74 to your computer and use it in GitHub Desktop.
Get Datadog Top Metrics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import urllib.parse | |
import urllib.request | |
import json | |
import os | |
import csv, sys | |
from urllib.error import HTTPError | |
def http_get(path): | |
try: | |
headers = { | |
'Content-Type': 'application/json', | |
'DD-API-KEY': os.getenv('DD_API_KEY'), | |
'DD-APPLICATION-KEY': os.getenv('DD_APPLICATION_KEY') | |
} | |
url = 'https://api.datadoghq.com/api/v1/'+path | |
req = urllib.request.Request(url=url, headers=headers) | |
response = urllib.request.urlopen(req) | |
result = response.read() | |
return json.loads(result) | |
except HTTPError as err: | |
if err.code == 403: | |
print(err.read().decode()) | |
else: | |
raise | |
t = http_get("usage/top_avg_metrics?month=2020-05") | |
if t is not None: | |
data = t['usage'] | |
output = csv.writer(sys.stdout) | |
output.writerow(data[0].keys()) | |
for metric in data: | |
output.writerow(metric.values()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment