import logging import time from prometheus_client import Gauge, REGISTRY, write_to_textfile from sqlalchemy import create_engine, text from sqlalchemy.engine import Engine logging.basicConfig(level=logging.INFO) log = logging.getLogger("my_textfile_exporter") # Prometheus metrics WEIGHT_GAUGE = Gauge( "mysql_instance_weight", "Weight of the MariaDB instance", ["hostname", "instance_group", "section", "role"], ) def fetch(engine: Engine, sql: str, params: dict | None = None) -> list[dict]: with engine.begin() as tx: result = tx.execute(text(sql), params or {}) return [dict(r._mapping) for r in result.fetchall()] def main() -> None: uri = "mysql+pymysql://user:pass@zarcillo.db.domain/zarcillo" output_path = "/var/lib/prometheus/node-exporter/mystuff.prom" engine: Engine = create_engine(uri, pool_pre_ping=True, future=True) sql = """ SELECT a,b,c,myval FROM ... """ while True: try: rows = fetch(engine, sql) # We expect one row, myval is the metric value, everything else is a label row = rows[0] val = row.pop("myval") WEIGHT_GAUGE.clear() WEIGHT_GAUGE.labels(**row).set(float(val)) # Write atomically write_to_textfile(output_path, REGISTRY) log.debug(f"Successfully wrote metrics to {output_path}") except Exception as e: log.error(f"Failed to generate metrics file: {e}") raise time.sleep(60) if __name__ == "__main__": main()