create prometheus clan service

This commit is contained in:
2025-08-25 07:21:21 +02:00
parent dc5ffa3c66
commit f463644f1b
4 changed files with 120 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
{
imports = [
./prometheus/flake-module.nix
];
}

View File

@@ -0,0 +1,110 @@
{ self, ... }:
{ lib, ... }:
{
_class = "clan.service";
manifest.name = "prometheus";
# Only works with zerotier (until a unified network module is ready)
roles.scraper = {
interface = {
options.extraScrapeConfigs = lib.mkOption {
type = lib.types.listOf lib.types.attrs;
description = "A list of additional scrape configurations.";
};
};
perInstance =
{
settings,
roles,
...
}:
{
nixosModule =
{ config, lib, ... }:
{
services.prometheus.enable = true;
services.prometheus.scrapeConfigs =
let
allExporters = lib.unique (
lib.concatLists (
lib.map (machine: lib.attrNames machine.settings.exporters) (lib.attrValues roles.target.machines)
)
);
hasExporter =
exporter: machine: lib.hasAttr exporter roles.target.machines.${machine}.settings.exporters;
mkScrapeConfig = (
exporter:
let
machinesWithExporter = lib.filter (hasExporter exporter) (lib.attrNames roles.target.machines);
in
{
job_name = exporter;
static_configs = lib.map (machineName: {
targets =
let
targetConfig = self.nixosConfigurations.${machineName}.config;
targetHost = targetConfig.clan.core.vars.generators.zerotier.files.zerotier-ip.value;
in
[
"${targetHost}:${toString targetConfig.services.prometheus.exporters.${exporter}.port}"
];
labels.instance = machineName;
}) machinesWithExporter;
}
);
in
(lib.map mkScrapeConfig allExporters) ++ settings.extraScrapeConfigs;
clan.core.state.prometheus.folders = [ config.services.prometheus.stateDir ];
};
};
};
roles.target = {
interface =
{ lib, ... }:
{
options = {
exporters = lib.mkOption {
type = lib.types.attrs;
default = { };
example = {
node = {
enabledCollectors = [ "systemd" ];
port = 9002;
};
};
description = "Attribute set of exporters to enable";
};
};
};
perInstance =
{
instanceName,
settings,
machine,
roles,
...
}:
{
nixosModule =
{ config, lib, ... }:
{
services.prometheus.exporters = builtins.mapAttrs (
name: exporterSettings:
exporterSettings
// {
enable = true;
}
) settings.exporters;
networking.firewall.interfaces."zts7mq7onf".allowedTCPPorts = lib.map (
exporterName: config.services.prometheus.exporters.${exporterName}.port
) (lib.attrNames settings.exporters);
};
};
};
}

View File

@@ -0,0 +1,4 @@
{ self, lib, ... }:
{
clan.modules."@rpqt/prometheus" = lib.modules.importApply ./default.nix { inherit self; };
}

View File

@@ -18,6 +18,7 @@
inputs.clan-core.flakeModules.default inputs.clan-core.flakeModules.default
inputs.nix-topology.flakeModule inputs.nix-topology.flakeModule
./clanServices/flake-module.nix
./devShells/flake-module.nix ./devShells/flake-module.nix
./machines/flake-module.nix ./machines/flake-module.nix
./modules/flake-module.nix ./modules/flake-module.nix