From 4c7f2c91337b756f6c3a3ae44892524480bebed1 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Sat, 8 Jun 2024 14:04:35 +0100 Subject: [PATCH 01/40] Laravel: removed older hook/register. --- src/Instrumentation/Laravel/_register.php | 18 ----------- .../Laravel/src/Hooks/LaravelHook.php | 14 --------- .../Laravel/src/Hooks/LaravelHookTrait.php | 31 ------------------- 3 files changed, 63 deletions(-) delete mode 100644 src/Instrumentation/Laravel/_register.php delete mode 100644 src/Instrumentation/Laravel/src/Hooks/LaravelHook.php delete mode 100644 src/Instrumentation/Laravel/src/Hooks/LaravelHookTrait.php diff --git a/src/Instrumentation/Laravel/_register.php b/src/Instrumentation/Laravel/_register.php deleted file mode 100644 index c265131fb..000000000 --- a/src/Instrumentation/Laravel/_register.php +++ /dev/null @@ -1,18 +0,0 @@ -instrument(); - } - - return self::$instance; - } -} From e2e039a3081a8a6bfdf739731b53bed94cb14db2 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Sat, 8 Jun 2024 14:05:10 +0100 Subject: [PATCH 02/40] Laravel: add new Hook interface for SPI. --- .../Laravel/src/Hooks/Hook.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/Instrumentation/Laravel/src/Hooks/Hook.php diff --git a/src/Instrumentation/Laravel/src/Hooks/Hook.php b/src/Instrumentation/Laravel/src/Hooks/Hook.php new file mode 100644 index 000000000..a1fe717bb --- /dev/null +++ b/src/Instrumentation/Laravel/src/Hooks/Hook.php @@ -0,0 +1,22 @@ + Date: Sat, 8 Jun 2024 14:09:40 +0100 Subject: [PATCH 03/40] Laravel: load Laravel instruments via SPI. --- .../Laravel/src/LaravelInstrumentation.php | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/src/Instrumentation/Laravel/src/LaravelInstrumentation.php b/src/Instrumentation/Laravel/src/LaravelInstrumentation.php index 799577f3e..6909e348d 100644 --- a/src/Instrumentation/Laravel/src/LaravelInstrumentation.php +++ b/src/Instrumentation/Laravel/src/LaravelInstrumentation.php @@ -4,33 +4,38 @@ namespace OpenTelemetry\Contrib\Instrumentation\Laravel; -use OpenTelemetry\API\Instrumentation\CachedInstrumentation; -use OpenTelemetry\SDK\Common\Configuration\Configuration; +use Nevay\SPI\ServiceLoader; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\ConfigurationRegistry; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Instrumentation; +use OpenTelemetry\API\Instrumentation\ConfigurationResolver; +use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; -class LaravelInstrumentation +class LaravelInstrumentation implements Instrumentation { - public const NAME = 'laravel'; + public const INSTRUMENTATION_NAME = 'io.opentelemetry.contrib.php.laravel'; - public static function register(): void + public function register(HookManager $hookManager, ConfigurationRegistry $configuration, Context $context): void { - $instrumentation = new CachedInstrumentation('io.opentelemetry.contrib.php.laravel'); - - Hooks\Illuminate\Console\Command::hook($instrumentation); - Hooks\Illuminate\Contracts\Console\Kernel::hook($instrumentation); - Hooks\Illuminate\Contracts\Http\Kernel::hook($instrumentation); - Hooks\Illuminate\Contracts\Queue\Queue::hook($instrumentation); - Hooks\Illuminate\Foundation\Application::hook($instrumentation); - Hooks\Illuminate\Foundation\Console\ServeCommand::hook($instrumentation); - Hooks\Illuminate\Queue\SyncQueue::hook($instrumentation); - Hooks\Illuminate\Queue\Queue::hook($instrumentation); - Hooks\Illuminate\Queue\Worker::hook($instrumentation); + $config = $configuration->get(LaravelConfiguration::class) ?? LaravelConfiguration::default(); + + if (! $config->enabled) { + return; + } + + $logger = $context->loggerProvider->getLogger(self::INSTRUMENTATION_NAME); + $meter = $context->meterProvider->getMeter(self::INSTRUMENTATION_NAME); + $tracer = $context->tracerProvider->getTracer(self::INSTRUMENTATION_NAME); + + foreach (ServiceLoader::load(Hook::class) as $hook) { + /** @var Hook $hook */ + $hook->instrument($hookManager, $config, $logger, $meter, $tracer); + } } public static function shouldTraceCli(): bool { - return PHP_SAPI !== 'cli' || ( - class_exists(Configuration::class) - && Configuration::getBoolean('OTEL_PHP_TRACE_CLI_ENABLED', false) - ); + return PHP_SAPI !== 'cli' || (new ConfigurationResolver())->getBoolean('OTEL_PHP_TRACE_CLI_ENABLED'); } } From 1deb2b4e9a2c11262cae98d74858cf5540362d21 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Sat, 8 Jun 2024 14:12:41 +0100 Subject: [PATCH 04/40] Laravel: watchers accept TracerInterface. --- .../Laravel/src/Watchers/ClientRequestWatcher.php | 6 +++--- src/Instrumentation/Laravel/src/Watchers/QueryWatcher.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Instrumentation/Laravel/src/Watchers/ClientRequestWatcher.php b/src/Instrumentation/Laravel/src/Watchers/ClientRequestWatcher.php index 855c9f57a..11317b54a 100644 --- a/src/Instrumentation/Laravel/src/Watchers/ClientRequestWatcher.php +++ b/src/Instrumentation/Laravel/src/Watchers/ClientRequestWatcher.php @@ -10,10 +10,10 @@ use Illuminate\Http\Client\Events\ResponseReceived; use Illuminate\Http\Client\Request; use Illuminate\Http\Client\Response; -use OpenTelemetry\API\Instrumentation\CachedInstrumentation; use OpenTelemetry\API\Trace\SpanInterface; use OpenTelemetry\API\Trace\SpanKind; use OpenTelemetry\API\Trace\StatusCode; +use OpenTelemetry\API\Trace\TracerInterface; use OpenTelemetry\SemConv\TraceAttributes; use Symfony\Component\HttpFoundation\Response as HttpResponse; @@ -25,7 +25,7 @@ class ClientRequestWatcher extends Watcher protected array $spans = []; public function __construct( - private CachedInstrumentation $instrumentation, + private readonly TracerInterface $tracer, ) { } @@ -52,7 +52,7 @@ public function recordRequest(RequestSending $request): void if ($parsedUrl->has('query')) { $processedUrl .= '?' . $parsedUrl->get('query'); } - $span = $this->instrumentation->tracer()->spanBuilder($request->request->method()) + $span = $this->tracer->spanBuilder($request->request->method()) ->setSpanKind(SpanKind::KIND_CLIENT) ->setAttributes([ TraceAttributes::HTTP_REQUEST_METHOD => $request->request->method(), diff --git a/src/Instrumentation/Laravel/src/Watchers/QueryWatcher.php b/src/Instrumentation/Laravel/src/Watchers/QueryWatcher.php index ff1c82d50..17ae6bb3d 100644 --- a/src/Instrumentation/Laravel/src/Watchers/QueryWatcher.php +++ b/src/Instrumentation/Laravel/src/Watchers/QueryWatcher.php @@ -7,14 +7,14 @@ use Illuminate\Contracts\Foundation\Application; use Illuminate\Database\Events\QueryExecuted; use Illuminate\Support\Str; -use OpenTelemetry\API\Instrumentation\CachedInstrumentation; use OpenTelemetry\API\Trace\SpanKind; +use OpenTelemetry\API\Trace\TracerInterface; use OpenTelemetry\SemConv\TraceAttributes; class QueryWatcher extends Watcher { public function __construct( - private CachedInstrumentation $instrumentation, + private readonly TracerInterface $tracer, ) { } @@ -38,7 +38,7 @@ public function recordQuery(QueryExecuted $query): void $operationName = null; } /** @psalm-suppress ArgumentTypeCoercion */ - $span = $this->instrumentation->tracer()->spanBuilder('sql ' . $operationName) + $span = $this->tracer->spanBuilder('sql ' . $operationName) ->setSpanKind(SpanKind::KIND_CLIENT) ->setStartTimestamp($this->calculateQueryStartTime($nowInNs, $query->time)) ->startSpan(); From 98af07f917b4306686bb1844c79f2455d89efcbc Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Sun, 9 Jun 2024 19:09:27 +0100 Subject: [PATCH 05/40] Laravel: hook Foundation\Application. --- .../Illuminate/Foundation/Application.php | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Application.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Application.php index f76b2986b..9a8c0ee16 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Application.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Application.php @@ -6,32 +6,38 @@ use Illuminate\Contracts\Foundation\Application as ApplicationContract; use Illuminate\Foundation\Application as FoundationalApplication; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHook; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHookTrait; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Logs\LoggerInterface; +use OpenTelemetry\API\Metrics\MeterInterface; +use OpenTelemetry\API\Trace\TracerInterface; +use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\CacheWatcher; use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\ClientRequestWatcher; use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\ExceptionWatcher; use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\LogWatcher; use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\QueryWatcher; use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\Watcher; -use function OpenTelemetry\Instrumentation\hook; use Throwable; -class Application implements LaravelHook +class Application implements Hook { - use LaravelHookTrait; - - public function instrument(): void - { - hook( + public function instrument( + HookManager $hookManager, + LaravelConfiguration $configuration, + LoggerInterface $logger, + MeterInterface $meter, + TracerInterface $tracer, + ): void { + $hookManager->hook( FoundationalApplication::class, '__construct', - post: function (FoundationalApplication $application, array $params, mixed $returnValue, ?Throwable $exception) { + postHook: function (FoundationalApplication $application, array $params, mixed $returnValue, ?Throwable $exception) use ($tracer) { $this->registerWatchers($application, new CacheWatcher()); - $this->registerWatchers($application, new ClientRequestWatcher($this->instrumentation)); + $this->registerWatchers($application, new ClientRequestWatcher($tracer)); $this->registerWatchers($application, new ExceptionWatcher()); $this->registerWatchers($application, new LogWatcher()); - $this->registerWatchers($application, new QueryWatcher($this->instrumentation)); + $this->registerWatchers($application, new QueryWatcher($tracer)); }, ); } From 1ba04c2e0d551d3f8f8e042fe9a999ef3a28f297 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Sun, 9 Jun 2024 19:11:25 +0100 Subject: [PATCH 06/40] Laravel: convert more hooks. --- .../src/Hooks/Illuminate/Console/Command.php | 34 ++++++----- .../Illuminate/Contracts/Console/Kernel.php | 34 ++++++----- .../Illuminate/Contracts/Http/Kernel.php | 34 ++++++----- .../Illuminate/Contracts/Queue/Queue.php | 60 ++++++++++--------- .../Foundation/Console/ServeCommand.php | 26 ++++---- .../src/Hooks/Illuminate/Queue/Queue.php | 30 +++++----- .../src/Hooks/Illuminate/Queue/SyncQueue.php | 34 ++++++----- .../src/Hooks/Illuminate/Queue/Worker.php | 47 ++++++++------- 8 files changed, 170 insertions(+), 129 deletions(-) diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php index d45153904..46c54b39d 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php @@ -5,34 +5,40 @@ namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Console; use Illuminate\Console\Command as IlluminateCommand; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Logs\LoggerInterface; +use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\Span; +use OpenTelemetry\API\Trace\TracerInterface; use OpenTelemetry\Context\Context; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHook; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHookTrait; +use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; -use function OpenTelemetry\Instrumentation\hook; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; use OpenTelemetry\SemConv\TraceAttributes; use Throwable; -class Command implements LaravelHook +class Command implements Hook { - use LaravelHookTrait; use PostHookTrait; - public function instrument(): void - { - $this->hookExecute(); + public function instrument( + HookManager $hookManager, + LaravelConfiguration $configuration, + LoggerInterface $logger, + MeterInterface $meter, + TracerInterface $tracer, + ): void { + $this->hookExecute($hookManager, $tracer); } - protected function hookExecute(): bool + protected function hookExecute(HookManager $hookManager, TracerInterface $tracer): void { - return hook( + $hookManager->hook( IlluminateCommand::class, 'execute', - pre: function (IlluminateCommand $command, array $params, string $class, string $function, ?string $filename, ?int $lineno) { + preHook: function (IlluminateCommand $command, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { /** @psalm-suppress ArgumentTypeCoercion */ - $builder = $this->instrumentation - ->tracer() + $builder = $tracer ->spanBuilder(sprintf('Command %s', $command->getName() ?: 'unknown')) ->setAttribute(TraceAttributes::CODE_FUNCTION, $function) ->setAttribute(TraceAttributes::CODE_NAMESPACE, $class) @@ -45,7 +51,7 @@ protected function hookExecute(): bool return $params; }, - post: function (IlluminateCommand $command, array $params, ?int $exitCode, ?Throwable $exception) { + postHook: function (IlluminateCommand $command, array $params, ?int $exitCode, ?Throwable $exception) { $scope = Context::storage()->scope(); if (!$scope) { return; diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php index edf961f91..7e079518e 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php @@ -6,41 +6,47 @@ use Illuminate\Console\Command; use Illuminate\Contracts\Console\Kernel as KernelContract; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Logs\LoggerInterface; +use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\Span; use OpenTelemetry\API\Trace\SpanKind; use OpenTelemetry\API\Trace\StatusCode; +use OpenTelemetry\API\Trace\TracerInterface; use OpenTelemetry\Context\Context; +use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Queue\AttributesBuilder; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHook; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHookTrait; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; -use function OpenTelemetry\Instrumentation\hook; use OpenTelemetry\SemConv\TraceAttributes; use Throwable; -class Kernel implements LaravelHook +class Kernel implements Hook { use AttributesBuilder; - use LaravelHookTrait; use PostHookTrait; - public function instrument(): void - { + public function instrument( + HookManager $hookManager, + LaravelConfiguration $configuration, + LoggerInterface $logger, + MeterInterface $meter, + TracerInterface $tracer, + ): void { if (LaravelInstrumentation::shouldTraceCli()) { - $this->hookHandle(); + $this->hookHandle($hookManager, $tracer); } } - private function hookHandle(): bool + private function hookHandle(HookManager $hookManager, TracerInterface $tracer): void { - return hook( + $hookManager->hook( KernelContract::class, 'handle', - pre: function (KernelContract $kernel, array $params, string $class, string $function, ?string $filename, ?int $lineno) { + preHook: function (KernelContract $kernel, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { /** @psalm-suppress ArgumentTypeCoercion */ - $builder = $this->instrumentation - ->tracer() + $builder = $tracer ->spanBuilder('Artisan handler') ->setSpanKind(SpanKind::KIND_PRODUCER) ->setAttribute(TraceAttributes::CODE_FUNCTION, $function) @@ -54,7 +60,7 @@ private function hookHandle(): bool return $params; }, - post: function (KernelContract $kernel, array $params, ?int $exitCode, ?Throwable $exception) { + postHook: function (KernelContract $kernel, array $params, ?int $exitCode, ?Throwable $exception) { $scope = Context::storage()->scope(); if (!$scope) { return; diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php index e9e353584..6e6dd8b8c 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php @@ -8,41 +8,47 @@ use Illuminate\Http\Request; use Illuminate\Routing\Route; use OpenTelemetry\API\Globals; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Logs\LoggerInterface; +use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\Span; use OpenTelemetry\API\Trace\SpanInterface; use OpenTelemetry\API\Trace\SpanKind; use OpenTelemetry\API\Trace\StatusCode; +use OpenTelemetry\API\Trace\TracerInterface; use OpenTelemetry\Context\Context; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHook; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHookTrait; +use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; use OpenTelemetry\Contrib\Instrumentation\Laravel\Propagators\HeadersPropagator; use OpenTelemetry\Contrib\Instrumentation\Laravel\Propagators\ResponsePropagationSetter; -use function OpenTelemetry\Instrumentation\hook; use OpenTelemetry\SemConv\TraceAttributes; use Symfony\Component\HttpFoundation\Response; use Throwable; -class Kernel implements LaravelHook +class Kernel implements Hook { - use LaravelHookTrait; use PostHookTrait; - public function instrument(): void - { - $this->hookHandle(); + public function instrument( + HookManager $hookManager, + LaravelConfiguration $configuration, + LoggerInterface $logger, + MeterInterface $meter, + TracerInterface $tracer, + ): void { + $this->hookHandle($hookManager, $tracer); } - protected function hookHandle(): bool + protected function hookHandle(HookManager $hookManager, TracerInterface $tracer): void { - return hook( + $hookManager->hook( KernelContract::class, 'handle', - pre: function (KernelContract $kernel, array $params, string $class, string $function, ?string $filename, ?int $lineno) { + preHook: function (KernelContract $kernel, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { $request = ($params[0] instanceof Request) ? $params[0] : null; /** @psalm-suppress ArgumentTypeCoercion */ - $builder = $this->instrumentation - ->tracer() + $builder = $tracer ->spanBuilder(sprintf('%s', $request?->method() ?? 'unknown')) ->setSpanKind(SpanKind::KIND_SERVER) ->setAttribute(TraceAttributes::CODE_FUNCTION, $function) @@ -75,7 +81,7 @@ protected function hookHandle(): bool return [$request]; }, - post: function (KernelContract $kernel, array $params, ?Response $response, ?Throwable $exception) { + postHook: function (KernelContract $kernel, array $params, ?Response $response, ?Throwable $exception) { $scope = Context::storage()->scope(); if (!$scope) { return; diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php index c58800db9..ac081397d 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php @@ -7,36 +7,43 @@ use DateInterval; use DateTimeInterface; use Illuminate\Contracts\Queue\Queue as QueueContract; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Logs\LoggerInterface; +use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\SpanKind; +use OpenTelemetry\API\Trace\TracerInterface; use OpenTelemetry\Context\Context; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Queue\AttributesBuilder; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHook; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHookTrait; +use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; -use function OpenTelemetry\Instrumentation\hook; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; use OpenTelemetry\SemConv\TraceAttributes; use OpenTelemetry\SemConv\TraceAttributeValues; use Throwable; -class Queue implements LaravelHook +class Queue implements Hook { use AttributesBuilder; - use LaravelHookTrait; use PostHookTrait; - public function instrument(): void - { - $this->hookBulk(); - $this->hookLater(); - $this->hookPushRaw(); + public function instrument( + HookManager $hookManager, + LaravelConfiguration $configuration, + LoggerInterface $logger, + MeterInterface $meter, + TracerInterface $tracer, + ): void { + $this->hookBulk($hookManager, $tracer); + $this->hookLater($hookManager, $tracer); + $this->hookPushRaw($hookManager, $tracer); } - protected function hookBulk(): bool + protected function hookBulk(HookManager $hookManager, TracerInterface $tracer): void { - return hook( + $hookManager->hook( QueueContract::class, 'bulk', - pre: function (QueueContract $queue, array $params, string $class, string $function, ?string $filename, ?int $lineno) { + preHook: function (QueueContract $queue, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { $attributes = array_merge([ TraceAttributes::CODE_FUNCTION => $function, TraceAttributes::CODE_NAMESPACE => $class, @@ -46,8 +53,7 @@ protected function hookBulk(): bool ], $this->contextualMessageSystemAttributes($queue, [])); /** @psalm-suppress ArgumentTypeCoercion */ - $span = $this->instrumentation - ->tracer() + $span = $tracer ->spanBuilder(vsprintf('%s %s', [ /** @phan-suppress-next-line PhanUndeclaredMethod */ method_exists($queue, 'getQueue') ? $queue->getQueue($params[2] ?? null) : $queue->getConnectionName(), @@ -61,18 +67,18 @@ protected function hookBulk(): bool return $params; }, - post: function (QueueContract $queue, array $params, $returnValue, ?Throwable $exception) { + postHook: function (QueueContract $queue, array $params, $returnValue, ?Throwable $exception) { $this->endSpan($exception); }, ); } - protected function hookLater(): bool + protected function hookLater(HookManager $hookManager, TracerInterface $tracer): void { - return hook( + $hookManager->hook( QueueContract::class, 'later', - pre: function (QueueContract $queue, array $params, string $class, string $function, ?string $filename, ?int $lineno) { + preHook: function (QueueContract $queue, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { $estimateDeliveryTimestamp = match (true) { is_int($params[0]) => (new \DateTimeImmutable())->add(new DateInterval("PT{$params[0]}S"))->getTimestamp(), $params[0] instanceof DateInterval => (new \DateTimeImmutable())->add($params[0])->getTimestamp(), @@ -89,8 +95,7 @@ protected function hookLater(): bool ]; /** @psalm-suppress ArgumentTypeCoercion */ - $span = $this->instrumentation - ->tracer() + $span = $tracer ->spanBuilder(vsprintf('%s %s', [ /** @phan-suppress-next-line PhanUndeclaredMethod */ method_exists($queue, 'getQueue') ? $queue->getQueue($params[2] ?? null) : $queue->getConnectionName(), @@ -104,25 +109,24 @@ protected function hookLater(): bool return $params; }, - post: function (QueueContract $queue, array $params, $returnValue, ?Throwable $exception) { + postHook: function (QueueContract $queue, array $params, $returnValue, ?Throwable $exception) { $this->endSpan($exception); }, ); } - protected function hookPushRaw(): bool + protected function hookPushRaw(HookManager $hookManager, TracerInterface $tracer): void { - return hook( + $hookManager->hook( QueueContract::class, 'pushRaw', - pre: function (QueueContract $queue, array $params, string $class, string $function, ?string $filename, ?int $lineno) { + preHook: function (QueueContract $queue, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { /** @phan-suppress-next-line PhanParamTooFewUnpack */ $attributes = $this->buildMessageAttributes($queue, ...$params); $parent = Context::getCurrent(); /** @psalm-suppress ArgumentTypeCoercion */ - $span = $this->instrumentation - ->tracer() + $span = $tracer ->spanBuilder(vsprintf('%s %s', [ $attributes[TraceAttributes::MESSAGING_DESTINATION_NAME], TraceAttributeValues::MESSAGING_OPERATION_CREATE, @@ -135,7 +139,7 @@ protected function hookPushRaw(): bool return $params; }, - post: function (QueueContract $queue, array $params, $returnValue, ?Throwable $exception) { + postHook: function (QueueContract $queue, array $params, $returnValue, ?Throwable $exception) { $this->endSpan($exception); }, ); diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php index 190113d85..c63fd3bf1 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php @@ -5,23 +5,29 @@ namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Foundation\Console; use Illuminate\Foundation\Console\ServeCommand as FoundationServeCommand; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHook; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHookTrait; -use function OpenTelemetry\Instrumentation\hook; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Logs\LoggerInterface; +use OpenTelemetry\API\Metrics\MeterInterface; +use OpenTelemetry\API\Trace\TracerInterface; +use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; /** * Instrument Laravel's local PHP development server. */ -class ServeCommand implements LaravelHook +class ServeCommand implements Hook { - use LaravelHookTrait; - - public function instrument(): void - { - hook( + public function instrument( + HookManager $hookManager, + LaravelConfiguration $configuration, + LoggerInterface $logger, + MeterInterface $meter, + TracerInterface $tracer, + ): void { + $hookManager->hook( FoundationServeCommand::class, 'handle', - pre: static function (FoundationServeCommand $serveCommand, array $params, string $class, string $function, ?string $filename, ?int $lineno) { + preHook: static function (FoundationServeCommand $serveCommand, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { if (!property_exists(FoundationServeCommand::class, 'passthroughVariables')) { return; } diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Queue.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Queue.php index b1a0c7aa3..86c84e9dc 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Queue.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Queue.php @@ -5,28 +5,30 @@ namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Queue; use Illuminate\Queue\Queue as AbstractQueue; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Logs\LoggerInterface; +use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHook; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHookTrait; -use function OpenTelemetry\Instrumentation\hook; +use OpenTelemetry\API\Trace\TracerInterface; +use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; use Throwable; -class Queue implements LaravelHook +class Queue implements Hook { use AttributesBuilder; - use LaravelHookTrait; - public function instrument(): void - { - $this->hookAbstractQueueCreatePayloadArray(); - } - - protected function hookAbstractQueueCreatePayloadArray(): bool - { - return hook( + public function instrument( + HookManager $hookManager, + LaravelConfiguration $configuration, + LoggerInterface $logger, + MeterInterface $meter, + TracerInterface $tracer, + ): void { + $hookManager->hook( AbstractQueue::class, 'createPayloadArray', - post: function (AbstractQueue $queue, array $params, array $payload, ?Throwable $exception): array { + postHook: function (AbstractQueue $queue, array $params, array $payload, ?Throwable $exception): array { TraceContextPropagator::getInstance()->inject($payload); return $payload; diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php index 19d85cc2e..2906a6fef 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php @@ -5,35 +5,41 @@ namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Queue; use Illuminate\Queue\SyncQueue as LaravelSyncQueue; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Logs\LoggerInterface; +use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\SpanKind; +use OpenTelemetry\API\Trace\TracerInterface; use OpenTelemetry\Context\Context; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHook; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHookTrait; +use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; -use function OpenTelemetry\Instrumentation\hook; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; use OpenTelemetry\SemConv\TraceAttributes; use Throwable; -class SyncQueue implements LaravelHook +class SyncQueue implements Hook { use AttributesBuilder; - use LaravelHookTrait; use PostHookTrait; - public function instrument(): void - { - $this->hookPush(); + public function instrument( + HookManager $hookManager, + LaravelConfiguration $configuration, + LoggerInterface $logger, + MeterInterface $meter, + TracerInterface $tracer, + ): void { + $this->hookPush($hookManager, $tracer); } - protected function hookPush(): bool + protected function hookPush(HookManager $hookManager, TracerInterface $tracer): void { - return hook( + $hookManager->hook( LaravelSyncQueue::class, 'push', - pre: function (LaravelSyncQueue $queue, array $params, string $class, string $function, ?string $filename, ?int $lineno) { + preHook: function (LaravelSyncQueue $queue, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { /** @psalm-suppress ArgumentTypeCoercion */ - $span = $this->instrumentation - ->tracer() + $span = $tracer ->spanBuilder(vsprintf('%s %s', [ $queue->getConnectionName(), 'process', @@ -49,7 +55,7 @@ protected function hookPush(): bool Context::storage()->attach($span->storeInContext(Context::getCurrent())); }, - post: function (LaravelSyncQueue $queue, array $params, mixed $returnValue, ?Throwable $exception) { + postHook: function (LaravelSyncQueue $queue, array $params, mixed $returnValue, ?Throwable $exception) { $this->endSpan($exception); }, ); diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php index 0e295a690..e7db53051 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php @@ -6,36 +6,43 @@ use Illuminate\Contracts\Queue\Job; use Illuminate\Queue\Worker as QueueWorker; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Logs\LoggerInterface; +use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator; use OpenTelemetry\API\Trace\Span; use OpenTelemetry\API\Trace\SpanKind; +use OpenTelemetry\API\Trace\TracerInterface; use OpenTelemetry\Context\Context; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHook; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHookTrait; +use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; -use function OpenTelemetry\Instrumentation\hook; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; use OpenTelemetry\SemConv\TraceAttributes; use OpenTelemetry\SemConv\TraceAttributeValues; use Throwable; -class Worker implements LaravelHook +class Worker implements Hook { use AttributesBuilder; - use LaravelHookTrait; use PostHookTrait; - public function instrument(): void - { - $this->hookWorkerProcess(); - $this->hookWorkerGetNextJob(); + public function instrument( + HookManager $hookManager, + LaravelConfiguration $configuration, + LoggerInterface $logger, + MeterInterface $meter, + TracerInterface $tracer, + ): void { + $this->hookWorkerProcess($hookManager, $tracer); + $this->hookWorkerGetNextJob($hookManager, $tracer); } - private function hookWorkerProcess(): bool + private function hookWorkerProcess(HookManager $hookManager, TracerInterface $tracer): void { - return hook( + $hookManager->hook( QueueWorker::class, 'process', - pre: function (QueueWorker $worker, array $params, string $class, string $function, ?string $filename, ?int $lineno) { + preHook: function (QueueWorker $worker, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { $connectionName = $params[0]; /** @var Job $job */ $job = $params[1]; @@ -48,8 +55,7 @@ private function hookWorkerProcess(): bool $attributes = $this->buildMessageAttributes($queue, $job->getRawBody(), $job->getQueue()); /** @psalm-suppress ArgumentTypeCoercion */ - $span = $this->instrumentation - ->tracer() + $span = $tracer ->spanBuilder(vsprintf('%s %s', [ $attributes[TraceAttributes::MESSAGING_DESTINATION_NAME], 'process', @@ -63,7 +69,7 @@ private function hookWorkerProcess(): bool return $params; }, - post: function (QueueWorker $worker, array $params, $returnValue, ?Throwable $exception) { + postHook: function (QueueWorker $worker, array $params, $returnValue, ?Throwable $exception) { $scope = Context::storage()->scope(); if (!$scope) { return; @@ -82,12 +88,12 @@ private function hookWorkerProcess(): bool ); } - private function hookWorkerGetNextJob(): bool + private function hookWorkerGetNextJob(HookManager $hookManager, TracerInterface $tracer): bool { - return hook( + $hookManager->hook( QueueWorker::class, 'getNextJob', - pre: function (QueueWorker $worker, array $params, string $class, string $function, ?string $filename, ?int $lineno) { + preHook: function (QueueWorker $worker, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { /** @var \Illuminate\Contracts\Queue\Queue $connection */ $connection = $params[0]; $queue = $params[1]; @@ -95,8 +101,7 @@ private function hookWorkerGetNextJob(): bool $attributes = $this->buildMessageAttributes($connection, '', $queue); /** @psalm-suppress ArgumentTypeCoercion */ - $span = $this->instrumentation - ->tracer() + $span = $tracer ->spanBuilder(vsprintf('%s %s', [ $attributes[TraceAttributes::MESSAGING_DESTINATION_NAME], TraceAttributeValues::MESSAGING_OPERATION_RECEIVE, @@ -109,7 +114,7 @@ private function hookWorkerGetNextJob(): bool return $params; }, - post: function (QueueWorker $worker, array $params, ?Job $job, ?Throwable $exception) { + postHook: function (QueueWorker $worker, array $params, ?Job $job, ?Throwable $exception) { $scope = Context::storage()->scope(); if (!$scope) { return; From f3428c55ff7e60000a2d84dc05e9b5b24b4f0d31 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Sun, 9 Jun 2024 19:23:45 +0100 Subject: [PATCH 07/40] Laravel: added LaravelComponentProvider and LaravelConfiguration. --- .../Laravel/src/LaravelComponentProvider.php | 29 +++++++++++++++++++ .../Laravel/src/LaravelConfiguration.php | 28 ++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/Instrumentation/Laravel/src/LaravelComponentProvider.php create mode 100644 src/Instrumentation/Laravel/src/LaravelConfiguration.php diff --git a/src/Instrumentation/Laravel/src/LaravelComponentProvider.php b/src/Instrumentation/Laravel/src/LaravelComponentProvider.php new file mode 100644 index 000000000..bd460ab91 --- /dev/null +++ b/src/Instrumentation/Laravel/src/LaravelComponentProvider.php @@ -0,0 +1,29 @@ +canBeDisabled() + ->addDefaultsIfNotSet() + ; + } +} diff --git a/src/Instrumentation/Laravel/src/LaravelConfiguration.php b/src/Instrumentation/Laravel/src/LaravelConfiguration.php new file mode 100644 index 000000000..7c8509973 --- /dev/null +++ b/src/Instrumentation/Laravel/src/LaravelConfiguration.php @@ -0,0 +1,28 @@ + Date: Mon, 10 Jun 2024 18:21:39 +0100 Subject: [PATCH 08/40] Laravel: PoC composer changes for SPI. --- src/Instrumentation/Laravel/composer.json | 46 ++++++++++++++++++----- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/src/Instrumentation/Laravel/composer.json b/src/Instrumentation/Laravel/composer.json index 65372b3b1..c39a4392e 100644 --- a/src/Instrumentation/Laravel/composer.json +++ b/src/Instrumentation/Laravel/composer.json @@ -9,18 +9,16 @@ "minimum-stability": "dev", "prefer-stable": true, "require": { - "php": "^8.0", + "php": "^8.1", "ext-json": "*", "ext-opentelemetry": "*", - "laravel/framework": "^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0", - "open-telemetry/api": "^1.0", - "open-telemetry/sem-conv": "^1.24" + "laravel/framework": "^10.0 || ^11.0", + "open-telemetry/opentelemetry": "dev-auto-instrumentation-registration" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.50", "guzzlehttp/guzzle": "*", "nunomaduro/collision": "*", - "open-telemetry/sdk": "^1.0", "orchestra/testbench": ">=7.41.3", "phan/phan": "^5.0", "php-http/mock-client": "*", @@ -34,21 +32,49 @@ "autoload": { "psr-4": { "OpenTelemetry\\Contrib\\Instrumentation\\Laravel\\": "src/" - }, - "files": [ - "_register.php" - ] + } }, "autoload-dev": { "psr-4": { "OpenTelemetry\\Tests\\Contrib\\Instrumentation\\Laravel\\": "tests/" } }, + "extra": { + "spi": { + "OpenTelemetry\\Config\\SDK\\Configuration\\ComponentProvider": [ + "OpenTelemetry\\Contrib\\Instrumentation\\Laravel\\LaravelComponentProvider" + ], + "OpenTelemetry\\API\\Instrumentation\\AutoInstrumentation\\Instrumentation": [ + "OpenTelemetry\\Contrib\\Instrumentation\\Laravel\\LaravelInstrumentation" + ], + "OpenTelemetry\\Contrib\\Instrumentation\\Laravel\\Hooks\\Hook": [ + "OpenTelemetry\\Contrib\\Instrumentation\\Laravel\\Hooks\\Illuminate\\Console\\Command", + + "OpenTelemetry\\Contrib\\Instrumentation\\Laravel\\Hooks\\Illuminate\\Contracts\\Console\\Kernel", + "OpenTelemetry\\Contrib\\Instrumentation\\Laravel\\Hooks\\Illuminate\\Contracts\\Http\\Kernel", + "OpenTelemetry\\Contrib\\Instrumentation\\Laravel\\Hooks\\Illuminate\\Contracts\\Queue\\Queue", + + "OpenTelemetry\\Contrib\\Instrumentation\\Laravel\\Hooks\\Illuminate\\Foundation\\Console\\ServeCommand", + "OpenTelemetry\\Contrib\\Instrumentation\\Laravel\\Hooks\\Illuminate\\Foundation\\Application", + + "OpenTelemetry\\Contrib\\Instrumentation\\Laravel\\Hooks\\Illuminate\\Queue\\Queue", + "OpenTelemetry\\Contrib\\Instrumentation\\Laravel\\Hooks\\Illuminate\\Queue\\SyncQueue", + "OpenTelemetry\\Contrib\\Instrumentation\\Laravel\\Hooks\\Illuminate\\Queue\\Worker" + ] + } + }, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/brettmc/opentelemetry-php" + } + ], "config": { "lock": false, "sort-packages": true, "allow-plugins": { - "php-http/discovery": false + "php-http/discovery": false, + "tbachert/spi": true } } } From c1ed471aea20d6c61286fda12908069549fab972 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Wed, 19 Jun 2024 13:48:05 +0100 Subject: [PATCH 09/40] Laravel: register with SPI ServiceLoader in case SPI plugin is not allowed. --- src/Instrumentation/Laravel/_register.php | 24 +++++++++++++++++++++++ src/Instrumentation/Laravel/composer.json | 5 ++++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/Instrumentation/Laravel/_register.php diff --git a/src/Instrumentation/Laravel/_register.php b/src/Instrumentation/Laravel/_register.php new file mode 100644 index 000000000..142839ef5 --- /dev/null +++ b/src/Instrumentation/Laravel/_register.php @@ -0,0 +1,24 @@ + Date: Sun, 30 Jun 2024 19:19:20 +0100 Subject: [PATCH 10/40] Laravel: enabled spi-config.autoload-files composer config for SPI plugin cleanup. --- src/Instrumentation/Laravel/_register.php | 5 ++++- src/Instrumentation/Laravel/composer.json | 20 +++----------------- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/src/Instrumentation/Laravel/_register.php b/src/Instrumentation/Laravel/_register.php index 142839ef5..1515731dd 100644 --- a/src/Instrumentation/Laravel/_register.php +++ b/src/Instrumentation/Laravel/_register.php @@ -1,4 +1,8 @@ Date: Mon, 1 Jul 2024 19:41:13 +0100 Subject: [PATCH 11/40] Laravel: fix return type. --- .../Laravel/src/Hooks/Illuminate/Queue/Worker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php index e7db53051..a375ed145 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php @@ -88,7 +88,7 @@ private function hookWorkerProcess(HookManager $hookManager, TracerInterface $tr ); } - private function hookWorkerGetNextJob(HookManager $hookManager, TracerInterface $tracer): bool + private function hookWorkerGetNextJob(HookManager $hookManager, TracerInterface $tracer): void { $hookManager->hook( QueueWorker::class, From d5cdeca3eb84d0caf18f7bc959c43a28b7a721a9 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Sun, 25 Aug 2024 20:51:39 +0100 Subject: [PATCH 12/40] Laravel: updates after AutoInstrumentation in API was merged to main. --- src/Instrumentation/Laravel/composer.json | 4 ++-- src/Instrumentation/Laravel/src/Hooks/Hook.php | 4 ++-- .../Laravel/src/Hooks/Illuminate/Console/Command.php | 6 +++--- .../Hooks/Illuminate/Contracts/Console/Kernel.php | 6 +++--- .../src/Hooks/Illuminate/Contracts/Http/Kernel.php | 6 +++--- .../src/Hooks/Illuminate/Contracts/Queue/Queue.php | 12 ++++++------ .../src/Hooks/Illuminate/Foundation/Application.php | 4 ++-- .../Illuminate/Foundation/Console/ServeCommand.php | 4 ++-- .../Laravel/src/Hooks/Illuminate/Queue/Queue.php | 4 ++-- .../Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php | 6 +++--- .../Laravel/src/Hooks/Illuminate/Queue/Worker.php | 8 ++++---- .../Laravel/src/LaravelConfiguration.php | 2 +- .../Laravel/src/LaravelInstrumentation.php | 4 ++-- 13 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/Instrumentation/Laravel/composer.json b/src/Instrumentation/Laravel/composer.json index 26a2d0931..20a875020 100644 --- a/src/Instrumentation/Laravel/composer.json +++ b/src/Instrumentation/Laravel/composer.json @@ -13,7 +13,7 @@ "ext-json": "*", "ext-opentelemetry": "*", "laravel/framework": "^10.0 || ^11.0", - "open-telemetry/opentelemetry": "dev-auto-instrumentation-registration" + "open-telemetry/opentelemetry": "dev-main as 1.x-dev" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.50", @@ -55,7 +55,7 @@ "repositories": [ { "type": "vcs", - "url": "https://github.com/brettmc/opentelemetry-php" + "url": "https://github.com/open-telemetry/opentelemetry-php" } ], "config": { diff --git a/src/Instrumentation/Laravel/src/Hooks/Hook.php b/src/Instrumentation/Laravel/src/Hooks/Hook.php index a1fe717bb..02594b096 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Hook.php +++ b/src/Instrumentation/Laravel/src/Hooks/Hook.php @@ -4,7 +4,7 @@ namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks; -use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; use OpenTelemetry\API\Logs\LoggerInterface; use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\TracerInterface; @@ -13,7 +13,7 @@ interface Hook { public function instrument( - HookManager $hookManager, + HookManagerInterface $hookManager, LaravelConfiguration $configuration, LoggerInterface $logger, MeterInterface $meter, diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php index 46c54b39d..5ef969bcb 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php @@ -5,7 +5,7 @@ namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Console; use Illuminate\Console\Command as IlluminateCommand; -use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; use OpenTelemetry\API\Logs\LoggerInterface; use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\Span; @@ -22,7 +22,7 @@ class Command implements Hook use PostHookTrait; public function instrument( - HookManager $hookManager, + HookManagerInterface $hookManager, LaravelConfiguration $configuration, LoggerInterface $logger, MeterInterface $meter, @@ -31,7 +31,7 @@ public function instrument( $this->hookExecute($hookManager, $tracer); } - protected function hookExecute(HookManager $hookManager, TracerInterface $tracer): void + protected function hookExecute(HookManagerInterface $hookManager, TracerInterface $tracer): void { $hookManager->hook( IlluminateCommand::class, diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php index 7e079518e..1209b7f6f 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php @@ -6,7 +6,7 @@ use Illuminate\Console\Command; use Illuminate\Contracts\Console\Kernel as KernelContract; -use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; use OpenTelemetry\API\Logs\LoggerInterface; use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\Span; @@ -28,7 +28,7 @@ class Kernel implements Hook use PostHookTrait; public function instrument( - HookManager $hookManager, + HookManagerInterface $hookManager, LaravelConfiguration $configuration, LoggerInterface $logger, MeterInterface $meter, @@ -39,7 +39,7 @@ public function instrument( } } - private function hookHandle(HookManager $hookManager, TracerInterface $tracer): void + private function hookHandle(HookManagerInterface $hookManager, TracerInterface $tracer): void { $hookManager->hook( KernelContract::class, diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php index 6e6dd8b8c..7191fca0e 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php @@ -8,7 +8,7 @@ use Illuminate\Http\Request; use Illuminate\Routing\Route; use OpenTelemetry\API\Globals; -use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; use OpenTelemetry\API\Logs\LoggerInterface; use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\Span; @@ -31,7 +31,7 @@ class Kernel implements Hook use PostHookTrait; public function instrument( - HookManager $hookManager, + HookManagerInterface $hookManager, LaravelConfiguration $configuration, LoggerInterface $logger, MeterInterface $meter, @@ -40,7 +40,7 @@ public function instrument( $this->hookHandle($hookManager, $tracer); } - protected function hookHandle(HookManager $hookManager, TracerInterface $tracer): void + protected function hookHandle(HookManagerInterface $hookManager, TracerInterface $tracer): void { $hookManager->hook( KernelContract::class, diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php index ac081397d..950833e06 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php @@ -7,14 +7,14 @@ use DateInterval; use DateTimeInterface; use Illuminate\Contracts\Queue\Queue as QueueContract; -use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; use OpenTelemetry\API\Logs\LoggerInterface; use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\SpanKind; use OpenTelemetry\API\Trace\TracerInterface; use OpenTelemetry\Context\Context; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Queue\AttributesBuilder; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; +use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Queue\AttributesBuilder; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; use OpenTelemetry\SemConv\TraceAttributes; @@ -27,7 +27,7 @@ class Queue implements Hook use PostHookTrait; public function instrument( - HookManager $hookManager, + HookManagerInterface $hookManager, LaravelConfiguration $configuration, LoggerInterface $logger, MeterInterface $meter, @@ -38,7 +38,7 @@ public function instrument( $this->hookPushRaw($hookManager, $tracer); } - protected function hookBulk(HookManager $hookManager, TracerInterface $tracer): void + protected function hookBulk(HookManagerInterface $hookManager, TracerInterface $tracer): void { $hookManager->hook( QueueContract::class, @@ -73,7 +73,7 @@ protected function hookBulk(HookManager $hookManager, TracerInterface $tracer): ); } - protected function hookLater(HookManager $hookManager, TracerInterface $tracer): void + protected function hookLater(HookManagerInterface $hookManager, TracerInterface $tracer): void { $hookManager->hook( QueueContract::class, @@ -115,7 +115,7 @@ protected function hookLater(HookManager $hookManager, TracerInterface $tracer): ); } - protected function hookPushRaw(HookManager $hookManager, TracerInterface $tracer): void + protected function hookPushRaw(HookManagerInterface $hookManager, TracerInterface $tracer): void { $hookManager->hook( QueueContract::class, diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Application.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Application.php index 9a8c0ee16..63a5e6486 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Application.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Application.php @@ -6,7 +6,7 @@ use Illuminate\Contracts\Foundation\Application as ApplicationContract; use Illuminate\Foundation\Application as FoundationalApplication; -use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; use OpenTelemetry\API\Logs\LoggerInterface; use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\TracerInterface; @@ -23,7 +23,7 @@ class Application implements Hook { public function instrument( - HookManager $hookManager, + HookManagerInterface $hookManager, LaravelConfiguration $configuration, LoggerInterface $logger, MeterInterface $meter, diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php index c63fd3bf1..585dec844 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php @@ -5,7 +5,7 @@ namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Foundation\Console; use Illuminate\Foundation\Console\ServeCommand as FoundationServeCommand; -use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; use OpenTelemetry\API\Logs\LoggerInterface; use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\TracerInterface; @@ -18,7 +18,7 @@ class ServeCommand implements Hook { public function instrument( - HookManager $hookManager, + HookManagerInterface $hookManager, LaravelConfiguration $configuration, LoggerInterface $logger, MeterInterface $meter, diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Queue.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Queue.php index 86c84e9dc..ff6a55837 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Queue.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Queue.php @@ -5,7 +5,7 @@ namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Queue; use Illuminate\Queue\Queue as AbstractQueue; -use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; use OpenTelemetry\API\Logs\LoggerInterface; use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator; @@ -19,7 +19,7 @@ class Queue implements Hook use AttributesBuilder; public function instrument( - HookManager $hookManager, + HookManagerInterface $hookManager, LaravelConfiguration $configuration, LoggerInterface $logger, MeterInterface $meter, diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php index 2906a6fef..e154e2948 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php @@ -5,7 +5,7 @@ namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Queue; use Illuminate\Queue\SyncQueue as LaravelSyncQueue; -use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; use OpenTelemetry\API\Logs\LoggerInterface; use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\SpanKind; @@ -23,7 +23,7 @@ class SyncQueue implements Hook use PostHookTrait; public function instrument( - HookManager $hookManager, + HookManagerInterface $hookManager, LaravelConfiguration $configuration, LoggerInterface $logger, MeterInterface $meter, @@ -32,7 +32,7 @@ public function instrument( $this->hookPush($hookManager, $tracer); } - protected function hookPush(HookManager $hookManager, TracerInterface $tracer): void + protected function hookPush(HookManagerInterface $hookManager, TracerInterface $tracer): void { $hookManager->hook( LaravelSyncQueue::class, diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php index a375ed145..1eab9b129 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php @@ -6,7 +6,7 @@ use Illuminate\Contracts\Queue\Job; use Illuminate\Queue\Worker as QueueWorker; -use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; use OpenTelemetry\API\Logs\LoggerInterface; use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator; @@ -27,7 +27,7 @@ class Worker implements Hook use PostHookTrait; public function instrument( - HookManager $hookManager, + HookManagerInterface $hookManager, LaravelConfiguration $configuration, LoggerInterface $logger, MeterInterface $meter, @@ -37,7 +37,7 @@ public function instrument( $this->hookWorkerGetNextJob($hookManager, $tracer); } - private function hookWorkerProcess(HookManager $hookManager, TracerInterface $tracer): void + private function hookWorkerProcess(HookManagerInterface $hookManager, TracerInterface $tracer): void { $hookManager->hook( QueueWorker::class, @@ -88,7 +88,7 @@ private function hookWorkerProcess(HookManager $hookManager, TracerInterface $tr ); } - private function hookWorkerGetNextJob(HookManager $hookManager, TracerInterface $tracer): void + private function hookWorkerGetNextJob(HookManagerInterface $hookManager, TracerInterface $tracer): void { $hookManager->hook( QueueWorker::class, diff --git a/src/Instrumentation/Laravel/src/LaravelConfiguration.php b/src/Instrumentation/Laravel/src/LaravelConfiguration.php index 7c8509973..398def4be 100644 --- a/src/Instrumentation/Laravel/src/LaravelConfiguration.php +++ b/src/Instrumentation/Laravel/src/LaravelConfiguration.php @@ -17,7 +17,7 @@ private function __construct( public static function fromArray(array $properties): self { return new self( - enabled: $properties['enabled'] ?? (class_exists(Sdk::class) ? !Sdk::isDisabled() : true), + enabled: (class_exists(Sdk::class) && !Sdk::isDisabled()) || $properties['enabled'], ); } diff --git a/src/Instrumentation/Laravel/src/LaravelInstrumentation.php b/src/Instrumentation/Laravel/src/LaravelInstrumentation.php index 39c8d64e1..da87ba4e2 100644 --- a/src/Instrumentation/Laravel/src/LaravelInstrumentation.php +++ b/src/Instrumentation/Laravel/src/LaravelInstrumentation.php @@ -7,7 +7,7 @@ use Nevay\SPI\ServiceLoader; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\ConfigurationRegistry; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context; -use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Instrumentation; use OpenTelemetry\API\Instrumentation\ConfigurationResolver; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; @@ -17,7 +17,7 @@ class LaravelInstrumentation implements Instrumentation { public const INSTRUMENTATION_NAME = 'io.opentelemetry.contrib.php.laravel'; - public function register(HookManager $hookManager, ConfigurationRegistry $configuration, Context $context): void + public function register(HookManagerInterface $hookManager, ConfigurationRegistry $configuration, Context $context): void { $config = $configuration->get(LaravelConfiguration::class) ?? LaravelConfiguration::default(); From e494ac2e3b16f2b3296bb3664c972bd7f2e4298a Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Tue, 27 Aug 2024 21:14:21 +0100 Subject: [PATCH 13/40] Laravel: use Configuration\ConfigProperties API for instrumentation registration. --- src/Instrumentation/Laravel/src/LaravelInstrumentation.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Instrumentation/Laravel/src/LaravelInstrumentation.php b/src/Instrumentation/Laravel/src/LaravelInstrumentation.php index da87ba4e2..a2c58d4d0 100644 --- a/src/Instrumentation/Laravel/src/LaravelInstrumentation.php +++ b/src/Instrumentation/Laravel/src/LaravelInstrumentation.php @@ -5,7 +5,7 @@ namespace OpenTelemetry\Contrib\Instrumentation\Laravel; use Nevay\SPI\ServiceLoader; -use OpenTelemetry\API\Instrumentation\AutoInstrumentation\ConfigurationRegistry; +use OpenTelemetry\API\Configuration\ConfigProperties; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Instrumentation; @@ -17,7 +17,7 @@ class LaravelInstrumentation implements Instrumentation { public const INSTRUMENTATION_NAME = 'io.opentelemetry.contrib.php.laravel'; - public function register(HookManagerInterface $hookManager, ConfigurationRegistry $configuration, Context $context): void + public function register(HookManagerInterface $hookManager, ConfigProperties $configuration, Context $context): void { $config = $configuration->get(LaravelConfiguration::class) ?? LaravelConfiguration::default(); From ea2e4bb74f8f5a78a8962894bde4ba2931b92c6d Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Tue, 27 Aug 2024 21:17:35 +0100 Subject: [PATCH 14/40] Laravel: use updated TraceAttributeValues::MESSAGING_OPERATION_TYPE_* constants from sem-conv 1.26. --- .../Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php | 4 ++-- .../Laravel/src/Hooks/Illuminate/Queue/Worker.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php index 950833e06..f187013c7 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php @@ -57,7 +57,7 @@ protected function hookBulk(HookManagerInterface $hookManager, TracerInterface $ ->spanBuilder(vsprintf('%s %s', [ /** @phan-suppress-next-line PhanUndeclaredMethod */ method_exists($queue, 'getQueue') ? $queue->getQueue($params[2] ?? null) : $queue->getConnectionName(), - TraceAttributeValues::MESSAGING_OPERATION_PUBLISH, + TraceAttributeValues::MESSAGING_OPERATION_TYPE_PUBLISH, ])) ->setSpanKind(SpanKind::KIND_PRODUCER) ->setAttributes($attributes) @@ -129,7 +129,7 @@ protected function hookPushRaw(HookManagerInterface $hookManager, TracerInterfac $span = $tracer ->spanBuilder(vsprintf('%s %s', [ $attributes[TraceAttributes::MESSAGING_DESTINATION_NAME], - TraceAttributeValues::MESSAGING_OPERATION_CREATE, + TraceAttributeValues::MESSAGING_OPERATION_TYPE_CREATE, ])) ->setSpanKind(SpanKind::KIND_PRODUCER) ->setAttributes($attributes) diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php index 1eab9b129..71aa49ac0 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php @@ -104,7 +104,7 @@ private function hookWorkerGetNextJob(HookManagerInterface $hookManager, TracerI $span = $tracer ->spanBuilder(vsprintf('%s %s', [ $attributes[TraceAttributes::MESSAGING_DESTINATION_NAME], - TraceAttributeValues::MESSAGING_OPERATION_RECEIVE, + TraceAttributeValues::MESSAGING_OPERATION_TYPE_RECEIVE, ])) ->setSpanKind(SpanKind::KIND_CONSUMER) ->setAttributes($attributes) From ea05ad42ca6a02ead223ccb493ef3d3d874ad8d3 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Wed, 2 Oct 2024 21:10:59 +0100 Subject: [PATCH 15/40] Laravel: added ComponentProvider namespace. --- .../src/{ => ComponentProvider}/LaravelComponentProvider.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) rename src/Instrumentation/Laravel/src/{ => ComponentProvider}/LaravelComponentProvider.php (85%) diff --git a/src/Instrumentation/Laravel/src/LaravelComponentProvider.php b/src/Instrumentation/Laravel/src/ComponentProvider/LaravelComponentProvider.php similarity index 85% rename from src/Instrumentation/Laravel/src/LaravelComponentProvider.php rename to src/Instrumentation/Laravel/src/ComponentProvider/LaravelComponentProvider.php index bd460ab91..dd53978dc 100644 --- a/src/Instrumentation/Laravel/src/LaravelComponentProvider.php +++ b/src/Instrumentation/Laravel/src/ComponentProvider/LaravelComponentProvider.php @@ -2,12 +2,13 @@ declare(strict_types=1); -namespace OpenTelemetry\Contrib\Instrumentation\Laravel; +namespace OpenTelemetry\Contrib\Instrumentation\Laravel\ComponentProvider; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\InstrumentationConfiguration; use OpenTelemetry\Config\SDK\Configuration\ComponentProvider; use OpenTelemetry\Config\SDK\Configuration\ComponentProviderRegistry; use OpenTelemetry\Config\SDK\Configuration\Context; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; class LaravelComponentProvider implements ComponentProvider From d67da5646734b778c16cbe949142943300cde44c Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Wed, 2 Oct 2024 21:16:05 +0100 Subject: [PATCH 16/40] Laravel: bump otel dependencies to 1.1. --- src/Instrumentation/Laravel/composer.json | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Instrumentation/Laravel/composer.json b/src/Instrumentation/Laravel/composer.json index 20a875020..d271fd0c2 100644 --- a/src/Instrumentation/Laravel/composer.json +++ b/src/Instrumentation/Laravel/composer.json @@ -13,12 +13,15 @@ "ext-json": "*", "ext-opentelemetry": "*", "laravel/framework": "^10.0 || ^11.0", - "open-telemetry/opentelemetry": "dev-main as 1.x-dev" + "open-telemetry/api": "^1.1", + "open-telemetry/sem-conv": "^1.24" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.50", "guzzlehttp/guzzle": "*", "nunomaduro/collision": "*", + "open-telemetry/sdk": "^1.1", + "open-telemetry/sdk-configuration": "^0.0.5", "orchestra/testbench": ">=7.41.3", "phan/phan": "^5.0", "php-http/mock-client": "*", @@ -48,16 +51,10 @@ }, "spi": { "OpenTelemetry\\Config\\SDK\\Configuration\\ComponentProvider": [ - "OpenTelemetry\\Contrib\\Instrumentation\\Laravel\\LaravelComponentProvider" + "OpenTelemetry\\Contrib\\Instrumentation\\Laravel\\ComponentProvider\\LaravelComponentProvider" ] } }, - "repositories": [ - { - "type": "vcs", - "url": "https://github.com/open-telemetry/opentelemetry-php" - } - ], "config": { "lock": false, "sort-packages": true, From 190f6289d2502a65e7f38b06ae6cc5fe31e0944c Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Wed, 2 Oct 2024 21:19:05 +0100 Subject: [PATCH 17/40] Laravel: respect current way of disabling instrumentation. --- src/Instrumentation/Laravel/src/LaravelConfiguration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Instrumentation/Laravel/src/LaravelConfiguration.php b/src/Instrumentation/Laravel/src/LaravelConfiguration.php index 398def4be..dd331859e 100644 --- a/src/Instrumentation/Laravel/src/LaravelConfiguration.php +++ b/src/Instrumentation/Laravel/src/LaravelConfiguration.php @@ -17,7 +17,7 @@ private function __construct( public static function fromArray(array $properties): self { return new self( - enabled: (class_exists(Sdk::class) && !Sdk::isDisabled()) || $properties['enabled'], + enabled: (class_exists(Sdk::class) && !Sdk::isInstrumentationDisabled('laravel')) || $properties['enabled'], ); } From 74128819360297447017b89165df43ff46c5fbe4 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Wed, 2 Oct 2024 22:02:36 +0100 Subject: [PATCH 18/40] Laravel: sem-conv 1.24. --- src/Instrumentation/Laravel/src/LaravelInstrumentation.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Instrumentation/Laravel/src/LaravelInstrumentation.php b/src/Instrumentation/Laravel/src/LaravelInstrumentation.php index a2c58d4d0..ab894d0a9 100644 --- a/src/Instrumentation/Laravel/src/LaravelInstrumentation.php +++ b/src/Instrumentation/Laravel/src/LaravelInstrumentation.php @@ -25,9 +25,9 @@ public function register(HookManagerInterface $hookManager, ConfigProperties $co return; } - $logger = $context->loggerProvider->getLogger(self::INSTRUMENTATION_NAME, schemaUrl: Version::VERSION_1_25_0->url()); - $meter = $context->meterProvider->getMeter(self::INSTRUMENTATION_NAME, schemaUrl: Version::VERSION_1_25_0->url()); - $tracer = $context->tracerProvider->getTracer(self::INSTRUMENTATION_NAME, schemaUrl: Version::VERSION_1_25_0->url()); + $logger = $context->loggerProvider->getLogger(self::INSTRUMENTATION_NAME, schemaUrl: Version::VERSION_1_24_0->url()); + $meter = $context->meterProvider->getMeter(self::INSTRUMENTATION_NAME, schemaUrl: Version::VERSION_1_24_0->url()); + $tracer = $context->tracerProvider->getTracer(self::INSTRUMENTATION_NAME, schemaUrl: Version::VERSION_1_24_0->url()); foreach (ServiceLoader::load(Hook::class) as $hook) { /** @var Hook $hook */ From d2887bc3a6e7537860f47ae67fbc69ba17fe8fcc Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Wed, 2 Oct 2024 22:42:01 +0100 Subject: [PATCH 19/40] Laravel: bump phpunit. --- src/Instrumentation/Laravel/composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Instrumentation/Laravel/composer.json b/src/Instrumentation/Laravel/composer.json index d271fd0c2..4935a793c 100644 --- a/src/Instrumentation/Laravel/composer.json +++ b/src/Instrumentation/Laravel/composer.json @@ -27,7 +27,7 @@ "php-http/mock-client": "*", "phpstan/phpstan": "^1.1", "phpstan/phpstan-phpunit": "^1.0", - "phpunit/phpunit": "^9.5", + "phpunit/phpunit": "^10.5 || ^11.3", "psalm/plugin-phpunit": "^0.18.4", "spatie/laravel-ignition": "*", "vimeo/psalm": "^5.0" From cf6deaf609fbf4449afa2bbf58eed73b2739768d Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Wed, 2 Oct 2024 22:44:29 +0100 Subject: [PATCH 20/40] Laravel: tests bootstrap via SPI. --- src/Instrumentation/Laravel/phpunit.xml.dist | 6 ++- .../LogRecordExporterInMemory.php | 26 ++++++++++ .../SpanExporterInMemory.php | 26 ++++++++++ .../Laravel/tests/Fixtures/TestStorage.php | 22 ++++++++ .../tests/Fixtures/otel-instrumentation.yaml | 18 +++++++ .../Laravel/tests/Integration/TestCase.php | 50 ++++--------------- .../Laravel/tests/bootstrap.php | 19 +++++++ 7 files changed, 125 insertions(+), 42 deletions(-) create mode 100644 src/Instrumentation/Laravel/tests/Fixtures/ComponentProvider/LogRecordExporterInMemory.php create mode 100644 src/Instrumentation/Laravel/tests/Fixtures/ComponentProvider/SpanExporterInMemory.php create mode 100644 src/Instrumentation/Laravel/tests/Fixtures/TestStorage.php create mode 100644 src/Instrumentation/Laravel/tests/Fixtures/otel-instrumentation.yaml create mode 100644 src/Instrumentation/Laravel/tests/bootstrap.php diff --git a/src/Instrumentation/Laravel/phpunit.xml.dist b/src/Instrumentation/Laravel/phpunit.xml.dist index 0906e0e22..0af9a508c 100644 --- a/src/Instrumentation/Laravel/phpunit.xml.dist +++ b/src/Instrumentation/Laravel/phpunit.xml.dist @@ -5,6 +5,7 @@ xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd" backupGlobals="false" backupStaticAttributes="false" + bootstrap="tests/bootstrap.php" cacheResult="false" colors="false" convertErrorsToExceptions="true" @@ -41,7 +42,10 @@ - + + + + diff --git a/src/Instrumentation/Laravel/tests/Fixtures/ComponentProvider/LogRecordExporterInMemory.php b/src/Instrumentation/Laravel/tests/Fixtures/ComponentProvider/LogRecordExporterInMemory.php new file mode 100644 index 000000000..21a41dda4 --- /dev/null +++ b/src/Instrumentation/Laravel/tests/Fixtures/ComponentProvider/LogRecordExporterInMemory.php @@ -0,0 +1,26 @@ +exchangeArray([]); + } +} diff --git a/src/Instrumentation/Laravel/tests/Fixtures/otel-instrumentation.yaml b/src/Instrumentation/Laravel/tests/Fixtures/otel-instrumentation.yaml new file mode 100644 index 000000000..15a55c70d --- /dev/null +++ b/src/Instrumentation/Laravel/tests/Fixtures/otel-instrumentation.yaml @@ -0,0 +1,18 @@ +file_format: '0.3' + +logger_provider: + processors: + - simple: + exporter: + test/in_memory_exporter: {} + +tracer_provider: + processors: + - simple: + exporter: + test/in_memory_exporter: {} + +instrumentation: + php: + laravel: + enabled: true diff --git a/src/Instrumentation/Laravel/tests/Integration/TestCase.php b/src/Instrumentation/Laravel/tests/Integration/TestCase.php index 56c1abbbf..c842e6cdf 100644 --- a/src/Instrumentation/Laravel/tests/Integration/TestCase.php +++ b/src/Instrumentation/Laravel/tests/Integration/TestCase.php @@ -5,56 +5,24 @@ namespace OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Integration; use ArrayObject; -use OpenTelemetry\API\Instrumentation\Configurator; -use OpenTelemetry\Context\ScopeInterface; -use OpenTelemetry\SDK\Common\Attribute\Attributes; -use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory; -use OpenTelemetry\SDK\Logs\Exporter\InMemoryExporter as LogInMemoryExporter; -use OpenTelemetry\SDK\Logs\LoggerProvider; -use OpenTelemetry\SDK\Logs\Processor\SimpleLogRecordProcessor; -use OpenTelemetry\SDK\Trace\ImmutableSpan; -use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter as SpanInMemoryExporter; -use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor; -use OpenTelemetry\SDK\Trace\TracerProvider; +use OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\TestStorage; use Orchestra\Testbench\TestCase as BaseTestCase; +use PHPUnit\Framework\Attributes\After; +use PHPUnit\Framework\Attributes\Before; abstract class TestCase extends BaseTestCase { - protected ScopeInterface $scope; - /** @var ArrayObject|ImmutableSpan[] $storage */ protected ArrayObject $storage; - protected ArrayObject $loggerStorage; - protected TracerProvider $tracerProvider; - protected LoggerProvider $loggerProvider; - public function setUp(): void + #[Before] + public function setUpTestStorage(): void { - parent::setUp(); - - $this->storage = new ArrayObject(); - $this->tracerProvider = new TracerProvider( - new SimpleSpanProcessor( - new SpanInMemoryExporter($this->storage), - ), - ); - - $this->loggerProvider = new LoggerProvider( - new SimpleLogRecordProcessor( - new LogInMemoryExporter($this->storage), - ), - new InstrumentationScopeFactory(Attributes::factory()) - ); - - $this->scope = Configurator::create() - ->withTracerProvider($this->tracerProvider) - ->withLoggerProvider($this->loggerProvider) - ->activate(); + $this->storage = TestStorage::getInstance(); } - public function tearDown(): void + #[After] + public function tearDownTestStorage(): void { - parent::tearDown(); - - $this->scope->detach(); + TestStorage::reset(); } } diff --git a/src/Instrumentation/Laravel/tests/bootstrap.php b/src/Instrumentation/Laravel/tests/bootstrap.php new file mode 100644 index 000000000..14dc9ac51 --- /dev/null +++ b/src/Instrumentation/Laravel/tests/bootstrap.php @@ -0,0 +1,19 @@ + Date: Wed, 2 Oct 2024 23:01:32 +0100 Subject: [PATCH 21/40] Laravel: skip PHP 8.0 in CI. --- .github/workflows/php.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index aaa66b315..a67d1f43f 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -76,6 +76,8 @@ jobs: php-version: 7.4 - project: 'Instrumentation/Laravel' php-version: 7.4 + - project: 'Instrumentation/Laravel' + php-version: 8.0 - project: 'Instrumentation/CodeIgniter' php-version: 7.4 - project: 'Instrumentation/Yii' From 45a693262228a1adfe1d428cafbb40340a68042c Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Wed, 2 Oct 2024 23:18:50 +0100 Subject: [PATCH 22/40] Laravel: lint. --- .../Laravel/src/Watchers/ClientRequestWatcher.php | 3 ++- src/Instrumentation/Laravel/src/Watchers/QueryWatcher.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Instrumentation/Laravel/src/Watchers/ClientRequestWatcher.php b/src/Instrumentation/Laravel/src/Watchers/ClientRequestWatcher.php index 3f83efb3d..1913a027d 100644 --- a/src/Instrumentation/Laravel/src/Watchers/ClientRequestWatcher.php +++ b/src/Instrumentation/Laravel/src/Watchers/ClientRequestWatcher.php @@ -52,7 +52,8 @@ public function recordRequest(RequestSending $request): void if ($parsedUrl->has('query')) { $processedUrl .= '?' . $parsedUrl->get('query'); } - $span = $this->tracer->spanBuilder($request->request->method()) + $span = $this->tracer + ->spanBuilder($request->request->method()) ->setSpanKind(SpanKind::KIND_CLIENT) ->setAttributes([ TraceAttributes::HTTP_REQUEST_METHOD => $request->request->method(), diff --git a/src/Instrumentation/Laravel/src/Watchers/QueryWatcher.php b/src/Instrumentation/Laravel/src/Watchers/QueryWatcher.php index 17ae6bb3d..a237089ad 100644 --- a/src/Instrumentation/Laravel/src/Watchers/QueryWatcher.php +++ b/src/Instrumentation/Laravel/src/Watchers/QueryWatcher.php @@ -38,7 +38,8 @@ public function recordQuery(QueryExecuted $query): void $operationName = null; } /** @psalm-suppress ArgumentTypeCoercion */ - $span = $this->tracer->spanBuilder('sql ' . $operationName) + $span = $this->tracer + ->spanBuilder('sql ' . $operationName) ->setSpanKind(SpanKind::KIND_CLIENT) ->setStartTimestamp($this->calculateQueryStartTime($nowInNs, $query->time)) ->startSpan(); From ee8cebed917106e38ff01eb54b53dcc552edbf76 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Wed, 2 Oct 2024 23:20:52 +0100 Subject: [PATCH 23/40] Laravel: nullify missing attributes to reduce noise. --- .../Laravel/src/Watchers/ClientRequestWatcher.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Instrumentation/Laravel/src/Watchers/ClientRequestWatcher.php b/src/Instrumentation/Laravel/src/Watchers/ClientRequestWatcher.php index 1913a027d..e78a2d24c 100644 --- a/src/Instrumentation/Laravel/src/Watchers/ClientRequestWatcher.php +++ b/src/Instrumentation/Laravel/src/Watchers/ClientRequestWatcher.php @@ -58,10 +58,10 @@ public function recordRequest(RequestSending $request): void ->setAttributes([ TraceAttributes::HTTP_REQUEST_METHOD => $request->request->method(), TraceAttributes::URL_FULL => $processedUrl, - TraceAttributes::URL_PATH => $parsedUrl['path'] ?? '', - TraceAttributes::URL_SCHEME => $parsedUrl['scheme'] ?? '', - TraceAttributes::SERVER_ADDRESS => $parsedUrl['host'] ?? '', - TraceAttributes::SERVER_PORT => $parsedUrl['port'] ?? '', + TraceAttributes::URL_PATH => $parsedUrl['path'] ?? null, + TraceAttributes::URL_SCHEME => $parsedUrl['scheme'] ?? null, + TraceAttributes::SERVER_ADDRESS => $parsedUrl['host'] ?? null, + TraceAttributes::SERVER_PORT => $parsedUrl['port'] ?? null, ]) ->startSpan(); $this->spans[$this->createRequestComparisonHash($request->request)] = $span; From a6a677e11cb6875ff63f32cd6ee68ef196e07f6b Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Wed, 2 Oct 2024 23:52:54 +0100 Subject: [PATCH 24/40] Laravel: remove dev-dependencies which are not explicitly required. --- src/Instrumentation/Laravel/composer.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Instrumentation/Laravel/composer.json b/src/Instrumentation/Laravel/composer.json index 4935a793c..3d9009b01 100644 --- a/src/Instrumentation/Laravel/composer.json +++ b/src/Instrumentation/Laravel/composer.json @@ -19,7 +19,6 @@ "require-dev": { "friendsofphp/php-cs-fixer": "^3.50", "guzzlehttp/guzzle": "*", - "nunomaduro/collision": "*", "open-telemetry/sdk": "^1.1", "open-telemetry/sdk-configuration": "^0.0.5", "orchestra/testbench": ">=7.41.3", @@ -29,7 +28,6 @@ "phpstan/phpstan-phpunit": "^1.0", "phpunit/phpunit": "^10.5 || ^11.3", "psalm/plugin-phpunit": "^0.18.4", - "spatie/laravel-ignition": "*", "vimeo/psalm": "^5.0" }, "autoload": { From 6a8d0ef651ff8e202a383690aabafaf39dc79e44 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Thu, 3 Oct 2024 20:25:59 +0100 Subject: [PATCH 25/40] Laravel: bump target_php_version in phan. --- src/Instrumentation/Laravel/.phan/config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Instrumentation/Laravel/.phan/config.php b/src/Instrumentation/Laravel/.phan/config.php index da2ac2d99..6473a9aa8 100644 --- a/src/Instrumentation/Laravel/.phan/config.php +++ b/src/Instrumentation/Laravel/.phan/config.php @@ -42,7 +42,7 @@ // // Note that the **only** effect of choosing `'5.6'` is to infer that functions removed in php 7.0 exist. // (See `backward_compatibility_checks` for additional options) - 'target_php_version' => '8.0', + 'target_php_version' => '8.1', // If enabled, missing properties will be created when // they are first seen. If false, we'll report an From 60fc7815a45e678fe7ba43346aed17b2d8ea490c Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Thu, 3 Oct 2024 21:54:14 +0100 Subject: [PATCH 26/40] Laravel: suppress PhanTypeMismatchReturn in LaravelComponentProvider. --- .../Laravel/src/ComponentProvider/LaravelComponentProvider.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Instrumentation/Laravel/src/ComponentProvider/LaravelComponentProvider.php b/src/Instrumentation/Laravel/src/ComponentProvider/LaravelComponentProvider.php index dd53978dc..311b6fe3d 100644 --- a/src/Instrumentation/Laravel/src/ComponentProvider/LaravelComponentProvider.php +++ b/src/Instrumentation/Laravel/src/ComponentProvider/LaravelComponentProvider.php @@ -13,6 +13,9 @@ class LaravelComponentProvider implements ComponentProvider { + /** + * @phan-suppress PhanTypeMismatchReturn + */ public function createPlugin(array $properties, Context $context): InstrumentationConfiguration { return LaravelConfiguration::fromArray($properties); From 0d7dcf47b7c9bea4737b2d75151cbe8102ecff66 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Thu, 3 Oct 2024 23:13:26 +0100 Subject: [PATCH 27/40] Laravel: remove property_exists check that supported Laravel versions do not require. --- .../src/Hooks/Illuminate/Foundation/Console/ServeCommand.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php index 585dec844..52649cccd 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php @@ -28,10 +28,6 @@ public function instrument( FoundationServeCommand::class, 'handle', preHook: static function (FoundationServeCommand $serveCommand, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { - if (!property_exists(FoundationServeCommand::class, 'passthroughVariables')) { - return; - } - foreach ($_ENV as $key => $value) { if (str_starts_with($key, 'OTEL_') && !in_array($key, FoundationServeCommand::$passthroughVariables)) { FoundationServeCommand::$passthroughVariables[] = $key; From 16dc809ba409823c6edf24759e6317e3d11f4032 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Thu, 24 Oct 2024 23:11:28 +0100 Subject: [PATCH 28/40] Laravel: pass instrumentation Context to hooks --- .../Laravel/src/Hooks/Hook.php | 12 ++++------ .../src/Hooks/Illuminate/Console/Command.php | 17 ++++++++------ .../Illuminate/Contracts/Console/Kernel.php | 18 ++++++++------- .../Illuminate/Contracts/Http/Kernel.php | 17 ++++++++------ .../Illuminate/Contracts/Queue/Queue.php | 17 ++++++++------ .../Illuminate/Foundation/Application.php | 23 ++++++++++++------- .../Foundation/Console/ServeCommand.php | 14 ++++------- .../src/Hooks/Illuminate/Queue/Queue.php | 12 ++++------ .../src/Hooks/Illuminate/Queue/SyncQueue.php | 17 ++++++++------ .../src/Hooks/Illuminate/Queue/Worker.php | 17 ++++++++------ .../Laravel/src/LaravelInstrumentation.php | 17 ++++++++------ 11 files changed, 98 insertions(+), 83 deletions(-) diff --git a/src/Instrumentation/Laravel/src/Hooks/Hook.php b/src/Instrumentation/Laravel/src/Hooks/Hook.php index 02594b096..cdf41d2a4 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Hook.php +++ b/src/Instrumentation/Laravel/src/Hooks/Hook.php @@ -4,19 +4,15 @@ namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context as InstrumentationContext; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; -use OpenTelemetry\API\Logs\LoggerInterface; -use OpenTelemetry\API\Metrics\MeterInterface; -use OpenTelemetry\API\Trace\TracerInterface; -use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; interface Hook { public function instrument( + LaravelInstrumentation $instrumentation, HookManagerInterface $hookManager, - LaravelConfiguration $configuration, - LoggerInterface $logger, - MeterInterface $meter, - TracerInterface $tracer, + InstrumentationContext $context, ): void; } diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php index 5ef969bcb..72012a6f1 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php @@ -5,16 +5,16 @@ namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Console; use Illuminate\Console\Command as IlluminateCommand; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context as InstrumentationContext; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; -use OpenTelemetry\API\Logs\LoggerInterface; -use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\Span; use OpenTelemetry\API\Trace\TracerInterface; use OpenTelemetry\Context\Context; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; -use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; use OpenTelemetry\SemConv\TraceAttributes; +use OpenTelemetry\SemConv\Version; use Throwable; class Command implements Hook @@ -22,12 +22,15 @@ class Command implements Hook use PostHookTrait; public function instrument( + LaravelInstrumentation $instrumentation, HookManagerInterface $hookManager, - LaravelConfiguration $configuration, - LoggerInterface $logger, - MeterInterface $meter, - TracerInterface $tracer, + InstrumentationContext $context, ): void { + $tracer = $context->tracerProvider->getTracer( + $instrumentation->buildProviderName('console', 'command'), + schemaUrl: Version::VERSION_1_24_0->url(), + ); + $this->hookExecute($hookManager, $tracer); } diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php index 1209b7f6f..3c186e179 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php @@ -6,9 +6,8 @@ use Illuminate\Console\Command; use Illuminate\Contracts\Console\Kernel as KernelContract; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context as InstrumentationContext; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; -use OpenTelemetry\API\Logs\LoggerInterface; -use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\Span; use OpenTelemetry\API\Trace\SpanKind; use OpenTelemetry\API\Trace\StatusCode; @@ -17,9 +16,9 @@ use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Queue\AttributesBuilder; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; -use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; use OpenTelemetry\SemConv\TraceAttributes; +use OpenTelemetry\SemConv\Version; use Throwable; class Kernel implements Hook @@ -28,13 +27,16 @@ class Kernel implements Hook use PostHookTrait; public function instrument( + LaravelInstrumentation $instrumentation, HookManagerInterface $hookManager, - LaravelConfiguration $configuration, - LoggerInterface $logger, - MeterInterface $meter, - TracerInterface $tracer, + InstrumentationContext $context, ): void { - if (LaravelInstrumentation::shouldTraceCli()) { + $tracer = $context->tracerProvider->getTracer( + $instrumentation->buildProviderName('console', 'kernel'), + schemaUrl: Version::VERSION_1_24_0->url(), + ); + + if ($instrumentation->shouldTraceCli()) { $this->hookHandle($hookManager, $tracer); } } diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php index 887685ce9..1a00ce385 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php @@ -8,9 +8,8 @@ use Illuminate\Http\Request; use Illuminate\Routing\Route; use OpenTelemetry\API\Globals; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context as InstrumentationContext; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; -use OpenTelemetry\API\Logs\LoggerInterface; -use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\Span; use OpenTelemetry\API\Trace\SpanInterface; use OpenTelemetry\API\Trace\SpanKind; @@ -19,10 +18,11 @@ use OpenTelemetry\Context\Context; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; -use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; use OpenTelemetry\Contrib\Instrumentation\Laravel\Propagators\HeadersPropagator; use OpenTelemetry\Contrib\Instrumentation\Laravel\Propagators\ResponsePropagationSetter; use OpenTelemetry\SemConv\TraceAttributes; +use OpenTelemetry\SemConv\Version; use Symfony\Component\HttpFoundation\Response; use Throwable; @@ -31,12 +31,15 @@ class Kernel implements Hook use PostHookTrait; public function instrument( + LaravelInstrumentation $instrumentation, HookManagerInterface $hookManager, - LaravelConfiguration $configuration, - LoggerInterface $logger, - MeterInterface $meter, - TracerInterface $tracer, + InstrumentationContext $context, ): void { + $tracer = $context->tracerProvider->getTracer( + $instrumentation->buildProviderName('http', 'kernel'), + schemaUrl: Version::VERSION_1_24_0->url(), + ); + $this->hookHandle($hookManager, $tracer); } diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php index f187013c7..b851d3608 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php @@ -7,18 +7,18 @@ use DateInterval; use DateTimeInterface; use Illuminate\Contracts\Queue\Queue as QueueContract; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context as InstrumentationContext; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; -use OpenTelemetry\API\Logs\LoggerInterface; -use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\SpanKind; use OpenTelemetry\API\Trace\TracerInterface; use OpenTelemetry\Context\Context; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Queue\AttributesBuilder; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; -use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; use OpenTelemetry\SemConv\TraceAttributes; use OpenTelemetry\SemConv\TraceAttributeValues; +use OpenTelemetry\SemConv\Version; use Throwable; class Queue implements Hook @@ -27,12 +27,15 @@ class Queue implements Hook use PostHookTrait; public function instrument( + LaravelInstrumentation $instrumentation, HookManagerInterface $hookManager, - LaravelConfiguration $configuration, - LoggerInterface $logger, - MeterInterface $meter, - TracerInterface $tracer, + InstrumentationContext $context, ): void { + $tracer = $context->tracerProvider->getTracer( + $instrumentation->buildProviderName('queue'), + schemaUrl: Version::VERSION_1_24_0->url(), + ); + $this->hookBulk($hookManager, $tracer); $this->hookLater($hookManager, $tracer); $this->hookPushRaw($hookManager, $tracer); diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Application.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Application.php index 2f5afb5fb..2556273eb 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Application.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Application.php @@ -6,12 +6,10 @@ use Illuminate\Contracts\Foundation\Application as ApplicationContract; use Illuminate\Foundation\Application as FoundationalApplication; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context as InstrumentationContext; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; -use OpenTelemetry\API\Logs\LoggerInterface; -use OpenTelemetry\API\Metrics\MeterInterface; -use OpenTelemetry\API\Trace\TracerInterface; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; -use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\CacheWatcher; use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\ClientRequestWatcher; use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\ExceptionWatcher; @@ -19,17 +17,26 @@ use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\QueryWatcher; use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\RedisCommand\RedisCommandWatcher; use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\Watcher; +use OpenTelemetry\SemConv\Version; use Throwable; class Application implements Hook { public function instrument( + LaravelInstrumentation $instrumentation, HookManagerInterface $hookManager, - LaravelConfiguration $configuration, - LoggerInterface $logger, - MeterInterface $meter, - TracerInterface $tracer, + InstrumentationContext $context, ): void { + $logger = $context->loggerProvider->getLogger( + $instrumentation->buildProviderName('foundation', 'application'), + schemaUrl: Version::VERSION_1_24_0->url(), + ); + + $tracer = $context->tracerProvider->getTracer( + $instrumentation->buildProviderName('foundation', 'application'), + schemaUrl: Version::VERSION_1_24_0->url(), + ); + $hookManager->hook( FoundationalApplication::class, '__construct', diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php index 52649cccd..9501bc552 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php @@ -5,12 +5,10 @@ namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Foundation\Console; use Illuminate\Foundation\Console\ServeCommand as FoundationServeCommand; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context as InstrumentationContext; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; -use OpenTelemetry\API\Logs\LoggerInterface; -use OpenTelemetry\API\Metrics\MeterInterface; -use OpenTelemetry\API\Trace\TracerInterface; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; -use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; /** * Instrument Laravel's local PHP development server. @@ -18,16 +16,14 @@ class ServeCommand implements Hook { public function instrument( + LaravelInstrumentation $instrumentation, HookManagerInterface $hookManager, - LaravelConfiguration $configuration, - LoggerInterface $logger, - MeterInterface $meter, - TracerInterface $tracer, + InstrumentationContext $context, ): void { $hookManager->hook( FoundationServeCommand::class, 'handle', - preHook: static function (FoundationServeCommand $serveCommand, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { + preHook: static function (FoundationServeCommand $serveCommand, array $params, string $class, string $function, ?string $filename, ?int $lineno) { foreach ($_ENV as $key => $value) { if (str_starts_with($key, 'OTEL_') && !in_array($key, FoundationServeCommand::$passthroughVariables)) { FoundationServeCommand::$passthroughVariables[] = $key; diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Queue.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Queue.php index ff6a55837..5a46fe4e4 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Queue.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Queue.php @@ -5,13 +5,11 @@ namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Queue; use Illuminate\Queue\Queue as AbstractQueue; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context as InstrumentationContext; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; -use OpenTelemetry\API\Logs\LoggerInterface; -use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator; -use OpenTelemetry\API\Trace\TracerInterface; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; -use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; use Throwable; class Queue implements Hook @@ -19,11 +17,9 @@ class Queue implements Hook use AttributesBuilder; public function instrument( + LaravelInstrumentation $instrumentation, HookManagerInterface $hookManager, - LaravelConfiguration $configuration, - LoggerInterface $logger, - MeterInterface $meter, - TracerInterface $tracer, + InstrumentationContext $context, ): void { $hookManager->hook( AbstractQueue::class, diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php index e154e2948..115e798f0 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php @@ -5,16 +5,16 @@ namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Queue; use Illuminate\Queue\SyncQueue as LaravelSyncQueue; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context as InstrumentationContext; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; -use OpenTelemetry\API\Logs\LoggerInterface; -use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\SpanKind; use OpenTelemetry\API\Trace\TracerInterface; use OpenTelemetry\Context\Context; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; -use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; use OpenTelemetry\SemConv\TraceAttributes; +use OpenTelemetry\SemConv\Version; use Throwable; class SyncQueue implements Hook @@ -23,12 +23,15 @@ class SyncQueue implements Hook use PostHookTrait; public function instrument( + LaravelInstrumentation $instrumentation, HookManagerInterface $hookManager, - LaravelConfiguration $configuration, - LoggerInterface $logger, - MeterInterface $meter, - TracerInterface $tracer, + InstrumentationContext $context, ): void { + $tracer = $context->tracerProvider->getTracer( + $instrumentation->buildProviderName('queue', 'sync'), + schemaUrl: Version::VERSION_1_24_0->url(), + ); + $this->hookPush($hookManager, $tracer); } diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php index 71aa49ac0..ca2f31fc6 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php @@ -6,9 +6,8 @@ use Illuminate\Contracts\Queue\Job; use Illuminate\Queue\Worker as QueueWorker; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context as InstrumentationContext; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; -use OpenTelemetry\API\Logs\LoggerInterface; -use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator; use OpenTelemetry\API\Trace\Span; use OpenTelemetry\API\Trace\SpanKind; @@ -16,9 +15,10 @@ use OpenTelemetry\Context\Context; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; -use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; use OpenTelemetry\SemConv\TraceAttributes; use OpenTelemetry\SemConv\TraceAttributeValues; +use OpenTelemetry\SemConv\Version; use Throwable; class Worker implements Hook @@ -27,12 +27,15 @@ class Worker implements Hook use PostHookTrait; public function instrument( + LaravelInstrumentation $instrumentation, HookManagerInterface $hookManager, - LaravelConfiguration $configuration, - LoggerInterface $logger, - MeterInterface $meter, - TracerInterface $tracer, + InstrumentationContext $context, ): void { + $tracer = $context->tracerProvider->getTracer( + $instrumentation->buildProviderName('queue', 'worker'), + schemaUrl: Version::VERSION_1_24_0->url(), + ); + $this->hookWorkerProcess($hookManager, $tracer); $this->hookWorkerGetNextJob($hookManager, $tracer); } diff --git a/src/Instrumentation/Laravel/src/LaravelInstrumentation.php b/src/Instrumentation/Laravel/src/LaravelInstrumentation.php index ab894d0a9..2cbf04011 100644 --- a/src/Instrumentation/Laravel/src/LaravelInstrumentation.php +++ b/src/Instrumentation/Laravel/src/LaravelInstrumentation.php @@ -11,7 +11,6 @@ use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Instrumentation; use OpenTelemetry\API\Instrumentation\ConfigurationResolver; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; -use OpenTelemetry\SemConv\Version; class LaravelInstrumentation implements Instrumentation { @@ -25,17 +24,21 @@ public function register(HookManagerInterface $hookManager, ConfigProperties $co return; } - $logger = $context->loggerProvider->getLogger(self::INSTRUMENTATION_NAME, schemaUrl: Version::VERSION_1_24_0->url()); - $meter = $context->meterProvider->getMeter(self::INSTRUMENTATION_NAME, schemaUrl: Version::VERSION_1_24_0->url()); - $tracer = $context->tracerProvider->getTracer(self::INSTRUMENTATION_NAME, schemaUrl: Version::VERSION_1_24_0->url()); - foreach (ServiceLoader::load(Hook::class) as $hook) { /** @var Hook $hook */ - $hook->instrument($hookManager, $config, $logger, $meter, $tracer); + $hook->instrument($this, $hookManager, $context); } } - public static function shouldTraceCli(): bool + public function buildProviderName(string ...$component): string + { + return implode('.', [ + self::INSTRUMENTATION_NAME, + ...$component, + ]); + } + + public function shouldTraceCli(): bool { return PHP_SAPI !== 'cli' || (new ConfigurationResolver())->getBoolean('OTEL_PHP_TRACE_CLI_ENABLED'); } From fc1df3ff99b596201414aab0feaac32d0a73235d Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Thu, 24 Oct 2024 23:30:32 +0100 Subject: [PATCH 29/40] Laravel: suppress PhanDeprecatedClassConstant for deprecated sem-conv attributes. --- src/Instrumentation/Laravel/src/Watchers/QueryWatcher.php | 4 +++- .../Laravel/src/Watchers/RedisCommand/RedisCommandWatcher.php | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Instrumentation/Laravel/src/Watchers/QueryWatcher.php b/src/Instrumentation/Laravel/src/Watchers/QueryWatcher.php index a237089ad..98d3385d4 100644 --- a/src/Instrumentation/Laravel/src/Watchers/QueryWatcher.php +++ b/src/Instrumentation/Laravel/src/Watchers/QueryWatcher.php @@ -27,8 +27,10 @@ public function register(Application $app): void /** * Record a query. + * + * @psalm-suppress UndefinedThisPropertyFetch + * @phan-suppress PhanDeprecatedClassConstant */ - /** @psalm-suppress UndefinedThisPropertyFetch */ public function recordQuery(QueryExecuted $query): void { $nowInNs = (int) (microtime(true) * 1E9); diff --git a/src/Instrumentation/Laravel/src/Watchers/RedisCommand/RedisCommandWatcher.php b/src/Instrumentation/Laravel/src/Watchers/RedisCommand/RedisCommandWatcher.php index 4a60f9ac0..984c74a67 100644 --- a/src/Instrumentation/Laravel/src/Watchers/RedisCommand/RedisCommandWatcher.php +++ b/src/Instrumentation/Laravel/src/Watchers/RedisCommand/RedisCommandWatcher.php @@ -37,8 +37,10 @@ public function register(Application $app): void /** * Record a Redis command. + * + * @psalm-suppress UndefinedThisPropertyFetch + * @phan-suppress PhanDeprecatedClassConstant */ - /** @psalm-suppress UndefinedThisPropertyFetch */ public function recordRedisCommand(CommandExecuted $event): void { $nowInNs = (int) (microtime(true) * 1E9); From c9a83fe5bfd71d2f5086d666fd2a061555e0fbd7 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Thu, 24 Oct 2024 23:55:13 +0100 Subject: [PATCH 30/40] Laravel: drop ext-opentelemetry hard requirement. --- src/Instrumentation/Laravel/composer.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Instrumentation/Laravel/composer.json b/src/Instrumentation/Laravel/composer.json index 3d9009b01..7c5d47817 100644 --- a/src/Instrumentation/Laravel/composer.json +++ b/src/Instrumentation/Laravel/composer.json @@ -11,7 +11,6 @@ "require": { "php": "^8.1", "ext-json": "*", - "ext-opentelemetry": "*", "laravel/framework": "^10.0 || ^11.0", "open-telemetry/api": "^1.1", "open-telemetry/sem-conv": "^1.24" @@ -43,6 +42,9 @@ "OpenTelemetry\\Tests\\Contrib\\Instrumentation\\Laravel\\": "tests/" } }, + "suggest": { + "ext-opentelemetry": "Required to provide auto-instrumentation hooks." + }, "extra": { "spi-config": { "autoload-files": true From a3cf950aa53db4240427eac0eced82791f6aff86 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Sat, 26 Oct 2024 23:24:13 +0100 Subject: [PATCH 31/40] Laravel: use TextMapPropagatorInterface from InstrumentationContext instead of Globals. --- src/Instrumentation/Laravel/composer.json | 2 +- .../Hooks/Illuminate/Contracts/Http/Kernel.php | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Instrumentation/Laravel/composer.json b/src/Instrumentation/Laravel/composer.json index 7c5d47817..2de1a66ab 100644 --- a/src/Instrumentation/Laravel/composer.json +++ b/src/Instrumentation/Laravel/composer.json @@ -12,7 +12,7 @@ "php": "^8.1", "ext-json": "*", "laravel/framework": "^10.0 || ^11.0", - "open-telemetry/api": "^1.1", + "open-telemetry/api": "^1.1.1", "open-telemetry/sem-conv": "^1.24" }, "require-dev": { diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php index 1a00ce385..a1a52cc24 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php @@ -7,7 +7,6 @@ use Illuminate\Contracts\Http\Kernel as KernelContract; use Illuminate\Http\Request; use Illuminate\Routing\Route; -use OpenTelemetry\API\Globals; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context as InstrumentationContext; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; use OpenTelemetry\API\Trace\Span; @@ -16,6 +15,7 @@ use OpenTelemetry\API\Trace\StatusCode; use OpenTelemetry\API\Trace\TracerInterface; use OpenTelemetry\Context\Context; +use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; @@ -40,15 +40,18 @@ public function instrument( schemaUrl: Version::VERSION_1_24_0->url(), ); - $this->hookHandle($hookManager, $tracer); + $this->hookHandle($hookManager, $tracer, $context->propagator); } - protected function hookHandle(HookManagerInterface $hookManager, TracerInterface $tracer): void - { + protected function hookHandle( + HookManagerInterface $hookManager, + TracerInterface $tracer, + TextMapPropagatorInterface $propagator, + ): void { $hookManager->hook( KernelContract::class, 'handle', - preHook: function (KernelContract $kernel, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { + preHook: function (KernelContract $kernel, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer, $propagator) { $request = ($params[0] instanceof Request) ? $params[0] : null; /** @psalm-suppress ArgumentTypeCoercion */ $builder = $tracer @@ -61,7 +64,7 @@ protected function hookHandle(HookManagerInterface $hookManager, TracerInterface $parent = Context::getCurrent(); if ($request) { /** @phan-suppress-next-line PhanAccessMethodInternal */ - $parent = Globals::propagator()->extract($request, HeadersPropagator::instance()); + $parent = $propagator->extract($request, HeadersPropagator::instance()); $span = $builder ->setParent($parent) ->setAttribute(TraceAttributes::URL_FULL, $request->fullUrl()) From f18ab42f7d1526d57b36cf6e87486247925e084d Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Mon, 28 Oct 2024 22:29:40 +0000 Subject: [PATCH 32/40] Laravel: update LaravelConfiguration enabled logic with default config. --- src/Instrumentation/Laravel/src/LaravelConfiguration.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Instrumentation/Laravel/src/LaravelConfiguration.php b/src/Instrumentation/Laravel/src/LaravelConfiguration.php index dd331859e..48e5acf43 100644 --- a/src/Instrumentation/Laravel/src/LaravelConfiguration.php +++ b/src/Instrumentation/Laravel/src/LaravelConfiguration.php @@ -17,12 +17,15 @@ private function __construct( public static function fromArray(array $properties): self { return new self( - enabled: (class_exists(Sdk::class) && !Sdk::isInstrumentationDisabled('laravel')) || $properties['enabled'], + enabled: $properties['enabled'], ); } public static function default(): self { - return self::fromArray([]); + return self::fromArray([ + // Enabled by default if the OpenTelemetry SDK is not present. If it is, check whether disabled explicitly. + 'enabled' => !class_exists(Sdk::class) || !Sdk::isInstrumentationDisabled('laravel'), + ]); } } From b0d6da3c84c720cfcb0e498f49eaa430a739854c Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Wed, 30 Oct 2024 22:15:18 +0000 Subject: [PATCH 33/40] Laravel: added suggested propagators to composer.json. --- src/Instrumentation/Laravel/composer.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Instrumentation/Laravel/composer.json b/src/Instrumentation/Laravel/composer.json index 2de1a66ab..97c259961 100644 --- a/src/Instrumentation/Laravel/composer.json +++ b/src/Instrumentation/Laravel/composer.json @@ -43,7 +43,9 @@ } }, "suggest": { - "ext-opentelemetry": "Required to provide auto-instrumentation hooks." + "ext-opentelemetry": "Required to provide auto-instrumentation hooks.", + "open-telemetry/opentelemetry-propagation-server-timing": "Automatically propagate the context to the client through server-timing headers.", + "open-telemetry/opentelemetry-propagation-traceresponse": "Automatically propagate the context to the client through trace-response headers." }, "extra": { "spi-config": { From fee0f0145ee8a187151adb033f88d975d7773911 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Wed, 17 Sep 2025 20:28:56 +0100 Subject: [PATCH 34/40] Laravel: updated semconv attributes --- .../src/Hooks/Illuminate/Console/Command.php | 8 +- .../Illuminate/Contracts/Console/Kernel.php | 8 +- .../Illuminate/Contracts/Http/Kernel.php | 47 +++--- .../Illuminate/Contracts/Queue/Queue.php | 26 +-- .../Illuminate/Database/Eloquent/Model.php | 159 +++++++++--------- .../Illuminate/Queue/AttributesBuilder.php | 21 ++- .../src/Hooks/Illuminate/Queue/SyncQueue.php | 8 +- .../src/Hooks/Illuminate/Queue/Worker.php | 11 +- 8 files changed, 148 insertions(+), 140 deletions(-) diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php index 8c8462891..8bd7c4dde 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php @@ -13,7 +13,7 @@ use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; -use OpenTelemetry\SemConv\TraceAttributes; +use OpenTelemetry\SemConv\Attributes\CodeAttributes; use OpenTelemetry\SemConv\Version; use Throwable; @@ -44,9 +44,9 @@ protected function hookExecute(HookManagerInterface $hookManager, TracerInterfac /** @psalm-suppress ArgumentTypeCoercion */ $builder = $tracer ->spanBuilder(sprintf('Command %s', $command->getName() ?: 'unknown')) - ->setAttribute(TraceAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) - ->setAttribute(TraceAttributes::CODE_FILE_PATH, $filename) - ->setAttribute(TraceAttributes::CODE_LINE_NUMBER, $lineno); + ->setAttribute(CodeAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) + ->setAttribute(CodeAttributes::CODE_FILE_PATH, $filename) + ->setAttribute(CodeAttributes::CODE_LINE_NUMBER, $lineno); $parent = Context::getCurrent(); $span = $builder->startSpan(); diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php index 204917ecf..950f86223 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php @@ -17,7 +17,7 @@ use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Queue\AttributesBuilder; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; -use OpenTelemetry\SemConv\TraceAttributes; +use OpenTelemetry\SemConv\Attributes\CodeAttributes; use OpenTelemetry\SemConv\Version; use Throwable; @@ -52,9 +52,9 @@ private function hookHandle(HookManagerInterface $hookManager, TracerInterface $ $builder = $tracer ->spanBuilder('Artisan handler') ->setSpanKind(SpanKind::KIND_PRODUCER) - ->setAttribute(TraceAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) - ->setAttribute(TraceAttributes::CODE_FILE_PATH, $filename) - ->setAttribute(TraceAttributes::CODE_LINE_NUMBER, $lineno); + ->setAttribute(CodeAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) + ->setAttribute(CodeAttributes::CODE_FILE_PATH, $filename) + ->setAttribute(CodeAttributes::CODE_LINE_NUMBER, $lineno); $parent = Context::getCurrent(); $span = $builder->startSpan(); diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php index 834e02bee..ae2d62b3c 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php @@ -21,7 +21,14 @@ use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; use OpenTelemetry\Contrib\Instrumentation\Laravel\Propagators\HeadersPropagator; use OpenTelemetry\Contrib\Instrumentation\Laravel\Propagators\ResponsePropagationSetter; -use OpenTelemetry\SemConv\TraceAttributes; +use OpenTelemetry\SemConv\Attributes\ClientAttributes; +use OpenTelemetry\SemConv\Attributes\CodeAttributes; +use OpenTelemetry\SemConv\Attributes\HttpAttributes; +use OpenTelemetry\SemConv\Attributes\NetworkAttributes; +use OpenTelemetry\SemConv\Attributes\ServerAttributes; +use OpenTelemetry\SemConv\Attributes\UrlAttributes; +use OpenTelemetry\SemConv\Attributes\UserAgentAttributes; +use OpenTelemetry\SemConv\Incubating\Attributes\HttpIncubatingAttributes; use OpenTelemetry\SemConv\Version; use Symfony\Component\HttpFoundation\Response; use Throwable; @@ -58,27 +65,27 @@ protected function hookHandle( $builder = $tracer ->spanBuilder(sprintf('%s', $request?->method() ?? 'unknown')) ->setSpanKind(SpanKind::KIND_SERVER) - ->setAttribute(TraceAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) - ->setAttribute(TraceAttributes::CODE_FILE_PATH, $filename) - ->setAttribute(TraceAttributes::CODE_LINE_NUMBER, $lineno); + ->setAttribute(CodeAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) + ->setAttribute(CodeAttributes::CODE_FILE_PATH, $filename) + ->setAttribute(CodeAttributes::CODE_LINE_NUMBER, $lineno); $parent = Context::getCurrent(); if ($request) { /** @phan-suppress-next-line PhanAccessMethodInternal */ $parent = $propagator->extract($request, HeadersPropagator::instance()); $span = $builder ->setParent($parent) - ->setAttribute(TraceAttributes::URL_FULL, $request->fullUrl()) - ->setAttribute(TraceAttributes::HTTP_REQUEST_METHOD, $request->method()) - ->setAttribute(TraceAttributes::HTTP_REQUEST_BODY_SIZE, $request->header('Content-Length')) - ->setAttribute(TraceAttributes::URL_SCHEME, $request->getScheme()) - ->setAttribute(TraceAttributes::NETWORK_PROTOCOL_VERSION, $request->getProtocolVersion()) - ->setAttribute(TraceAttributes::NETWORK_PEER_ADDRESS, $request->server('REMOTE_ADDR')) - ->setAttribute(TraceAttributes::URL_PATH, $this->httpTarget($request)) - ->setAttribute(TraceAttributes::SERVER_ADDRESS, $this->httpHostName($request)) - ->setAttribute(TraceAttributes::SERVER_PORT, $request->getPort()) - ->setAttribute(TraceAttributes::CLIENT_PORT, $request->server('REMOTE_PORT')) - ->setAttribute(TraceAttributes::CLIENT_ADDRESS, $request->ip()) - ->setAttribute(TraceAttributes::USER_AGENT_ORIGINAL, $request->userAgent()) + ->setAttribute(UrlAttributes::URL_FULL, $request->fullUrl()) + ->setAttribute(HttpAttributes::HTTP_REQUEST_METHOD, $request->method()) + ->setAttribute(HttpIncubatingAttributes::HTTP_REQUEST_BODY_SIZE, $request->header('Content-Length')) + ->setAttribute(UrlAttributes::URL_SCHEME, $request->getScheme()) + ->setAttribute(NetworkAttributes::NETWORK_PROTOCOL_VERSION, $request->getProtocolVersion()) + ->setAttribute(NetworkAttributes::NETWORK_PEER_ADDRESS, $request->server('REMOTE_ADDR')) + ->setAttribute(UrlAttributes::URL_PATH, $this->httpTarget($request)) + ->setAttribute(ServerAttributes::SERVER_ADDRESS, $this->httpHostName($request)) + ->setAttribute(ServerAttributes::SERVER_PORT, $request->getPort()) + ->setAttribute(ClientAttributes::CLIENT_PORT, $request->server('REMOTE_PORT')) + ->setAttribute(ClientAttributes::CLIENT_ADDRESS, $request->ip()) + ->setAttribute(UserAgentAttributes::USER_AGENT_ORIGINAL, $request->userAgent()) ->startSpan(); $request->attributes->set(SpanInterface::class, $span); } else { @@ -100,16 +107,16 @@ protected function hookHandle( if ($request && $route instanceof Route) { $span->updateName("{$request->method()} /" . ltrim($route->uri, '/')); - $span->setAttribute(TraceAttributes::HTTP_ROUTE, $route->uri); + $span->setAttribute(HttpAttributes::HTTP_ROUTE, $route->uri); } if ($response) { if ($response->getStatusCode() >= 500) { $span->setStatus(StatusCode::STATUS_ERROR); } - $span->setAttribute(TraceAttributes::HTTP_RESPONSE_STATUS_CODE, $response->getStatusCode()); - $span->setAttribute(TraceAttributes::NETWORK_PROTOCOL_VERSION, $response->getProtocolVersion()); - $span->setAttribute(TraceAttributes::HTTP_RESPONSE_BODY_SIZE, $response->headers->get('Content-Length')); + $span->setAttribute(HttpAttributes::HTTP_RESPONSE_STATUS_CODE, $response->getStatusCode()); + $span->setAttribute(NetworkAttributes::NETWORK_PROTOCOL_VERSION, $response->getProtocolVersion()); + $span->setAttribute(HttpIncubatingAttributes::HTTP_RESPONSE_BODY_SIZE, $response->headers->get('Content-Length')); // Propagate server-timing header to response, if ServerTimingPropagator is present if (class_exists('OpenTelemetry\Contrib\Propagation\ServerTiming\ServerTimingPropagator')) { diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php index 544635453..e9363b4b8 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php @@ -16,8 +16,8 @@ use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Queue\AttributesBuilder; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; -use OpenTelemetry\SemConv\TraceAttributes; -use OpenTelemetry\SemConv\TraceAttributeValues; +use OpenTelemetry\SemConv\Attributes\CodeAttributes; +use OpenTelemetry\SemConv\Incubating\Attributes\MessagingIncubatingAttributes; use OpenTelemetry\SemConv\Version; use Throwable; @@ -49,16 +49,16 @@ protected function hookBulk(HookManagerInterface $hookManager, TracerInterface $ 'bulk', preHook: function (QueueContract $queue, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { $attributes = array_merge([ - TraceAttributes::CODE_FUNCTION_NAME => sprintf('%s::%s', $class, $function), - TraceAttributes::CODE_FILE_PATH => $filename, - TraceAttributes::CODE_LINE_NUMBER => $lineno, - TraceAttributes::MESSAGING_BATCH_MESSAGE_COUNT => count($params[0] ?? []), + CodeAttributes::CODE_FUNCTION_NAME => sprintf('%s::%s', $class, $function), + CodeAttributes::CODE_FILE_PATH => $filename, + CodeAttributes::CODE_LINE_NUMBER => $lineno, + MessagingIncubatingAttributes::MESSAGING_BATCH_MESSAGE_COUNT => count($params[0] ?? []), ], $this->contextualMessageSystemAttributes($queue, [])); /** @psalm-suppress ArgumentTypeCoercion */ $span = $tracer ->spanBuilder(vsprintf('%s %s', [ - TraceAttributeValues::MESSAGING_OPERATION_TYPE_SEND, + MessagingIncubatingAttributes::MESSAGING_OPERATION_TYPE_VALUE_SEND, /** @phan-suppress-next-line PhanUndeclaredMethod */ method_exists($queue, 'getQueue') ? $queue->getQueue($params[2] ?? null) : $queue->getConnectionName(), ])) @@ -91,16 +91,16 @@ protected function hookLater(HookManagerInterface $hookManager, TracerInterface }; $attributes = [ - TraceAttributes::CODE_FUNCTION_NAME => sprintf('%s::%s', $class, $function), - TraceAttributes::CODE_FILE_PATH => $filename, - TraceAttributes::CODE_LINE_NUMBER => $lineno, + CodeAttributes::CODE_FUNCTION_NAME => sprintf('%s::%s', $class, $function), + CodeAttributes::CODE_FILE_PATH => $filename, + CodeAttributes::CODE_LINE_NUMBER => $lineno, 'messaging.message.delivery_timestamp' => $estimateDeliveryTimestamp, ]; /** @psalm-suppress ArgumentTypeCoercion */ $span = $tracer ->spanBuilder(vsprintf('%s %s', [ - TraceAttributeValues::MESSAGING_OPERATION_TYPE_CREATE, + MessagingIncubatingAttributes::MESSAGING_OPERATION_TYPE_VALUE_CREATE, /** @phan-suppress-next-line PhanUndeclaredMethod */ method_exists($queue, 'getQueue') ? $queue->getQueue($params[2] ?? null) : $queue->getConnectionName(), ])) @@ -132,8 +132,8 @@ protected function hookPushRaw(HookManagerInterface $hookManager, TracerInterfac /** @psalm-suppress ArgumentTypeCoercion */ $span = $tracer ->spanBuilder(vsprintf('%s %s', [ - TraceAttributeValues::MESSAGING_OPERATION_TYPE_CREATE, - $attributes[TraceAttributes::MESSAGING_DESTINATION_NAME], + MessagingIncubatingAttributes::MESSAGING_OPERATION_TYPE_VALUE_CREATE, + $attributes[MessagingIncubatingAttributes::MESSAGING_DESTINATION_NAME], ])) ->setSpanKind(SpanKind::KIND_PRODUCER) ->setAttributes($attributes) diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Database/Eloquent/Model.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Database/Eloquent/Model.php index 64151b426..813068640 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Database/Eloquent/Model.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Database/Eloquent/Model.php @@ -5,46 +5,55 @@ namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Database\Eloquent; use Illuminate\Database\Eloquent\Model as EloquentModel; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context as InstrumentationContext; +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; use OpenTelemetry\API\Trace\SpanKind; +use OpenTelemetry\API\Trace\TracerInterface; use OpenTelemetry\Context\Context; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHook; -use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHookTrait; +use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; -use function OpenTelemetry\Instrumentation\hook; -use OpenTelemetry\SemConv\TraceAttributes; +use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; +use OpenTelemetry\SemConv\Attributes\CodeAttributes; +use OpenTelemetry\SemConv\Version; use Throwable; -class Model implements LaravelHook +class Model implements Hook { - use LaravelHookTrait; use PostHookTrait; - public function instrument(): void - { - $this->hookFind(); - $this->hookPerformInsert(); - $this->hookPerformUpdate(); - $this->hookDelete(); - $this->hookGetModels(); - $this->hookDestroy(); - $this->hookRefresh(); + public function instrument( + LaravelInstrumentation $instrumentation, + HookManagerInterface $hookManager, + InstrumentationContext $context, + ): void { + $tracer = $context->tracerProvider->getTracer( + $instrumentation->buildProviderName('database', 'eloquent', 'model'), + schemaUrl: Version::VERSION_1_24_0->url(), + ); + + $this->hookFind($hookManager, $tracer); + $this->hookPerformInsert($hookManager, $tracer); + $this->hookPerformUpdate($hookManager, $tracer); + $this->hookDelete($hookManager, $tracer); + $this->hookGetModels($hookManager, $tracer); + $this->hookDestroy($hookManager, $tracer); + $this->hookRefresh($hookManager, $tracer); } - private function hookFind(): void + private function hookFind(HookManagerInterface $hookManager, TracerInterface $tracer): void { /** @psalm-suppress UnusedFunctionCall */ - hook( + $hookManager->hook( \Illuminate\Database\Eloquent\Builder::class, 'find', - pre: function ($builder, array $params, string $class, string $function, ?string $filename, ?int $lineno) { + preHook: function ($builder, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { $model = $builder->getModel(); - $builder = $this->instrumentation - ->tracer() + $builder = $tracer ->spanBuilder($model::class . '::find') ->setSpanKind(SpanKind::KIND_INTERNAL) - ->setAttribute(TraceAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) - ->setAttribute(TraceAttributes::CODE_FILE_PATH, $filename) - ->setAttribute(TraceAttributes::CODE_LINE_NUMBER, $lineno) + ->setAttribute(CodeAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) + ->setAttribute(CodeAttributes::CODE_FILE_PATH, $filename) + ->setAttribute(CodeAttributes::CODE_LINE_NUMBER, $lineno) ->setAttribute('laravel.eloquent.model', $model::class) ->setAttribute('laravel.eloquent.table', $model->getTable()) ->setAttribute('laravel.eloquent.operation', 'find'); @@ -55,26 +64,25 @@ private function hookFind(): void return $params; }, - post: function ($builder, array $params, $result, ?Throwable $exception) { + postHook: function ($builder, array $params, $result, ?Throwable $exception) { $this->endSpan($exception); } ); } - private function hookPerformUpdate(): void + private function hookPerformUpdate(HookManagerInterface $hookManager, TracerInterface $tracer): void { /** @psalm-suppress UnusedFunctionCall */ - hook( + $hookManager->hook( EloquentModel::class, 'performUpdate', - pre: function (EloquentModel $model, array $params, string $class, string $function, ?string $filename, ?int $lineno) { - $builder = $this->instrumentation - ->tracer() + preHook: function (EloquentModel $model, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { + $builder = $tracer ->spanBuilder($model::class . '::update') ->setSpanKind(SpanKind::KIND_INTERNAL) - ->setAttribute(TraceAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) - ->setAttribute(TraceAttributes::CODE_FILE_PATH, $filename) - ->setAttribute(TraceAttributes::CODE_LINE_NUMBER, $lineno) + ->setAttribute(CodeAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) + ->setAttribute(CodeAttributes::CODE_FILE_PATH, $filename) + ->setAttribute(CodeAttributes::CODE_LINE_NUMBER, $lineno) ->setAttribute('laravel.eloquent.model', $model::class) ->setAttribute('laravel.eloquent.table', $model->getTable()) ->setAttribute('laravel.eloquent.operation', 'update'); @@ -85,26 +93,25 @@ private function hookPerformUpdate(): void return $params; }, - post: function (EloquentModel $model, array $params, $result, ?Throwable $exception) { + postHook: function (EloquentModel $model, array $params, $result, ?Throwable $exception) { $this->endSpan($exception); } ); } - private function hookPerformInsert(): void + private function hookPerformInsert(HookManagerInterface $hookManager, TracerInterface $tracer): void { /** @psalm-suppress UnusedFunctionCall */ - hook( + $hookManager->hook( EloquentModel::class, 'performInsert', - pre: function (EloquentModel $model, array $params, string $class, string $function, ?string $filename, ?int $lineno) { - $builder = $this->instrumentation - ->tracer() + preHook: function (EloquentModel $model, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { + $builder = $tracer ->spanBuilder($model::class . '::create') ->setSpanKind(SpanKind::KIND_INTERNAL) - ->setAttribute(TraceAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) - ->setAttribute(TraceAttributes::CODE_FILE_PATH, $filename) - ->setAttribute(TraceAttributes::CODE_LINE_NUMBER, $lineno) + ->setAttribute(CodeAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) + ->setAttribute(CodeAttributes::CODE_FILE_PATH, $filename) + ->setAttribute(CodeAttributes::CODE_LINE_NUMBER, $lineno) ->setAttribute('laravel.eloquent.model', $model::class) ->setAttribute('laravel.eloquent.table', $model->getTable()) ->setAttribute('laravel.eloquent.operation', 'create'); @@ -115,26 +122,25 @@ private function hookPerformInsert(): void return $params; }, - post: function (EloquentModel $model, array $params, $result, ?Throwable $exception) { + postHook: function (EloquentModel $model, array $params, $result, ?Throwable $exception) { $this->endSpan($exception); } ); } - private function hookDelete(): void + private function hookDelete(HookManagerInterface $hookManager, TracerInterface $tracer): void { /** @psalm-suppress UnusedFunctionCall */ - hook( + $hookManager->hook( EloquentModel::class, 'delete', - pre: function (EloquentModel $model, array $params, string $class, string $function, ?string $filename, ?int $lineno) { - $builder = $this->instrumentation - ->tracer() + preHook: function (EloquentModel $model, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { + $builder = $tracer ->spanBuilder($model::class . '::delete') ->setSpanKind(SpanKind::KIND_INTERNAL) - ->setAttribute(TraceAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) - ->setAttribute(TraceAttributes::CODE_FILE_PATH, $filename) - ->setAttribute(TraceAttributes::CODE_LINE_NUMBER, $lineno) + ->setAttribute(CodeAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) + ->setAttribute(CodeAttributes::CODE_FILE_PATH, $filename) + ->setAttribute(CodeAttributes::CODE_LINE_NUMBER, $lineno) ->setAttribute('laravel.eloquent.model', $model::class) ->setAttribute('laravel.eloquent.table', $model->getTable()) ->setAttribute('laravel.eloquent.operation', 'delete'); @@ -145,27 +151,26 @@ private function hookDelete(): void return $params; }, - post: function (EloquentModel $model, array $params, $result, ?Throwable $exception) { + postHook: function (EloquentModel $model, array $params, $result, ?Throwable $exception) { $this->endSpan($exception); } ); } - private function hookGetModels(): void + private function hookGetModels(HookManagerInterface $hookManager, TracerInterface $tracer): void { /** @psalm-suppress UnusedFunctionCall */ - hook( + $hookManager->hook( \Illuminate\Database\Eloquent\Builder::class, 'getModels', - pre: function ($builder, array $params, string $class, string $function, ?string $filename, ?int $lineno) { + preHook: function ($builder, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { $model = $builder->getModel(); - $builder = $this->instrumentation - ->tracer() + $builder = $tracer ->spanBuilder($model::class . '::get') ->setSpanKind(SpanKind::KIND_INTERNAL) - ->setAttribute(TraceAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) - ->setAttribute(TraceAttributes::CODE_FILE_PATH, $filename) - ->setAttribute(TraceAttributes::CODE_LINE_NUMBER, $lineno) + ->setAttribute(CodeAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) + ->setAttribute(CodeAttributes::CODE_FILE_PATH, $filename) + ->setAttribute(CodeAttributes::CODE_LINE_NUMBER, $lineno) ->setAttribute('laravel.eloquent.model', $model::class) ->setAttribute('laravel.eloquent.table', $model->getTable()) ->setAttribute('laravel.eloquent.operation', 'get') @@ -177,30 +182,29 @@ private function hookGetModels(): void return $params; }, - post: function ($builder, array $params, $result, ?Throwable $exception) { + postHook: function ($builder, array $params, $result, ?Throwable $exception) { $this->endSpan($exception); } ); } - private function hookDestroy(): void + private function hookDestroy(HookManagerInterface $hookManager, TracerInterface $tracer): void { /** @psalm-suppress UnusedFunctionCall */ - hook( + $hookManager->hook( EloquentModel::class, 'destroy', - pre: function (string $modelClassName, array $params, string $class, string $function, ?string $filename, ?int $lineno) { + preHook: function (string $modelClassName, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { // The class-string is passed to the $model argument, because \Illuminate\Database\Eloquent\Model::destroy is static method. // Therefore, create a class instance from a class-string, and then get the table name from the getTable function. $model = new $modelClassName(); - $builder = $this->instrumentation - ->tracer() + $builder = $tracer ->spanBuilder($model::class . '::destroy') ->setSpanKind(SpanKind::KIND_INTERNAL) - ->setAttribute(TraceAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) - ->setAttribute(TraceAttributes::CODE_FILE_PATH, $filename) - ->setAttribute(TraceAttributes::CODE_LINE_NUMBER, $lineno) + ->setAttribute(CodeAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) + ->setAttribute(CodeAttributes::CODE_FILE_PATH, $filename) + ->setAttribute(CodeAttributes::CODE_LINE_NUMBER, $lineno) ->setAttribute('laravel.eloquent.model', $model::class) ->setAttribute('laravel.eloquent.table', $model->getTable()) ->setAttribute('laravel.eloquent.operation', 'destroy'); @@ -211,26 +215,25 @@ private function hookDestroy(): void return $params; }, - post: function ($model, array $params, $result, ?Throwable $exception) { + postHook: function ($model, array $params, $result, ?Throwable $exception) { $this->endSpan($exception); } ); } - private function hookRefresh(): void + private function hookRefresh(HookManagerInterface $hookManager, TracerInterface $tracer): void { /** @psalm-suppress UnusedFunctionCall */ - hook( + $hookManager->hook( EloquentModel::class, 'refresh', - pre: function (EloquentModel $model, array $params, string $class, string $function, ?string $filename, ?int $lineno) { - $builder = $this->instrumentation - ->tracer() + preHook: function (EloquentModel $model, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($tracer) { + $builder = $tracer ->spanBuilder($model::class . '::refresh') ->setSpanKind(SpanKind::KIND_INTERNAL) - ->setAttribute(TraceAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) - ->setAttribute(TraceAttributes::CODE_FILE_PATH, $filename) - ->setAttribute(TraceAttributes::CODE_LINE_NUMBER, $lineno) + ->setAttribute(CodeAttributes::CODE_FUNCTION_NAME, sprintf('%s::%s', $class, $function)) + ->setAttribute(CodeAttributes::CODE_FILE_PATH, $filename) + ->setAttribute(CodeAttributes::CODE_LINE_NUMBER, $lineno) ->setAttribute('laravel.eloquent.model', $model::class) ->setAttribute('laravel.eloquent.table', $model->getTable()) ->setAttribute('laravel.eloquent.operation', 'refresh'); @@ -241,7 +244,7 @@ private function hookRefresh(): void return $params; }, - post: function (EloquentModel $model, array $params, $result, ?Throwable $exception) { + postHook: function (EloquentModel $model, array $params, $result, ?Throwable $exception) { $this->endSpan($exception); } ); diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/AttributesBuilder.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/AttributesBuilder.php index 766179f39..6314f068f 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/AttributesBuilder.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/AttributesBuilder.php @@ -8,8 +8,7 @@ use Illuminate\Queue\BeanstalkdQueue; use Illuminate\Queue\RedisQueue; use Illuminate\Queue\SqsQueue; -use OpenTelemetry\SemConv\TraceAttributes; -use OpenTelemetry\SemConv\TraceAttributeValues; +use OpenTelemetry\SemConv\Incubating\Attributes\MessagingIncubatingAttributes; trait AttributesBuilder { @@ -23,9 +22,9 @@ private function buildMessageAttributes( $payload = json_decode($rawPayload, true) ?? []; return array_merge([ - TraceAttributes::MESSAGING_DESTINATION_NAME => '(anonymous)', - TraceAttributes::MESSAGING_MESSAGE_ID => $payload['uuid'] ?? $payload['id'] ?? null, - TraceAttributes::MESSAGING_MESSAGE_ENVELOPE_SIZE => strlen($rawPayload), + MessagingIncubatingAttributes::MESSAGING_DESTINATION_NAME => '(anonymous)', + MessagingIncubatingAttributes::MESSAGING_MESSAGE_ID => $payload['uuid'] ?? $payload['id'] ?? null, + MessagingIncubatingAttributes::MESSAGING_MESSAGE_ENVELOPE_SIZE => strlen($rawPayload), 'messaging.message.job_name' => $payload['displayName'] ?? $payload['job'] ?? null, 'messaging.message.attempts' => $payload['attempts'] ?? 0, 'messaging.message.max_exceptions' => $payload['maxExceptions'] ?? null, @@ -53,24 +52,24 @@ private function contextualMessageSystemAttributes( private function beanstalkContextualAttributes(BeanstalkdQueue $queue, array $_payload, ?string $queueName = null, array $_options = [], mixed ...$_params): array { return [ - TraceAttributes::MESSAGING_SYSTEM => 'beanstalk', - TraceAttributes::MESSAGING_DESTINATION_NAME => $queue->getQueue($queueName), + MessagingIncubatingAttributes::MESSAGING_SYSTEM => 'beanstalk', + MessagingIncubatingAttributes::MESSAGING_DESTINATION_NAME => $queue->getQueue($queueName), ]; } private function redisContextualAttributes(RedisQueue $queue, array $_payload, ?string $queueName = null, array $_options = [], mixed ...$_params): array { return [ - TraceAttributes::MESSAGING_SYSTEM => 'redis', - TraceAttributes::MESSAGING_DESTINATION_NAME => $queue->getQueue($queueName), + MessagingIncubatingAttributes::MESSAGING_SYSTEM => 'redis', + MessagingIncubatingAttributes::MESSAGING_DESTINATION_NAME => $queue->getQueue($queueName), ]; } private function awsSqsContextualAttributes(SqsQueue $queue, array $_payload, ?string $queueName = null, array $_options = [], mixed ...$_params): array { return [ - TraceAttributes::MESSAGING_SYSTEM => TraceAttributeValues::MESSAGING_SYSTEM_AWS_SQS, - TraceAttributes::MESSAGING_DESTINATION_NAME => $queue->getQueue($queueName), + MessagingIncubatingAttributes::MESSAGING_SYSTEM => MessagingIncubatingAttributes::MESSAGING_SYSTEM_VALUE_AWS_SQS, + MessagingIncubatingAttributes::MESSAGING_DESTINATION_NAME => $queue->getQueue($queueName), ]; } } diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php index 3412c1a1e..dc5d30314 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php @@ -13,7 +13,7 @@ use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; -use OpenTelemetry\SemConv\TraceAttributes; +use OpenTelemetry\SemConv\Attributes\CodeAttributes; use OpenTelemetry\SemConv\Version; use Throwable; @@ -50,9 +50,9 @@ protected function hookPush(HookManagerInterface $hookManager, TracerInterface $ ])) ->setSpanKind(SpanKind::KIND_INTERNAL) ->setAttributes([ - TraceAttributes::CODE_FUNCTION_NAME => sprintf('%s::%s', $class, $function), - TraceAttributes::CODE_FILE_PATH => $filename, - TraceAttributes::CODE_LINE_NUMBER => $lineno, + CodeAttributes::CODE_FUNCTION_NAME => sprintf('%s::%s', $class, $function), + CodeAttributes::CODE_FILE_PATH => $filename, + CodeAttributes::CODE_LINE_NUMBER => $lineno, ]) ->startSpan(); diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php index 14ddb4d36..c997b0510 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php @@ -16,8 +16,7 @@ use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Hook; use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait; use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; -use OpenTelemetry\SemConv\TraceAttributes; -use OpenTelemetry\SemConv\TraceAttributeValues; +use OpenTelemetry\SemConv\Incubating\Attributes\MessagingIncubatingAttributes; use OpenTelemetry\SemConv\Version; use Throwable; @@ -61,8 +60,8 @@ private function hookWorkerProcess(HookManagerInterface $hookManager, TracerInte /** @psalm-suppress ArgumentTypeCoercion */ $span = $tracer ->spanBuilder(vsprintf('%s %s', [ - TraceAttributeValues::MESSAGING_OPERATION_TYPE_PROCESS, - $attributes[TraceAttributes::MESSAGING_DESTINATION_NAME], + MessagingIncubatingAttributes::MESSAGING_OPERATION_TYPE_VALUE_PROCESS, + $attributes[MessagingIncubatingAttributes::MESSAGING_DESTINATION_NAME], ])) ->setSpanKind(SpanKind::KIND_CONSUMER) ->setParent($parent) @@ -108,8 +107,8 @@ private function hookWorkerGetNextJob(HookManagerInterface $hookManager, TracerI /** @psalm-suppress ArgumentTypeCoercion */ $span = $tracer ->spanBuilder(vsprintf('%s %s', [ - TraceAttributeValues::MESSAGING_OPERATION_TYPE_RECEIVE, - $attributes[TraceAttributes::MESSAGING_DESTINATION_NAME], + MessagingIncubatingAttributes::MESSAGING_OPERATION_TYPE_VALUE_RECEIVE, + $attributes[MessagingIncubatingAttributes::MESSAGING_DESTINATION_NAME], ])) ->setSpanKind(SpanKind::KIND_CONSUMER) ->setAttributes($attributes) From 1513e1b0551a4d3b6259a4e2b81afa99c49ed312 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Wed, 17 Sep 2025 20:35:37 +0100 Subject: [PATCH 35/40] Laravel: _register ordering --- src/Instrumentation/Laravel/_register.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Instrumentation/Laravel/_register.php b/src/Instrumentation/Laravel/_register.php index a75797b28..dd477037c 100644 --- a/src/Instrumentation/Laravel/_register.php +++ b/src/Instrumentation/Laravel/_register.php @@ -21,8 +21,8 @@ ServiceLoader::register(Hooks\Hook::class, Hooks\Illuminate\Database\Eloquent\Model::class); -ServiceLoader::register(Hooks\Hook::class, Hooks\Illuminate\Foundation\Console\ServeCommand::class); ServiceLoader::register(Hooks\Hook::class, Hooks\Illuminate\Foundation\Application::class); +ServiceLoader::register(Hooks\Hook::class, Hooks\Illuminate\Foundation\Console\ServeCommand::class); ServiceLoader::register(Hooks\Hook::class, Hooks\Illuminate\Queue\Queue::class); ServiceLoader::register(Hooks\Hook::class, Hooks\Illuminate\Queue\SyncQueue::class); From d65a213c667b95d98909369c66b4c3f885927870 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Wed, 17 Sep 2025 20:40:11 +0100 Subject: [PATCH 36/40] Laravel: updated ComponentProviders to fulfil most recent interface --- .../ComponentProvider/LaravelComponentProvider.php | 14 +++++++------- .../LogRecordExporterInMemory.php | 11 ++++++----- .../ComponentProvider/SpanExporterInMemory.php | 11 ++++++----- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/Instrumentation/Laravel/src/ComponentProvider/LaravelComponentProvider.php b/src/Instrumentation/Laravel/src/ComponentProvider/LaravelComponentProvider.php index 311b6fe3d..3d59b3368 100644 --- a/src/Instrumentation/Laravel/src/ComponentProvider/LaravelComponentProvider.php +++ b/src/Instrumentation/Laravel/src/ComponentProvider/LaravelComponentProvider.php @@ -4,12 +4,13 @@ namespace OpenTelemetry\Contrib\Instrumentation\Laravel\ComponentProvider; +use OpenTelemetry\API\Configuration\Config\ComponentProvider; +use OpenTelemetry\API\Configuration\Config\ComponentProviderRegistry; +use OpenTelemetry\API\Configuration\Context; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\InstrumentationConfiguration; -use OpenTelemetry\Config\SDK\Configuration\ComponentProvider; -use OpenTelemetry\Config\SDK\Configuration\ComponentProviderRegistry; -use OpenTelemetry\Config\SDK\Configuration\Context; use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelConfiguration; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; +use Symfony\Component\Config\Definition\Builder\NodeBuilder; class LaravelComponentProvider implements ComponentProvider { @@ -21,11 +22,10 @@ public function createPlugin(array $properties, Context $context): Instrumentati return LaravelConfiguration::fromArray($properties); } - public function getConfig(ComponentProviderRegistry $registry): ArrayNodeDefinition + public function getConfig(ComponentProviderRegistry $registry, NodeBuilder $builder): ArrayNodeDefinition { - $root = new ArrayNodeDefinition('laravel'); - - return $root + return $builder + ->arrayNode('laravel') ->canBeDisabled() ->addDefaultsIfNotSet() ; diff --git a/src/Instrumentation/Laravel/tests/Fixtures/ComponentProvider/LogRecordExporterInMemory.php b/src/Instrumentation/Laravel/tests/Fixtures/ComponentProvider/LogRecordExporterInMemory.php index 21a41dda4..e9aff332b 100644 --- a/src/Instrumentation/Laravel/tests/Fixtures/ComponentProvider/LogRecordExporterInMemory.php +++ b/src/Instrumentation/Laravel/tests/Fixtures/ComponentProvider/LogRecordExporterInMemory.php @@ -4,13 +4,14 @@ namespace OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\ComponentProvider; -use OpenTelemetry\Config\SDK\Configuration\ComponentProvider; -use OpenTelemetry\Config\SDK\Configuration\ComponentProviderRegistry; -use OpenTelemetry\Config\SDK\Configuration\Context; +use OpenTelemetry\API\Configuration\Config\ComponentProvider; +use OpenTelemetry\API\Configuration\Config\ComponentProviderRegistry; +use OpenTelemetry\API\Configuration\Context; use OpenTelemetry\SDK\Logs\Exporter\InMemoryExporter; use OpenTelemetry\SDK\Logs\LogRecordExporterInterface; use OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\TestStorage; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; +use Symfony\Component\Config\Definition\Builder\NodeBuilder; final class LogRecordExporterInMemory implements ComponentProvider { @@ -19,8 +20,8 @@ public function createPlugin(array $properties, Context $context): LogRecordExpo return new InMemoryExporter(TestStorage::getInstance()); } - public function getConfig(ComponentProviderRegistry $registry): ArrayNodeDefinition + public function getConfig(ComponentProviderRegistry $registry, NodeBuilder $builder): ArrayNodeDefinition { - return new ArrayNodeDefinition('test/in_memory_exporter'); + return $builder->arrayNode('test/in_memory_exporter'); } } diff --git a/src/Instrumentation/Laravel/tests/Fixtures/ComponentProvider/SpanExporterInMemory.php b/src/Instrumentation/Laravel/tests/Fixtures/ComponentProvider/SpanExporterInMemory.php index 048fb25a3..818a8173a 100644 --- a/src/Instrumentation/Laravel/tests/Fixtures/ComponentProvider/SpanExporterInMemory.php +++ b/src/Instrumentation/Laravel/tests/Fixtures/ComponentProvider/SpanExporterInMemory.php @@ -4,13 +4,14 @@ namespace OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\ComponentProvider; -use OpenTelemetry\Config\SDK\Configuration\ComponentProvider; -use OpenTelemetry\Config\SDK\Configuration\ComponentProviderRegistry; -use OpenTelemetry\Config\SDK\Configuration\Context; +use OpenTelemetry\API\Configuration\Config\ComponentProvider; +use OpenTelemetry\API\Configuration\Config\ComponentProviderRegistry; +use OpenTelemetry\API\Configuration\Context; use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter; use OpenTelemetry\SDK\Trace\SpanExporterInterface; use OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\TestStorage; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; +use Symfony\Component\Config\Definition\Builder\NodeBuilder; final class SpanExporterInMemory implements ComponentProvider { @@ -19,8 +20,8 @@ public function createPlugin(array $properties, Context $context): SpanExporterI return new InMemoryExporter(TestStorage::getInstance()); } - public function getConfig(ComponentProviderRegistry $registry): ArrayNodeDefinition + public function getConfig(ComponentProviderRegistry $registry, NodeBuilder $builder): ArrayNodeDefinition { - return new ArrayNodeDefinition('test/in_memory_exporter'); + return $builder->arrayNode('test/in_memory_exporter'); } } From 1c655310d5bb28185896a7459324821753a021e6 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Sun, 5 Oct 2025 13:31:25 +0100 Subject: [PATCH 37/40] Laravel: composer updates and component provider updates --- src/Instrumentation/Laravel/composer.json | 8 ++++---- .../Laravel/tests/Fixtures/otel-instrumentation.yaml | 4 ++-- src/Instrumentation/Laravel/tests/bootstrap.php | 4 +++- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Instrumentation/Laravel/composer.json b/src/Instrumentation/Laravel/composer.json index 3b94c6864..e8535abed 100644 --- a/src/Instrumentation/Laravel/composer.json +++ b/src/Instrumentation/Laravel/composer.json @@ -12,14 +12,14 @@ "php": "^8.1", "ext-json": "*", "laravel/framework": "^10.0 || ^11.0 || ^12.0", - "open-telemetry/api": "^1.1.1", + "open-telemetry/api": "^1.4", "open-telemetry/sem-conv": "^1.32" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.50", "guzzlehttp/guzzle": "*", - "open-telemetry/sdk": "^1.1", - "open-telemetry/sdk-configuration": "^0.0.5", + "open-telemetry/sdk": "^1.4", + "open-telemetry/sdk-configuration": "^0.2.0", "orchestra/testbench": ">=7.41.3", "phan/phan": "^5.0", "php-http/mock-client": "*", @@ -52,7 +52,7 @@ "autoload-files": true }, "spi": { - "OpenTelemetry\\Config\\SDK\\Configuration\\ComponentProvider": [ + "OpenTelemetry\\API\\Configuration\\Config\\ComponentProvider": [ "OpenTelemetry\\Contrib\\Instrumentation\\Laravel\\ComponentProvider\\LaravelComponentProvider" ] } diff --git a/src/Instrumentation/Laravel/tests/Fixtures/otel-instrumentation.yaml b/src/Instrumentation/Laravel/tests/Fixtures/otel-instrumentation.yaml index 15a55c70d..6d9e1e3b3 100644 --- a/src/Instrumentation/Laravel/tests/Fixtures/otel-instrumentation.yaml +++ b/src/Instrumentation/Laravel/tests/Fixtures/otel-instrumentation.yaml @@ -1,4 +1,4 @@ -file_format: '0.3' +file_format: '0.4' logger_provider: processors: @@ -12,7 +12,7 @@ tracer_provider: exporter: test/in_memory_exporter: {} -instrumentation: +instrumentation/development: php: laravel: enabled: true diff --git a/src/Instrumentation/Laravel/tests/bootstrap.php b/src/Instrumentation/Laravel/tests/bootstrap.php index 14dc9ac51..581ccce71 100644 --- a/src/Instrumentation/Laravel/tests/bootstrap.php +++ b/src/Instrumentation/Laravel/tests/bootstrap.php @@ -9,9 +9,11 @@ declare(strict_types=1); use Nevay\SPI\ServiceLoader; -use OpenTelemetry\Config\SDK\Configuration\ComponentProvider; +use OpenTelemetry\API\Configuration\Config\ComponentProvider; use OpenTelemetry\SDK\SdkAutoloader; +ServiceLoader::register(ComponentProvider::class, \OpenTelemetry\Contrib\Instrumentation\Laravel\ComponentProvider\LaravelComponentProvider::class); + ServiceLoader::register(ComponentProvider::class, \OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\ComponentProvider\LogRecordExporterInMemory::class); ServiceLoader::register(ComponentProvider::class, \OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\ComponentProvider\SpanExporterInMemory::class); From 709dd6cb5a637cb90cf8b03c865d857b7c865b78 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Sun, 5 Oct 2025 13:43:30 +0100 Subject: [PATCH 38/40] Laravel: lint --- src/Instrumentation/Laravel/_register.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Instrumentation/Laravel/_register.php b/src/Instrumentation/Laravel/_register.php index dd477037c..5904a0614 100644 --- a/src/Instrumentation/Laravel/_register.php +++ b/src/Instrumentation/Laravel/_register.php @@ -1,4 +1,5 @@ Date: Sun, 5 Oct 2025 13:46:59 +0100 Subject: [PATCH 39/40] Laravel: missing import --- .../Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php index f8e2f093b..7f12d3c75 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php @@ -7,6 +7,7 @@ use Illuminate\Contracts\Http\Kernel as KernelContract; use Illuminate\Http\Request; use Illuminate\Routing\Route; +use OpenTelemetry\API\Globals; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context as InstrumentationContext; use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface; use OpenTelemetry\API\Trace\Span; From 70a59963c954b7766f44d151430c0b840c7bb291 Mon Sep 17 00:00:00 2001 From: Chris Lightfoot-Wild Date: Sun, 5 Oct 2025 14:00:57 +0100 Subject: [PATCH 40/40] Laravel: psalm-suppress UnusedClass. SPI loads these. --- .../Laravel/src/Hooks/Illuminate/Console/Command.php | 1 + .../Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php | 1 + .../Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php | 1 + .../Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php | 1 + .../Laravel/src/Hooks/Illuminate/Database/Eloquent/Model.php | 1 + .../Laravel/src/Hooks/Illuminate/Foundation/Application.php | 1 + .../src/Hooks/Illuminate/Foundation/Console/ServeCommand.php | 2 ++ .../Laravel/src/Hooks/Illuminate/Queue/Queue.php | 1 + .../Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php | 1 + .../Laravel/src/Hooks/Illuminate/Queue/Worker.php | 1 + 10 files changed, 11 insertions(+) diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php index 8bd7c4dde..fd333922c 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Console/Command.php @@ -17,6 +17,7 @@ use OpenTelemetry\SemConv\Version; use Throwable; +/** @psalm-suppress UnusedClass */ class Command implements Hook { use PostHookTrait; diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php index 950f86223..18e89d784 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Console/Kernel.php @@ -21,6 +21,7 @@ use OpenTelemetry\SemConv\Version; use Throwable; +/** @psalm-suppress UnusedClass */ class Kernel implements Hook { use AttributesBuilder; diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php index 7f12d3c75..005572c05 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Http/Kernel.php @@ -34,6 +34,7 @@ use Symfony\Component\HttpFoundation\Response; use Throwable; +/** @psalm-suppress UnusedClass */ class Kernel implements Hook { use PostHookTrait; diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php index e9363b4b8..3c5e43cf1 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Contracts/Queue/Queue.php @@ -21,6 +21,7 @@ use OpenTelemetry\SemConv\Version; use Throwable; +/** @psalm-suppress UnusedClass */ class Queue implements Hook { use AttributesBuilder; diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Database/Eloquent/Model.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Database/Eloquent/Model.php index 813068640..8375d8e28 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Database/Eloquent/Model.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Database/Eloquent/Model.php @@ -17,6 +17,7 @@ use OpenTelemetry\SemConv\Version; use Throwable; +/** @psalm-suppress UnusedClass */ class Model implements Hook { use PostHookTrait; diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Application.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Application.php index 5b861f62e..8e5848233 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Application.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Application.php @@ -20,6 +20,7 @@ use OpenTelemetry\SemConv\Version; use Throwable; +/** @psalm-suppress UnusedClass */ class Application implements Hook { public function instrument( diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php index aa9c7dfbb..af0eb97f6 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Foundation/Console/ServeCommand.php @@ -12,6 +12,8 @@ /** * Instrument Laravel's local PHP development server. + * + * @psalm-suppress UnusedClass */ class ServeCommand implements Hook { diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Queue.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Queue.php index 628dca335..9ec1eba3d 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Queue.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Queue.php @@ -12,6 +12,7 @@ use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; use Throwable; +/** @psalm-suppress UnusedClass */ class Queue implements Hook { use AttributesBuilder; diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php index dc5d30314..f4a5400fe 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/SyncQueue.php @@ -17,6 +17,7 @@ use OpenTelemetry\SemConv\Version; use Throwable; +/** @psalm-suppress UnusedClass */ class SyncQueue implements Hook { use AttributesBuilder; diff --git a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php index c997b0510..b3c89db22 100644 --- a/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php +++ b/src/Instrumentation/Laravel/src/Hooks/Illuminate/Queue/Worker.php @@ -20,6 +20,7 @@ use OpenTelemetry\SemConv\Version; use Throwable; +/** @psalm-suppress UnusedClass */ class Worker implements Hook { use AttributesBuilder;