homek8s/infrastructure/logging/alloy/config.alloy
Lance Walters 4a933a0403 infra(logging): drop osint scrapy item payloads so 8 days fits the PVC
Measured during a crawl (2026-11-27 04:45-05:45Z): osint/news-scraper produced
~150 MB/h, 88 % of it one JSON item dump per crawled article (up to 4.7 kB per
line, the same rows are in the osint Postgres DB). At the observed 2.9x
compression that is ~10 GB over 192h and would not fit the 8Gi PVC, so drop
exactly those lines by prefix for that one container. Operational lines
(INFO/WARNING/ERROR, tracebacks, duplicate-URL warnings) still ship.

docs/logging.md: document the drop rule and the two measured regimes.
2026-11-27 00:45:42 -05:00

211 lines
5.5 KiB
Text

// Grafana Alloy — log collection for the house cluster.
//
// Three sources, and none of them touch the Kubernetes API (everything is read
// off the node's filesystem), so this DaemonSet needs ZERO RBAC beyond the
// default ServiceAccount:
//
// 1. kube/pods — /var/log/pods/<ns>_<pod>_<uid>/<container>/*.log
// 2. systemd-journal — /var/log/journal (kubelet, containerd, sshd, kernel)
// 3. docker/json — /var/lib/docker/containers/*/*-json.log (Pi-only
// services that are not pods: local image registry, Garage)
//
// `tail_from_end = false` on both file sources is deliberate: the nodes already
// hold history (pod log files reach back days; the linode's cilium-agent file
// reaches back 18 days, the Pi's registry json log 31 days). Ingesting those
// files from byte 0 is what gives the store real depth on day one instead of
// starting the 7-day clock at deploy time. The `timestamp` stages stamp every
// entry with the timestamp carried by the line itself rather than the ingest
// time — without that, backfill would land as "now" and be worthless.
//
// Positions live on a hostPath (/var/lib/alloy-data) so a pod restart resumes
// instead of re-shipping every byte.
logging {
level = "info"
format = "logfmt"
}
loki.write "local" {
endpoint {
url = "http://loki.logging.svc.cluster.local:3100/loki/api/v1/push"
}
external_labels = {
cluster = "homek8s",
}
}
// --------------------------------------------------------------- pod stdout
local.file_match "pod_logs" {
path_targets = [{ "__path__" = "/var/log/pods/*/*/*.log*" }]
sync_period = "15s"
}
discovery.relabel "pod_logs" {
targets = local.file_match.pod_logs.targets
rule {
source_labels = ["__path__"]
regex = "/var/log/pods/([^/]+)_([^_]+)_([^/]+)/([^/]+)/[^/]+"
target_label = "namespace"
replacement = "$1"
}
rule {
source_labels = ["__path__"]
regex = "/var/log/pods/([^/]+)_([^_]+)_([^/]+)/([^/]+)/[^/]+"
target_label = "pod"
replacement = "$2"
}
rule {
source_labels = ["__path__"]
regex = "/var/log/pods/([^/]+)_([^_]+)_([^/]+)/([^/]+)/[^/]+"
target_label = "container"
replacement = "$4"
}
rule {
target_label = "job"
replacement = "kube/pods"
}
rule {
target_label = "node"
replacement = sys.env("NODE_NAME")
}
}
loki.source.file "pod_logs" {
targets = discovery.relabel.pod_logs.output
forward_to = [loki.process.pod_logs.receiver]
tail_from_end = false
}
loki.process "pod_logs" {
// CRI format: "<RFC3339Nano> <stdout|stderr> <F|P> <message>"
stage.cri {}
stage.timestamp {
source = "time"
format = "RFC3339Nano"
}
stage.labels {
values = {
stream = "stream",
}
}
// osint/news-scraper prints one JSON-ish *item* dump per crawled article
// (`{'title': ..., 'url': ..., ... }`, up to 4.7 kB each). That is scraped
// payload, not an operational log line — the same rows are written to the
// osint Postgres DB — and it is ~88 % of the namespace's volume (measured
// ~150 MB/h during a crawl). Keeping it would push 8 days of retention past
// the 8Gi PVC, so it is dropped here, by exact prefix, for that one
// container. Every operational line (scrapy INFO/WARNING/ERROR, tracebacks,
// duplicate-URL warnings) still ships.
stage.match {
selector = "{namespace=\"osint\", container=\"news-scraper\"}"
stage.drop {
expression = "^\\{'title':"
drop_counter_reason = "osint_article_payload"
}
}
forward_to = [loki.write.local.receiver]
}
// ----------------------------------------------------------- systemd journal
loki.relabel "journal" {
forward_to = []
rule {
source_labels = ["__journal__systemd_unit"]
target_label = "unit"
}
rule {
source_labels = ["__journal_priority_keyword"]
target_label = "level"
}
rule {
source_labels = ["__journal__hostname"]
target_label = "host"
}
rule {
target_label = "job"
replacement = "systemd-journal"
}
rule {
target_label = "node"
replacement = sys.env("NODE_NAME")
}
}
loki.source.journal "system" {
path = "/var/log/journal"
// 192h matches the store's retention: read the whole retained window once,
// then follow. The default is 7h, which would drop most of the history.
max_age = "192h"
relabel_rules = loki.relabel.journal.rules
forward_to = [loki.write.local.receiver]
}
// ------------------------------------------------- non-pod Docker containers
local.file_match "docker_logs" {
path_targets = [{ "__path__" = "/var/lib/docker/containers/*/*-json.log" }]
sync_period = "30s"
}
discovery.relabel "docker_logs" {
targets = local.file_match.docker_logs.targets
rule {
source_labels = ["__path__"]
regex = "/var/lib/docker/containers/([0-9a-f]{12})[0-9a-f]*/[^/]+-json\\.log"
target_label = "container_id"
replacement = "$1"
}
rule {
target_label = "job"
replacement = "docker/json"
}
rule {
target_label = "node"
replacement = sys.env("NODE_NAME")
}
}
loki.source.file "docker_logs" {
targets = discovery.relabel.docker_logs.output
forward_to = [loki.process.docker_logs.receiver]
tail_from_end = false
}
loki.process "docker_logs" {
stage.json {
expressions = {
log = "log",
time = "time",
}
}
stage.timestamp {
source = "time"
format = "RFC3339Nano"
}
stage.output {
source = "log"
}
forward_to = [loki.write.local.receiver]
}