feat: add Rails request context support#144
Open
dustinbyrne wants to merge 10 commits intomainfrom
Open
Conversation
turnipdabeets
approved these changes
May 6, 2026
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
Add Rails request-scoped PostHog context so server-side captures and exception events during a request can inherit useful request metadata and, when enabled, PostHog tracing headers (
X-PostHog-Distinct-Id,X-PostHog-Session-Id).This aligns Ruby/Rails behavior with Node/Python server SDK context handling while keeping the context implementation internal to avoid expanding the public Ruby API surface.
Middleware behavior:
RequestContextnow always wraps Rails requests and applies request metadata ($current_url,$request_method,$request_path,$user_agent,$ip) to captures made during the request.config.use_tracing_headers.CaptureExceptionsonly extracts authenticated Rails user identity and exception-specific metadata; request context identity/session/properties are applied by the core capture path.💚 How did you test it?
bundle exec rspec spec/posthog/client_spec.rb spec/posthog/rails/request_context_spec.rb spec/posthog/rails/railtie_spec.rbbundle exec rubocop --force-exclusion lib/posthog/client.rb posthog-rails/lib/posthog/rails/configuration.rb posthog-rails/lib/posthog/rails/request_context.rb posthog-rails/lib/posthog/rails/request_metadata.rb posthog-rails/lib/posthog/rails/capture_exceptions.rb spec/posthog/rails/request_context_spec.rbpnpm test:unit src/__tests__/context.spec.ts --runInBand📝 Checklist
If releasing new changes
pnpm changesetto generate a changeset file