Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queue integration fixes #904

Merged
merged 6 commits into from
Jun 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading