-
Notifications
You must be signed in to change notification settings - Fork 8
chore(messenger): use Messenger instrumentation #173
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
cb84fec
19e51da
5f8c756
d59e8e2
0549614
5a10f47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
gaelreyrol marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Symfony\Messenger; | ||
|
||
use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\InstrumentationTypeEnum; | ||
use OpenTelemetry\API\Trace\SpanKind; | ||
use OpenTelemetry\API\Trace\StatusCode; | ||
use OpenTelemetry\API\Trace\TracerInterface; | ||
use OpenTelemetry\Context\Context; | ||
use OpenTelemetry\SDK\Trace\Span; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent; | ||
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent; | ||
use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent; | ||
use Symfony\Component\Messenger\Stamp\BusNameStamp; | ||
|
||
/** | ||
* Be aware if you start a span before this subscriber, it could leads to orphan span issue. | ||
* Be sure your span is properly ended. | ||
*/ | ||
class InstrumentationEventSubscriber implements EventSubscriberInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this subscriber is about worker events, I think we should name it |
||
{ | ||
private ?InstrumentationTypeEnum $instrumentationType = null; | ||
|
||
public function __construct( | ||
private readonly TracerInterface $tracer, | ||
private readonly LoggerInterface $logger, | ||
) { | ||
} | ||
|
||
public function setInstrumentationType(InstrumentationTypeEnum $instrumentationType): void | ||
{ | ||
$this->instrumentationType = $instrumentationType; | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
WorkerMessageReceivedEvent::class => ['startSpan'], | ||
WorkerMessageFailedEvent::class => ['endSpanOnError'], | ||
WorkerMessageHandledEvent::class => ['endSpanWithSuccess'], | ||
]; | ||
} | ||
|
||
public function startSpan(WorkerMessageReceivedEvent $event): void | ||
{ | ||
if (InstrumentationTypeEnum::Auto !== $this->instrumentationType) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to adapt this to handle manual instrumentation, for example if the message has the |
||
return; | ||
} | ||
|
||
$scope = Context::storage()->scope(); | ||
|
||
if (null !== $scope) { | ||
$this->logger->debug(sprintf('Using scope "%s"', spl_object_id($scope))); | ||
} else { | ||
$this->logger->debug('No active scope'); | ||
} | ||
|
||
$context = Context::getCurrent(); | ||
$span = $this->tracer | ||
->spanBuilder($event->getReceiverName()) | ||
->setParent($context) | ||
->setSpanKind(SpanKind::KIND_CONSUMER) | ||
->startSpan(); | ||
|
||
$busNameStamp = $event->getEnvelope()->last(BusNameStamp::class); | ||
|
||
if (null !== $busNameStamp) { | ||
$span->setAttribute('bus.name', $busNameStamp->getBusName()); | ||
} | ||
|
||
$this->logger->debug(sprintf('Starting span "%s"', $span->getContext()->getSpanId())); | ||
|
||
Context::storage() | ||
->attach( | ||
$span->storeInContext($context) | ||
) | ||
; | ||
} | ||
|
||
public function endSpanWithSuccess(WorkerMessageHandledEvent $event): void | ||
{ | ||
if (InstrumentationTypeEnum::Auto !== $this->instrumentationType) { | ||
return; | ||
} | ||
|
||
$scope = Context::storage()->scope(); | ||
|
||
if (null === $scope) { | ||
return; | ||
} | ||
|
||
$scope->detach(); | ||
|
||
$span = Span::fromContext($scope->context()); | ||
$span->setStatus(StatusCode::STATUS_OK); | ||
$this->logger->debug(sprintf('Ending span "%s"', $span->getContext()->getSpanId())); | ||
$span->end(); | ||
} | ||
|
||
public function endSpanOnError(WorkerMessageFailedEvent $event): void | ||
{ | ||
if (InstrumentationTypeEnum::Auto !== $this->instrumentationType) { | ||
return; | ||
} | ||
|
||
$scope = Context::storage()->scope(); | ||
|
||
if (null === $scope) { | ||
return; | ||
} | ||
|
||
$scope->detach(); | ||
|
||
$span = Span::fromContext($scope->context()); | ||
$span->setStatus(StatusCode::STATUS_ERROR); | ||
$span->setAttribute('exception.message', $event->getThrowable()->getMessage()); | ||
$previous = $event->getThrowable()->getPrevious(); | ||
|
||
if (null !== $previous) { | ||
$span->setAttribute('exception.previous.message', $previous->getMessage()); | ||
} | ||
|
||
$this->logger->debug(sprintf('Ending span "%s"', $span->getContext()->getSpanId())); | ||
$span->end(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,6 @@ | |
use Symfony\Component\Messenger\Middleware\MiddlewareInterface; | ||
use Symfony\Component\Messenger\Middleware\StackInterface; | ||
|
||
/** | ||
* @deprecated: span should be created from event subscriber to avoid issue with orphan span | ||
*/ | ||
class TraceableMessengerMiddleware implements MiddlewareInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there is misunderstanding here, we still need the middleware but maybe there is some adjustment to do. According to Symfony's documentation:
We need to find a way to either trace from the middleware when a synchronous message is dispatched or attach the trace context with a stamp when the message dispatched to the transport and trace it when the message is received and handled. Here is the rolling of events:
By reviewing this, I think we could allow span creation within the middleware in an asynchronous context too as it will detail the every phases of the message, the dispatch phase and the handling phase as with synchronous message the dispatch/handling phase happen at the same time. Still, we need to do some experiments, in each case to track down spans and make sure they are relevant, well linked and ended correctly. |
||
{ | ||
public function __construct( | ||
|
Uh oh!
There was an error while loading. Please reload this page.