Repository navigation
perf(rails): skip env keys that can't match a tracing header - #395
Open
thomasklemm wants to merge 1 commit into
Open
thomasklemm wants to merge 1 commit into
thomasklemm wants to merge 1 commit into
Conversation
When a tracing header such as X-POSTHOG-SESSION-ID is absent, which is the case on most server-rendered requests, extract_header falls back to scanning the Rack env and normalized every key with upcase and tr: two new Strings per key, for each of the two tracing headers, on every request (close to 300 objects with the 73 env keys a small Rails 8.1 app has at this middleware). For an ASCII String key, upcase and tr keep its length, so a key whose length matches none of the normalized target names cannot match one and is skipped before it is copied. Non-ASCII keys, whose length upcase can change (e.g. "st" to "ST"), and non-String keys are still normalized and compared as before, so Rack env keys match as before. Only a key object whose to_s returns something other than its own content, or that doesn't respond to is_a?, can now be judged differently.
thomasklemm
marked this pull request as ready for review
November 26, 2026 13:31
This branch has not been deployed
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.
💡 Motivation and Context
When a request has no
X-POSTHOG-DISTINCT-IDorX-POSTHOG-SESSION-IDheader, as most server-rendered page requests don't,TracingHeaders.extract_headerscans the Rack env and builds an upcased, translated copy of every key to compare it. That is two Strings per key for each of the two headers, on every request. While profiling a few Rails apps, this was the top posthog-rails line in Dawarich's request specs.For an ASCII key,
upcaseandtrkeep its length, so a key whose length matches none of the normalized names cannot match and is now skipped before it is copied. Non-ASCII keys, whose lengthupcasecan change ("st"becomes"ST"), and non-String keys are normalized and compared as before, so Rack env keys match as before. A key object that overrides core methods, such as ato_sthat returns something other than its own content, could now be judged differently.RequestContext#callfor a browser request without tracing headers, with the 73-key env a minimal Rails 8.1 app passes to it:RequestContext#call💚 How did you test it?
The new spec covers the two kinds of key only the scan finds: a lowercase Rack-style name, and one with a non-ASCII character that
upcaseturns into two ASCII letters. It fails with a length check that ignores the encoding or forgets theHTTP_name. I also compared the old and newextract_headeron several thousand env shapes (case,HTTP_prefix, dashes and underscores, frozen, binary and UTF-16 strings, non-ASCII and non-String keys): both return the same value or raise the same error. In Dawarich's request specs, the normalizing line went from 342 to 29 objects per request, with the same test results.📝 Checklist
If releasing new changes
.changeset/tracing-headers-allocations.md, posthog-rails patch)🤖 Agent context
Autonomy: Human-driven (agent-assisted)
Thomas Klemm directed the work; Amp (Claude) found the line with an allocation profiler on a few open-source Rails apps' test suites and prepared the change, the spec and the measurements. A plain length check was rejected because
upcasecan lengthen or shorten non-ASCII keys, so those keep the old comparison. The agent session is private and can't be linked.