Skip to content

Commit

Permalink
Queue integration fixes (#904)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
stayallive authored Jun 5, 2024
1 parent 3adfd3c commit f711fec
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/Sentry/Laravel/Features/QueueIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Sentry\Laravel\Features;

use Closure;
use Illuminate\Support\Str;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Queue\Events\JobExceptionOccurred;
use Illuminate\Queue\Events\JobProcessed;
Expand Down Expand Up @@ -68,7 +69,9 @@ public function onBoot(Dispatcher $events): void
->setOp(self::QUEUE_SPAN_OP_QUEUE_PUBLISH)
->setData([
'messaging.system' => 'laravel',
'messaging.destination.name' => $queue,
'messaging.message.id' => $payload['uuid'] ?? null,
// Jobs pushed onto the Redis driver are formatted as queues:<queue>
'messaging.destination.name' => Str::after($queue ?? '', 'queues:'),
'messaging.destination.connection' => $connection,
])
->setDescription($queue);
Expand Down Expand Up @@ -159,14 +162,16 @@ public function handleJobProcessingQueueEvent(JobProcessing $event): void
return;
}

// If there is a parent span we can record that job as a child unless configured to not do so
if ($parentSpan !== null && !$this->isTracingFeatureEnabled('queue_jobs')) {
// 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
if ($parentSpan !== null && (!$parentSpan->getSampled() || !$this->isTracingFeatureEnabled('queue_jobs'))) {
return;
}

$jobPayload = $event->job->payload();

if ($parentSpan === null) {
$baggage = $event->job->payload()[self::QUEUE_PAYLOAD_BAGGAGE_DATA] ?? null;
$traceParent = $event->job->payload()[self::QUEUE_PAYLOAD_TRACE_PARENT_DATA] ?? null;
$baggage = $jobPayload[self::QUEUE_PAYLOAD_BAGGAGE_DATA] ?? null;
$traceParent = $jobPayload[self::QUEUE_PAYLOAD_TRACE_PARENT_DATA] ?? null;

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

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

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

$receiveLatency = null;
if ($event->job->payload()[self::QUEUE_PAYLOAD_PUBLISH_TIME] !== null) {
$receiveLatency = microtime(true) - $event->job->payload()[self::QUEUE_PAYLOAD_PUBLISH_TIME];
}
$jobPublishedAt = $jobPayload[self::QUEUE_PAYLOAD_PUBLISH_TIME] ?? null;

$job = [
'messaging.system' => 'laravel',

'messaging.destination.name' => $event->job->getQueue(),
'messaging.destination.connection' => $event->connectionName,
'messaging.message.id' => (string) $event->job->getJobId(),

'messaging.message.id' => $jobPayload['uuid'] ?? null,
'messaging.message.envelope.size' => strlen($event->job->getRawBody()),
'messaging.message.body.size' => strlen(json_encode($event->job->payload()['data'])),
'messaging.message.body.size' => strlen(json_encode($jobPayload['data'] ?? [])),
'messaging.message.retry.count' => $event->job->attempts(),
'messaging.message.receive.latency' => $receiveLatency,
'messaging.message.receive.latency' => $jobPublishedAt !== null ? microtime(true) - $jobPublishedAt : null,
];

if ($context instanceof TransactionContext) {
Expand Down

0 comments on commit f711fec

Please sign in to comment.