Skip to content

Instantly share code, notes, and snippets.

Forked from aneeshkp/
Last active April 1, 2019 18:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
# from
import csv
import requests
import sys
def GetMetrixNames(url):
response = requests.get('{0}/api/v1/label/__name__/values'.format(url))
results = response.json()['data']
#Return metrix
return results
A simple program to print the result of a Prometheus query as CSV.
writer = csv.writer(sys.stdout)
# Write the header,
if len(sys.argv) != 2:
print('Usage: {0} http://prometheus:9090'.format(sys.argv[0]))
for metrixResult in metrixResults:
if metrixResult.startswith("collectd"):
response = requests.get('{0}/api/v1/query'.format(sys.argv[1]),
params={'query': metrixResult+'[1h]'})
results = response.json()['data']['result']
# Build a list of all labelnames used.
#gets all keys and discard __name__
labelnames = set()
for result in results:
# Canonicalize
labelnames = sorted(labelnames)
# Write the samples.
if writeHeader:
writer.writerow(['name', 'timestamp', 'value'] + labelnames)
for result in results:
l = [result['metric'].get('__name__', '')] + result['values']
for label in labelnames:
l.append(result['metric'].get(label, ''))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment