open-telemetry/context
OpenTelemetry Context for PHP: immutable, execution-scoped context propagation for tracing and telemetry. Activate/detach scopes for implicit propagation, with debug warnings for scope leaks. Supports async apps with fiber-based propagation and event loop binding.
tenant_id, user_id) to traces, which is critical for Laravel’s multi-tenant and user-specific workflows. This integrates seamlessly with Laravel’s existing request()->user() or auth()->tenant() patterns.bindContext() utility abstracts this complexity, reducing boilerplate.Kernel.php), where each request naturally inherits and propagates context through the stack.Context::activate()/detach() pattern can be adopted incrementally in middleware, services, or repositories without disrupting existing flows.OTEL_PHP_FIBERS_ENABLED and preloading vendor/autoload.php (for non-CLI SAPIs). This is a one-time configuration change with minimal runtime overhead.bindContext() helper provides a drop-in solution for wrapping callbacks, though custom event loop integrations may need adjustments for performance-sensitive use cases.| Risk Area | Assessment | Mitigation |
|---|---|---|
| Fiber/Event Loop Overhead | Fiber support requires NTS PHP + ext-ffi, which may not be available in all hosting environments. Event loop integrations (ReactPHP/Amp) could introduce subtle bugs if not tested rigorously. |
Phase rollout: Start with synchronous middleware, then test fibers/Swoole in staging. Provide fallback mechanisms (e.g., disable fibers if ext-ffi is missing). |
| Context Leaks | Improper detach() calls or early exits (e.g., die()) can cause memory leaks or lost traces. Debug scopes warn in dev but may not catch all edge cases. |
Enable OTEL_PHP_DEBUG_SCOPES_DISABLED in production only after thorough testing. Use try-finally blocks religiously and validate context cleanup in CI. |
| PHP Version Compatibility | Drops PHP 7.4/8.0 support; requires PHP 8.1+. Laravel 9+ is compatible, but legacy apps may need upgrades. | Audit Laravel/PHP version matrix. If upgrading is infeasible, evaluate alternatives like stack/stack (but lose OpenTelemetry compliance). |
| Async Race Conditions | Concurrent fibers/event loops may lead to context corruption if not properly synchronized. | Test with high-concurrency workloads (e.g., load-test WebSocket gateways). Use Context::getCurrent() defensively in async code. |
| Baggage Propagation | Custom baggage (e.g., tenant_id) must be explicitly set and propagated. Missing baggage in downstream services breaks trace correlation. |
Document baggage requirements in API contracts. Use Laravel’s request()->attributes or app()->bind() to sync baggage with existing context. |
| Vendor Lock-in | OpenTelemetry is open, but some backends (e.g., Datadog) may require proprietary extensions. | Evaluate backend compatibility early. Use OTLP (OpenTelemetry Protocol) for maximum portability. |
Async Strategy:
bindContext() suffice?Observability Backend:
Laravel Ecosystem:
tenant_id) with Laravel’s auth/tenant systems?Performance:
Compliance:
Team Readiness:
Laravel Core:
Context::activate()/detach() around the middleware pipeline to propagate traces for HTTP requests.Context to Laravel’s container for dependency injection (e.g., app(Context::class)).Context::getBaggage() to attach/detach metadata (e.g., request()->attributes ↔ baggage).Async Laravel:
OTEL_PHP_FIBERS_ENABLED and preload autoload.php. Test with Swoole\Coroutine or PHP fibers.bindContext() with ReactPHP/Amp loops. For custom loops, implement Context::getCurrent() restoration.Context::storeInBaggage() and Context::extractFromBaggage().HTTP Clients:
Propagator to inject/extract trace context in headers.CLI/Artisan:
Artisan::command() in Context::activate() to trace CLI workflows (e.g., php artisan queue:work).WebSockets:
authenticate middleware).| Phase | Components | Actions | Dependencies |
|---|---|---|---|
| Phase 1 | Synchronous Laravel | 1. Add open-telemetry/context and opentelemetry-php/sdk.2. Instrument middleware to propagate traces.3. Test HTTP requests and CLI commands. |
Laravel 9+, PHP 8.1+, OpenTelemetry SDK. |
| Phase 2 | Async Workers (Queues, Swoole) | 1. Enable fiber support (OTEL_PHP_FIBERS_ENABLED).2. Wrap job dispatches in Context::activate().3. Test queue retries and Swoole coroutines. |
Swoole extension, ext-ffi (if using fibers). |
| Phase 3 | Event Loops (ReactPHP/Amp) | 1. Integrate bindContext() with event loops.2. Test WebSocket gateways and async APIs. |
ReactPHP/Amp, custom event loop code. |
| Phase 4 | Baggage and Custom Metadata | 1. Attach baggage (e.g., tenant_id) in middleware.2. Sync with Laravel’s auth/tenant systems.3. Validate trace correlation across services. |
OpenTelemetry exporter (Jaeger/Datadog). |
| Phase 5 | Observability Backend Integration | 1. Configure OTLP/Jaeger/Datadog exporter.2. Validate end-to-end traceability.3. Set up alerts for dropped traces. | Backend-specific SDK (e.g., ddtrace for Datadog). |
How can I help you explore Laravel packages today?