-
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 4 commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Symfony\Messenger\Amqp; | ||
|
||
use FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Context\Propagator\TraceStampPropagator; | ||
use OpenTelemetry\Context\Context; | ||
use OpenTelemetry\Context\Propagation\MultiTextMapPropagator; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Messenger\Bridge\Amqp\Transport\AmqpStamp; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Middleware\MiddlewareInterface; | ||
use Symfony\Component\Messenger\Middleware\StackInterface; | ||
|
||
/** | ||
* Be aware the app consuming the message must be able to denormalize the stamp. | ||
*/ | ||
readonly class AddStampForPropagationMiddleware implements MiddlewareInterface | ||
{ | ||
public function __construct( | ||
private MultiTextMapPropagator $propagator, | ||
private ?LoggerInterface $logger = null, | ||
) { | ||
} | ||
|
||
public function handle(Envelope $envelope, StackInterface $stack): Envelope | ||
{ | ||
if (null !== $envelope->last(AmqpStamp::class)) { | ||
$this->onMessageSent($envelope); | ||
} | ||
|
||
return $stack->next()->handle($envelope, $stack); | ||
} | ||
|
||
private function onMessageSent(Envelope &$envelope): void | ||
{ | ||
$scope = Context::storage()->scope(); | ||
|
||
if (null === $scope) { | ||
$this->logger?->debug('No active scope'); | ||
} | ||
|
||
$this->propagator->inject($envelope, new TraceStampPropagator(), Context::getCurrent()); | ||
$this->logger?->debug('Trace stamp added to envelope for propagation'); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Symfony\Messenger; | ||
|
||
use Symfony\Component\Messenger\Stamp\StampInterface; | ||
|
||
/** | ||
* @doc: https://www.w3.org/TR/trace-context/ | ||
* | ||
* You can see how the trace parent generated here: https://github.com/open-telemetry/opentelemetry-php/blob/main/src/API/Trace/Propagation/TraceContextPropagator.php | ||
*/ | ||
readonly class TraceStamp implements StampInterface | ||
{ | ||
public function __construct( | ||
private string $traceParent, | ||
) { | ||
} | ||
|
||
public function getTraceParent(): string | ||
{ | ||
return $this->traceParent; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Context\Propagator; | ||
|
||
use OpenTelemetry\API\Baggage\Propagation\BaggagePropagator; | ||
use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator; | ||
use OpenTelemetry\Context\Propagation\MultiTextMapPropagator; | ||
|
||
class PropagatorFactory | ||
{ | ||
/** | ||
* Default propagators from https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/#general-sdk-configuration. | ||
*/ | ||
public static function createDefault(): MultiTextMapPropagator | ||
{ | ||
return new MultiTextMapPropagator([ | ||
BaggagePropagator::getInstance(), | ||
TraceContextPropagator::getInstance(), | ||
]); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Context\Propagator; | ||
|
||
use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\Symfony\Messenger\TraceStamp; | ||
use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator; | ||
use OpenTelemetry\Context\Propagation\PropagationSetterInterface; | ||
use Symfony\Component\Messenger\Envelope; | ||
|
||
class TraceStampPropagator implements PropagationSetterInterface | ||
{ | ||
/** | ||
* @param mixed $carrier | ||
*/ | ||
public function set(&$carrier, string $key, string $value): void | ||
{ | ||
if (!$carrier instanceof Envelope) { | ||
throw new \InvalidArgumentException(sprintf('The carrier for trace stamp propagation must be instance of %s', Envelope::class)); | ||
} | ||
|
||
if (TraceContextPropagator::TRACEPARENT !== $key) { | ||
return; | ||
} | ||
|
||
$carrier = $carrier->with(new TraceStamp($value)); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.