← Back to Pull requests

apiusagemonitoring: prune filter cache of removed routes #4200

Merged
hdimer merged 5 files (135 lines) on Aug 24, 2026 16:07 UTC
View on GitHub
hdimer hdimer commented on Aug 20, 2026 03:44 UTC
Author

Fixes #3552.

apiUsageMonitoringSpec.filterMap, added in #3551, is only ever written and read, never pruned. Every distinct filter-arg key the process sees stays cached for the lifetime of the instance, so on a long-running skipper whose routes churn the cache grows without bound. Note it is not only route churn: when the kubernetes admission webhook is enabled, ValidateRoute also goes through CreateFilter, so a parseable apiUsageMonitoring config that never becomes a live route still adds a permanent entry.

The spec now implements routing.PostProcessor: after each route table rebuild it collects the *apiUsageMonitoringFilter instances the new routes reference and drops every cache entry that is not among them. That is the cleanup you pointed at in the issue; the shape is closest to OpenPolicyAgentRegistry.Do, which reaps by instance identity rather than by route id like the scheduler does.

A post-processor rather than a pre-processor because filter instances only exist after route creation, so that is the first point where "still in use" is knowable.

No delete delay, unlike the scheduler queues and the OPA instances: those defer because something gets closed, whereas dropping a map entry here does not touch the filter. A route still holding the pointer keeps working, the entry is simply not served from cache again, and the worst case of a mis-scoped route slice is one recompute rather than a closed resource.

The mutex in Do is load-bearing, not decoration. Two Do calls cannot overlap, but the admission webhook path above reaches CreateFilter from request goroutines, so the sweep genuinely races the cache write.

skipper.go registers it via an interface assertion, which also encodes "monitoring is disabled": with -api-usage-monitoring=false the spec is a noopSpec that caches nothing and does not implement Do.

Tested: two cached filters and a route table referencing one of them, placed on the second route and behind another filter so the test fails if the scan stops at the first route or the first filter; the referenced entry survives and still hits the cache, the other is dropped and recreated. Plus an empty route table emptying the cache. Ran gofmt -s, go build ./..., go vet, staticcheck, and the package and routing tests with -race.

Written with AI assistance (Claude); I reviewed the change, wrote and ran the tests, and I stand behind it.

Diff: 135 lines (+128 / -7) across 5 files Reviewer: szuecs Review duration: 6502m
All Vouch review confidence checks passed
Residual risk 0.06 = Change Risk (0.38) × (1 − Review Confidence (0.85)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.38 · 135 lines diff across 5 files
0.38
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.80 · 5 substantive comment(s)
0.80
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
85%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.294
Real PR Conversation Timeline
13 items
szuecs szuecs commented on Aug 24, 2026 08:20 UTC
Contributor

Thanks your write up seems to be nailing it :)

szuecs szuecs approved these changes on Aug 24, 2026 08:23 UTC
Approved Reviewer

Approved these changes.

szuecs szuecs commented on Aug 24, 2026 08:48 UTC
Contributor

@hdimer can you please fetch newest changes from origin/master and rebase your branch on this and do a force push?
The tests break because of Go 1.27 and the old linter version.

AlexanderYastrebov AlexanderYastrebov commented on code on Aug 24, 2026 11:02 UTC
Code Review
skipper.go:1
@@ -2514,6 +2516,12 @@ func run(o Options, sig chan os.Signal, idleConnsCH chan struct{}) error {
 		ro.PostProcessors = append(ro.PostProcessors, failClosedRatelimitPostProcessor)
 	}
 
+	// the api usage monitoring spec caches filters and implements
+	// routing.PostProcessor to prune that cache, unless it is disabled
+	if pp, ok := apiUsageMonitoringFilter.(routing.PostProcessor); ok {

Why conditional if we know it implements the interface?

hdimer hdimer commented on code on Aug 24, 2026 13:47 UTC
Code Review
skipper.go:1
@@ -2514,6 +2516,12 @@ func run(o Options, sig chan os.Signal, idleConnsCH chan struct{}) error {
 		ro.PostProcessors = append(ro.PostProcessors, failClosedRatelimitPostProcessor)
 	}
 
+	// the api usage monitoring spec caches filters and implements
+	// routing.PostProcessor to prune that cache, unless it is disabled
+	if pp, ok := apiUsageMonitoringFilter.(routing.PostProcessor); ok {

When api-usage-monitoring is disabled (the default), NewApiUsageMonitoring returns a noopSpec, which implements filters.Spec but not routing.PostProcessor, so a plain assertion would panic at startup. The comma-ok guard skips registering the cache-pruning post-processor in that case. I can instead gate on o.ApiUsageMonitoringEnable explicitly if you find that clearer, though that recouples the two call sites to the flag; the type check keeps this block self-contained. Happy to go whichever way you prefer.

hdimer hdimer commented on Aug 24, 2026 13:47 UTC
Author

@szuecs rebased onto the latest master and force-pushed. Builds and go test ./filters/apiusagemonitoring/ pass locally on Go 1.27, so the earlier failure should clear.

szuecs szuecs commented on code on Aug 24, 2026 13:51 UTC
Code Review
skipper.go:1
@@ -2514,6 +2516,12 @@ func run(o Options, sig chan os.Signal, idleConnsCH chan struct{}) error {
 		ro.PostProcessors = append(ro.PostProcessors, failClosedRatelimitPostProcessor)
 	}
 
+	// the api usage monitoring spec caches filters and implements
+	// routing.PostProcessor to prune that cache, unless it is disabled
+	if pp, ok := apiUsageMonitoringFilter.(routing.PostProcessor); ok {

Maybe it would make sense to implement routing.PostProcessor with empty body for noopSpec

szuecs szuecs commented on Aug 24, 2026 14:07 UTC
Contributor

:+1:

hdimer hdimer commented on code on Aug 24, 2026 14:11 UTC
Code Review
skipper.go:1
@@ -2514,6 +2516,12 @@ func run(o Options, sig chan os.Signal, idleConnsCH chan struct{}) error {
 		ro.PostProcessors = append(ro.PostProcessors, failClosedRatelimitPostProcessor)
 	}
 
+	// the api usage monitoring spec caches filters and implements
+	// routing.PostProcessor to prune that cache, unless it is disabled
+	if pp, ok := apiUsageMonitoringFilter.(routing.PostProcessor); ok {

Good call, done. noopSpec now implements routing.PostProcessor with a noop Do that returns the routes unchanged, so the registration in skipper.go is unconditional and the type-assertion guard is gone. Added a small test that the disabled spec satisfies the interface and leaves routes untouched. Force-pushed.

szuecs szuecs commented on code on Aug 24, 2026 15:49 UTC
Code Review
skipper.go:1
@@ -2514,6 +2516,12 @@ func run(o Options, sig chan os.Signal, idleConnsCH chan struct{}) error {
 		ro.PostProcessors = append(ro.PostProcessors, failClosedRatelimitPostProcessor)
 	}
 
+	// the api usage monitoring spec caches filters and implements
+	// routing.PostProcessor to prune that cache, unless it is disabled
+	if pp, ok := apiUsageMonitoringFilter.(routing.PostProcessor); ok {

Thanks!

szuecs szuecs commented on Aug 24, 2026 15:49 UTC
Contributor

:+1:

MustafaSaber MustafaSaber approved these changes on Aug 24, 2026 16:06 UTC
Approved Reviewer

Approved these changes.

Reviewers
Assignees
hdimer hdimer
Labels
risk: low confidence: 85.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 38.2% (MEDIUM) · Higher = larger blast radius
38.2%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 85.5% · Lower = rushed or shallow review
85.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 5.5% · Safe to merge
5.5%