-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Laravel: message processing on the consumer side.
- Loading branch information
1 parent
1a596ff
commit 69117c8
Showing
3 changed files
with
162 additions
and
55 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
65 changes: 65 additions & 0 deletions
65
src/Instrumentation/Laravel/src/Hooks/Queue/AttributesBuilder.php
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,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Queue; | ||
|
||
use Illuminate\Contracts\Queue\Queue as QueueContract; | ||
use Illuminate\Queue\RedisQueue; | ||
use Illuminate\Queue\SqsQueue; | ||
use OpenTelemetry\SemConv\TraceAttributes; | ||
use OpenTelemetry\SemConv\TraceAttributeValues; | ||
|
||
trait AttributesBuilder | ||
{ | ||
private function buildMessageAttributes( | ||
QueueContract $queue, | ||
string $rawPayload, | ||
string $queueName = null, | ||
array $options = [], | ||
mixed ...$params, | ||
): array { | ||
$payload = json_decode($rawPayload, true) ?? []; | ||
|
||
return array_merge([ | ||
TraceAttributes::MESSAGING_MESSAGE_ID => $payload['uuid'] ?? $payload['id'] ?? null, | ||
TraceAttributes::MESSAGING_MESSAGE_ENVELOPE_SIZE => strlen($rawPayload), | ||
'messaging.message.job_name' => $payload['displayName'] ?? $payload['job'] ?? null, | ||
'messaging.message.attempts' => $payload['attempts'] ?? 0, | ||
'messaging.message.max_exceptions' => $payload['maxExceptions'] ?? null, | ||
'messaging.message.max_tries' => $payload['maxTries'] ?? null, | ||
'messaging.message.retry_until' => $payload['retryUntil'] ?? null, | ||
'messaging.message.timeout' => $payload['timeout'] ?? null, | ||
], $this->contextualMessageSystemAttributes($queue, $payload, $queueName, $options, ...$params)); | ||
} | ||
|
||
private function contextualMessageSystemAttributes( | ||
QueueContract $queue, | ||
array $payload, | ||
string $queueName = null, | ||
array $options = [], | ||
mixed ...$params, | ||
): array { | ||
return match (true) { | ||
$queue instanceof RedisQueue => $this->redisContextualAttributes($queue, $payload, $queueName, $options, ...$params), | ||
$queue instanceof SqsQueue => $this->awsSqsContextualAttributes($queue, $payload, $queueName, $options, ...$params), | ||
default => [], | ||
}; | ||
} | ||
|
||
private function redisContextualAttributes(RedisQueue $queue, array $payload, string $queueName = null, array $options = [], mixed ...$params): array | ||
{ | ||
return [ | ||
TraceAttributes::MESSAGING_SYSTEM => 'redis', | ||
TraceAttributes::MESSAGING_DESTINATION_NAME => $queue->getQueue($queueName), | ||
]; | ||
} | ||
|
||
private function awsSqsContextualAttributes(SqsQueue $queue, array $payload, string $queueName = null, array $options = [], mixed ...$params): array | ||
{ | ||
return [ | ||
TraceAttributes::MESSAGING_SYSTEM => TraceAttributeValues::MESSAGING_SYSTEM_AWS_SQS, | ||
TraceAttributes::MESSAGING_DESTINATION_NAME => $queue->getQueue($queueName), | ||
]; | ||
} | ||
} |
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,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Queue; | ||
|
||
use Illuminate\Contracts\Queue\Job; | ||
use Illuminate\Queue\Worker as QueueWorker; | ||
use Illuminate\Queue\WorkerOptions; | ||
use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator; | ||
use OpenTelemetry\API\Trace\Span; | ||
use OpenTelemetry\API\Trace\SpanKind; | ||
use OpenTelemetry\API\Trace\StatusCode; | ||
use OpenTelemetry\Context\Context; | ||
use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\HookInstance; | ||
use function OpenTelemetry\Instrumentation\hook; | ||
use OpenTelemetry\SemConv\TraceAttributes; | ||
use Throwable; | ||
|
||
class Worker | ||
{ | ||
use AttributesBuilder; | ||
use HookInstance; | ||
|
||
public function instrument(): void | ||
{ | ||
$this->hookWorkerProcess(); | ||
} | ||
|
||
private function hookWorkerProcess(): bool | ||
{ | ||
return hook( | ||
QueueWorker::class, | ||
'process', | ||
pre: function (QueueWorker $worker, array $params, string $class, string $function, ?string $filename, ?int $lineno) { | ||
$connectionName = (is_string($params[0] ?? null) ? $params[0] : null); | ||
$job = ($params[1] instanceof Job ? $params[1] : null); | ||
$workerOptions = ($params[2] instanceof WorkerOptions ? $params[2] : null); | ||
|
||
$parent = TraceContextPropagator::getInstance()->extract( | ||
$job?->payload() ?? [], | ||
); | ||
|
||
$queue = $worker->getManager()->connection($connectionName); | ||
$attributes = $this->buildMessageAttributes($queue, $job->getRawBody(), $job->getQueue()); | ||
|
||
$span = $this->instrumentation | ||
->tracer() | ||
->spanBuilder(vsprintf('%s %s', [ | ||
$attributes[TraceAttributes::MESSAGING_DESTINATION_NAME] ?? '(anonymous)', | ||
'process', | ||
])) | ||
->setSpanKind(SpanKind::KIND_CONSUMER) | ||
->setParent($parent) | ||
->startSpan() | ||
->setAttributes($attributes); | ||
|
||
Context::storage()->attach($span->storeInContext($parent)); | ||
|
||
return $params; | ||
}, | ||
post: function (QueueWorker $worker, array $params, $returnValue, ?Throwable $exception) { | ||
$scope = Context::storage()->scope(); | ||
if (!$scope) { | ||
return; | ||
} | ||
|
||
$scope->detach(); | ||
$span = Span::fromContext($scope->context()); | ||
if ($exception) { | ||
$span->recordException($exception, [TraceAttributes::EXCEPTION_ESCAPED => true]); | ||
$span->setStatus(StatusCode::STATUS_ERROR, $exception->getMessage()); | ||
} | ||
|
||
$connectionName = (is_string($params[0] ?? null) ? $params[0] : null); | ||
$job = ($params[1] instanceof Job ? $params[1] : null); | ||
$workerOptions = ($params[2] instanceof WorkerOptions ? $params[2] : null); | ||
|
||
$span->setAttributes([ | ||
'messaging.message.deleted' => $job?->isDeleted(), | ||
'messaging.message.released' => $job?->isReleased(), | ||
]); | ||
|
||
$span->end(); | ||
}, | ||
); | ||
} | ||
} |