Skip to content

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

jvocampings
Copy link

@jvocampings jvocampings commented Apr 17, 2025

WIP.

Related to issue #172

Encapsulate consumption of message in a Span. Based on Messenger events.

Missing instrumentation when a message is dispatched. Not sure how to implement it.

edit: I assume we could reuse TraceableMessengerTransport to decorate the method send.

@jvocampings jvocampings requested a review from a team as a code owner April 17, 2025 15:35
Comment on lines 35 to 36
WorkerRunningEvent::class => ['endSpan'],
WorkerStoppedEvent::class => ['endSpan'],
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
You have to thing about the fact that a message can handled synchronously and it won't trigger worker events.

According to Symfony's documentation:

The middleware are executed when the message is dispatched but also again when a message is received via the worker (for messages that were sent to a transport to be handled asynchronously). Keep this in mind if you create your own middleware.

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:

  1. a message is dispatched
  2. the message goes through middlewares -> span start if sync
  3. the message is handled if synchronous -> span end if sync
  4. the message is sent to transport if asynchronous -> attach tracing context without span creation
  5. the message is received by the worker -> span start
  6. the message goes through middlewares -> no span creation
  7. the message is successfully handled by the worker -> span end
  8. the messagge has failed to be handled by the worker -> span end

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.

@gaelreyrol
Copy link
Contributor

gaelreyrol commented Apr 18, 2025

edit: I assume we could reuse TraceableMessengerTransport to decorate the method send.
You mean attach the trace context to the message?

Thanks again for your preliminary work on this, it's not an easy part and it will take some time to get the right implementation!

This PR might also close this issue #168.

@jvocampings jvocampings changed the title chore(messenger): use Messenger events to start/end span for instrumentation chore(messenger): use Messenger instrumentation Apr 18, 2025
@jvocampings
Copy link
Author

edit: I assume we could reuse TraceableMessengerTransport to decorate the method send.
You mean attach the trace context to the message?

Ah I was talking about Instrumentation and not Propagation.
When I dispatch a message which is consumed asynchronously, I'd like to create a span to have a trace of it and to see how many time it took to send messages to my broker (rabbitmq, sqs etc)

@jvocampings
Copy link
Author

jvocampings commented Apr 18, 2025

@gaelreyrol I made some modifications related to what you asked. I'll be on holidays for 2 weeks. After that there are big changes at my work then I don't know if I'll be able to reactive. I'll do my best.

@gaelreyrol
Copy link
Contributor

Ah I was talking about Instrumentation and not Propagation. When I dispatch a message which is consumed asynchronously, I'd like to create a span to have a trace of it and to see how many time it took to send messages to my broker (rabbitmq, sqs etc)

It can't be done from the event and the TraceableMessengerTransport is already doing that, you just need to wrap the transport DNS with the trace() decorator. Check out this file: https://github.com/FriendsOfOpenTelemetry/opentelemetry-bundle/blob/main/tests/Functional/Application/config/packages/framework.yaml#L14

I made some modifications related to what you asked. I'll be on holiday for 2 weeks. After that there are big changes at my work then I don't know if I'll be able to reactive. I'll do my best.

Thank you very much, don't worry at all, have a good holiday! I might work on your PR directly if I have enough time.

* 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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 WorkerMessageEventSubscriber.


public function startSpan(WorkerMessageReceivedEvent $event): void
{
if (InstrumentationTypeEnum::Auto !== $this->instrumentationType) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 #[Traceable] attribute, it should be instrumented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants