Skip to content

Commit b2103d4

Browse files
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`.
1 parent 83f0504 commit b2103d4

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/ConsoleInstrumentation.php

+18
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,27 @@
1212
use OpenTelemetry\API\Trace\StatusCode;
1313
use OpenTelemetry\Context\Context;
1414
use function OpenTelemetry\Instrumentation\hook;
15+
use OpenTelemetry\SDK\Common\Configuration\Configuration;
1516
use OpenTelemetry\SemConv\TraceAttributes;
1617
use Throwable;
1718

1819
class 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',

tests/Integration/ConsoleInstrumentationTest.php

+9-8
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)