NixOS/system/services/telemetry/alloy.nix

155 lines
4.4 KiB
Nix
Raw Normal View History

2024-09-06 21:13:31 +07:00
{ config, lib, inputs, unstable, ... }:
let
cfg = config.profile.services.telemetry.alloy;
webguiListenAddress = "0.0.0.0:5319";
domain = "alloy.tigor.web.id";
2024-09-07 08:59:44 +07:00
inherit (lib.strings) optionalString;
2024-09-06 21:13:31 +07:00
in
{
imports = [
# Grafana Alloy is still in unstable options.
"${inputs.nixpkgs-unstable}/nixos/modules/services/monitoring/alloy.nix"
];
config = lib.mkIf cfg.enable {
services.alloy = {
enable = true;
extraFlags = [
''--server.http.listen-addr=${webguiListenAddress}''
];
package = unstable.grafana-alloy;
};
sops = {
secrets =
let
opts = { };
in
{
"caddy/basic_auth/username" = opts;
"caddy/basic_auth/password" = opts;
};
templates = {
"alloy-basic-auth".content = /*sh*/ ''
ALLOY_USERNAME=${config.sops.placeholder."caddy/basic_auth/username"}
ALLOY_PASSWORD=${config.sops.placeholder."caddy/basic_auth/password"}
'';
};
};
services.caddy.virtualHosts.${domain}.extraConfig = ''
@require_auth not remote_ip private_ranges
basicauth @require_auth {
{$ALLOY_USERNAME} {$ALLOY_PASSWORD}
}
reverse_proxy ${webguiListenAddress}
'';
systemd.services.caddy.serviceConfig.EnvironmentFile = [
config.sops.templates."alloy-basic-auth".path
];
environment.etc."alloy/config.alloy".text =
let
lokiConfig = config.services.loki.configuration;
2024-09-07 08:59:44 +07:00
tempoServer = config.services.tempo.settings.server;
2024-09-07 10:41:59 +07:00
mimirServer = config.services.mimir.configuration.server;
2024-09-06 21:13:31 +07:00
in
/*hcl*/ ''
otelcol.receiver.otlp "homeserver" {
grpc {
endpoint = "0.0.0.0:5317"
}
http {
endpoint = "0.0.0.0:5318"
}
output {
2024-09-07 10:41:59 +07:00
metrics = [otelcol.processor.batch.default.input]
2024-09-06 21:13:31 +07:00
logs = [otelcol.processor.batch.default.input]
2024-09-07 08:59:44 +07:00
traces = [otelcol.processor.batch.default.input]
2024-09-06 21:13:31 +07:00
}
}
otelcol.processor.batch "default" {
output {
2024-09-07 10:41:59 +07:00
metrics = [otelcol.exporter.prometheus.mimir.input]
2024-09-06 21:13:31 +07:00
logs = [otelcol.exporter.loki.default.input]
2024-09-07 08:59:44 +07:00
traces = [otelcol.exporter.otlp.tempo.input]
2024-09-06 21:13:31 +07:00
}
}
otelcol.exporter.loki "default" {
forward_to = [loki.write.default.receiver]
}
2024-09-07 10:41:59 +07:00
otelcol.exporter.prometheus "mimir" {
forward_to = [prometheus.remote_write.mimir.receiver]
}
2024-09-06 21:13:31 +07:00
loki.write "default" {
endpoint {
url = "http://${lokiConfig.server.http_listen_address}:${toString lokiConfig.server.http_listen_port}/loki/api/v1/push"
2024-09-06 21:13:31 +07:00
}
}
2024-09-07 08:59:44 +07:00
loki.relabel "journal" {
forward_to = []
rule {
source_labels = ["__journal__systemd_unit"]
target_label = "unit"
}
rule {
source_labels = ["__journal__hostname"]
target_label = "host"
}
rule {
source_labels = [ "__journal__systemd_user_unit" ]
target_label = "user_unit"
}
rule {
source_labels = [ "__journal__transport" ]
target_label = "transport"
}
rule {
source_labels = [ "__journal_priority_keyword" ]
target_label = "severity"
}
}
loki.source.journal "read" {
forward_to = [loki.write.default.receiver]
relabel_rules = loki.relabel.journal.rules
labels = {
job = "systemd-journal",
component = "loki.source.journal",
}
}
2024-09-07 08:59:44 +07:00
otelcol.exporter.otlp "tempo" {
client {
endpoint = "${tempoServer.http_listen_address}:${toString tempoServer.http_listen_port}"
}
}
2024-09-07 10:41:59 +07:00
prometheus.exporter.unix "system" {}
prometheus.scrape "system" {
targets = prometheus.exporter.unix.system.targets
forward_to = [prometheus.remote_write.mimir.receiver]
}
2024-09-07 10:41:59 +07:00
prometheus.remote_write "mimir" {
endpoint {
url = "http://${mimirServer.http_listen_address}:${toString mimirServer.http_listen_port}/api/v1/push"
2024-09-07 10:41:59 +07:00
}
}
2024-09-06 21:13:31 +07:00
'';
};
}