From 1693f644eee5cbf24425edfc06e5ef2da772a88d Mon Sep 17 00:00:00 2001 From: emilylange Date: Mon, 8 Jul 2024 00:34:30 +0200 Subject: [PATCH] grafana-agent: make `bagel.monitoring.grafana-agent.exporters` an attrset This allows us to use multiple jobs, one for each additional exporter, and set their `job_name` accordingly. `job_name` is exported as `job` label on the resulting metrics. This allows us to quickly get an understanding what metrics of an exporter are actually available by simply filtering all metrics by `{job="$jobname"}` --- services/monitoring/agent.nix | 26 +++++++++++++--------- services/monitoring/exporters/cadvisor.nix | 2 +- services/monitoring/exporters/nginx.nix | 4 +--- services/monitoring/exporters/postgres.nix | 4 +--- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/services/monitoring/agent.nix b/services/monitoring/agent.nix index 3fc2e0d..17274ba 100644 --- a/services/monitoring/agent.nix +++ b/services/monitoring/agent.nix @@ -13,14 +13,20 @@ in enable = (mkEnableOption "Grafana Agent") // { default = true; }; exporters = mkOption { - description = "List of all exporters to scrape"; - type = types.listOf (types.submodule { + description = '' + Set of additional exporters to scrape. + + The attribute name will be used as `job_name` + internally, which ends up exported as `job` label + on all metrics of that exporter. + ''; + type = types.attrsOf (types.submodule { options.port = mkOption { description = "Exporter port"; type = types.int; }; }); - default = []; + default = {}; }; }; @@ -45,14 +51,12 @@ in configs = [ { name = config.networking.hostName; - scrape_configs = [ - { - job_name = config.networking.hostName; - static_configs = [ - { targets = map (e: "localhost:" + (toString e.port)) config.bagel.monitoring.grafana-agent.exporters; } - ]; - } - ]; + scrape_configs = lib.mapAttrsToList (name: value: { + job_name = name; + static_configs = [ + { targets = "localhost:" + (toString value.port); } + ]; + }) config.bagel.monitoring.grafana-agent.exporters; } ]; }; diff --git a/services/monitoring/exporters/cadvisor.nix b/services/monitoring/exporters/cadvisor.nix index 285eaae..6f2bfba 100644 --- a/services/monitoring/exporters/cadvisor.nix +++ b/services/monitoring/exporters/cadvisor.nix @@ -17,6 +17,6 @@ in listenAddress = "0.0.0.0"; }; - bagel.monitoring.grafana-agent.exporters = [ { port = 9102; } ]; + bagel.monitoring.grafana-agent.exporters.cadvisor.port = 9102; }; } diff --git a/services/monitoring/exporters/nginx.nix b/services/monitoring/exporters/nginx.nix index 1f190c7..e066c79 100644 --- a/services/monitoring/exporters/nginx.nix +++ b/services/monitoring/exporters/nginx.nix @@ -30,8 +30,6 @@ in ]; }; - bagel.monitoring.grafana-agent.exporters = [ - { port = 9103; } - ]; + bagel.monitoring.grafana-agent.exporters.nginxlog.port = 9103; }; } \ No newline at end of file diff --git a/services/monitoring/exporters/postgres.nix b/services/monitoring/exporters/postgres.nix index 8ce3f45..c6a9469 100644 --- a/services/monitoring/exporters/postgres.nix +++ b/services/monitoring/exporters/postgres.nix @@ -24,8 +24,6 @@ in services.postgresql.settings.shared_preload_libraries = "pg_stat_statements"; - bagel.monitoring.grafana-agent.exporters = [ - { port = 9104; } - ]; + bagel.monitoring.grafana-agent.exporters.postgres.port = 9104; }; } \ No newline at end of file