fix(metrics): second-scale buckets for edge_request_duration #186
Reference in New Issue
Block a user
Delete Branch "fix/edge-latency-histogram-buckets"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Fixes the constantly-flapping "Gateway request latency p99 high" alert (fires then immediately resolves on nearly every app open, on both the test and prod contours).
Root cause
edge_request_durationrecords durations in seconds (WithUnit("s"),time.Since(start).Seconds()) but was created without explicit bucket boundaries, so it used the OTel SDK's default millisecond-calibrated boundaries (0, 5, 10, 25, …). Real request latencies are milliseconds (0.001–0.5 s), so every request under 5 s fell into the single first bucketle=5.histogram_quantile(0.99, …)then interpolates0.99 × 5 = 4.95s— uniformly for every message type, including trivially-fast ones.Confirmed on the contour's Prometheus: p99 = 4.950s for
profile.get,friends.list,games.list, … — impossible as real latency.That 4.95s > the 1s alert threshold, so the alert tripped on essentially every request and flapped fire/resolve as the sparse 5m rate window shifted.
Fix
Set explicit second-scale bucket boundaries on the histogram (
0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10) straddling the 1s SLO, so the p99 reflects the real (mostly sub-second) latency and the alert fires only on genuine slowness. No alert-rule change needed.Tests
TestEdgeMetricextended to assert the histogram carries a sub-second boundary (guards against reverting to the ms-default that caused the flap).go build/vet/test ./gateway/...+gofmtgreen.After deploy
Takes effect once the gateway is redeployed (buckets are set at instrument creation). New samples then bucket correctly; the p99 panel/alert become meaningful within ~5 min of traffic.