From 1948264b92c88ebfe5176a4d61a23f01b457009a Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Fri, 21 May 2021 13:26:23 +0200 Subject: [PATCH] Add script to generate time series The text import format supports specifying a time stamp [1]. Use this to generate a time series for thing_count and thing_failures. ./things-series > http/metrics podman pod rm -f learn-metrics || true podman play kube learn-metrics.yaml First non-trivial query from "indefinitely long lookback" counters: "How many tests happened in the last 5 minutes? rate(thing_count{job="host-http"}[5m]) * 5 * 60 The re-scaling is because the natural unit is "1/s" and we want the unit to be the same as the original counter. [1] https://prometheus.io/docs/instrumenting/exposition_formats/ --- things-series | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 things-series diff --git a/things-series b/things-series new file mode 100755 index 0000000..c67cd62 --- /dev/null +++ b/things-series @@ -0,0 +1,28 @@ +#!/usr/bin/python3 +import time + +# time series in 5s steps +series = { + 'thing_count': { + 'type': 'counter', + 'help': 'how many things ran', + 'series': [0] * 6 + [1] * 6 + [2] * 6 + [7] * 12 + [8] * 12 + }, + 'thing_failures': { + 'type': 'counter', + 'help': 'how many things failed', + 'series': [0] * 12 + [1] * 6 + [5] * 24 + }, +} + +num = len(series[list(series)[0]]['series']) +now = int(time.time() * 1000) +timestamps = [now - 5000 * (num - i) for i in range(num)] + +for metric, data in series.items(): + assert len(data['series']) == num + print(f'# TYPE {metric} {data["type"]}') + print(f'# HELP {metric} {data["help"]}') + for (time, datum) in zip(timestamps, data['series']): + print(f'{metric} {datum} {time}') + print() -- 2.39.2