collector dashboard: query the service.name the collectors emit, lint dashboard names
This commit is contained in:
parent
067a260b85
commit
181c42dae1
1 changed files with 107 additions and 25 deletions
67
scripts/check-dashboard-service-names.sh
Normal file
67
scripts/check-dashboard-service-names.sh
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#!/bin/sh
|
||||
# CI lint: every literal `"service.name"="<x>"` selector in a shipped grafana
|
||||
# dashboard must name a service.name that something emits. A dashboard
|
||||
# querying a name nobody sets renders empty panels with no error.
|
||||
#
|
||||
# `known` lists the emitted names, one `name|file|anchor` per line. The
|
||||
# anchor is a fixed string that must still appear in that file, so an entry
|
||||
# cannot outlive the code that sets the name. Regex selectors
|
||||
# (`"service.name"=~"$service"`) are template variables and are not checked.
|
||||
#
|
||||
set -eu
|
||||
|
||||
dashboards_dir="nix/host-modules/swarm-grafana/dashboards"
|
||||
|
||||
# bao: `collectorServiceName` upserted on the store's collector.
|
||||
# authelia, forgejo: `serviceName` of the journal forwarder in each module.
|
||||
# swarm-controller: the resource of the controller's own metrics.
|
||||
# otelcol-contrib: the unset default of the collector binary, which the hive
|
||||
# collector and swarm-otel both run (`pkgs.opentelemetry-collector-contrib`)
|
||||
# and which stamps their self-metrics.
|
||||
known='bao|nix/host-modules/swarm-bao.nix|collectorServiceName = "bao"
|
||||
authelia|nix/host-modules/swarm-authelia.nix|serviceName = "authelia"
|
||||
forgejo|nix/host-modules/hive-forge/default.nix|serviceName = "forgejo"
|
||||
swarm-controller|swarm-controller/src/vcs_metrics.rs|with_service_name("swarm-controller")
|
||||
otelcol-contrib|nix/host-modules/otel.nix|package = pkgs.opentelemetry-collector-contrib'
|
||||
|
||||
violations=0
|
||||
|
||||
while IFS='|' read -r name file anchor; do
|
||||
if ! grep -qF -- "$anchor" "$file" 2>/dev/null; then
|
||||
violations=$((violations + 1))
|
||||
printf '::error file=%s::service.name "%s" is listed as emitted but %s no longer contains: %s\n' \
|
||||
"$file" "$name" "$file" "$anchor"
|
||||
fi
|
||||
done <<EOF2
|
||||
$known
|
||||
EOF2
|
||||
|
||||
names="$(printf '%s\n' "$known" | cut -d'|' -f1)"
|
||||
checked=0
|
||||
|
||||
for f in "$dashboards_dir"/*.json; do
|
||||
# The expr strings escape the quotes, so the raw file holds
|
||||
# service.name backslash-quote-equals-backslash-quote name.
|
||||
used_names="$(grep -oE '\\"service\.name\\"=\\"[^\\]+\\"' "$f" | sed -E 's/.*=\\"([^\\]+)\\"$/\1/' | sort -u || true)"
|
||||
while IFS= read -r used; do
|
||||
[ -n "$used" ] || continue
|
||||
checked=$((checked + 1))
|
||||
if ! printf '%s\n' "$names" | grep -qxF -- "$used"; then
|
||||
violations=$((violations + 1))
|
||||
printf '::error file=%s::queries service.name "%s", which nothing emits (known: %s)\n' \
|
||||
"$f" "$used" "$(printf '%s' "$names" | tr '\n' ' ')"
|
||||
fi
|
||||
done <<EOF3
|
||||
$used_names
|
||||
EOF3
|
||||
done
|
||||
|
||||
if [ "$checked" -eq 0 ]; then
|
||||
echo 'check-dashboard-service-names: found zero service.name selectors — extractor or tree changed' >&2
|
||||
exit 1
|
||||
fi
|
||||
if [ "$violations" -gt 0 ]; then
|
||||
printf 'check-dashboard-service-names: %s violation(s)\n' "$violations" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "check-dashboard-service-names: $checked selector name(s) checked, clean" >&2
|
||||
Loading…
Reference in a new issue