From 2a601c33797e70083c10582f5f4b21e316732f68 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Thu, 4 Apr 2024 00:03:06 +0000 Subject: [PATCH] Laravel: disable CLI Console kernel tracing by default (#249) * Laravel: tracing Console kernel in long-running CLI scripts is disabled by default but can be optionally enabled via `OTEL_PHP_TRACE_CLI_ENABLED`. --- .../Laravel/src/ConsoleInstrumentation.php | 18 ++++++++++++++++++ .../Integration/ConsoleInstrumentationTest.php | 17 +++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/Instrumentation/Laravel/src/ConsoleInstrumentation.php b/src/Instrumentation/Laravel/src/ConsoleInstrumentation.php index 05fa0688..94362de0 100644 --- a/src/Instrumentation/Laravel/src/ConsoleInstrumentation.php +++ b/src/Instrumentation/Laravel/src/ConsoleInstrumentation.php @@ -12,12 +12,27 @@ use OpenTelemetry\API\Trace\StatusCode; use OpenTelemetry\Context\Context; use function OpenTelemetry\Instrumentation\hook; +use OpenTelemetry\SDK\Common\Configuration\Configuration; use OpenTelemetry\SemConv\TraceAttributes; use Throwable; class ConsoleInstrumentation { public static function register(CachedInstrumentation $instrumentation): void + { + if (self::shouldTraceCli()) { + self::hookConsoleKernel($instrumentation); + } + + self::hookCommandExecution($instrumentation); + } + + private static function shouldTraceCli(): bool + { + return PHP_SAPI !== 'cli' || Configuration::getBoolean('OTEL_PHP_TRACE_CLI_ENABLED', false); + } + + private static function hookConsoleKernel(CachedInstrumentation $instrumentation): void { hook( Kernel::class, @@ -58,7 +73,10 @@ public static function register(CachedInstrumentation $instrumentation): void $span->end(); } ); + } + private static function hookCommandExecution(CachedInstrumentation $instrumentation): void + { hook( Command::class, 'execute', diff --git a/src/Instrumentation/Laravel/tests/Integration/ConsoleInstrumentationTest.php b/src/Instrumentation/Laravel/tests/Integration/ConsoleInstrumentationTest.php index 4abc6d50..16a7f984 100644 --- a/src/Instrumentation/Laravel/tests/Integration/ConsoleInstrumentationTest.php +++ b/src/Instrumentation/Laravel/tests/Integration/ConsoleInstrumentationTest.php @@ -27,14 +27,15 @@ public function test_command_tracing(): void * * @see \Illuminate\Foundation\Console\OptimizeClearCommand::handle() for the additional commands/spans. */ - $count = 8; - $this->assertCount($count, $this->storage); - - $span = $this->storage[--$count]; - $this->assertSame('Artisan handler', $span->getName()); - - $span = $this->storage[--$count]; - $this->assertSame('Command optimize:clear', $span->getName()); + $this->assertCount(7, $this->storage); + + $this->assertSame('Command event:clear', $this->storage[0]->getName()); + $this->assertSame('Command view:clear', $this->storage[1]->getName()); + $this->assertSame('Command cache:clear', $this->storage[2]->getName()); + $this->assertSame('Command route:clear', $this->storage[3]->getName()); + $this->assertSame('Command config:clear', $this->storage[4]->getName()); + $this->assertSame('Command clear-compiled', $this->storage[5]->getName()); + $this->assertSame('Command optimize:clear', $this->storage[6]->getName()); } private function kernel(): Kernel