{ config, lib, pkgs, ... }: let cfg = config.exporters; in { options.exporters = { sqlExporterPort = lib.mkOption { type = lib.types.int; }; }; imports = [ ]; config = { services.prometheus-sql-exporter = { enable = true; port = cfg.sqlExporterPort; config = { target = { # This URL should be postgresql:///postgres?host=/run/postgresql # but sql_exporter uses xo/dburl which isn't spec-compliant: https://github.com/xo/dburl/issues/46 data_source_name = "postgresql:/run/postgresql:/postgres"; collectors = ["db-sizes"]; }; collectors = [ { collector_name = "db-sizes"; metrics = [ { metric_name = "pg_db_size_bytes"; help = "disk space used by the database"; type = "gauge"; key_labels = ["database_name"]; values = ["size"]; query = "SELECT datname AS database_name, pg_database_size(datname) as size from pg_database"; } ]; } ]; }; }; monitoring.prometheusScrapeConfigs = [ { job_name = "sql"; static_configs = [{ targets = [ "localhost:${toString cfg.sqlExporterPort}" ]; }]; } ]; }; }