Files
nixos-servers/system/monitoring/logs.nix
Torjus Håkestad 7b804450a3 promtail: add hostname/tier/role labels and journal priority level mapping
Align Promtail labels with Prometheus by adding hostname, tier, and role
static labels to both journal and varlog scrape configs. Add pipeline
stages to map journal PRIORITY field to a level label for reliable
severity filtering across the fleet.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-13 23:40:14 +01:00

81 lines
2.1 KiB
Nix

{ config, lib, ... }:
let
hostLabels = {
hostname = config.networking.hostName;
tier = config.homelab.host.tier;
} // lib.optionalAttrs (config.homelab.host.role != null) {
role = config.homelab.host.role;
};
in
{
# Configure journald
services.journald = {
rateLimitInterval = "10s";
extraConfig = ''
SystemMaxUse=100M
SystemKeepFree=1G
'';
};
# Configure promtail
services.promtail = {
enable = true;
configuration = {
server = {
http_listen_address = "0.0.0.0";
http_listen_port = 9099;
grpc_listen_address = "0.0.0.0";
grpc_listen_port = 9098;
};
clients = [
{
url = "http://monitoring01.home.2rjus.net:3100/loki/api/v1/push";
}
];
scrape_configs = [
{
job_name = "journal";
journal = {
json = true;
labels = {
job = "systemd-journal";
} // hostLabels;
};
relabel_configs = [
{
source_labels = [ "__journal__systemd_unit" ];
target_label = "systemd_unit";
}
];
pipeline_stages = [
# Extract PRIORITY from journal JSON
{ json.expressions.priority = "PRIORITY"; }
# Map numeric PRIORITY to level name
{
template = {
source = "priority";
template = ''{{ if or (eq .Value "0") (eq .Value "1") (eq .Value "2") }}critical{{ else if eq .Value "3" }}error{{ else if eq .Value "4" }}warning{{ else if eq .Value "5" }}notice{{ else if eq .Value "6" }}info{{ else if eq .Value "7" }}debug{{ end }}'';
};
}
# Attach as level label
{ labels.level = "priority"; }
];
}
{
job_name = "varlog";
static_configs = [
{
targets = [ "localhost" ];
labels = {
job = "varlog";
__path__ = "/var/log/**/*.log";
} // hostLabels;
}
];
}
];
};
};
}