Skip to content

Commit f711fec

Browse files
authored
Queue integration fixes (#904)
* Guard against missing payload key * Add missing job publish ID * Get job ID from payload instead of job * Remove “queues:” prefix for Redis driver * Do not process job if parent span is not sampled * Use payload UUID as message ID
1 parent 3adfd3c commit f711fec

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/Sentry/Laravel/Features/QueueIntegration.php

+16-14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Sentry\Laravel\Features;
44

55
use Closure;
6+
use Illuminate\Support\Str;
67
use Illuminate\Contracts\Events\Dispatcher;
78
use Illuminate\Queue\Events\JobExceptionOccurred;
89
use Illuminate\Queue\Events\JobProcessed;
@@ -68,7 +69,9 @@ public function onBoot(Dispatcher $events): void
6869
->setOp(self::QUEUE_SPAN_OP_QUEUE_PUBLISH)
6970
->setData([
7071
'messaging.system' => 'laravel',
71-
'messaging.destination.name' => $queue,
72+
'messaging.message.id' => $payload['uuid'] ?? null,
73+
// Jobs pushed onto the Redis driver are formatted as queues:<queue>
74+
'messaging.destination.name' => Str::after($queue ?? '', 'queues:'),
7275
'messaging.destination.connection' => $connection,
7376
])
7477
->setDescription($queue);
@@ -159,14 +162,16 @@ public function handleJobProcessingQueueEvent(JobProcessing $event): void
159162
return;
160163
}
161164

162-
// If there is a parent span we can record that job as a child unless configured to not do so
163-
if ($parentSpan !== null && !$this->isTracingFeatureEnabled('queue_jobs')) {
165+
// If there is a parent span we can record the job as a child unless the parent is not sample or we are configured to not do so
166+
if ($parentSpan !== null && (!$parentSpan->getSampled() || !$this->isTracingFeatureEnabled('queue_jobs'))) {
164167
return;
165168
}
166169

170+
$jobPayload = $event->job->payload();
171+
167172
if ($parentSpan === null) {
168-
$baggage = $event->job->payload()[self::QUEUE_PAYLOAD_BAGGAGE_DATA] ?? null;
169-
$traceParent = $event->job->payload()[self::QUEUE_PAYLOAD_TRACE_PARENT_DATA] ?? null;
173+
$baggage = $jobPayload[self::QUEUE_PAYLOAD_BAGGAGE_DATA] ?? null;
174+
$traceParent = $jobPayload[self::QUEUE_PAYLOAD_TRACE_PARENT_DATA] ?? null;
170175

171176
$context = continueTrace($traceParent ?? '', $baggage ?? '');
172177

@@ -180,22 +185,19 @@ public function handleJobProcessingQueueEvent(JobProcessing $event): void
180185

181186
$resolvedJobName = $event->job->resolveName();
182187

183-
$receiveLatency = null;
184-
if ($event->job->payload()[self::QUEUE_PAYLOAD_PUBLISH_TIME] !== null) {
185-
$receiveLatency = microtime(true) - $event->job->payload()[self::QUEUE_PAYLOAD_PUBLISH_TIME];
186-
}
188+
$jobPublishedAt = $jobPayload[self::QUEUE_PAYLOAD_PUBLISH_TIME] ?? null;
187189

188190
$job = [
189191
'messaging.system' => 'laravel',
190-
192+
191193
'messaging.destination.name' => $event->job->getQueue(),
192194
'messaging.destination.connection' => $event->connectionName,
193-
194-
'messaging.message.id' => (string) $event->job->getJobId(),
195+
196+
'messaging.message.id' => $jobPayload['uuid'] ?? null,
195197
'messaging.message.envelope.size' => strlen($event->job->getRawBody()),
196-
'messaging.message.body.size' => strlen(json_encode($event->job->payload()['data'])),
198+
'messaging.message.body.size' => strlen(json_encode($jobPayload['data'] ?? [])),
197199
'messaging.message.retry.count' => $event->job->attempts(),
198-
'messaging.message.receive.latency' => $receiveLatency,
200+
'messaging.message.receive.latency' => $jobPublishedAt !== null ? microtime(true) - $jobPublishedAt : null,
199201
];
200202

201203
if ($context instanceof TransactionContext) {

0 commit comments

Comments
 (0)