Created
March 6, 2025 17:36
-
-
Save lukeyeager/0526e003eab16f7f608483638bfd31bd to your computer and use it in GitHub Desktop.
Python prometheus exporter w/ timestamps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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