diff --git a/src/Instrumentation/Laravel/src/ConsoleInstrumentation.php b/src/Instrumentation/Laravel/src/ConsoleInstrumentation.php index 975bb834..ce52d194 100644 --- a/src/Instrumentation/Laravel/src/ConsoleInstrumentation.php +++ b/src/Instrumentation/Laravel/src/ConsoleInstrumentation.php @@ -60,15 +60,18 @@ public static function register(CachedInstrumentation $instrumentation): void hook( Command::class, 'execute', - pre: static function (Command $command, array $params, string $class, string $function, ?string $filename, ?int $lineno) { - $scope = Context::storage()->scope(); - if (!$scope) { - return $params; - } - $span = Span::fromContext($scope->context()); - $span->addEvent('command starting', [ - 'command' => $command->getName(), - ]); + pre: static function (Command $command, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($instrumentation) { + /** @psalm-suppress ArgumentTypeCoercion */ + $builder = $instrumentation->tracer() + ->spanBuilder(sprintf('Command %s', $command->getName() ?: 'unknown')) + ->setAttribute(TraceAttributes::CODE_FUNCTION, $function) + ->setAttribute(TraceAttributes::CODE_NAMESPACE, $class) + ->setAttribute(TraceAttributes::CODE_FILEPATH, $filename) + ->setAttribute(TraceAttributes::CODE_LINENO, $lineno); + $parent = Context::getCurrent(); + + $span = $builder->startSpan(); + Context::storage()->attach($span->storeInContext($parent)); return $params; }, @@ -77,9 +80,9 @@ public static function register(CachedInstrumentation $instrumentation): void if (!$scope) { return; } + $scope->detach(); $span = Span::fromContext($scope->context()); $span->addEvent('command finished', [ - 'command' => $command->getName(), 'exit-code' => $exitCode, ]); diff --git a/src/Instrumentation/Laravel/tests/Integration/ConsoleInstrumentationTest.php b/src/Instrumentation/Laravel/tests/Integration/ConsoleInstrumentationTest.php index 7ff52e47..5a6bef1b 100644 --- a/src/Instrumentation/Laravel/tests/Integration/ConsoleInstrumentationTest.php +++ b/src/Instrumentation/Laravel/tests/Integration/ConsoleInstrumentationTest.php @@ -54,15 +54,12 @@ public function test_command_tracing(): void ); $this->assertEquals(Command::SUCCESS, $exitCode); - $this->assertCount(1, $this->storage); + $this->assertCount(8, $this->storage); /** @var ImmutableSpan $span */ $span = $this->storage->offsetGet(0); $this->assertSame('Artisan handler', $span->getName()); - $this->assertCount(14, $span->getEvents()); - $event = $span->getEvents()[0]; - $this->assertSame([ - 'command' => 'optimize:clear', - ], $event->getAttributes()->toArray()); + $span = $this->storage->offsetGet(1); + $this->assertSame('Command optimize:clear', $span->getName()); } }