alloy: Enable endpoints conditionally based on requirements

This commit is contained in:
SebastianStork 2025-09-21 18:36:23 +02:00
parent 9bc7a2e737
commit 3f85a77fc9
4 changed files with 75 additions and 61 deletions

View file

@ -79,9 +79,9 @@
alloy = {
enable = true;
domain = "alloy-${config.networking.hostName}.${tailscaleDomain}";
collect = {
hostMetrics = true;
victorialogsMetrics = true;
collect.metrics = {
system = true;
victorialogs = true;
};
};

View file

@ -56,7 +56,7 @@
alloy = {
enable = true;
domain = "alloy-${config.networking.hostName}.${tailscaleDomain}";
collect.hostMetrics = true;
collect.metrics.system = true;
};
caddy.virtualHosts =

View file

@ -78,8 +78,8 @@
enable = true;
domain = "alloy-${config.networking.hostName}.${config.custom.services.tailscale.domain}";
collect = {
hostMetrics = true;
sshdLogs = true;
metrics.system = true;
logs.sshd = true;
};
};

View file

@ -22,20 +22,22 @@ in
default = "https://logs.${config.custom.services.tailscale.domain}/insert/loki/api/v1/push";
};
collect = {
hostMetrics = lib.mkEnableOption "";
victorialogsMetrics = lib.mkEnableOption "";
sshdLogs = lib.mkEnableOption "";
metrics = {
system = lib.mkEnableOption "";
victorialogs = lib.mkEnableOption "";
};
logs.sshd = lib.mkEnableOption "";
};
};
config = lib.mkIf cfg.enable {
assertions = [
{
assertion = cfg.collect.victorialogsMetrics -> config.services.victorialogs.enable;
assertion = cfg.collect.metrics.victorialogs -> config.services.victorialogs.enable;
message = "Collecting VictoriaLogs metrics requires the VictoriaLogs service to be enabled.";
}
{
assertion = cfg.collect.sshdLogs -> config.services.openssh.enable;
assertion = cfg.collect.logs.sshd -> config.services.openssh.enable;
message = "Collecting OpenSSH logs requires the OpenSSH service to be enabled.";
}
];
@ -53,57 +55,69 @@ in
];
};
environment.etc = {
"alloy/endpoints.alloy".text = ''
prometheus.remote_write "default" {
endpoint {
url = "${cfg.metricsEndpoint}"
}
}
environment.etc =
let
isTrue = x: x;
anyIsTrue = attrs: attrs |> lib.attrValues |> lib.any isTrue;
in
{
"alloy/metrics-endpoint.alloy" = {
enable = cfg.collect.metrics |> anyIsTrue;
text = ''
prometheus.remote_write "default" {
endpoint {
url = "${cfg.metricsEndpoint}"
}
}
'';
};
"alloy/logs-endpoint.alloy" = {
enable = cfg.collect.logs |> anyIsTrue;
text = ''
loki.write "default" {
endpoint {
url = "${cfg.logsEndpoint}"
}
}
'';
};
"alloy/system-metrics.alloy" = {
enable = cfg.collect.metrics.system;
text = ''
prometheus.exporter.unix "default" {
enable_collectors = ["systemd"]
}
loki.write "default" {
endpoint {
url = "${cfg.logsEndpoint}"
}
}
'';
"alloy/host-metrics.alloy" = lib.mkIf cfg.collect.hostMetrics {
text = ''
prometheus.exporter.unix "default" {
enable_collectors = ["systemd"]
}
prometheus.scrape "node_exporter" {
targets = prometheus.exporter.unix.default.targets
forward_to = [prometheus.remote_write.default.receiver]
scrape_interval = "15s"
}
'';
prometheus.scrape "node_exporter" {
targets = prometheus.exporter.unix.default.targets
forward_to = [prometheus.remote_write.default.receiver]
scrape_interval = "15s"
}
'';
};
"alloy/victorialogs-metrics.alloy" = {
enable = cfg.collect.metrics.victorialogs;
text = ''
prometheus.scrape "victorialogs" {
targets = [{
__address__ = "localhost:${builtins.toString config.custom.services.victorialogs.port}",
job = "victorialogs",
instance = constants.hostname,
}]
forward_to = [prometheus.remote_write.default.receiver]
scrape_interval = "15s"
}
'';
};
"alloy/sshd-logs.alloy" = {
enable = cfg.collect.logs.sshd;
text = ''
loki.source.journal "sshd" {
matches = "_SYSTEMD_UNIT=sshd.service"
forward_to = [loki.write.default.receiver]
}
'';
};
};
"alloy/victorialogs-metrics.alloy" = lib.mkIf cfg.collect.victorialogsMetrics {
text = ''
prometheus.scrape "victorialogs" {
targets = [{
__address__ = "localhost:${builtins.toString config.custom.services.victorialogs.port}",
job = "victorialogs",
instance = constants.hostname,
}]
forward_to = [prometheus.remote_write.default.receiver]
scrape_interval = "15s"
}
'';
};
"alloy/sshd-logs.alloy" = lib.mkIf cfg.collect.sshdLogs {
text = ''
loki.source.journal "sshd" {
matches = "_SYSTEMD_UNIT=sshd.service"
forward_to = [loki.write.default.receiver]
}
'';
};
};
};
}