Skip to content

feat: add Rails request context support#144

Open
dustinbyrne wants to merge 10 commits intomainfrom
feat/request-context
Open

feat: add Rails request context support#144
dustinbyrne wants to merge 10 commits intomainfrom
feat/request-context

Conversation

@dustinbyrne
Copy link
Copy Markdown
Contributor

@dustinbyrne dustinbyrne commented May 5, 2026

💡 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:

  • RequestContext now always wraps Rails requests and applies request metadata ($current_url, $request_method, $request_path, $user_agent, $ip) to captures made during the request.
  • PostHog tracing header identity/session usage is controlled separately via config.use_tracing_headers.
  • CaptureExceptions only 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.rb
  • bundle 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.rb
  • Validated behavior against local Node SDK context tests: pnpm test:unit src/__tests__/context.spec.ts --runInBand
  • Validated Python context/capture behavior with targeted pytest coverage for contexts and capture exception paths

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran pnpm changeset to generate a changeset file

@dustinbyrne dustinbyrne marked this pull request as ready for review May 6, 2026 17:39
@dustinbyrne dustinbyrne requested a review from a team as a code owner May 6, 2026 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants