Repository navigation
perf: encode a summary once per batch rather than on every observation - #81
Merged
kamilmysliwiec merged 1 commit intoNov 9, 2026
Merged
Conversation
Every `observe` on a summary from `TracerService` re-encoded it into the outgoing batch, and encoding a summary sorts a copy of its sample once for each of p50, p95 and p99. The sample holds at most 2,048 observations per label, so the cost of one `observe` grew with the window until the sample was full and then stayed there: about 0.15 ms with 500 observations since the last flush, about 1 ms from 2,048 on. A summary observed into the batch is now encoded when the batch is written, so `observe` no longer sorts and the quantiles are worked out once per batch.
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.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Every
observeon a summary fromTracerServicere-encodes it into the outgoing batch (upsertCustomMetric), and encoding a summary sorts a copy of its sample once for each ofp50,p95andp99. The sample keeps at most 2,048 observations per label, so the cost of oneobservegrows with the number of observations since the last flush until the sample is full, and then stays at that level:observebeforeThat time is spent on the main thread, inside the request that calls
observe. Measured on Node 22 against the built package, timing the last 200 ofnobservations of one summary in a single window:What is the new behavior?
A summary observed into the batch is remembered and encoded once, when the batch is written in
encodeAndWrite(about 1-3 ms per batch in the runs above, up from about 0.1 ms).observeno longer sorts. Counters and gauges are buffered as before. The values that reach the collector are unchanged, since the summary is encoded from the same window, just once. Only the order withincustomchanges: summaries now come at the end of the array.Does this PR introduce a breaking change?
Other information