forked from open-telemetry/opentelemetry-php-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7371e07
commit 8c26008
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers; | ||
|
||
use Illuminate\Contracts\Foundation\Application; | ||
use Illuminate\Queue\Events\JobProcessed; | ||
use Illuminate\Queue\Events\JobProcessing; | ||
use Illuminate\Queue\Queue; | ||
use OpenTelemetry\API\Instrumentation\CachedInstrumentation; | ||
use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator; | ||
use OpenTelemetry\API\Trace\Span; | ||
use OpenTelemetry\API\Trace\SpanKind; | ||
use OpenTelemetry\Context\Context; | ||
use OpenTelemetry\SemConv\TraceAttributes; | ||
|
||
class QueueWatcher extends Watcher | ||
{ | ||
public function __construct( | ||
private CachedInstrumentation $instrumentation, | ||
) { | ||
} | ||
|
||
/** @psalm-suppress UndefinedInterfaceMethod */ | ||
public function register(Application $app): void | ||
{ | ||
Queue::createPayloadUsing(function ($connection, $queue, $payload) { | ||
$opentelemetry = []; | ||
TraceContextPropagator::getInstance()->inject($opentelemetry); | ||
|
||
return [ | ||
'opentelemetry' => $opentelemetry, | ||
]; | ||
}); | ||
|
||
$app['events']->listen(JobProcessing::class, [$this, 'handleJobProcessing']); | ||
$app['events']->listen(JobProcessed::class, [$this, 'handleJobProcessed']); | ||
} | ||
|
||
public function handleJobProcessing(JobProcessing $jobProcessing): void | ||
{ | ||
$parent = TraceContextPropagator::getInstance()->extract( | ||
$jobProcessing->job->payload()['opentelemetry'] ?? [], | ||
); | ||
|
||
$span = $this->instrumentation | ||
->tracer() | ||
->spanBuilder('queue') | ||
->setSpanKind(SpanKind::KIND_INTERNAL) | ||
->setParent($parent) | ||
->startSpan(); | ||
|
||
$span->setAttributes([ | ||
TraceAttributes::MESSAGING_DESTINATION_NAME => $jobProcessing->job->getQueue(), | ||
TraceAttributes::MESSAGING_MESSAGE_ID => $jobProcessing->job->uuid(), | ||
TraceAttributes::MESSAGING_MESSAGE_ENVELOPE_SIZE => strlen($jobProcessing->job->getRawBody()), | ||
]); | ||
|
||
$span->addEvent('job', [ | ||
'connection' => $jobProcessing->connectionName, | ||
'job.id' => $jobProcessing->job->getJobId(), | ||
'job.name' => $jobProcessing->job->getName(), | ||
'raw.body' => $jobProcessing->job->getRawBody(), | ||
]); | ||
|
||
Context::storage()->attach($span->storeInContext($parent)); | ||
} | ||
|
||
public function handleJobProcessed(JobProcessed $jobProcessed): void | ||
{ | ||
$scope = Context::storage()->scope(); | ||
if (!$scope) { | ||
return; | ||
} | ||
$scope->detach(); | ||
$span = Span::fromContext($scope->context()); | ||
|
||
$span->addEvent('processed', [ | ||
'deleted' => $jobProcessed->job->isDeleted(), | ||
'released' => $jobProcessed->job->isReleased(), | ||
]); | ||
|
||
$span->end(); | ||
} | ||
} |