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

Add span for publishing a job to the queue #833

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Changes from 1 commit
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
43 changes: 43 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,43 @@ 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([
'job' => $jobName,
'connection' => $event->connectionName,
stayallive marked this conversation as resolved.
Show resolved Hide resolved
])
->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
Loading