Skip to content

fix(memory): cap metrics-db event size, add DB size limit, eliminate clone in log_metrics - #1678

Closed
Siddhant-K-code wants to merge 3 commits into
mainfrom
devin/1782811882-cap-metrics-memory
Closed

Siddhant-K-code wants to merge 3 commits into
mainfrom
devin/1782811882-cap-metrics-memory

Conversation

@Siddhant-K-code

@Siddhant-K-code Siddhant-K-code commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Three targeted fixes for the memory/disk growth issue (PD-23, #1677):

1. Per-event size cap (telemetry_worker.rs): Skip events with serialized JSON > 64KB before DB insertion. Raw transcript events (assistant responses with full file content) average 50-150KB and are the primary source of unbounded DB growth. Useful telemetry (committed values, agent usage, small session events) are 1-10KB and pass through unaffected.

2. DB file size cap (telemetry_worker.rs): Stop inserting when metrics-db exceeds 512MB. Prevents unbounded disk growth when metrics upload is unavailable (no login, offline, API failures). Checked via std::fs::metadata before each batch insert.

3. Eliminate .to_vec() clone (observability/mod.rs): log_metrics now consumes the Vec<MetricEvent> via into_iter() instead of borrowing chunks and cloning them back. Eliminates doubling of peak memory during batch chunking.

// Before (clones each chunk):
for chunk in events.chunks(MAX) { submit(chunk.to_vec()); }

// After (owned iteration, zero-copy):
let mut iter = events.into_iter().peekable();
while iter.peek().is_some() { submit(iter.by_ref().take(MAX).collect()); }

Validation Results

Measured via /proc/<pid>/status RSS polling at 500ms intervals, fresh daemon restart with synthetic Cursor transcript files.

Controlled scenarios (uniform event size):

Scenario Transcripts Branch Peak RSS metrics-db
Heavy (100KB events) 976 MB main 1,511 MB 982 MB
Heavy (100KB events) 976 MB fix 51 MB 0 MB

Realistic scenario (mixed sizes: 40% @1kb, 30% @5kb, 20% @30KB, 10% @150KB):

Metric main fix Reduction
Peak RSS 411 MB 113 MB 73% ↓
Final RSS 214 MB 103 MB 52% ↓
metrics-db 227 MB 79 MB 65% ↓

Side effects

  • Events > 64KB are dropped from the metrics DB (not uploaded to server). These are raw transcript blobs - the original .jsonl files on disk still contain the full content. All small/medium telemetry events (metadata, stats, tool usage) pass through unaffected.
  • When DB exceeds 512MB, new events are silently dropped until space is freed (via upload delivery + pruning). This caps worst-case disk usage.
  • No breaking API changes; no behavior change for users with working metrics upload and normal transcript volumes.

Open in Devin Review

…rics

Two targeted fixes for the memory/disk growth issue (PD-23):

1. Cap metrics-db row size (telemetry_worker.rs):
   - Skip inserting events with serialized JSON > 256KB into the metrics DB.
   - Large raw transcript events (avg 79KB, up to several MB) were causing
     unbounded DB growth (2.4GB from 1.9GB of transcripts in our tests).
   - Most useful telemetry events are 1-10KB; oversized ones provide no
     proportional value and are now dropped with a debug log.

2. Eliminate .to_vec() clone in log_metrics (observability/mod.rs):
   - log_metrics already takes Vec<MetricEvent> by value but was borrowing
     chunks and cloning them back via .to_vec(), doubling peak memory.
   - Now consumes the Vec via into_iter().take() to produce owned chunks
     without any cloning.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@Siddhant-K-code Siddhant-K-code self-assigned this Jun 30, 2026
@devin-ai-integration

Copy link
Copy Markdown
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

- Reduce MAX_METRIC_EVENT_JSON_BYTES from 256KB to 64KB: most useful
  telemetry events are 1-10KB; raw transcript events averaging 50-100KB
  are the primary source of DB bloat.
- Add MAX_METRICS_DB_FILE_BYTES (512MB): stop inserting when the DB
  file exceeds this limit, preventing unbounded disk growth when
  metrics upload is unavailable.
- Expose MetricsDatabase::db_path() for the file-size check.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration devin-ai-integration Bot changed the title fix(memory): cap metrics-db event size and eliminate clone in log_metrics fix(memory): cap metrics-db event size, add DB size limit, eliminate clone in log_metrics Jun 30, 2026
@devin-ai-integration
devin-ai-integration Bot marked this pull request as draft June 30, 2026 10:39
@Siddhant-K-code
Siddhant-K-code deleted the devin/1782811882-cap-metrics-memory branch June 30, 2026 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant