fix(backend): cap repo-index metric cardinality and add OOM diagnostics - #1526
Open
better-stack-hq[bot] wants to merge 1 commit into
Open
fix(backend): cap repo-index metric cardinality and add OOM diagnostics#1526better-stack-hq[bot] wants to merge 1 commit into
better-stack-hq[bot] wants to merge 1 commit into
Conversation
Add Node's --heapsnapshot-near-heap-limit to the backend program so a heap snapshot is captured to a persisted directory if the process approaches its heap limit again, giving a definitive object graph instead of only a generic "JavaScript heap out of memory" crash log. Also fix an identified metric-cardinality leak: repo-scoped Prometheus metrics keyed by repo name are never cleared, so every distinct repo name observed over the process's lifetime stays resident in the registry forever, even after the repo is deleted. Remove those time series once a repo is actually deleted.
Contributor
|
@better-stack-hq[bot] your pull request is missing a changelog! |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 92ff09a. Configure here.
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.

💬 AI SRE · 👥 Sourcebot team · 👤 Requested by Brendan Kellam
Summary
--heapsnapshot-near-heap-limit=3(writing to a persistedDATA_CACHE_DIR/heap-snapshotsdirectory) to thebackendsupervisord program, so if the process approaches its heap limit again, we get an actual heap snapshot to diagnose the retained object graph — instead of only the genericFATAL ERROR: JavaScript heap out of memorycrash log.PromClient's repo-scoped metrics (activeRepoIndexJobs,pendingRepoIndexJobs,repoIndexJob{Reattempts,Fail,Success}Total) are keyed by repo name but are never cleared. Every distinct repo name ever observed over the process's lifetime stays resident in theprom-clientregistry forever, even after the repo is deleted. AddPromClient.removeRepoMetrics()and call it once a repo is actually deleted inRepoIndexManager's cleanup job.Root cause
app.sourcebot.dev'sbackendprocess (node packages/backend/dist/index.js) hitFATAL ERROR: Ineffective mark-compacts near heap limit ... JavaScript heap out of memoryafter ~20.7h of uptime, causing a ~48s window of 503s while supervisord restarted the container.Static review of the backend surfaced one confirmed correctness bug that contributes to unbounded heap growth over the process's lifetime (unbounded Prometheus label cardinality for repo names that are never released). Given the repo count in this deployment (~150-160, fairly stable), this alone may not fully account for the ~4GB heap exhausted, so this PR also adds heap-snapshot-on-near-limit diagnostics to get a definitive answer if this recurs, rather than guessing further from static analysis alone.
Test plan
yarn workspace @sourcebot/backend buildandyarn workspace @sourcebot/backend testin CI to confirm the TypeScript changes compile.supervisord status backendpicks up the newNODE_OPTIONSand that$DATA_CACHE_DIR/heap-snapshotsexists after container start./metricsno longer shows stalerepo="<deleted-repo>"series after a repo is deleted and its CLEANUP job runs.🤖 Generated with Better Stack AI SRE
Note
Medium Risk
Changes long-running backend process behavior (metrics lifecycle and NODE_OPTIONS) in response to production OOMs; logic is narrow but affects observability and memory under load.
Overview
Addresses backend heap growth and OOM crashes by bounding Prometheus memory and adding post-mortem heap dumps.
Metric cleanup: Adds
PromClient.removeRepoMetrics()to drop all repo-labeled index/cleanup time series when a repo is permanently removed, and invokes it from the CLEANUP job path right after the DB delete. This stopsprom-clientfrom retaining every historicalrepolabel combination for the life of the process.OOM diagnostics: Ensures
$DATA_CACHE_DIR/heap-snapshotsexists at container start and configures the backend supervisord program withNODE_OPTIONS(--heapsnapshot-near-heap-limit=3and--diagnostic-dirpointing at that directory) so near-limit heap pressure writes snapshots for analysis instead of only a generic OOM log.Reviewed by Cursor Bugbot for commit 92ff09a. Bugbot is set up for automated code reviews on this repo. Configure here.