Files
junction/tests/retry-metrics.sh
Hermes db422ba36d Latenz als Histogramm statt als Mittelwert
/status und /metrics zeigten die letzte Dauer und einen Durchschnitt. Ein Durchschnitt
versteckt genau die Anfragen, die weh tun: ein Ziel, das 95 Prozent schnell und 5 Prozent
in einer Minute beantwortet, hat einen harmlosen Mittelwert. Jetzt zählt jeder Endpunkt
ein Histogramm mit festen Grenzen (0,1 s bis 60 s, plus +Inf) und exportiert es in
kumulativer Form, damit histogram_quantile je Ziel rechnet. /status nennt dazu p95_ms,
ausdrücklich als Obergrenze des Eimers, in dem die 95-Prozent-Marke liegt — und -1,
solange nichts gemessen wurde.

Neue Alarmregel JunctionEndpointSlowP95 (p95 über 20 s, 15 Minuten, nur bei Verkehr) in
packaging/prometheus-junction.yml; auf n95 geladen, health=ok.

Beim Testen einen Fehler gebaut und gefunden, der auf dem laufenden Router sofort
zugeschlagen hätte: die Histogramm-Ausgabe ließ die Zustandssperre gesperrt zurück. Der
ERSTE /metrics-Scrape ging noch durch, danach antwortete der ganze Prozess nicht mehr —
Prometheus scrapt alle 15 Sekunden, der Router wäre still gestorben. Gefunden, weil ich
den Ablauf Status → Scrape → Status → Anfrage einzeln durchgespielt habe, nicht weil die
Suite es gemerkt hätte (sie scrapt einmal). Jetzt steht die Sperre richtig und der Ablauf
ist auf n95 live geprüft: Scrape, Status, Chat, 20 Sekunden später wieder alles 200.

Tests: drei Prüfungen mehr — +Inf-Eimer gleich der Zahl bedienter Anfragen, Eimer
monoton, p95 im Status. make test: PASS, 232 Prüfungen, 160 s.
2026-11-08 13:21:45 +00:00

121 lines
5.6 KiB
Bash

# Retry vor Failover, Schlüssel-Pool, /metrics — sourced von tests/run.sh.
# Erwartet PORT, TMP, PIDS, contains, log_has, ok, bad aus run.sh.
echo "== 25. Wiederholung vor dem Weiterziehen =="
# Ein Endpunkt, der die erste Anfrage mit 503 abweist und danach bedient. Ohne Retry
# müsste junction weiterziehen (und die Kette hätte kein zweites Ziel) — die Anfrage
# wäre also verloren, obwohl der Endpunkt eine Sekunde später gesund ist.
P_RETRY=$((PORT+30))
P_RETRYJ=$((PORT+40))
P_MET=$((PORT+37))
STUB_STATE="$TMP/retry.state" tests/stub $P_RETRY "RETRY" --fail-n 1 & PIDS+=($!)
: > "$TMP/retry.state"
sleep 0.4
cat > "$TMP/retry.conf" <<CFG
listen 127.0.0.1:$P_RETRYJ
retry_num 1
retry_backoff 0.1
metrics tcp 127.0.0.1:$P_MET
endpoint box {
base http://127.0.0.1:$P_RETRY/v1
context 64000
timeout 10
idle_timeout 5
}
req for "*" check none pass box
CFG
./junction --config "$TMP/retry.conf" > "$TMP/retry.log" 2>&1 & PIDS+=($!)
wait_port $P_RETRYJ 10
R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \
-d '{"model":"m","messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \
"http://127.0.0.1:$P_RETRYJ/v1/chat/completions")
contains "$R" "served by RETRY" "a 503 is retried on the same endpoint instead of failing over"
log_has "$TMP/retry.log" "retry endpoint=box" "the retry is logged with its reason"
log_has "$TMP/retry.log" "reason=upstream_status_503" "the log names the upstream status"
M=$(curl -s -m 10 "http://127.0.0.1:$P_RETRYJ/metrics")
contains "$M" "junction_retries_total 1" "the retry counter is exported"
contains "$M" "junction_requests_total{outcome=\"ok\"} 1" "the served request is counted"
S=$(curl -s -m 10 "http://127.0.0.1:$P_RETRYJ/status")
contains "$S" '"retries":{"count":1' "/status reports the retry count too"
# Latenz als Histogramm: ein Mittelwert versteckt genau die Anfragen, die weh tun.
# Der +Inf-Eimer muss die Zahl der bedienten Anfragen tragen, und die Eimer müssen
# kumulativ sein — sonst rechnet histogram_quantile in Prometheus Unsinn.
inf=$(printf '%s' "$M" | sed -n 's/^junction_endpoint_latency_seconds_bucket{endpoint="box",le="+Inf"} \([0-9]*\)$/\1/p')
cnt=$(printf '%s' "$M" | sed -n 's/^junction_endpoint_latency_seconds_count{endpoint="box"} \([0-9]*\)$/\1/p')
if [ -n "$inf" ] && [ "$inf" = "$cnt" ] && [ "${inf:-0}" -ge 1 ]; then
ok "the latency histogram is cumulative and +Inf equals the served count ($inf)"
else
bad "latency histogram: +Inf='${inf:-}' count='${cnt:-}'"
fi
printf '%s' "$M" | awk '
/^junction_endpoint_latency_seconds_bucket\{endpoint="gated",le="/ {
if ($0 ~ /le="\+Inf"/) next;
n = split($0, parts, " ");
v = parts[n] + 0;
if (v < prev) bad = 1;
prev = v;
}
END { exit bad ? 1 : 0 }
' && ok "the buckets are monotone" || bad "the buckets are not monotone"
contains "$S" '"p95_ms"' "/status reports a p95 upper bound per endpoint"
echo "== 26. Schlüssel-Pool pro Endpunkt =="
# Zwei Konten bei einem Anbieter: der erste Schlüssel ist gesperrt (429), der zweite
# nicht. junction muss auf den zweiten rotieren, statt den Endpunkt aufzugeben.
P_POOL=$((PORT+35))
P_POOLJ=$((PORT+36))
P_KEY_FIRST=first-key-aaa
P_KEY_SECOND=second-key-bbb
tests/stub $P_POOL "POOL" --deny-auth "first-key" & PIDS+=($!)
sleep 0.4
cat > "$TMP/pool.conf" <<CFG
listen 127.0.0.1:$P_POOLJ
retry_num 1
retry_backoff 0.1
endpoint box {
base http://127.0.0.1:$P_POOL/v1
api_key_env P_KEY_FIRST
api_key_env P_KEY_SECOND
context 64000
timeout 10
idle_timeout 5
}
req for "*" check none pass box
CFG
P_KEY_FIRST=$P_KEY_FIRST P_KEY_SECOND=$P_KEY_SECOND \
./junction --config "$TMP/pool.conf" > "$TMP/pool.log" 2>&1 & PIDS+=($!)
wait_port $P_POOLJ 10
RH=$(curl -s -m 20 -D- -X POST -H 'Content-Type: application/json' \
-d '{"model":"m","messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \
"http://127.0.0.1:$P_POOLJ/v1/chat/completions")
contains "$RH" "X-Stub-Auth: Bearer $P_KEY_SECOND" "a 429 on the first key rotates to the second"
SP=$(curl -s -m 10 "http://127.0.0.1:$P_POOLJ/status")
contains "$SP" '"keys":2' "/status shows the pool size"
echo "== 27. /metrics =="
# Prometheus-Textformat: der Scraper soll nicht JSON nach Prometheus übersetzen müssen.
MH=$(curl -s -m 10 -D- -o /dev/null "http://127.0.0.1:$P_RETRYJ/metrics")
contains "$MH" "text/plain; version=0.0.4" "/metrics speaks the Prometheus text format"
contains "$M" "junction_endpoint_up{endpoint=\"box\"} 1" "an endpoint that serves is up 1"
contains "$M" "junction_inflight " "the queue is exported"
contains "$M" "junction_cache_hits_total" "cache counters are exported"
if printf '%s' "$M" | grep -qE '^junction_requests_total\{outcome="ok"\} [0-9]+$'; then
ok "every sample line is name{labels} value"
else
bad "every sample line is name{labels} value"
fi
# Eigener Listener für die Überwachung: dort gibt es nur /metrics. Ein Scraper soll
# die Zahlen sehen, aber nicht über /admin Endpunkte pausieren können.
MM=$(curl -s -m 10 "http://127.0.0.1:$P_MET/metrics")
contains "$MM" "junction_endpoint_up{endpoint=\"box\"}" "the metrics listener serves /metrics"
MX=$(curl -s -m 10 -o /dev/null -w '%{http_code}' -X POST -H 'Content-Type: application/json' \
-d '{"model":"m","messages":[{"role":"user","content":"x"}]}' \
"http://127.0.0.1:$P_MET/v1/chat/completions")
[ "$MX" = "404" ] && ok "the metrics listener refuses model traffic (404)" \
|| bad "the metrics listener refuses model traffic (got $MX)"
MA=$(curl -s -m 10 -o /dev/null -w '%{http_code}' -X POST "http://127.0.0.1:$P_MET/admin/pause/box")
[ "$MA" = "404" ] && ok "the metrics listener refuses /admin (404)" \
|| bad "the metrics listener refuses /admin (got $MA)"