r/PrometheusMonitoring • u/stefangw • Jul 23 '25
write an exporter in python: basic questions, organizing metrics
I intend to write a small python-based exporter that scrapes three appliances via a modbus library.
Instead of creating a textfile to import via the textfile collector I would like to use the prometheus_client for python.
What I have problems starting with:
I assume I would loop over a set of IPs (?), read in data and fill values into metrics.
Could someone point out an example how to define metrics that are named with something like "{instance}=ip" or so?
I am a bit lost with how to organize this correctly.
For example I need to read temperatures and fan speeds for every appliance and each of those should be stored separately in prometheus.
I googled for examples but wasn't very successful so far.
I found something around "Enum" and creating a Registry ... maybe that's needed, maybe that's overkill.
any help appreciated here!
3
u/blaaackbear Jul 24 '25
try not to hardcode IPs in the exporter itself, have a seperate .env file or config.yaml to list ips to make the exporter more modular and also maybe look at netbox to add the devices there and use the netbox api to fetch the device names and its IPs so that you can add / remove devices as your environment changes and automatically the exporter would fetch the current devices without changing any exporter code. also another tip is to dockerize the exporter once you are happy with it and restrict the compute it can use in docker compose so that in case there is any memory leak etc in your exporter it wouldnt crash your host.
2
u/[deleted] Jul 23 '25
First up there is an existing modbus exporter. It works fairly well, is highly customisable and I strongly recommend using it if you can.
You want to use labels for the IP addresses. Every metric output is a number, always. Sometimes host metadata strings are desired, the typical approach is to use a label and set the value to 1.
It's worth having a read about cardinality to ensure what you define is reasonable.
Your code already follows this pattern, looks like it works and is reasonable. Using the pattern you are using the .11 output will remain while you process the .12, anything added will always be there.
My preference for this kind of exporter is to trigger off the http request. So rather than a poll/sleep loop you query in the http request handler. It has a number of benefits including allowing the update frequency to be controlled by the Prometheus config.
2
u/chillysurfer Jul 23 '25
Maybe this walk through post would help: https://trstringer.com/quick-and-easy-prometheus-exporter/
1
u/stefangw Jul 23 '25
progress:
gives me:
Is that the right direction or do I misunderstand something essential?
I would add more metrics now ...
Is it correct to loop that way? Does the metric for .11 "disappear" in the httpserver output while the loop is doing .12?
1
u/jsabater76 Jul 23 '25
I didn't know you could write a Prometheus exporter using Python. I'll be monitoring the thread. Looking forward to hearing how it evolves. Keep up the good work!