Skip to content

Commit

Permalink
Laravel: ConsoleInstrumentation updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisLightfootWild committed Sep 3, 2023
1 parent bc4c216 commit 2c18066
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
23 changes: 13 additions & 10 deletions src/Instrumentation/Laravel/src/ConsoleInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand All @@ -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,
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 2c18066

Please sign in to comment.