justwatchcom / sql_exporter

Flexible SQL Exporter for Prometheus.
MIT License
393 stars 108 forks source link

Any chances to get a primary/standby label into the metrics? #53

Open mfuhrmann opened 2 years ago

mfuhrmann commented 2 years ago

I am running the SQL exporter on a PostgreSQL cluster and by default, my collected metrics get several labels added. Also, a hostname label which makes it easy to work with the values of the primary PostgreSQL cluster node. But only, if the primary role is always running on the same node. This works of course, because I know that host XYZ is usually the primary host.

But if you have a failover or maintenance, the primary role could be on another host. So I would have to change all my queries in Alertmanager or Grafana to point to another hostname. This makes of course no sense. For these cases, it would be awesome to have a label in the metrics that reflect if this metric comes from a primary or a standby/potential node.

For example the query:

postgres=# select pg_is_in_recovery();
pg_is_in_recovery
-------------------
t
(1 row)

would identify if this host is a primary or not. It returns True if recovery is still in progress(so the server is running in standby mode or slave).

Is there a way to get this label into a query definition?