monitoring(logs): a readable Loki — Logs dashboard + provisioning-reload runbook
The log store shipped in t_34ddaa83 but nothing could read it: the Loki datasource ConfigMap was live and written into Grafana's provisioning volume, and Grafana still listed only Prometheus + Alertmanager. Root cause was not the ConfigMap. The datasource sidecar writes the file and then POSTs /api/admin/provisioning/datasources/reload with monitoring/grafana-admin, which was 401-ing because Grafana's DB password had drifted from that Secret (the documented "the Secret is only the seed" behaviour) — Grafana reads provisioning/datasources only at startup, so the datasource never loaded. - Reset the live admin password to the Secret's value (in-pod `grafana cli admin reset-admin-password`, value read from the Secret and never printed), so seed and DB match again and the sidecar's reload returns 200. Documented in docs/grafana-persistence.md, including the sidecar consequence of drift. - New generated dashboard `logs` (uid: logs) with the two queries worth having: secret access over time by user from the apiserver audit stream, and pod errors by namespace, plus the runner namespaces and a note panel on field names. - The audit stream's names are the Alloy allowlist's, not the audit event's: objectRef.resource arrives as the stream label `resource` and user.username as the field `user`. `| json | objectRef_resource="secrets"` returns 0 rows (measured); the panels use `resource="secrets"` in the selector + `| json` for the user dimension. No pipeline, retention or filter change. - Runner panels discover their namespace via a `runner_ns` variable (.*(runner|forgejo).*) instead of pinning one name, so the t_2bbe34a8 move to ci-runners needs no edit here. - validate_panels.py now dispatches on the target's datasource and queries Loki for `loki` panels (a logs panel must return rows, not just series).
This commit is contained in:
parent
2ae67faf98
commit
6b91152a79
7 changed files with 828 additions and 35 deletions
|
|
@ -1,15 +1,20 @@
|
|||
#!/usr/bin/env python3
|
||||
"""Verify every panel on the three custom dashboards returns real data.
|
||||
"""Verify every panel on the custom dashboards returns real data.
|
||||
|
||||
Reads the dashboard JSON out of the provisioned ConfigMap YAML (i.e. exactly what
|
||||
Grafana loaded), extracts every PromQL expression, queries the Prometheus HTTP API
|
||||
and prints panel title -> PromQL -> series count -> sample.
|
||||
Grafana loaded), extracts every PromQL/LogQL expression, queries the matching
|
||||
datasource's HTTP API and prints panel title -> query -> series count -> sample.
|
||||
|
||||
Usage: validate_panels.py [DASHBOARD_DIR] [PROM_URL]
|
||||
Prometheus panels go to PROM; panels whose datasource is `loki` go to LOKI. A
|
||||
Loki panel is reported as OK only if its result is non-empty, which for a logs
|
||||
panel means at least one entry (the series count alone is not enough).
|
||||
|
||||
Usage: validate_panels.py [DASHBOARD_DIR] [PROM_URL] [LOKI_URL]
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
|
|
@ -18,10 +23,12 @@ import yaml
|
|||
DASH_DIR = sys.argv[1] if len(sys.argv) > 1 else os.path.expanduser(
|
||||
"~/Projects/homek8s/monitoring/controllers/home/dashboards")
|
||||
PROM = sys.argv[2] if len(sys.argv) > 2 else "http://127.0.0.1:9090"
|
||||
LOKI = sys.argv[3] if len(sys.argv) > 3 else "http://127.0.0.1:3100"
|
||||
|
||||
# Grafana template variable substitution for the "All" case, so the query the
|
||||
# script runs is the query Grafana runs.
|
||||
SUBST = {"$namespace": ".*", "${namespace}": ".*", "$__all": ".*"}
|
||||
SUBST = {"$namespace": ".*", "${namespace}": ".*", "$__all": ".*",
|
||||
"$runner_ns": ".*(runner|forgejo).*", "${runner_ns}": ".*(runner|forgejo).*"}
|
||||
|
||||
|
||||
def substitute(expr):
|
||||
|
|
@ -39,6 +46,39 @@ def query(expr):
|
|||
return body["data"]["result"], None
|
||||
|
||||
|
||||
def query_loki(expr, instant=False):
|
||||
if instant:
|
||||
url = "%s/loki/api/v1/query?%s" % (LOKI, urllib.parse.urlencode(
|
||||
{"query": expr, "time": "%d000000000" % time.time()}))
|
||||
else:
|
||||
now = int(time.time())
|
||||
url = "%s/loki/api/v1/query_range?%s" % (LOKI, urllib.parse.urlencode(
|
||||
{"query": expr, "start": "%d000000000" % (now - 21600),
|
||||
"end": "%d000000000" % now, "limit": 1000}))
|
||||
with urllib.request.urlopen(url, timeout=30) as r:
|
||||
body = json.load(r)
|
||||
if body.get("status") != "success":
|
||||
return None, 0, body.get("error") or body
|
||||
result = body["data"]["result"] or []
|
||||
# Range/metric queries return {metric|stream, values:[[ts,line],...]}; an
|
||||
# instant query returns {metric, value:[ts,val]} (singular). Count both, so a
|
||||
# stat panel's single sample is "1 row" rather than looking empty.
|
||||
rows = 0
|
||||
for x in result:
|
||||
if "values" in x:
|
||||
rows += len(x.get("values") or [])
|
||||
elif "value" in x:
|
||||
rows += 1
|
||||
return result, rows, None
|
||||
|
||||
|
||||
def ds_type(panel, target):
|
||||
ds = target.get("datasource") or panel.get("datasource")
|
||||
if isinstance(ds, dict):
|
||||
return ds.get("type")
|
||||
return None
|
||||
|
||||
|
||||
def panels_of(dash):
|
||||
out = []
|
||||
for p in dash.get("panels", []):
|
||||
|
|
@ -66,6 +106,20 @@ for fn in sorted(os.listdir(DASH_DIR)):
|
|||
if not expr:
|
||||
continue
|
||||
total += 1
|
||||
if ds_type(p, t) == "loki":
|
||||
result, rows, err = query_loki(expr, instant=bool(t.get("instant")))
|
||||
if err:
|
||||
bad += 1
|
||||
print(" FAIL %-46s %s\n ERROR: %s"
|
||||
% (p["title"][:46], expr.replace("\n", " ")[:110], err))
|
||||
continue
|
||||
if not result or not rows:
|
||||
bad += 1
|
||||
print(" EMPTY %-46s %s" % (p["title"][:46], expr.replace("\n", " ")[:110]))
|
||||
continue
|
||||
print(" OK %-46s %-3d series %-6d rows (loki)"
|
||||
% (p["title"][:46], len(result), rows))
|
||||
continue
|
||||
result, err = query(expr)
|
||||
if err:
|
||||
bad += 1
|
||||
|
|
|
|||
Loading…
Reference in a new issue