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>
This commit is contained in:
2026-02-13 23:40:14 +01:00
parent 2f0dad1acc
commit 7b804450a3

View File

@@ -1,4 +1,12 @@
{ config, ... }: { 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 # Configure journald
services.journald = { services.journald = {
@@ -32,17 +40,26 @@
json = true; json = true;
labels = { labels = {
job = "systemd-journal"; job = "systemd-journal";
}; } // hostLabels;
}; };
relabel_configs = [ relabel_configs = [
{ {
source_labels = [ "__journal__systemd_unit" ]; source_labels = [ "__journal__systemd_unit" ];
target_label = "systemd_unit"; target_label = "systemd_unit";
} }
];
pipeline_stages = [
# Extract PRIORITY from journal JSON
{ json.expressions.priority = "PRIORITY"; }
# Map numeric PRIORITY to level name
{ {
source_labels = [ "__journal__hostname" ]; template = {
target_label = "host"; 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"; }
]; ];
} }
{ {
@@ -53,8 +70,7 @@
labels = { labels = {
job = "varlog"; job = "varlog";
__path__ = "/var/log/**/*.log"; __path__ = "/var/log/**/*.log";
hostname = "${config.networking.hostName}"; } // hostLabels;
};
} }
]; ];
} }