Skip to content

Commit 2c70e34

Browse files
authored
Make queue.publish spans the parent of queue.process spans (#850)
1 parent 2529414 commit 2c70e34

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/Sentry/Laravel/Features/QueueIntegration.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class QueueIntegration extends Feature
3232
pushScope as private pushScopeTrait;
3333
}
3434

35+
private const QUEUE_SPAN_OP_QUEUE_PUBLISH = 'queue.publish';
36+
3537
private const QUEUE_PAYLOAD_BAGGAGE_DATA = 'sentry_baggage_data';
3638
private const QUEUE_PAYLOAD_TRACE_PARENT_DATA = 'sentry_trace_parent_data';
3739

@@ -57,7 +59,22 @@ public function onBoot(Dispatcher $events): void
5759
$events->listen(JobExceptionOccurred::class, [$this, 'handleJobExceptionOccurredQueueEvent']);
5860

5961
if ($this->isTracingFeatureEnabled('queue_jobs') || $this->isTracingFeatureEnabled('queue_job_transactions')) {
60-
Queue::createPayloadUsing(static function (?string $connection, ?string $queue, ?array $payload): ?array {
62+
Queue::createPayloadUsing(function (?string $connection, ?string $queue, ?array $payload): ?array {
63+
$parentSpan = SentrySdk::getCurrentHub()->getSpan();
64+
65+
if ($parentSpan !== null) {
66+
$context = (new SpanContext)
67+
->setOp(self::QUEUE_SPAN_OP_QUEUE_PUBLISH)
68+
->setData([
69+
'messaging.system' => 'laravel',
70+
'messaging.destination.name' => $queue,
71+
'messaging.destination.connection' => $connection,
72+
])
73+
->setDescription($queue);
74+
75+
$this->pushSpan($parentSpan->startChild($context));
76+
}
77+
6178
if ($payload !== null) {
6279
$payload[self::QUEUE_PAYLOAD_BAGGAGE_DATA] = getBaggage();
6380
$payload[self::QUEUE_PAYLOAD_TRACE_PARENT_DATA] = getTraceparent();
@@ -70,10 +87,10 @@ public function onBoot(Dispatcher $events): void
7087

7188
public function handleJobQueueingEvent(JobQueueing $event): void
7289
{
73-
$parentSpan = SentrySdk::getCurrentHub()->getSpan();
90+
$currentSpan = SentrySdk::getCurrentHub()->getSpan();
7491

7592
// If there is no tracing span active there is no need to handle the event
76-
if ($parentSpan === null) {
93+
if ($currentSpan === null || $currentSpan->getOp() !== self::QUEUE_SPAN_OP_QUEUE_PUBLISH) {
7794
return;
7895
}
7996

@@ -85,16 +102,11 @@ public function handleJobQueueingEvent(JobQueueing $event): void
85102
$jobName = get_class($jobName);
86103
}
87104

88-
$context = (new SpanContext)
89-
->setOp('queue.publish')
105+
$currentSpan
90106
->setData([
91-
'messaging.system' => 'laravel',
92107
'messaging.laravel.job' => $jobName,
93-
'messaging.destination.connection' => $event->connectionName,
94108
])
95109
->setDescription($jobName);
96-
97-
$this->pushSpan($parentSpan->startChild($context));
98110
}
99111

100112
public function handleJobQueuedEvent(JobQueued $event): void

0 commit comments

Comments
 (0)