refactor: optimize custom metric aggregator memory#18319
Merged
parametalol merged 2 commits intomasterfrom Jan 13, 2026
Merged
Conversation
Contributor
|
Images are ready for the commit at 680c2be. To use with deploy scripts, first |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #18319 +/- ##
=======================================
Coverage 48.92% 48.93%
=======================================
Files 2629 2629
Lines 197814 197826 +12
=======================================
+ Hits 96787 96797 +10
- Misses 93642 93643 +1
- Partials 7385 7386 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `central/metrics/custom/tracker/tracker_base.go:311-312` </location>
<code_context>
if !gr.trySetRunning() {
return nil
}
+ // Recreate aggregator if config has changed since last run.
+ if gr.config != cfg {
+ gr.aggregator = makeAggregator(cfg.metrics, cfg.filters, tracker.getters)
+ gr.config = cfg
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Pointer-based config comparison may miss changes if the configuration is mutated in place
This relies on pointer inequality (`gr.config != cfg`) rather than config content or a version, so it only works if callers always replace the `*Configuration` instead of mutating or reusing it. If a `Configuration` is updated in place, the pointers remain equal, the aggregator won’t be rebuilt, and metrics can become stale. Either enforce/clarify immutability of `Configuration` or introduce a version/epoch or other change indicator instead of comparing pointers.
Suggested implementation:
```golang
// Recreate aggregator if config has changed since last run.
// Use a configuration version/epoch instead of pointer identity so that
// in-place configuration mutations are also detected.
if gr.configVersion != cfg.Version {
gr.aggregator = makeAggregator(cfg.metrics, cfg.filters, tracker.getters)
gr.configVersion = cfg.Version
gr.config = cfg
}
```
To fully implement this correctly, you will also need to:
1. **Add a version/epoch field to the configuration type** (whatever `cfg` is):
- For example:
```go
type Configuration struct {
Version uint64
// existing fields: metrics, filters, ...
}
```
- Ensure that any code that mutates the configuration either:
- Treats it as immutable and creates a new `Configuration` with an incremented `Version`, or
- Explicitly increments `Version` whenever the configuration is changed in place.
2. **Extend the `gatherer` struct to store the last-seen config version**:
- In the `gatherer[F]` type definition, add:
```go
configVersion uint64
config *Configuration // or the actual config type, if not already present
```
(If `config` is already present in the struct, just add `configVersion`.)
3. **Initialize `configVersion` when a new gatherer is created**:
- Wherever a new `gatherer` is constructed (likely in the same file), set:
```go
gr.configVersion = cfg.Version
gr.config = cfg
```
- This ensures the first run starts with the correct baseline version.
4. **Update all existing references to config comparison**:
- If there are other places that compare `gr.config` and `cfg` by pointer (`gr.config != cfg` or `==`), update them to compare `configVersion` vs `cfg.Version` instead, or remove redundant checks if they become unnecessary.
These additional changes ensure the aggregator is safely rebuilt whenever the configuration changes, regardless of whether the change is via replacement or in-place mutation.
</issue_to_address>
### Comment 2
<location> `central/metrics/custom/tracker/tracker_base_test.go:360-369` </location>
<code_context>
+func TestTrackerBase_Gather_afterReconfiguration(t *testing.T) {
</code_context>
<issue_to_address>
**suggestion (testing):** Also assert that old metrics are no longer gathered after reconfiguration
To fully validate the unregister path and aggregator re-creation, please also assert that metrics from the initial configuration (e.g., keys from `md1`) are absent from `newMetrics`. This will confirm we’re not emitting stale metrics after a config change and that old aggregations are correctly cleaned up.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This was referenced Dec 23, 2025
janisz
reviewed
Jan 13, 2026
janisz
approved these changes
Jan 13, 2026
e92df27 to
680c2be
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.
Description
An aggregator allocates a map of maps for counting the findings and populating the metrics output.
We don't need to do it actually on every gathering, so this PR optimizes aggregator initialization to create it once
per gatherer.
This will also help for future trackers of type counter, where it would be even more expensive to create an aggregator
on each increment event.
User-facing documentation
Testing and quality
Automated testing
How I validated my change
Benchmark Results
The benchmarks (not committed) compare the old approach (creating new aggregator per cycle) vs the new approach (reusing with reset):
The optimization in commit e92df27 saves approximately: