I’d like to build a multi-target exporter, similar to the blackbox exporter, where it takes an IP address as part of the URL path and uses that to collect metrics from hardware devices. This would be my first time writing a Prometheus exporter. From the Python client examples, I’ve created a CustomCollector class which collects only when scraped, running with start_http_server. But I can’t figure out how to access the URL path as a variable. Is that possible with this method or do I need to use a different web server like Flask or WSGI? A very simple example is below.
from prometheus_client.core import GaugeMetricFamily, REGISTRY from prometheus_client.registry import Collector from prometheus_client import start_http_server import random import time class CustomCollector(Collector): def collect(self): """Yield metrics only when scraped""" metrics = self.read_from_device() for metric in metrics: yield GaugeMetricFamily(metric['name'], 'help text', value=metric['value']) def read_from_device(self): """Blocking code that reads and parses data from hardware""" return [ {'name': 'request_processing_seconds', 'value': random.random()}, ] REGISTRY.register(CustomCollector()) if __name__ == '__main__': start_http_server(port=8000) while True: time.sleep(86400)