refactor(monitoring): static exporter discovery

This commit is contained in:
arcnmx 2024-05-30 13:41:24 -07:00
parent 452e76e173
commit 4d1a542384
2 changed files with 97 additions and 20 deletions

View file

@ -5,26 +5,37 @@
...
}: let
inherit (gensokyo-zone) systems;
inherit (lib.attrsets) filterAttrs mapAttrsToList;
inherit (lib.modules) mkDefault;
inherit (lib.attrsets) attrValues;
inherit (lib.lists) filter concatMap;
nodeExporterSystems =
filterAttrs (
_: system:
filter (
system:
system.config.access.online.enable &&
system.config.exports.services.prometheus-exporters-node.enable
system.config.exports.prometheus.exporter.services != [ ]
)
systems;
(attrValues systems);
mkPortTarget = { system, service, portName }: let
port = service.ports.${portName};
in "${access.getAddressFor system.config.name "lan"}:${toString port.port}";
mkServiceConfig = system: serviceName: let
service = system.config.exports.services.${serviceName};
targets = map (portName: mkPortTarget {
inherit system service portName;
}) service.prometheus.exporter.ports;
in {
job_name = "${system.config.name}-${service.id}";
static_configs = [
{
inherit targets;
labels = mkDefault service.prometheus.exporter.labels;
}
];
};
mapSystem = system: map (mkServiceConfig system) system.config.exports.prometheus.exporter.services;
in {
services.prometheus = {
port = 9090;
scrapeConfigs =
mapAttrsToList (_: system: {
job_name = "${system.config.name}-node-exporter";
static_configs = [ {
targets = [
"${access.getAddressFor system.config.name "local"}:${toString system.config.exports.services.prometheus-exporters-node.ports.default.port}"
];
} ];
})
nodeExporterSystems;
port = mkDefault 9090;
scrapeConfigs = concatMap mapSystem nodeExporterSystems;
};
}

View file

@ -1,11 +1,56 @@
{
lib,
let
portModule = {
lib,
...
}: let
inherit (lib.options) mkEnableOption;
in {
options.prometheus = with lib.types; {
exporter.enable = mkEnableOption "prometheus metrics endpoint";
};
};
serviceModule = {
config,
lib,
...
}: let
inherit (lib.options) mkOption;
inherit (lib.modules) mkOptionDefault;
inherit (lib.attrsets) attrNames filterAttrs;
exporterPorts = filterAttrs (_: port: port.enable && port.prometheus.exporter.enable) config.ports;
in {
options = with lib.types; {
prometheus = {
exporter = {
ports = mkOption {
type = listOf str;
};
labels = mkOption {
type = attrsOf str;
default = { };
};
};
};
ports = mkOption {
type = attrsOf (submoduleWith {
modules = [portModule];
});
};
};
config.prometheus = {
exporter.ports = mkOptionDefault (attrNames exporterPorts);
};
};
in {
config,
gensokyo-zone,
lib,
...
}: let
inherit (gensokyo-zone.lib) mapListToAttrs mapAlmostOptionDefaults mkAlmostOptionDefault;
inherit (lib.modules) mkIf;
inherit (lib.attrsets) nameValuePair;
inherit (lib.options) mkOption;
inherit (lib.modules) mkIf mkOptionDefault;
inherit (lib.attrsets) attrNames filterAttrs nameValuePair;
mkExporter = { name, port }: nameValuePair "prometheus-exporters-${name}" ({config, ...}: {
nixos = {
serviceAttrPath = ["services" "prometheus" "exporters" name];
@ -19,12 +64,31 @@
ports.default = mapAlmostOptionDefaults {
inherit port;
protocol = "http";
} // {
prometheus.exporter.enable = true;
};
});
exporters = mapListToAttrs mkExporter [
{ name = "node"; port = 9091; }
];
in {
options.exports = with lib.types; {
prometheus = {
exporter.services = mkOption {
type = listOf str;
};
};
services = mkOption {
type = attrsOf (submoduleWith {
modules = [serviceModule];
});
};
};
config.exports.prometheus = let
exporterServices = filterAttrs (_: service: service.enable && service.prometheus.exporter.ports != [ ]) config.exports.services;
in {
exporter.services = mkOptionDefault (attrNames exporterServices);
};
config.exports.services = {
prometheus = {config, ...}: {
id = mkAlmostOptionDefault "prometheus";
@ -88,6 +152,8 @@ in {
ports.default = mapAlmostOptionDefaults {
port = 9094;
protocol = "http";
} // {
prometheus.exporter.enable = true;
};
};
} // exporters;