alloy: Push metrics to prometheus instead of victoriametrics

This commit is contained in:
SebastianStork 2026-02-28 23:47:15 +01:00
parent 1b599abdbe
commit 8f6f63fbaa
Signed by: SebastianStork
SSH key fingerprint: SHA256:tRrGdjYOwgHxpSc/wTOZQZEjxcb15P0tyXRsbAfd+2Q
2 changed files with 26 additions and 13 deletions

View file

@ -1,4 +1,9 @@
{ config, lib, ... }: {
config,
lib,
allHosts,
...
}:
let let
cfg = config.custom.services.alloy; cfg = config.custom.services.alloy;
in in
@ -13,10 +18,6 @@ in
type = lib.types.port; type = lib.types.port;
default = 12345; default = 12345;
}; };
metricsEndpoint = lib.mkOption {
type = lib.types.nonEmptyStr;
default = "https://metrics.${config.custom.networking.overlay.domain}/prometheus/api/v1/write";
};
collect.metrics = { collect.metrics = {
system = lib.mkEnableOption "" // { system = lib.mkEnableOption "" // {
default = true; default = true;
@ -47,19 +48,30 @@ in
environment.etc = environment.etc =
let let
isTrue = x: x; anyIsTrue = attrs: attrs |> lib.attrValues |> lib.any lib.id;
anyIsTrue = attrs: attrs |> lib.attrValues |> lib.any isTrue;
prometheusEndpoints =
allHosts
|> lib.attrValues
|> lib.filter (host: host.config.custom.services.prometheus.enable)
|> lib.map (host: "https://${host.config.custom.services.prometheus.domain}/api/v1/write");
in in
{ {
"alloy/metrics-endpoint.alloy" = { "alloy/metrics-endpoint.alloy" = {
enable = cfg.collect.metrics |> anyIsTrue; enable = cfg.collect.metrics |> anyIsTrue;
text = '' text =
prometheus.remote_write "default" { prometheusEndpoints
|> lib.map (url: ''
endpoint { endpoint {
url = "${cfg.metricsEndpoint}" url = "${url}"
} }
} '')
''; |> lib.concatLines
|> (endpoints: ''
prometheus.remote_write "default" {
${endpoints}
}
'');
}; };
"alloy/system-metrics.alloy" = { "alloy/system-metrics.alloy" = {
enable = cfg.collect.metrics.system; enable = cfg.collect.metrics.system;
@ -85,7 +97,7 @@ in
instance = constants.hostname, instance = constants.hostname,
}] }]
forward_to = [prometheus.remote_write.default.receiver] forward_to = [prometheus.remote_write.default.receiver]
scrape_interval = "15s" scrape_interval = "30s"
} }
''; '';
}; };

View file

@ -30,6 +30,7 @@ in
inherit (cfg) port; inherit (cfg) port;
webExternalUrl = "https://${cfg.domain}"; webExternalUrl = "https://${cfg.domain}";
extraFlags = [ "--web.enable-remote-write-receiver" ];
globalConfig = { globalConfig = {
scrape_interval = "30s"; scrape_interval = "30s";
external_labels.monitor = "global"; external_labels.monitor = "global";