From 21c6d9b7bc2b93d7e15d215f9b73061c25e48888 Mon Sep 17 00:00:00 2001 From: Ryan Yin Date: Sun, 26 Jul 2026 15:16:46 +0800 Subject: [PATCH] fix(monitoring): drop $vars from telegram template to survive envsubst The NixOS alertmanager module pipes the generated config through envsubst to inject secrets, which silently replaced the template's $i/$a variables with empty strings, so every telegram notification failed with a template parse error since Friday's deploy. Rewrite the template without variables: range + dot, define/template for the per-alert block, and an if/else split to cap a group at 5 alerts (slice errors when fewer than 5). Verified with amtool template render against post-envsubst content for both branches. Signed-off-by: Ryan Yin --- hosts/idols-aquamarine/monitoring/alert.nix | 22 +++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/hosts/idols-aquamarine/monitoring/alert.nix b/hosts/idols-aquamarine/monitoring/alert.nix index 8983e17e..d5ff69ff 100644 --- a/hosts/idols-aquamarine/monitoring/alert.nix +++ b/hosts/idols-aquamarine/monitoring/alert.nix @@ -126,6 +126,13 @@ # Message template # Telegram limits a message to 4096 chars, so only render the first 5 alerts # in a group and keep the fields minimal (no full label dump). + # + # WARNING: never use template variables ($i, $a, ...) here. The NixOS + # alertmanager module pipes the generated config through `envsubst` + # (to inject $TELEGRAM_BOT_TOKEN etc.), which silently replaces any + # $var in this template with an empty string and breaks rendering at + # notify time. Use `{{ range .Alerts }}` + dot, and `define`/`template` + # for reuse instead. message = '' {{- if eq .Status "firing" }} 🟑 ε‘Šθ­¦θ§¦ε‘ [{{ .CommonLabels.severity | title }}] {{ .CommonLabels.alertname }} (ε…± {{ len .Alerts }} 村) @@ -137,18 +144,21 @@ β€’ Cluster: {{ with .CommonLabels.cluster }}{{ . }}{{ else }}N/A{{ end }} β€’ Env: {{ with .CommonLabels.env }}{{ . }}{{ else }}N/A{{ end }} β€’ Namespace: {{ with .CommonLabels.namespace }}{{ . }}{{ else }}N/A{{ end }} - {{- range $i, $a := .Alerts }} - {{- if lt $i 5 }} + {{- define "alert" }} ━━━━━━━━ - πŸ“Œ {{ with $a.Annotations.summary }}{{ . }}{{ else }}{{ $a.Labels.instance }}{{ end }} - {{- with $a.Labels.nodename }} + πŸ“Œ {{ with .Annotations.summary }}{{ . }}{{ else }}{{ .Labels.instance }}{{ end }} + {{- with .Labels.nodename }} 🏠 {{ . }} {{- end }} - πŸ”— Grafana Explore Β· ⏱ {{ $a.StartsAt.Format "2006-01-02 15:04:05" }} + πŸ”— Grafana Explore Β· ⏱ {{ .StartsAt.Format "2006-01-02 15:04:05" }} {{- end }} + {{- if le (len .Alerts) 5 }} + {{- range .Alerts }}{{ template "alert" . }} + {{- end }} + {{- else }} + {{- range slice .Alerts 0 5 }}{{ template "alert" . }} {{- end }} - {{- if gt (len .Alerts) 5 }} ⚠️ ε¦ζœ‰ {{ len (slice .Alerts 5) }} ζ‘εŒη»„ε‘Šθ­¦ζœͺεˆ—ε‡ΊοΌŒθ―·ζŸ₯ηœ‹ Alertmanager。 {{- end }}