Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shiny-shoes-feel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"posthog-php": minor
---

Add an optional OpenTelemetry context processor for session and person IDs on application spans.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ SDK usage examples and code snippets live in the official documentation so they
- [Laravel integration docs](https://posthog.com/docs/libraries/laravel)
- [Upgrade notes](UPGRADING.md), including GeoIP configuration and compatibility

## OpenTelemetry context

The optional `PostHog\OpenTelemetry\ContextSpanProcessor` adds session and person IDs to application spans inside this client's request context. Register it on the application's existing OpenTelemetry provider. OpenTelemetry remains an optional application dependency.

A client with no project key keeps request context but adds no span attributes. A valid-key client with the `noop` consumer still adds them.

The registered processor also enables request span enrichment when `withContext()` starts. This lookup skips Fibers because uninitialized OpenTelemetry Fiber storage can return another execution path's span. Child spans still use the active Fiber's PostHog context. New Fibers and queue jobs need explicit context propagation.

Existing span attributes take precedence. The processor preserves the application's provider, sampling, and exporters. See the [PHP tracing guide](https://posthog.com/docs/distributed-tracing/installation/php) and [Laravel guide](https://posthog.com/docs/libraries/laravel) for setup.

## Questions?

### [Join our Slack community.](https://join.slack.com/t/posthogusers/shared_invite/enQtOTY0MzU5NjAwMDY3LTc2MWQ0OTZlNjhkODk3ZDI3NDVjMDE1YjgxY2I4ZjI4MzJhZmVmNjJkN2NmMGJmMzc2N2U3Yjc3ZjI5NGFlZDQ)
Expand Down
228 changes: 228 additions & 0 deletions api/public-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -3396,6 +3396,234 @@
}
}
},
"PostHog\\Internal\\SpanContext": {
"type": "class",
"abstract": false,
"final": true,
"readonly": false,
"extends": null,
"implements": [],
"constants": [],
"properties": [],
"methods": {
"enrich": {
"static": true,
"abstract": false,
"final": false,
"returnType": "void",
"parameters": [
{
"name": "client",
"type": "PostHog\\Client",
"byReference": false,
"variadic": false,
"optional": false,
"default": null,
"defaultConstant": null,
"hasDefault": false
},
{
"name": "span",
"type": "OpenTelemetry\\API\\Trace\\SpanInterface",
"byReference": false,
"variadic": false,
"optional": false,
"default": null,
"defaultConstant": null,
"hasDefault": false
}
]
},
"enrichCurrent": {
"static": true,
"abstract": false,
"final": false,
"returnType": "void",
"parameters": [
{
"name": "client",
"type": "PostHog\\Client",
"byReference": false,
"variadic": false,
"optional": false,
"default": null,
"defaultConstant": null,
"hasDefault": false
}
]
},
"register": {
"static": true,
"abstract": false,
"final": false,
"returnType": "void",
"parameters": [
{
"name": "client",
"type": "PostHog\\Client",
"byReference": false,
"variadic": false,
"optional": false,
"default": null,
"defaultConstant": null,
"hasDefault": false
},
{
"name": "processor",
"type": "object",
"byReference": false,
"variadic": false,
"optional": false,
"default": null,
"defaultConstant": null,
"hasDefault": false
}
]
},
"unregister": {
"static": true,
"abstract": false,
"final": false,
"returnType": "void",
"parameters": [
{
"name": "client",
"type": "PostHog\\Client",
"byReference": false,
"variadic": false,
"optional": false,
"default": null,
"defaultConstant": null,
"hasDefault": false
},
{
"name": "processor",
"type": "object",
"byReference": false,
"variadic": false,
"optional": false,
"default": null,
"defaultConstant": null,
"hasDefault": false
}
]
}
}
},
"PostHog\\OpenTelemetry\\ContextSpanProcessor": {
"type": "class",
"abstract": false,
"final": true,
"readonly": false,
"extends": null,
"implements": [
"OpenTelemetry\\SDK\\Trace\\SpanProcessorInterface"
],
"constants": [],
"properties": [],
"methods": {
"__construct": {
"static": false,
"abstract": false,
"final": false,
"returnType": null,
"parameters": [
{
"name": "client",
"type": "PostHog\\Client",
"byReference": false,
"variadic": false,
"optional": false,
"default": null,
"defaultConstant": null,
"hasDefault": false
}
]
},
"forceFlush": {
"static": false,
"abstract": false,
"final": false,
"returnType": "bool",
"parameters": [
{
"name": "cancellation",
"type": "?OpenTelemetry\\SDK\\Common\\Future\\CancellationInterface",
"byReference": false,
"variadic": false,
"optional": true,
"default": null,
"defaultConstant": null,
"hasDefault": true
}
]
},
"onEnd": {
"static": false,
"abstract": false,
"final": false,
"returnType": "void",
"parameters": [
{
"name": "span",
"type": "OpenTelemetry\\SDK\\Trace\\ReadableSpanInterface",
"byReference": false,
"variadic": false,
"optional": false,
"default": null,
"defaultConstant": null,
"hasDefault": false
}
]
},
"onStart": {
"static": false,
"abstract": false,
"final": false,
"returnType": "void",
"parameters": [
{
"name": "span",
"type": "OpenTelemetry\\SDK\\Trace\\ReadWriteSpanInterface",
"byReference": false,
"variadic": false,
"optional": false,
"default": null,
"defaultConstant": null,
"hasDefault": false
},
{
"name": "parentContext",
"type": "OpenTelemetry\\Context\\ContextInterface",
"byReference": false,
"variadic": false,
"optional": false,
"default": null,
"defaultConstant": null,
"hasDefault": false
}
]
},
"shutdown": {
"static": false,
"abstract": false,
"final": false,
"returnType": "bool",
"parameters": [
{
"name": "cancellation",
"type": "?OpenTelemetry\\SDK\\Common\\Future\\CancellationInterface",
"byReference": false,
"variadic": false,
"optional": true,
"default": null,
"defaultConstant": null,
"hasDefault": true
}
]
}
}
},
"PostHog\\PostHog": {
"type": "class",
"abstract": false,
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"require-dev": {
"phpunit/phpunit": "^11.0",
"squizlabs/php_codesniffer": "^4.0",
"ext-zlib": "*"
"ext-zlib": "*",
"open-telemetry/sdk": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
Loading
Loading