Skip to content

Instantly share code, notes, and snippets.

@Signorini
Created July 11, 2018 14:27
Show Gist options
  • Save Signorini/87966ac7f8bebb0cfd318358ece8eb85 to your computer and use it in GitHub Desktop.
Save Signorini/87966ac7f8bebb0cfd318358ece8eb85 to your computer and use it in GitHub Desktop.
Fetch a metric query JSON from DataDog and submit it to a Cachet server so it can be graphed.
#! /usr/bin/python
from datadog import initialize, api
import requests
import time
# DataDog bootstrap
options = {
'api_key': 'yourkeyhere',
'app_key': 'yourkeyhere'
}
now = int(time.time())
query = 'DataDog query for what you want to graph'
# Cachet bootstrap
url = "https://demo.cachethq.io/api/v1/metrics/id of metric that you want to submit data for/points"
api_token = 'youtokenhere'
header = {'X-Cachet-Token': api_token, 'Content-Type': 'application/json'}
# Poll DataDog and parse for the data we're interested in
initialize(**options)
response = api.Metric.query(start=now - 90, end=now, query=query) # gets last 90 seconds of data adjust as you see fit
data = response.get('series')[0].get('pointlist')
# Loop over data pairs and submit them to Cachet
for datum in data:
timestamp = int(round(datum[0] / 1000, 0))
response_time = round(datum[1] * 1000, 2)
payload = "{\"value\":%s,\"timestamp\":\"%s\"}" % (response_time, timestamp)
response = requests.request("POST", url, headers=header, data=payload)
# Catch errors and print them to STDOUT
if response.status_code != 200:
print(response.text)
print ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment