File tree Expand file tree Collapse file tree 2 files changed +27
-8
lines changed
Expand file tree Collapse file tree 2 files changed +27
-8
lines changed Original file line number Diff line number Diff line change 1212use OpenTelemetry \API \Trace \StatusCode ;
1313use OpenTelemetry \Context \Context ;
1414use function OpenTelemetry \Instrumentation \hook ;
15+ use OpenTelemetry \SDK \Common \Configuration \Configuration ;
1516use OpenTelemetry \SemConv \TraceAttributes ;
1617use Throwable ;
1718
1819class ConsoleInstrumentation
1920{
2021 public static function register (CachedInstrumentation $ instrumentation ): void
22+ {
23+ if (self ::shouldTraceCli ()) {
24+ self ::hookConsoleKernel ($ instrumentation );
25+ }
26+
27+ self ::hookCommandExecution ($ instrumentation );
28+ }
29+
30+ private static function shouldTraceCli (): bool
31+ {
32+ return PHP_SAPI !== 'cli ' || Configuration::getBoolean ('OTEL_PHP_TRACE_CLI_ENABLED ' , false );
33+ }
34+
35+ private static function hookConsoleKernel (CachedInstrumentation $ instrumentation ): void
2136 {
2237 hook (
2338 Kernel::class,
@@ -58,7 +73,10 @@ public static function register(CachedInstrumentation $instrumentation): void
5873 $ span ->end ();
5974 }
6075 );
76+ }
6177
78+ private static function hookCommandExecution (CachedInstrumentation $ instrumentation ): void
79+ {
6280 hook (
6381 Command::class,
6482 'execute ' ,
Original file line number Diff line number Diff line change @@ -27,14 +27,15 @@ public function test_command_tracing(): void
2727 *
2828 * @see \Illuminate\Foundation\Console\OptimizeClearCommand::handle() for the additional commands/spans.
2929 */
30- $ count = 8 ;
31- $ this ->assertCount ($ count , $ this ->storage );
32-
33- $ span = $ this ->storage [--$ count ];
34- $ this ->assertSame ('Artisan handler ' , $ span ->getName ());
35-
36- $ span = $ this ->storage [--$ count ];
37- $ this ->assertSame ('Command optimize:clear ' , $ span ->getName ());
30+ $ this ->assertCount (7 , $ this ->storage );
31+
32+ $ this ->assertSame ('Command event:clear ' , $ this ->storage [0 ]->getName ());
33+ $ this ->assertSame ('Command view:clear ' , $ this ->storage [1 ]->getName ());
34+ $ this ->assertSame ('Command cache:clear ' , $ this ->storage [2 ]->getName ());
35+ $ this ->assertSame ('Command route:clear ' , $ this ->storage [3 ]->getName ());
36+ $ this ->assertSame ('Command config:clear ' , $ this ->storage [4 ]->getName ());
37+ $ this ->assertSame ('Command clear-compiled ' , $ this ->storage [5 ]->getName ());
38+ $ this ->assertSame ('Command optimize:clear ' , $ this ->storage [6 ]->getName ());
3839 }
3940
4041 private function kernel (): Kernel
You can’t perform that action at this time.
0 commit comments