Skip to content

Commit

Permalink
Laravel: refining queue hooks.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisLightfootWild committed Apr 9, 2024
1 parent 69117c8 commit cf5bd87
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
34 changes: 34 additions & 0 deletions src/Instrumentation/Laravel/src/Hooks/PostHookHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks;

use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\Context\Context;
use OpenTelemetry\SemConv\TraceAttributes;
use Throwable;

trait PostHookHandler
{
private function endSpan(?Throwable $exception = null): void
{
$scope = Context::storage()->scope();
if (!$scope) {
return;
}

$scope->detach();
$span = Span::fromContext($scope->context());

if ($exception) {
$span->recordException($exception, [
TraceAttributes::EXCEPTION_ESCAPED => true,
]);
$span->setStatus(StatusCode::STATUS_ERROR, $exception->getMessage());
}

$span->end();
}
}
18 changes: 5 additions & 13 deletions src/Instrumentation/Laravel/src/Hooks/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
use Illuminate\Contracts\Queue\Queue as QueueContract;
use Illuminate\Queue\Queue as AbstractQueue;
use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator;
use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Queue\AttributesBuilder;
use function OpenTelemetry\Instrumentation\hook;
Expand All @@ -21,6 +19,7 @@ class Queue
{
use AttributesBuilder;
use HookInstance;
use PostHookHandler;

public function instrument(): void
{
Expand Down Expand Up @@ -55,12 +54,12 @@ protected function hookQueuePushRaw(): bool
$span = $this->instrumentation
->tracer()
->spanBuilder(vsprintf('%s %s', [
$attributes[TraceAttributes::MESSAGING_DESTINATION_NAME] ?? '(anonymous)',
$attributes[TraceAttributes::MESSAGING_DESTINATION_NAME],
TraceAttributeValues::MESSAGING_OPERATION_PUBLISH,
]))
->setSpanKind(SpanKind::KIND_PRODUCER)
->startSpan()
->setAttributes($attributes);
->setAttributes($attributes)
->startSpan();

Context::storage()->attach($span->storeInContext($parent));

Expand All @@ -72,14 +71,7 @@ protected function hookQueuePushRaw(): bool
return;
}

$scope->detach();
$span = Span::fromContext($scope->context());
if ($exception) {
$span->recordException($exception, [TraceAttributes::EXCEPTION_ESCAPED => true]);
$span->setStatus(StatusCode::STATUS_ERROR, $exception->getMessage());
}

$span->end();
$this->endSpan($exception);
},
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ 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),
'messaging.message.job_name' => $payload['displayName'] ?? $payload['job'] ?? null,
Expand Down
12 changes: 6 additions & 6 deletions src/Instrumentation/Laravel/src/Hooks/Queue/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator;
use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\HookInstance;
use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookHandler;
use function OpenTelemetry\Instrumentation\hook;
use OpenTelemetry\SemConv\TraceAttributes;
use Throwable;
Expand All @@ -21,6 +21,7 @@ class Worker
{
use AttributesBuilder;
use HookInstance;
use PostHookHandler;

public function instrument(): void
{
Expand All @@ -47,13 +48,13 @@ private function hookWorkerProcess(): bool
$span = $this->instrumentation
->tracer()
->spanBuilder(vsprintf('%s %s', [
$attributes[TraceAttributes::MESSAGING_DESTINATION_NAME] ?? '(anonymous)',
$attributes[TraceAttributes::MESSAGING_DESTINATION_NAME],
'process',
]))
->setSpanKind(SpanKind::KIND_CONSUMER)
->setParent($parent)
->startSpan()
->setAttributes($attributes);
->setAttributes($attributes)
->startSpan();

Context::storage()->attach($span->storeInContext($parent));

Expand All @@ -65,7 +66,6 @@ private function hookWorkerProcess(): bool
return;
}

$scope->detach();
$span = Span::fromContext($scope->context());
if ($exception) {
$span->recordException($exception, [TraceAttributes::EXCEPTION_ESCAPED => true]);
Expand All @@ -81,7 +81,7 @@ private function hookWorkerProcess(): bool
'messaging.message.released' => $job?->isReleased(),
]);

$span->end();
$this->endSpan($exception);
},
);
}
Expand Down

0 comments on commit cf5bd87

Please sign in to comment.