optimize hot path by caching contexual attribute - #4690
Merged
Conversation
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
jgao54
force-pushed
the
fix-ctx-optimization
branch
from
August 13, 2026 09:58
aa619f5 to
bd9603c
Compare
jgao54
force-pushed
the
fix-ctx-optimization
branch
from
August 13, 2026 10:03
bd9603c to
fc4b5a2
Compare
Contributor
Author
|
Claude caught the scenario that when there are more than one mirrors in the flow pulling records concurrently, gauge will record in an interleaving manner with different context (since gauge/counter are singletons), and the cache wouldn't help much. Updated PR to have one cached entry per mirror instead. Benchmark shows the result before and after: Before: After: |
jgao54
force-pushed
the
fix-ctx-optimization
branch
from
August 13, 2026 10:15
fc4b5a2 to
e906058
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.
Two related fixes in this PR
1. Cache contextual attribute
All our counters/gauges are implemented with
ContextAware*, which when executingRecordwill attempt to build attributes based on context:This is intentional to standardize attributes (see
buildContextualAttributes). However, on hot path like in thePullRecordloop, this can get pretty expensive and wasteful. We have observed flow-worker spending ~14% CPU just rebuilding attributes from the same context and generating the same result:Fix here is to front the context building with a cache. Benchmark result on my machine shows ~3.6x improvement:
2. Reduce gauge record frequency for MongoDB
A second optimization is in the MongoDB PullRecord loop, where instead of recording the gauge on every change event, only do so once per second. This is because SyncGauge is still not free (as seen in benchmark above), and Record just updates the cached value in the Gauge, not actually exporting it, so is essentially wasted work. This was attributing to another 5% of the cpu:
As a follow-up, worth also apply this change for PG/MySQL, but Mongo tends to be the most bottlenecked on CDC due to slow JSON processing, so prioritizing it here (also considered generalizing this mechanism into the gauge itself, but it's a bit unconventional to build sampling into gauge (more reasonable for traces).