prometheus-scrape-target-labels #30

Merged
torjus merged 5 commits from prometheus-scrape-target-labels into master 2026-02-07 16:27:38 +00:00
Showing only changes of commit 23e561cf49 - Show all commits

View File

@@ -57,9 +57,11 @@ let
labels = hostConfig'.labels or { }; labels = hostConfig'.labels or { };
}; };
# Build effective labels for a host (only include non-default values) # Build effective labels for a host
# Always includes hostname; only includes tier/priority/role if non-default
buildEffectiveLabels = host: buildEffectiveLabels = host:
(lib.optionalAttrs (host.tier != "prod") { tier = host.tier; }) { hostname = host.hostname; }
// (lib.optionalAttrs (host.tier != "prod") { tier = host.tier; })
// (lib.optionalAttrs (host.priority != "high") { priority = host.priority; }) // (lib.optionalAttrs (host.priority != "high") { priority = host.priority; })
// (lib.optionalAttrs (host.role != null) { role = host.role; }) // (lib.optionalAttrs (host.role != null) { role = host.role; })
// host.labels; // host.labels;
@@ -73,6 +75,10 @@ let
lib.mapAttrsToList extractHostMonitoring nixosConfigs lib.mapAttrsToList extractHostMonitoring nixosConfigs
); );
# Extract hostname from a target string like "gunter.home.2rjus.net:9100"
extractHostnameFromTarget = target:
builtins.head (lib.splitString "." target);
# Build target entries with labels for each host # Build target entries with labels for each host
flakeEntries = map flakeEntries = map
(host: { (host: {
@@ -81,9 +87,12 @@ let
}) })
hostList; hostList;
# External targets have no labels # External targets get hostname extracted from the target string
externalEntries = map externalEntries = map
(target: { inherit target; labels = { }; }) (target: {
inherit target;
labels = { hostname = extractHostnameFromTarget target; };
})
(externalTargets.nodeExporter or [ ]); (externalTargets.nodeExporter or [ ]);
allEntries = flakeEntries ++ externalEntries; allEntries = flakeEntries ++ externalEntries;
@@ -94,13 +103,13 @@ let
grouped = lib.groupBy labelKey allEntries; grouped = lib.groupBy labelKey allEntries;
# Convert groups to static_configs format # Convert groups to static_configs format
# Every flake host now has at least a hostname label
staticConfigs = lib.mapAttrsToList staticConfigs = lib.mapAttrsToList
(key: entries: (key: entries:
let let
labels = (builtins.head entries).labels; labels = (builtins.head entries).labels;
in in
{ targets = map (e: e.target) entries; } { targets = map (e: e.target) entries; labels = labels; }
// (lib.optionalAttrs (labels != { }) { inherit labels; })
) )
grouped; grouped;
in in
@@ -143,6 +152,7 @@ let
labelKey = t: builtins.toJSON t.hostLabels; labelKey = t: builtins.toJSON t.hostLabels;
groupedByLabels = lib.groupBy labelKey targets; groupedByLabels = lib.groupBy labelKey targets;
# Every flake host now has at least a hostname label
staticConfigs = lib.mapAttrsToList staticConfigs = lib.mapAttrsToList
(key: labelTargets: (key: labelTargets:
let let
@@ -151,8 +161,7 @@ let
(t: "${t.hostname}.home.2rjus.net:${toString t.port}") (t: "${t.hostname}.home.2rjus.net:${toString t.port}")
labelTargets; labelTargets;
in in
{ targets = targetAddrs; } { targets = targetAddrs; labels = labels; }
// (lib.optionalAttrs (labels != { }) { inherit labels; })
) )
groupedByLabels; groupedByLabels;