Skip to content

Instantly share code, notes, and snippets.

@posilva
Created June 8, 2020 15:50
Show Gist options
  • Save posilva/40ed2488095fe2a34f70d81621e61e74 to your computer and use it in GitHub Desktop.
Save posilva/40ed2488095fe2a34f70d81621e61e74 to your computer and use it in GitHub Desktop.
Get Datadog Top Metrics
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