From 6578f1a626216717d582f0c4bbb3d4f62dddbbca Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Sat, 15 Nov 2025 22:37:50 +0100 Subject: [PATCH] feat: cleaning up some metrics & adding labels --- config/alloy/metrics.alloy.nix | 30 +++++++++++++++++++++++------- config/metrics-prometheus.nix | 16 ++++++++++++++-- config/proxy-traefik.nix | 31 +++++++++++++++++++++++++++++++ config/yarrr-arr.nix | 2 +- containers/db.nix | 4 +++- containers/dns.nix | 6 +++--- containers/yarrr.nix | 6 +++--- modules/containers.nix | 6 +++--- 8 files changed, 81 insertions(+), 20 deletions(-) diff --git a/config/alloy/metrics.alloy.nix b/config/alloy/metrics.alloy.nix index 9c5b21c..69cabfd 100644 --- a/config/alloy/metrics.alloy.nix +++ b/config/alloy/metrics.alloy.nix @@ -27,15 +27,31 @@ prometheus.relabel "filter_metrics" { rule { - action = "drop" - source_labels = [ "env" ] - regex = "dev" + source_labels = ["__name__"] + regex = ".*_build_info" + action = "drop" } rule { - action = "replace" - regex = "127\\.0\\.0\\.1" - target_label = "instance" - replacement = "${tools.build_ip container}" + source_labels = ["__name__"] + regex = "go_.*" + action = "drop" + } + rule { + source_labels = [ "env" ] + regex = "dev" + action = "drop" + } + rule { + target_label = "host" + replacement = "${tools.build_hostname container}" + } + rule { + target_label = "host_ip" + replacement = "${tools.build_ip container}" + } + rule { + target_label = "service" + replacement = "alloy" } forward_to = [prometheus.remote_write.metrics_service.receiver] } diff --git a/config/metrics-prometheus.nix b/config/metrics-prometheus.nix index 18619cc..465f75b 100644 --- a/config/metrics-prometheus.nix +++ b/config/metrics-prometheus.nix @@ -21,7 +21,14 @@ in { job_name = "prometheus"; static_configs = [ - { targets = [ "localhost:9090" ]; } + { + targets = [ "localhost:9090" ]; + labels = { + host = tools.build_hostname "metrics"; + host_ip = tools.build_ip "metrics"; + service = "prometheus"; + }; + } ]; } ] @@ -33,8 +40,13 @@ in in { job_name = container; - static_configs = map (port: { + static_configs = lib.mapAttrsToList (service: port: { targets = [ "${container_ip}:${toString port}" ]; + labels = { + host = tools.build_hostname container; + host_ip = tools.build_ip container; + service = service; + }; }) def.logging.prometheusPorts; } ) config.my-lxc diff --git a/config/proxy-traefik.nix b/config/proxy-traefik.nix index 8e05470..1f3c831 100644 --- a/config/proxy-traefik.nix +++ b/config/proxy-traefik.nix @@ -16,6 +16,37 @@ let ]; in { + environment.etc."alloy/logs-traefik.alloy".text = + (import ./alloy/default-journal-logger.alloy.nix { + inherit tools; + container = "proxy"; + service = "traefik"; + additional_stages = '' + stage.regex { + expression = "^(?P\\S+) (?P\\S+) (?P\\S+) \\[(?P[^\\]]+)\\] \"(?P\\S+) (?P\\S+) HTTP/(?P\\S+)\" (?P\\d+) (?P\\d+) \"(?P[^\"]*)\" \"(?P[^\"]*)\" (?P\\d+) \"(?P[^\"]*)\" \"(?P[^\"]*)\" (?P\\d+)ms$" + } + + stage.timestamp { + source = "timestamp" + format = "02/Jan/2006:15:04:05 -0700" + } + + stage.labels { + values = { + client_ip = "", + ident = "", + auth_id = "", + method = "", + status = "", + referrer = "", + router = "", + upstream = "", + } + } + + ''; + }).out; + services = { traefik = { enable = true; diff --git a/config/yarrr-arr.nix b/config/yarrr-arr.nix index 040c0a3..5b96586 100644 --- a/config/yarrr-arr.nix +++ b/config/yarrr-arr.nix @@ -5,7 +5,7 @@ ... }: { - environment.etc."yarrr.env".source = config.age.secrets.yarrr-env; + environment.etc."yarrr.env".source = config.age.secrets.yarrr-env.path; services = { bazarr = { diff --git a/containers/db.nix b/containers/db.nix index e93982c..8ad47a6 100644 --- a/containers/db.nix +++ b/containers/db.nix @@ -24,7 +24,9 @@ logging = { enable = true; metricsEnable = true; - prometheusPorts = [ 9187 ]; + prometheusPorts = { + postgres = 9187; + }; }; private = true; auth = true; diff --git a/containers/dns.nix b/containers/dns.nix index 6a7d53c..f2de34d 100644 --- a/containers/dns.nix +++ b/containers/dns.nix @@ -24,9 +24,9 @@ logging = { enable = true; metricsEnable = true; - prometheusPorts = [ - 9167 - ]; + prometheusPorts = { + unbound = 9167; + }; }; private = true; auth = true; diff --git a/containers/yarrr.nix b/containers/yarrr.nix index 299823b..f97f370 100644 --- a/containers/yarrr.nix +++ b/containers/yarrr.nix @@ -30,9 +30,9 @@ in logging = { enable = true; metricsEnable = true; - prometheusPorts = [ - 9708 - ]; + prometheusPorts = { + yarrr = 9708; + }; }; otherDomains = [ { diff --git a/modules/containers.nix b/modules/containers.nix index 0425acd..0e94065 100644 --- a/modules/containers.nix +++ b/modules/containers.nix @@ -142,9 +142,9 @@ in enable = mkEnableOption "Whether to enable default logs collection"; metricsEnable = mkEnableOption "Whether to enable default metrics collection"; prometheusPorts = mkOption { - type = listOf int; - description = "Ports of Prometheus Exporters"; - default = [ ]; + type = attrsOf int; + description = "Service => ports of Prometheus Exporters"; + default = { }; }; alloyConfig = mkOption { type = attrsOf path;