-
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.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
gaelreyrol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Symfony\Messenger\EventSubscriber; | ||
|
||
use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\InstrumentationTypeEnum; | ||
use OpenTelemetry\API\Trace\StatusCode; | ||
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\WorkerRunningEvent; | ||
use Symfony\Component\Messenger\Event\WorkerStoppedEvent; | ||
|
||
class EndSpanEventSubscriber implements EventSubscriberInterface | ||
{ | ||
private ?InstrumentationTypeEnum $instrumentationType = null; | ||
|
||
public function __construct( | ||
private readonly LoggerInterface $logger | ||
) { | ||
|
||
} | ||
|
||
public function setInstrumentationType(InstrumentationTypeEnum $instrumentationType): void | ||
{ | ||
$this->instrumentationType = $instrumentationType; | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
WorkerRunningEvent::class => ['endSpan'], | ||
WorkerStoppedEvent::class => ['endSpan'], | ||
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. We should not end a span on running/stopped worker events but on WorkerMessageReceived and WorkerMessageHandled events. 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. ended on WorkerMessageHandledEvent. But I don't understand why it should also be ended on WorkerMessageReceived 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 am sorry I mean, start a span on WorkerMessageReceived event and end span on WorkerMessageHandled event. |
||
WorkerMessageFailedEvent::class => ['hydrateSpanWithError'], | ||
]; | ||
} | ||
|
||
public function endSpan(WorkerRunningEvent|WorkerStoppedEvent $event): void | ||
{ | ||
if ($this->instrumentationType !== InstrumentationTypeEnum::Auto) { | ||
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 hydrateSpanWithError(WorkerMessageFailedEvent $event): void | ||
gaelreyrol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if ($this->instrumentationType !== InstrumentationTypeEnum::Auto) { | ||
return; | ||
} | ||
|
||
$scope = Context::storage()->scope(); | ||
|
||
if (null === $scope) { | ||
return; | ||
} | ||
|
||
$span = Span::fromContext($scope->context()); | ||
$span->setAttribute('exception.message', $event->getThrowable()->getMessage()); | ||
$previous = $event->getThrowable()->getPrevious(); | ||
if ($previous !== null) { | ||
$span->setAttribute('exception.previous.message', $previous->getMessage()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Symfony\Messenger\EventSubscriber; | ||
|
||
use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\InstrumentationTypeEnum; | ||
use OpenTelemetry\API\Trace\SpanKind; | ||
use OpenTelemetry\API\Trace\TracerInterface; | ||
use OpenTelemetry\Context\Context; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
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 StartSpanEventSubscriber implements EventSubscriberInterface | ||
{ | ||
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'], | ||
]; | ||
} | ||
|
||
public function startSpan(WorkerMessageReceivedEvent $event): void | ||
{ | ||
if ($this->instrumentationType !== InstrumentationTypeEnum::Auto) { | ||
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 ($busNameStamp !== null) { | ||
$span->setAttribute('bus.name', $busNameStamp->getBusName()); | ||
} | ||
|
||
$this->logger->debug(sprintf('Starting span "%s"', $span->getContext()->getSpanId())); | ||
|
||
Context::storage() | ||
->attach( | ||
$span->storeInContext($context) | ||
) | ||
; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ | |
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.