snix/ops/modules/o11y/alloy.nix
Ilan Joselevich 91d02d8c84 style: Switch to nixfmt from nixpkgs-fmt
Most of the ecosystem has moved to this formatter,
and many people configured their editors to autoformat it with this formatter.

Closes: https://git.snix.dev/snix/snix/issues/62
Change-Id: Icf39e7836c91fc2ae49fbe22a40a639105bfb0bd
Reviewed-on: https://cl.snix.dev/c/snix/+/30671
Reviewed-by: Florian Klink <flokli@flokli.de>
Tested-by: besadii
Autosubmit: Ilan Joselevich <personal@ilanjoselevich.com>
2025-08-10 13:40:23 +00:00

109 lines
2.7 KiB
Nix

{
depot,
config,
lib,
...
}:
let
cfg = config.infra.monitoring.alloy;
inherit (lib)
mkEnableOption
mkOption
mkIf
types
mapAttrs'
nameValuePair
;
in
{
options.infra.monitoring.alloy = {
enable = (mkEnableOption "Grafana Alloy") // {
default = true;
};
exporters = mkOption {
description = ''
Set of additional exporters to scrape.
The attribute name will be used as `job_name`
internally, which ends up exported as `job` label
on all metrics of that exporter.
'';
type = types.attrsOf (
types.submodule (
{ config, name, ... }:
{
options.port = mkOption {
description = "Exporter port";
type = types.int;
};
}
)
);
default = { };
};
};
config = mkIf cfg.enable {
age.secrets.alloy-password.file = depot.ops.secrets."grafana-agent-password.age";
services.alloy.enable = true;
environment.etc = {
"alloy/config.alloy".text = ''
prometheus.exporter.unix "default" {
enable_collectors = [
"processes",
// cannot work currently, as alloy cannot talk to dbus:
// "systemd"
]
}
// Configure node exporter
prometheus.scrape "node_exporter" {
targets = prometheus.exporter.unix.default.targets
forward_to = [prometheus.remote_write.mimir.receiver]
}
// Configure a prometheus.scrape component to collect Alloy metrics.
prometheus.exporter.self "default" {}
prometheus.scrape "self" {
targets = prometheus.exporter.self.default.targets
forward_to = [prometheus.remote_write.mimir.receiver]
}
prometheus.remote_write "mimir" {
endpoint {
url = "https://mimir.snix.dev/api/v1/push"
basic_auth {
username = "promtail" // FUTUREWORK: rename this
password_file = format("%s/metrics_remote_write_password", env("CREDENTIALS_DIRECTORY"))
}
}
external_labels = {
hostname = constants.hostname,
}
}
'';
}
// (mapAttrs' (
name: v:
nameValuePair "alloy/scrape_${name}.alloy" {
text = ''
prometheus.scrape "${name}" {
targets = [
{"__address__" = "localhost:${toString v.port}"},
]
forward_to = [prometheus.remote_write.mimir.receiver]
}
'';
}
) cfg.exporters);
systemd.services.alloy.serviceConfig = {
LoadCredential = [
"metrics_remote_write_password:${config.age.secrets.alloy-password.path}"
];
};
};
}