feat: add request context support#140
Draft
dustinbyrne wants to merge 9 commits intomainfrom
Draft
Conversation
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 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:
RequestContextprovides 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.$process_person_profile: false.captureException()and automatic exception capture can inherit request context through the core capture path.contextFromHeaders()only readsX-PostHog-Distinct-Id/X-PostHog-Session-Id; framework integrations should expose their ownuse_tracing_headersoption and skip this helper when disabled.use_tracing_headersintegration-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
If releasing new changes
pnpm changesetto generate a changeset file