11# Laravel OpenTelemetry
22
3- This package provides a simple way to add [ OpenTelemetry] ( https://opentelemetry.io/ ) to your Laravel application.
3+ This package provides a simple way to add [ OpenTelemetry] ( https://opentelemetry.io/ ) ** manual instrumentation ** to your Laravel application.
44
55[ ![ CI] ( https://github.com/overtrue/laravel-open-telemetry/workflows/Test/badge.svg )] ( https://github.com/overtrue/laravel-open-telemetry/actions )
66[ ![ Latest Stable Version] ( https://poser.pugx.org/overtrue/laravel-open-telemetry/v/stable.svg )] ( https://packagist.org/packages/overtrue/laravel-open-telemetry )
77[ ![ Latest Unstable Version] ( https://poser.pugx.org/overtrue/laravel-open-telemetry/v/unstable.svg )] ( https://packagist.org/packages/overtrue/laravel-open-telemetry )
88[ ![ Total Downloads] ( https://poser.pugx.org/overtrue/laravel-open-telemetry/downloads )] ( https://packagist.org/packages/overtrue/laravel-open-telemetry )
99[ ![ License] ( https://poser.pugx.org/overtrue/laravel-open-telemetry/license )] ( https://packagist.org/packages/overtrue/laravel-open-telemetry )
1010
11+ ## 🎯 Package Positioning
12+
13+ ### vs Official Auto-Instrumentation Package
14+
15+ - ** [ Official Package] ( https://packagist.org/packages/open-telemetry/opentelemetry-auto-laravel ) ** : Automatic instrumentation using hooks, zero code changes required
16+ - ** This Package** : Manual instrumentation with Laravel-style APIs, providing fine-grained control and additional features
17+
18+ ### When to Use This Package
19+
20+ - ✅ Need precise control over span attributes and lifecycle
21+ - ✅ Want to integrate deeply with Laravel events and services
22+ - ✅ Prefer explicit instrumentation with Laravel facades
23+ - ✅ Need custom watchers and middleware
24+ - ✅ Building complex tracing scenarios
25+
26+ ### When to Use Official Auto-Instrumentation
27+
28+ - ✅ Want zero-code instrumentation
29+ - ✅ Need basic request/response tracing
30+ - ✅ Prefer automatic framework detection
31+
1132## Installation
1233
1334You can install the package via composer:
@@ -16,40 +37,123 @@ You can install the package via composer:
1637composer require overtrue/laravel-open-telemetry
1738```
1839
19- ## Usage
20-
21- ### Configuration
40+ ## Configuration
2241
23- Publish the configuration file:
42+ ### Publish Configuration File
2443
2544``` bash
2645php artisan vendor:publish --provider=" Overtrue\LaravelOpenTelemetry\OpenTelemetryServiceProvider" --tag=" config"
2746```
2847
29- ### Update the environment variables
48+ ### 📋 Configuration Categories
49+
50+ #### 🟢 Required Configuration (Recommended)
51+
52+ These configurations have defaults but should be explicitly set:
53+
54+ ``` dotenv
55+ # Basic Configuration - Highly Recommended
56+ OTEL_ENABLED=true # Enable/disable package functionality
57+ OTEL_SERVICE_NAME=my-laravel-app # Service name for identification
58+ ```
59+
60+ #### 🟡 Important Optional Configuration
61+
62+ Configure based on your use case:
63+
64+ ``` dotenv
65+ # SDK Behavior Control
66+ OTEL_SDK_AUTO_INITIALIZE=true # Auto-initialize SDK (default: true)
67+ OTEL_SERVICE_VERSION=1.0.0 # Service version (default: 1.0.0)
68+
69+ # Exporter Configuration
70+ OTEL_TRACES_EXPORTER=console # Trace export method (default: console)
71+ # OR
72+ OTEL_TRACES_EXPORTER=otlp # Use OTLP exporter
73+ OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318 # OTLP endpoint
74+ ```
75+
76+ #### 🔵 Fully Optional Configuration
77+
78+ These have sensible defaults and work without configuration:
79+
80+ ``` dotenv
81+ # Request Tracing Control
82+ OTEL_AUTO_TRACE_REQUESTS=true # Auto-trace HTTP requests (default: true)
83+
84+ # Header Handling
85+ OTEL_ALLOWED_HEADERS=* # Allowed headers (default: common headers)
86+ OTEL_SENSITIVE_HEADERS=authorization,cookie # Sensitive headers (default: common sensitive headers)
87+
88+ # Path Filtering
89+ OTEL_IGNORE_PATHS=horizon*,telescope* # Ignored paths (default: common admin paths)
90+
91+ # Response Headers
92+ OTEL_RESPONSE_TRACE_HEADER_NAME=X-Trace-Id # Trace ID in response header (default: X-Trace-Id)
93+
94+ # OTLP Detailed Configuration (only needed when using OTLP)
95+ OTEL_EXPORTER_OTLP_HEADERS= # OTLP headers (default: empty)
96+ OTEL_EXPORTER_OTLP_TIMEOUT=10 # OTLP timeout (default: 10 seconds)
97+
98+ # Other Exporters (usually not needed)
99+ OTEL_METRICS_EXPORTER=none # Metrics export (default: none)
100+ OTEL_LOGS_EXPORTER=none # Logs export (default: none)
101+ ```
102+
103+ ### 🚀 Configuration Examples by Use Case
104+
105+ #### Scenario 1: Quick Start/Testing
106+ ``` dotenv
107+ # Only need this one!
108+ OTEL_ENABLED=true
109+ ```
110+ Everything else uses defaults, spans output to console.
30111
31- > You can refer to [ OpenTelemetry SDK Configuration Instructions] ( https://opentelemetry.io/docs/languages/sdk-configuration/general )
112+ #### Scenario 2: Production Environment (External Collector)
113+ ``` dotenv
114+ OTEL_ENABLED=true
115+ OTEL_SERVICE_NAME=my-production-app
116+ OTEL_SERVICE_VERSION=2.1.0
117+ OTEL_TRACES_EXPORTER=otlp
118+ OTEL_EXPORTER_OTLP_ENDPOINT=https://your-collector.com:4318
119+ ```
32120
121+ #### Scenario 3: Development Environment (Detailed Configuration)
33122``` dotenv
34123OTEL_ENABLED=true
124+ OTEL_SERVICE_NAME=my-dev-app
125+ OTEL_TRACES_EXPORTER=console
35126OTEL_AUTO_TRACE_REQUESTS=true
127+ OTEL_IGNORE_PATHS=_debugbar*,telescope*
128+ OTEL_SENSITIVE_HEADERS=authorization,cookie,x-api-key
129+ ```
130+
131+ #### Scenario 4: Disabled/Testing Environment
132+ ``` dotenv
133+ OTEL_ENABLED=false
134+ # OR
135+ OTEL_TRACES_EXPORTER=none
136+ ```
137+
138+ ### Alternative: Official Auto-Instrumentation Setup
139+
140+ If you prefer automatic instrumentation (requires ` ext-opentelemetry ` and official auto package):
141+
142+ ``` bash
143+ composer require open-telemetry/opentelemetry-auto-laravel
144+ ```
145+
146+ ``` dotenv
36147OTEL_PHP_AUTOLOAD_ENABLED=true
37148OTEL_PHP_TRACE_CLI_ENABLED=true
38- OTEL_SERVICE_NAME=my-app
39- OTEL_TRACES_EXPORTER=otlp
40- #OTEL_EXPORTER_OTLP_PROTOCOL=grpc
41- OTEL_EXPORTER_OTLP_ENDPOINT=http://collector:4318
42149OTEL_PROPAGATORS=baggage,tracecontext
43-
44- OTEL_ALLOWED_HEADERS=*
45- OTEL_SENSITIVE_HEADERS=authorization,authorization,proxy-authorization
46- OTEL_IGNORE_PATHS=/foo,/bar*
47150```
48- and other environment variables, you can find them in the configuration file: ` config/otle.php ` .
151+
152+ ## Usage
49153
50154### Register the middleware
51155
52- you can register the middleware in the ` app/Http/Kernel.php ` :
156+ You can register the middleware in the ` app/Http/Kernel.php ` :
53157
54158``` php
55159protected $middleware = [
@@ -60,61 +164,150 @@ protected $middleware = [
60164
61165or you can set the env variable ` OTEL_AUTO_TRACE_REQUESTS ` to ` true ` to enable it automatically.
62166
63- ### Custom span
167+ ### Manual Span Creation
64168
65- You can create a custom span by using the ` Overtrue\LaravelOpenTelemetry\Facades\Measure ` facade:
169+ You can create custom spans using the ` Overtrue\LaravelOpenTelemetry\Facades\Measure ` facade:
170+
171+ #### Simple Span
66172
67173``` php
68174use Overtrue\LaravelOpenTelemetry\Facades\Measure;
69175
70176Measure::span('your-span-name')->measure(function() {
71- // ...
177+ // Your code here
72178});
73179```
74180
75- or manually start and end a span:
181+ #### Manual Start/End
76182
77183``` php
78- Measure::start('your-span-name');
184+ $span = Measure::start('your-span-name');
79185
80- // ...
186+ // Your code here
81187
82- Measure::end();
188+ Measure::end('your-span-name' );
83189```
84190
85- and you can modify the span attributes by using a closure:
191+ #### With Attributes
86192
87193``` php
88- Measure::start('your-span-name', function($span ) {
89- $span ->setAttribute('key', 'value');
90- // ...
194+ Measure::start('your-span-name', function($spanBuilder ) {
195+ $spanBuilder ->setAttribute('key', 'value');
196+ $spanBuilder->setSpanKind(\OpenTelemetry\API\Trace\SpanKind::KIND_CLIENT);
91197});
92198
93- // ...
94- Measure::end();
199+ // Your code here
200+
201+ Measure::end('your-span-name');
95202```
96203
97- of course, you can get the span instance by using the ` Measure::span() ` method:
204+ #### Direct Span Control
98205
99206``` php
100- $span = Measure::span('your-span-name');
101- $span ->setAttribute('key', 'value');
102- $scope = $span->activate ();
207+ $spanBuilder = Measure::span('your-span-name');
208+ $spanBuilder ->setAttribute('key', 'value');
209+ $span = $spanBuilder->start ();
103210
104- // ...
211+ // Your code here
105212
106213$span->end();
107- $scope->detach();
108214```
109- ### test events
110215
111- You can test the events by using the command:
216+ ### Check Tracing Status
217+
218+ ``` php
219+ // Check if tracing is recording
220+ if (Measure::isRecording()) {
221+ // Add expensive tracing operations
222+ }
223+
224+ // Get detailed status
225+ $status = Measure::getStatus();
226+ // Returns: ['is_recording' => bool, 'active_spans_count' => int, ...]
227+ ```
228+
229+ ### Test Tracing Setup
230+
231+ You can test your tracing setup using the command:
112232
113233``` bash
114- php artisan otle:test
234+ php artisan otel:test
235+ ```
236+
237+ This command will:
238+ - Check your OpenTelemetry configuration
239+ - Create test spans with attributes and events
240+ - Display trace information and diagnostics
241+ - Show environment variable status
242+
243+ ## Troubleshooting
244+
245+ ### NonRecordingSpan Issue
246+
247+ If you see ` NonRecordingSpan ` or traces not being recorded:
248+
249+ 1 . ** Check basic configuration:**
250+ ``` dotenv
251+ OTEL_ENABLED=true
252+ OTEL_SDK_AUTO_INITIALIZE=true
253+ ```
254+
255+ 2 . ** Run the test command:**
256+ ``` bash
257+ php artisan otel:test
258+ ```
259+
260+ 3 . ** Check the output for specific guidance on missing configuration**
261+
262+ ### Common Issues
263+
264+ - ** Traces not appearing** : Ensure ` OTEL_TRACES_EXPORTER ` is set to ` console ` or ` otlp `
265+ - ** Performance concerns** : Set ` OTEL_ENABLED=false ` in non-production environments if needed
266+ - ** Too many traces** : Use ` OTEL_IGNORE_PATHS ` to filter out unwanted requests
267+
268+ ## Advanced Features
269+
270+ ### Custom Watchers
271+
272+ Create custom watchers to trace specific events:
273+
274+ ``` php
275+ use Overtrue\LaravelOpenTelemetry\Watchers\Watcher;
276+ use Illuminate\Contracts\Foundation\Application;
277+
278+ class CustomWatcher implements Watcher
279+ {
280+ public function register(Application $app): void
281+ {
282+ // Register your event listeners
283+ }
284+ }
115285```
116286
117- it will create a span with the name ` test-event ` and the attributes ` key1 ` and ` key2 ` .
287+ Then add it to ` config/otel.php ` :
288+
289+ ``` php
290+ 'watchers' => [
291+ // ... existing watchers
292+ App\Watchers\CustomWatcher::class,
293+ ],
294+ ```
295+
296+ ### Context Propagation
297+
298+ ``` php
299+ // Extract context from headers (useful for incoming requests)
300+ $context = Measure::extractContextFromPropagationHeaders($headers);
301+
302+ // Get propagation headers (useful for outgoing requests)
303+ $headers = Measure::propagationHeaders();
304+ ```
305+
306+ ## Requirements
307+
308+ - PHP 8.4+
309+ - Laravel 10.0+ | 11.0+
310+ - OpenTelemetry SDK
118311
119312## Contributing
120313
@@ -136,7 +329,6 @@ Many thanks to Jetbrains for kindly providing a license for me to work on this a
136329
137330[ ![ ] ( https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg )] ( https://www.jetbrains.com/?from=https://github.com/overtrue )
138331
139-
140332## License
141333
142- MIT
334+ MIT
0 commit comments