mirror of
https://github.com/gensokyo-zone/infrastructure.git
synced 2026-02-09 12:29:19 -08:00
refactor(monitoring): static exporter discovery
This commit is contained in:
parent
452e76e173
commit
4d1a542384
2 changed files with 97 additions and 20 deletions
|
|
@ -5,26 +5,37 @@
|
||||||
...
|
...
|
||||||
}: let
|
}: let
|
||||||
inherit (gensokyo-zone) systems;
|
inherit (gensokyo-zone) systems;
|
||||||
inherit (lib.attrsets) filterAttrs mapAttrsToList;
|
inherit (lib.modules) mkDefault;
|
||||||
|
inherit (lib.attrsets) attrValues;
|
||||||
|
inherit (lib.lists) filter concatMap;
|
||||||
nodeExporterSystems =
|
nodeExporterSystems =
|
||||||
filterAttrs (
|
filter (
|
||||||
_: system:
|
system:
|
||||||
system.config.access.online.enable &&
|
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 {
|
in {
|
||||||
services.prometheus = {
|
services.prometheus = {
|
||||||
port = 9090;
|
port = mkDefault 9090;
|
||||||
scrapeConfigs =
|
scrapeConfigs = concatMap mapSystem nodeExporterSystems;
|
||||||
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;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,56 @@
|
||||||
{
|
let
|
||||||
lib,
|
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,
|
gensokyo-zone,
|
||||||
|
lib,
|
||||||
...
|
...
|
||||||
}: let
|
}: let
|
||||||
inherit (gensokyo-zone.lib) mapListToAttrs mapAlmostOptionDefaults mkAlmostOptionDefault;
|
inherit (gensokyo-zone.lib) mapListToAttrs mapAlmostOptionDefaults mkAlmostOptionDefault;
|
||||||
inherit (lib.modules) mkIf;
|
inherit (lib.options) mkOption;
|
||||||
inherit (lib.attrsets) nameValuePair;
|
inherit (lib.modules) mkIf mkOptionDefault;
|
||||||
|
inherit (lib.attrsets) attrNames filterAttrs nameValuePair;
|
||||||
mkExporter = { name, port }: nameValuePair "prometheus-exporters-${name}" ({config, ...}: {
|
mkExporter = { name, port }: nameValuePair "prometheus-exporters-${name}" ({config, ...}: {
|
||||||
nixos = {
|
nixos = {
|
||||||
serviceAttrPath = ["services" "prometheus" "exporters" name];
|
serviceAttrPath = ["services" "prometheus" "exporters" name];
|
||||||
|
|
@ -19,12 +64,31 @@
|
||||||
ports.default = mapAlmostOptionDefaults {
|
ports.default = mapAlmostOptionDefaults {
|
||||||
inherit port;
|
inherit port;
|
||||||
protocol = "http";
|
protocol = "http";
|
||||||
|
} // {
|
||||||
|
prometheus.exporter.enable = true;
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
exporters = mapListToAttrs mkExporter [
|
exporters = mapListToAttrs mkExporter [
|
||||||
{ name = "node"; port = 9091; }
|
{ name = "node"; port = 9091; }
|
||||||
];
|
];
|
||||||
in {
|
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 = {
|
config.exports.services = {
|
||||||
prometheus = {config, ...}: {
|
prometheus = {config, ...}: {
|
||||||
id = mkAlmostOptionDefault "prometheus";
|
id = mkAlmostOptionDefault "prometheus";
|
||||||
|
|
@ -88,6 +152,8 @@ in {
|
||||||
ports.default = mapAlmostOptionDefaults {
|
ports.default = mapAlmostOptionDefaults {
|
||||||
port = 9094;
|
port = 9094;
|
||||||
protocol = "http";
|
protocol = "http";
|
||||||
|
} // {
|
||||||
|
prometheus.exporter.enable = true;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
} // exporters;
|
} // exporters;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue