open-telemetry / opentelemetry-java-instrumentation

OpenTelemetry auto-instrumentation and instrumentation libraries for Java
https://opentelemetry.io
Apache License 2.0
1.94k stars 850 forks source link

Couldn't get Producer span in rocketmq SendMessageHook #10817

Open wang007 opened 7 months ago

wang007 commented 7 months ago

Describe the bug

I want to add message body to Producer Span but Couldn't get Producer span in rocketmq SendMessageHook.

image

This seems to be the result of not calling Context.attch()

image

my code image

Steps to reproduce

ignore

Expected behavior

ignore

Actual behavior

ignore

Javaagent or library instrumentation version

1.32.0

Environment

JDK: 1.8 OS: mac

Additional context

No response

trask commented 7 months ago

cc @aaron-ai

aaron-ai commented 7 months ago

@wang007 Could you provide me with more details about the motivation? Usually, we don't need to record the message body in span.

wang007 commented 7 months ago

As you can see from the first image above, I've mounted a sendmessage hook image

  1. I want to customize some properties to the producer span, but I can't do that right now.
  2. The ability to customize should be given to the user
Cirilla-zmh commented 5 months ago

@wang007 The situation you mentioned occurs because the advice for sendMessageHook does not execute the context.makeCurrent() method to replace the current thread's context after starting a span. This may imply that the designers do not intend for others to retrieve context information via the OTel SDK at the time point of sending messages, nor to modify the span.

In fact, for the 4.x version of the rocketmq-client, the org.apache.rocketmq.client.producer.DefaultMQProducer#getDefaultMQProducerImpl() method has been deprecated. Perhaps we should not obtain this object and register hook components at runtime.

Cirilla-zmh commented 5 months ago

If you're keen on capturing the body information of a message, there might be two approaches:

  1. Within the opentelemetry-java-instrumentation project, extract the body in io.opentelemetry.instrumentation.rocketmqclient.v4_8.TracingSendMessageHookImpl#sendMessageBefore() and inject it into an attribute. Incidentally, is there any plan from OTel to consistently log this attribute across all messaging-type instrumentations as features? @trask If so, I might be interested in contributing.
  2. Use the OTel SDK in your application code to create a span and record the message.body within this newly created span.

I hope my answer can be of help to you.

wang007 commented 5 months ago

I'm using the second solution, but I need to create an extra span. The issue is raised because other third-party components can get the current span content.

If you think that's how it's designed, close the issue.

thx @Cirilla-zmh

Cirilla-zmh commented 5 months ago

The issue is raised because other third-party components can get the current span content. If you think that's how it's designed, close the issue.

I believe the body of the message carries significant information, and it might be worth considering logging this information. We could implement it as a configurable feature.

I would be happy to contribute to this. But before doing so, I would like to know if contributions of this kind are welcome. @trask @aaron-ai @steverao

trask commented 5 months ago

I believe the body of the message carries significant information, and it might be worth considering logging this information. We could implement it as a configurable feature.

I would be happy to contribute to this. But before doing so, I would like to know if contributions of this kind are welcome. @trask @aaron-ai @steverao

yes, I believe capturing message bodies would be accepted as an opt-in feature, similar to capturing request/response bodies in https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/8778