Skip to content

Commit

Permalink
Add span for publishing a job to the queue (#833)
Browse files Browse the repository at this point in the history
* Add span for publishing a job to the queue

* CR
  • Loading branch information
stayallive authored Jan 29, 2024
1 parent 894e434 commit a1da725
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Sentry/Laravel/Features/QueueIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Sentry\Laravel\Features;

use Closure;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Queue\Events\JobExceptionOccurred;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\Events\JobQueued;
use Illuminate\Queue\Events\JobQueueing;
use Illuminate\Queue\Events\WorkerStopping;
use Illuminate\Queue\Queue;
use Sentry\Breadcrumb;
Expand Down Expand Up @@ -45,6 +48,9 @@ public function isApplicable(): bool

public function onBoot(Dispatcher $events): void
{
$events->listen(JobQueueing::class, [$this, 'handleJobQueueingEvent']);
$events->listen(JobQueued::class, [$this, 'handleJobQueuedEvent']);

$events->listen(JobProcessed::class, [$this, 'handleJobProcessedQueueEvent']);
$events->listen(JobProcessing::class, [$this, 'handleJobProcessingQueueEvent']);
$events->listen(WorkerStopping::class, [$this, 'handleWorkerStoppingQueueEvent']);
Expand All @@ -62,6 +68,44 @@ public function onBoot(Dispatcher $events): void
}
}

public function handleJobQueueingEvent(JobQueueing $event): void
{
$parentSpan = SentrySdk::getCurrentHub()->getSpan();

// If there is no tracing span active there is no need to handle the event
if ($parentSpan === null) {
return;
}

$jobName = $event->job;

if ($jobName instanceof Closure) {
$jobName = 'Closure';
} elseif (is_object($jobName)) {
$jobName = get_class($jobName);
}

$context = (new SpanContext)
->setOp('queue.publish')
->setData([
'messaging.system' => 'laravel',
'messaging.laravel.job' => $jobName,
'messaging.destination.connection' => $event->connectionName,
])
->setDescription($jobName);

$this->pushSpan($parentSpan->startChild($context));
}

public function handleJobQueuedEvent(JobQueued $event): void
{
$span = $this->maybePopSpan();

if ($span !== null) {
$span->finish();
}
}

public function handleJobProcessedQueueEvent(JobProcessed $event): void
{
$this->finishJobWithStatus(SpanStatus::ok());
Expand Down

0 comments on commit a1da725

Please sign in to comment.