Skip to content

Instantly share code, notes, and snippets.

@lukeyeager
Created March 6, 2025 17:36
Show Gist options
  • Save lukeyeager/0526e003eab16f7f608483638bfd31bd to your computer and use it in GitHub Desktop.
Save lukeyeager/0526e003eab16f7f608483638bfd31bd to your computer and use it in GitHub Desktop.
Python prometheus exporter w/ timestamps
#!/usr/bin/env python3
import time
import prometheus_client
import prometheus_client.core
class Collector(prometheus_client.registry.Collector):
def collect(self):
now = time.time()
now_1s = int(round(now))
now_10s = now_1s - (now_1s % 10)
m = prometheus_client.core.GaugeMetricFamily('time_mod_1', '')
m.add_metric([], now_1s, timestamp=now_1s)
yield m
m = prometheus_client.core.GaugeMetricFamily('time_mod_10', '')
m.add_metric([], now_10s, timestamp=now_10s)
yield m
registry = prometheus_client.CollectorRegistry()
registry.register(Collector())
_, thread = prometheus_client.start_http_server(registry=registry, port=8080, addr='0.0.0.0')
thread.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment