Repository navigation
Unbounded concurrent Monitoring API requests exhaust memory and OOM-kill the container #537 - #536
Open
ntmspavan wants to merge 1 commit into
Conversation
ntmspavan
force-pushed
the
fix/monitoring-max-concurrency-oom
branch
4 times, most recently
from
October 7, 2026 19:21
38ec6f0 to
8debe5d
Compare
reportMonitoringMetrics starts one goroutine per metric descriptor per project with no cap, and each holds a fully decoded TimeSeries.List response while in flight. When google.projects.filter (or a long google.project-ids list) resolves to many projects, or a project has many metric descriptors, the number of in-flight responses is unbounded and can push a memory-limited container past its limit and get it OOM killed. Fix this without adding any configuration: - Cap concurrent TimeSeries.List requests process-wide at a fixed internal limit (maxConcurrentTimeSeriesRequests), shared by every project's collector, so a scrape never holds more than that many decoded responses in memory at once. - Read the container's cgroup memory limit at startup and set GOMEMLIMIT to 90% of it via automemlimit, so the garbage collector reclaims more aggressively as usage approaches the real limit. GOMEMLIMIT and AUTOMEMLIMIT still override it. Signed-off-by: Pavan Nalam <pavan.nalam3693@gmail.com>
ntmspavan
force-pushed
the
fix/monitoring-max-concurrency-oom
branch
from
October 7, 2026 19:30
8debe5d to
5ac415f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #537.
Problem
reportMonitoringMetricsstarts one goroutine per metric descriptor per project, with no cap. Whengoogle.projects.filter(or a longgoogle.project-idslist) resolves to many projects, or a project has many metric descriptors, the number of in-flightTimeSeries.Listrequests is unbounded. Each one holds a fully decoded JSON response in memory. In a memory-constrained pod the heap can pass the container's memory limit and the pod is OOM-killed.Fix
Cap concurrent
TimeSeries.Listrequests process-wide at a fixed internal limit (maxConcurrentTimeSeriesRequests = 20incollectors/monitoring_collector.go), shared by every project's collector. This bounds how many decoded responses a scrape holds in memory at once, however many projects or descriptors it covers. It is an internal constant, not a new flag. 20 is a conservative starting value, and I'm happy to tune it based on feedback or profiling.At startup, read the container's cgroup memory limit and set
GOMEMLIMITto 90% of it via automemlimit (https://github.com/KimMachineGun/automemlimit), so the garbage collector reclaims more aggressively as usage nears the real limit. It applies only when a container memory limit exists (for exampleresources.limits.memory). Otherwise it changes nothing. TheGOMEMLIMITandAUTOMEMLIMITenvironment variables still override it.Trade-offs
monitoring.metrics-prefixesor raisingscrape_timeouthelps in large setups.Testing
go build ./...,go vet ./...,gofmt -l .andgolangci-lint run ./...are clean.go test -race ./...passes.TestTimeSeriesRequestLimiterBoundsConcurrency, which fails if concurrency exceeds the limit or never reaches it.