From c0d8abd8029b7fa969794ca8170610d34ceaf847 Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Mon, 29 Aug 2016 18:26:47 +0100 Subject: [PATCH 1/5] Link to lovoo exporter. --- jenkins_exporter/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/jenkins_exporter/README.md b/jenkins_exporter/README.md index 3bba6c7..619e0a8 100644 --- a/jenkins_exporter/README.md +++ b/jenkins_exporter/README.md @@ -3,3 +3,5 @@ This is an example of how to write an exporter for Prometheus in Python. For more information see http://www.robustperception.io/writing-a-jenkins-exporter-in-python + +For production use, we recommend https://github.com/lovoo/jenkins_exporter which improves on this example. From 90c251dc8601c34476e8ad3a632e612c339c4600 Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Sun, 30 Oct 2016 20:12:25 +0000 Subject: [PATCH 2/5] Add example program to output queries in CSV --- csv/README.md | 8 ++++++++ csv/query_csv.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 csv/README.md create mode 100644 csv/query_csv.py diff --git a/csv/README.md b/csv/README.md new file mode 100644 index 0000000..01070de --- /dev/null +++ b/csv/README.md @@ -0,0 +1,8 @@ +# CSV examples + +This directory contains brief example programs that output +data from Prometheus in a CSV format. + +It is presumed you have the `requests` package installed. + +For more information see https://www.robustperception.io/prometheus-query-results-as-csv diff --git a/csv/query_csv.py b/csv/query_csv.py new file mode 100644 index 0000000..519f028 --- /dev/null +++ b/csv/query_csv.py @@ -0,0 +1,35 @@ +import csv +import requests +import sys + +""" +A simple program to print the result of a Prometheus query as CSV. +""" + +if len(sys.argv) != 3: + print('Usage: {0} http://prometheus:9090 a_query'.format(sys.argv[0])) + sys.exit(1) + +response = requests.get('{0}/api/v1/query'.format(sys.argv[1]), + params={'query': sys.argv[2]}) +results = response.json()['data']['result'] + +# Build a list of all labelnames used. +labelnames = set() +for result in results: + labelnames.update(result['metric'].keys()) + +# Canonicalize +labelnames.remove('__name__') +labelnames = sorted(labelnames) + +writer = csv.writer(sys.stdout) +# Write the header, +writer.writerow(['name', 'timestamp', 'value'] + labelnames) + +# Write the sanples. +for result in results: + l = [result['metric'].get('__name__', '')] + result['value'] + for label in labelnames: + l.append(result['metric'].get(label, '')) + writer.writerow(l) From 9744e7849267d2cf6dabfae7927ade9476bcd52f Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Sun, 30 Oct 2016 20:17:38 +0000 Subject: [PATCH 3/5] Allow for no metric name --- csv/query_csv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/csv/query_csv.py b/csv/query_csv.py index 519f028..f50c766 100644 --- a/csv/query_csv.py +++ b/csv/query_csv.py @@ -20,7 +20,7 @@ labelnames.update(result['metric'].keys()) # Canonicalize -labelnames.remove('__name__') +labelnames.discard('__name__') labelnames = sorted(labelnames) writer = csv.writer(sys.stdout) From aa60711b7cc70d32435b296248dbf24db6fb93f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sylvain=20Veyri=C3=A9?= Date: Thu, 24 May 2018 11:13:53 +0200 Subject: [PATCH 4/5] Fix None case --- jenkins_exporter/jenkins_exporter.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/jenkins_exporter/jenkins_exporter.py b/jenkins_exporter/jenkins_exporter.py index 3ab8176..2eaf234 100755 --- a/jenkins_exporter/jenkins_exporter.py +++ b/jenkins_exporter/jenkins_exporter.py @@ -50,7 +50,10 @@ def collect(self): name = job['name'] for s in statuses: # If there's a null result, we want to export zeros. - status = job[s] or {} + if s in job and job[s]: + status=job[s] + else: + status={} metrics[s]['number'].add_metric([name], status.get('number', 0)) metrics[s]['duration'].add_metric([name], status.get('duration', 0) / 1000.0) metrics[s]['timestamp'].add_metric([name], status.get('timestamp', 0) / 1000.0) From a7edf15a41521296bd699edba528969eb5640e6d Mon Sep 17 00:00:00 2001 From: yngwiewang <64290793+yngwiewang@users.noreply.github.com> Date: Sun, 3 Oct 2021 16:52:44 +0800 Subject: [PATCH 5/5] fix a typo --- csv/query_csv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/csv/query_csv.py b/csv/query_csv.py index f50c766..43355f2 100644 --- a/csv/query_csv.py +++ b/csv/query_csv.py @@ -27,7 +27,7 @@ # Write the header, writer.writerow(['name', 'timestamp', 'value'] + labelnames) -# Write the sanples. +# Write the samples. for result in results: l = [result['metric'].get('__name__', '')] + result['value'] for label in labelnames: