← Back to Pull requests

feat(metrics): aggregate counter measurements at record time #152

Closed
unknown closed this pull request without merging on Nov 09, 2026 16:17 UTC
View on GitHub
unknown unknown commented on Nov 01, 2026 14:49 UTC
Author

Fixes #146 (Part 1: Counters and UpDownCounters).

Description


Previously, Counter.add() and UpDownCounter.add() appended each measurement as an individual DataPoint into an unbounded data_points array, which was only cleared at collection time. For high-frequency recordings on a fixed set of attributes, memory consumption grew linearly with the number of measurements (up to ~250MB peak RSS for 1M iterations).

This PR addresses Part 1 of #146 (as discussed with @agagniere and @daisuke8000) for synchronous sum instruments (Counter and UpDownCounter):
1. Record-Time In-Place Aggregation:
- Replaces data_points: std.ArrayListUnmanaged in Counter(T) with points: std.ArrayHashMapUnmanaged(Attributes, T, Attributes.ArrayHashContext, true).
- On add() / addWithSlice(), looks up existing attribute entries and accumulates delta in place.
- Preserves capacity across collections (points.clearRetainingCapacity()) to eliminate repeated map reallocations.
2. Zero-Heap Attribute Parsing:
- Implemented Attributes.fromBuffer(buffer, keyValues) so add(delta, attributes) resolves key-value pairs into a stack buffer without allocating heap memory for existing series updates.
3. Cardinality Limit Enforcement:
- Propagates cardinality_limit (defaulting to 2000 per the OpenTelemetry specification) to instruments.
- When the limit is reached, incoming new attribute sets are logged with a warning and dropped, while updates to existing series continue to aggregate.
4. Tests & Benchmarks:
- Added unit test verifying in-place aggregation without memory growth across 10,000 iterations.
- Added unit test verifying enforcement of cardinality_limit.
- Updated existing instrument and meter tests to match record-time aggregated semantics.
- Ran all unit tests (418/418 passed) and verified zero memory leaks with std.testing.allocator.
- Ran SDK counter benchmarks (UpDownCounter_Add_WithoutAttributes: ~398ns, Counter_Add_Without_Attributes: ~481ns).

Part 2 (Histograms) will follow in a separate PR in coordination with @daisuke8000 and PR #130.

Diff: 270 lines (+202 / -68) across 3 files Reviewer: agagniere Review duration: 11608m
All Vouch review confidence checks passed
Residual risk 0.16 = Change Risk (0.39) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.39 · 270 lines diff across 3 files
0.39
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
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.335
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #152"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.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: 39.4% (MEDIUM) · Higher = larger blast radius
39.4%
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: 60.5% · Lower = rushed or shallow review
60.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: 15.6% · Safe to merge
15.6%