Skip to content

feat: add request context support#140

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

feat: add request context support#140
dustinbyrne wants to merge 3 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 PHP server-side captures, exception events, and feature flag calls can inherit request metadata when framework middleware opts in.

This keeps the SDK framework-neutral while giving Laravel, Symfony, and other integrations a shared core API.

Behavior:

  • PostHog::withContext() / Client::withContext() wrap a callback with scoped context.
  • Context is isolated per Client and per Fiber, and restored automatically.
  • contextFromHeaders() extracts PostHog frontend tracing headers (X-PostHog-Distinct-Id, X-PostHog-Session-Id).
  • capture() applies contextual distinct ID, session ID, and properties while preserving explicit capture values.
  • Captures without a real distinct ID become personless events with $process_person_profile: false; generated IDs are not used for deprecated send_feature_flags evaluation.
  • Exception capture and feature flag helpers can use the current request context.
  • Laravel and Symfony examples show request-boundary usage with ['fresh' => true].

💚 How did you test it?

  • vendor/bin/phpcs --standard=phpcs.xml lib/RequestContext.php lib/Client.php lib/PostHog.php lib/ExceptionCapture.php test/RequestContextTest.php test/ExceptionCaptureTest.php examples/request-context-laravel-middleware.php examples/request-context-symfony-kernel.php
  • vendor/bin/phpunit --no-coverage --filter 'RequestContextTest|ExceptionCaptureTest'
  • 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

@dustinbyrne dustinbyrne force-pushed the feat/request-context branch 2 times, most recently from fba69fa to 7d3f75f Compare May 7, 2026 20:47
@dustinbyrne dustinbyrne force-pushed the feat/request-context branch from 7d3f75f to 4d8feea Compare May 7, 2026 22:04
Comment thread lib/Client.php
Comment on lines -253 to -255
if ($noDistinctIdProvided) {
$properties['$process_person_profile'] = false;
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This moved into applyCaptureContext

@dustinbyrne dustinbyrne marked this pull request as ready for review May 7, 2026 22:06
@dustinbyrne dustinbyrne requested a review from a team as a code owner May 7, 2026 22:06
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