open-telemetry/context
OpenTelemetry Context for PHP: immutable execution-scoped context propagation. Activate a context to create a scope and safely detach it (try/finally). Includes debug scope warnings, and optional async support for fibers and event loops.
tenant_id, user_id) to traces, which is critical for multi-tenant Laravel applications or compliance-driven use cases.bindContext() pattern automates context propagation in async callbacks, reducing boilerplate and errors.Context::activate()/detach() pattern maps cleanly to Laravel’s middleware lifecycle. Wrap middleware logic in a try/finally block to ensure context restoration.
public function handle($request, Closure $next) {
$scope = Context::getCurrent()->activate();
try {
return $next($request);
} finally {
$scope->detach();
}
}
$stack = HandlerStack::create();
$stack->push(Middleware::mapRequest(function (RequestInterface $request) {
return $request->withHeader('otel-context', Context::getCurrent()->getBaggage()->toString());
}));
dispatchSync or by wrapping job execution in activate()/detach().php artisan queue:work) inherit the root context by initializing it early in the bootstrap process.| Risk Area | Mitigation Strategy |
|---|---|
| Fiber/Async Complexity | Test thoroughly with Swoole/ReactPHP. Use OTEL_PHP_FIBERS_ENABLED cautiously in production. |
| Debug Scopes in Prod | Disable via OTEL_PHP_DEBUG_SCOPES_DISABLED if exit/die is used (not recommended). |
| Memory Leaks | Monitor ContextStorageNode GC after detach() (fixed in v1.2.1). |
| PHP Version Compatibility | Drop PHP 7.4/8.0 support (already done); ensure PHP 8.1+ for fibers. |
| Vendor Lock-in | Adhere to OpenTelemetry spec; avoid custom extensions. |
Async Workloads:
bindContext() in production?Observability Stack:
tenant_id to traces for multi-tenant apps?Laravel Ecosystem:
Performance:
activate()/detach() in high-throughput scenarios (e.g., 10K RPS)?Debugging:
detach() calls in production? (Debug scopes emit warnings by default.)OTEL_PHP_DEBUG_SCOPES_DISABLED) but risk silent leaks.Future-Proofing:
Context::getBaggage() to attach user_id to logs for correlation?spatie/laravel-query-logger).php artisan migrate).OTEL_PHP_FIBERS_ENABLED and preload vendor/autoload.php for non-CLI SAPIs.bindContext() for ReactPHP/Amp callbacks.activate()/detach() or use Laravel’s dispatchSync with context.otel-context).trace_id column).| Phase | Action Items | Dependencies |
|---|---|---|
| Phase 1: Core Tracing | 1. Add open-telemetry/context and opentelemetry-php/sdk to composer.json. |
OpenTelemetry backend (Jaeger/Datadog). |
| 2. Instrument middleware to activate context on incoming requests. | Laravel middleware stack. | |
| 3. Propagate context to HTTP clients (Guzzle) via headers. | Guzzle middleware. | |
| Phase 2: Async Support | 4. Enable fiber support (OTEL_PHP_FIBERS_ENABLED) for PHP 8.1+ apps. |
PHP 8.1+, ext-ffi. |
5. Implement bindContext() for event loops (ReactPHP/Amp). |
Event loop library. | |
6. Wrap queue jobs in activate()/detach() or use dispatchSync with context. |
Laravel Queues. | |
| Phase 3: Extended Observability | 7. Attach baggage (e.g., tenant_id) to traces/logs for correlation. |
OpenTelemetry exporter. |
| 8. Integrate with logging (Monolog) to include trace IDs in logs. | Monolog processor. |
ext-ffi for fiber support (PHP 8.1+).ext-swoole/ext-react for async integrations.opentelemetry-php/sdk (v1.x+).bindContext() with Swoole/ReactPHP in staging.user_id in logs).open-telemetry/context for breaking changes (e.g., PHP 8.2+ support).opentelemetry-php/sdk in lockstep to avoid compatibility issues.detach() in non-production. Disable via OTEL_PHP_DEBUG_SCOPES_DISABLED if needed (tradeoff: silent leaks).Context::getCurrent()->debugDump() for troubleshooting.OTEL_PHP_DEBUG_SCOPES_DISABLED: Disable debug warnings.OTEL_PHP_FIBERS_ENABLED: Enable fiber support (PHP 8.1+).detach(): Wrap activate() in try/finally blocks.vendor/autoload.php is preloaded for non-CLI SAPIs.otel-context header.How can I help you explore Laravel packages today?