-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCommand.php
63 lines (53 loc) · 2.21 KB
/
Command.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
declare(strict_types=1);
namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Console;
use Illuminate\Console\Command as IlluminateCommand;
use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHook;
use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHookTrait;
use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait;
use function OpenTelemetry\Instrumentation\hook;
use OpenTelemetry\SemConv\TraceAttributes;
use Throwable;
class Command implements LaravelHook
{
use LaravelHookTrait;
use PostHookTrait;
public function instrument(): void
{
$this->hookExecute();
}
protected function hookExecute(): bool
{
return hook(
IlluminateCommand::class,
'execute',
pre: function (IlluminateCommand $command, array $params, string $class, string $function, ?string $filename, ?int $lineno) {
/** @psalm-suppress ArgumentTypeCoercion */
$builder = $this->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;
},
post: function (IlluminateCommand $command, array $params, ?int $exitCode, ?Throwable $exception) {
$scope = Context::storage()->scope();
if (!$scope) {
return;
}
$span = Span::fromContext($scope->context());
$span->addEvent('command finished', [
'exit-code' => $exitCode,
]);
$this->endSpan($exception);
}
);
}
}