Skip to content

feat: add request context support#140

Draft
dustinbyrne wants to merge 9 commits intomainfrom
feat/request-context
Draft

feat: add request context support#140
dustinbyrne wants to merge 9 commits intomainfrom
feat/request-context

Conversation

@dustinbyrne
Copy link
Copy Markdown
Contributor

@dustinbyrne dustinbyrne commented May 6, 2026

💡 Motivation and Context

Add request-scoped PostHog context helpers so server-side captures and exception events during a PHP request can inherit useful request metadata and, when enabled by an integration/user middleware, PostHog tracing headers (X-PostHog-Distinct-Id, X-PostHog-Session-Id).

This aligns PHP SDK behavior with the server-side request context guidelines while keeping the implementation framework-neutral because this repo does not include first-party Laravel/Symfony middleware.

Behavior:

  • RequestContext provides scoped request context that is restored automatically and isolated across Fibers.
  • capture() applies context properties/session/distinct ID in the core capture path, with explicit capture values taking precedence.
  • Missing capture identity now generates a personless UUID event via $process_person_profile: false.
  • captureException() and automatic exception capture can inherit request context through the core capture path.
  • contextFromHeaders() only reads X-PostHog-Distinct-Id / X-PostHog-Session-Id; framework integrations should expose their own use_tracing_headers option and skip this helper when disabled.
  • Added a single-file Laravel/Symfony-style example showing how integration middleware can wrap the request while keeping use_tracing_headers integration-scoped.

💚 How did you test it?

  • php -l examples/request-context-frameworks.php
  • ./vendor/bin/phpunit --no-coverage test/RequestContextTest.php
  • ./vendor/bin/phpcs --standard=phpcs.xml lib/RequestContext.php lib/Client.php lib/PostHog.php test/RequestContextTest.php
  • ./vendor/bin/phpunit --no-coverage (passes with existing warnings/deprecations)

📝 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

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.

1 participant