-
Notifications
You must be signed in to change notification settings - Fork 1k
Implement consumer part of rocketmq new client instrumentation #7019
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
Merged
mateuszrzeszutek
merged 9 commits into
open-telemetry:main
from
aaron-ai:feature/support_rocketmq_consumer_instrumentation
Nov 15, 2022
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cfaba19
Implement consumer part of rocketmq new client instrumentation
aaron-ai 29e744e
Use RequestMessageRequest as the 'REQUEST' of receiving instrumantion
aaron-ai e8cfaed
Support receiveInstrumentationEnabled flag
aaron-ai c42f7ae
Remove unused code
aaron-ai 86c6977
Move SpanFinishingCallback to top-level class
aaron-ai 96b297b
Fix issues
aaron-ai 7baee58
Merge remote-tracking branch 'upstream/main' into feature/support_roc…
trask 894ee80
Flipping method order in RocketMqConsumerProcessAttributeExtractor
aaron-ai 3281882
Polish code
aaron-ai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...elemetry/javaagent/instrumentation/rocketmqclient/v5_0/ConsumeServiceInstrumentation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.rocketmqclient.v5_0; | ||
|
|
||
| import static net.bytebuddy.matcher.ElementMatchers.isConstructor; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
| import static net.bytebuddy.matcher.ElementMatchers.named; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
|
||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
| import net.bytebuddy.asm.Advice; | ||
| import net.bytebuddy.description.type.TypeDescription; | ||
| import net.bytebuddy.matcher.ElementMatcher; | ||
| import org.apache.rocketmq.client.apis.consumer.MessageListener; | ||
|
|
||
| final class ConsumeServiceInstrumentation implements TypeInstrumentation { | ||
| @Override | ||
| public ElementMatcher<TypeDescription> typeMatcher() { | ||
| // Instrument ConsumerService instead of MessageListener because lambda could not be enhanced. | ||
| return named("org.apache.rocketmq.client.java.impl.consumer.ConsumeService"); | ||
| } | ||
|
|
||
| @Override | ||
| public void transform(TypeTransformer transformer) { | ||
| transformer.applyAdviceToMethod( | ||
| isConstructor() | ||
| .and( | ||
| isPublic() | ||
| .and(takesArguments(5)) | ||
| .and( | ||
| takesArgument( | ||
| 1, named("org.apache.rocketmq.client.apis.consumer.MessageListener")))), | ||
| ConsumeServiceInstrumentation.class.getName() + "$ConstructorAdvice"); | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public static class ConstructorAdvice { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static void onEnter( | ||
| @Advice.Argument(value = 1, readOnly = false) MessageListener messageListener) { | ||
| // Replace messageListener by wrapper. | ||
| if (!(messageListener instanceof MessageListenerWrapper)) { | ||
| messageListener = new MessageListenerWrapper(messageListener); | ||
| } | ||
| } | ||
| } | ||
| } | ||
60 changes: 60 additions & 0 deletions
60
...ntelemetry/javaagent/instrumentation/rocketmqclient/v5_0/ConsumerImplInstrumentation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.rocketmqclient.v5_0; | ||
|
|
||
| import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
| import static net.bytebuddy.matcher.ElementMatchers.named; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
|
||
| import apache.rocketmq.v2.ReceiveMessageRequest; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
| import net.bytebuddy.asm.Advice; | ||
| import net.bytebuddy.description.type.TypeDescription; | ||
| import net.bytebuddy.matcher.ElementMatcher; | ||
| import org.apache.rocketmq.client.java.impl.consumer.ReceiveMessageResult; | ||
| import org.apache.rocketmq.shaded.com.google.common.util.concurrent.Futures; | ||
| import org.apache.rocketmq.shaded.com.google.common.util.concurrent.ListenableFuture; | ||
| import org.apache.rocketmq.shaded.com.google.common.util.concurrent.MoreExecutors; | ||
|
|
||
| final class ConsumerImplInstrumentation implements TypeInstrumentation { | ||
| @Override | ||
| public ElementMatcher<TypeDescription> typeMatcher() { | ||
| return named("org.apache.rocketmq.client.java.impl.consumer.ConsumerImpl"); | ||
| } | ||
|
|
||
| @Override | ||
| public void transform(TypeTransformer transformer) { | ||
| transformer.applyAdviceToMethod( | ||
| isMethod() | ||
| .and(named("receiveMessage")) | ||
| .and(takesArguments(3)) | ||
| .and(takesArgument(0, named("apache.rocketmq.v2.ReceiveMessageRequest"))) | ||
| .and(takesArgument(1, named("org.apache.rocketmq.client.java.route.MessageQueueImpl"))) | ||
| .and(takesArgument(2, named("java.time.Duration"))), | ||
| ConsumerImplInstrumentation.class.getName() + "$ReceiveMessageAdvice"); | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public static class ReceiveMessageAdvice { | ||
|
|
||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static Timer onStart() { | ||
| return Timer.start(); | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(suppress = Throwable.class) | ||
| public static void onExit( | ||
| @Advice.Argument(0) ReceiveMessageRequest request, | ||
| @Advice.Enter Timer timer, | ||
| @Advice.Return ListenableFuture<ReceiveMessageResult> future) { | ||
| ReceiveSpanFinishingCallback spanFinishingCallback = | ||
| new ReceiveSpanFinishingCallback(request, timer); | ||
| Futures.addCallback(future, spanFinishingCallback, MoreExecutors.directExecutor()); | ||
| } | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
...o/opentelemetry/javaagent/instrumentation/rocketmqclient/v5_0/MessageListenerWrapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.rocketmqclient.v5_0; | ||
|
|
||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.Scope; | ||
| import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
| import org.apache.rocketmq.client.apis.consumer.ConsumeResult; | ||
| import org.apache.rocketmq.client.apis.consumer.MessageListener; | ||
| import org.apache.rocketmq.client.apis.message.MessageView; | ||
|
|
||
| public final class MessageListenerWrapper implements MessageListener { | ||
| private final MessageListener delegator; | ||
|
|
||
| public MessageListenerWrapper(MessageListener delegator) { | ||
| this.delegator = delegator; | ||
| } | ||
|
|
||
| @Override | ||
| public ConsumeResult consume(MessageView messageView) { | ||
| Context parentContext = VirtualFieldStore.getContextByMessage(messageView); | ||
| if (parentContext == null) { | ||
| parentContext = Context.current(); | ||
| } | ||
| Instrumenter<MessageView, ConsumeResult> processInstrumenter = | ||
| RocketMqSingletons.consumerProcessInstrumenter(); | ||
| if (!processInstrumenter.shouldStart(parentContext, messageView)) { | ||
| return delegator.consume(messageView); | ||
| } | ||
| Context context = processInstrumenter.start(parentContext, messageView); | ||
| try (Scope ignored = context.makeCurrent()) { | ||
| ConsumeResult consumeResult = delegator.consume(messageView); | ||
| processInstrumenter.end(context, messageView, consumeResult, null); | ||
| return consumeResult; | ||
| } catch (Throwable t) { | ||
| processInstrumenter.end(context, messageView, null, t); | ||
| throw t; | ||
| } | ||
aaron-ai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
25 changes: 25 additions & 0 deletions
25
...java/io/opentelemetry/javaagent/instrumentation/rocketmqclient/v5_0/MessageMapGetter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.rocketmqclient.v5_0; | ||
|
|
||
| import io.opentelemetry.context.propagation.TextMapGetter; | ||
| import javax.annotation.Nullable; | ||
| import org.apache.rocketmq.client.apis.message.MessageView; | ||
|
|
||
| enum MessageMapGetter implements TextMapGetter<MessageView> { | ||
| INSTANCE; | ||
|
|
||
| @Override | ||
| public Iterable<String> keys(MessageView carrier) { | ||
| return carrier.getProperties().keySet(); | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public String get(@Nullable MessageView carrier, String key) { | ||
| return carrier == null ? null : carrier.getProperties().get(key); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...telemetry/javaagent/instrumentation/rocketmqclient/v5_0/ReceiveSpanFinishingCallback.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.rocketmqclient.v5_0; | ||
|
|
||
| import apache.rocketmq.v2.ReceiveMessageRequest; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
| import io.opentelemetry.instrumentation.api.internal.InstrumenterUtil; | ||
| import java.util.List; | ||
| import org.apache.rocketmq.client.apis.message.MessageView; | ||
| import org.apache.rocketmq.client.java.impl.consumer.ReceiveMessageResult; | ||
| import org.apache.rocketmq.client.java.message.MessageViewImpl; | ||
| import org.apache.rocketmq.shaded.com.google.common.util.concurrent.FutureCallback; | ||
|
|
||
| public final class ReceiveSpanFinishingCallback implements FutureCallback<ReceiveMessageResult> { | ||
|
|
||
| private final ReceiveMessageRequest request; | ||
| private final Timer timer; | ||
|
|
||
| public ReceiveSpanFinishingCallback(ReceiveMessageRequest request, Timer timer) { | ||
| this.request = request; | ||
| this.timer = timer; | ||
| } | ||
|
|
||
| @Override | ||
| public void onSuccess(ReceiveMessageResult receiveMessageResult) { | ||
| List<MessageViewImpl> messageViews = receiveMessageResult.getMessageViewImpls(); | ||
| // Don't create spans when no messages were received. | ||
| if (messageViews.isEmpty()) { | ||
| return; | ||
| } | ||
| String consumerGroup = request.getGroup().getName(); | ||
| for (MessageViewImpl messageView : messageViews) { | ||
| VirtualFieldStore.setConsumerGroupByMessage(messageView, consumerGroup); | ||
| } | ||
| Instrumenter<ReceiveMessageRequest, List<MessageView>> receiveInstrumenter = | ||
| RocketMqSingletons.consumerReceiveInstrumenter(); | ||
| Context parentContext = Context.current(); | ||
| if (receiveInstrumenter.shouldStart(parentContext, request)) { | ||
| Context context = | ||
| InstrumenterUtil.startAndEnd( | ||
| receiveInstrumenter, | ||
| parentContext, | ||
| request, | ||
| null, | ||
| null, | ||
| timer.startTime(), | ||
| timer.now()); | ||
| for (MessageViewImpl messageView : messageViews) { | ||
| VirtualFieldStore.setContextByMessage(messageView, context); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(Throwable throwable) { | ||
| Instrumenter<ReceiveMessageRequest, List<MessageView>> receiveInstrumenter = | ||
| RocketMqSingletons.consumerReceiveInstrumenter(); | ||
| Context parentContext = Context.current(); | ||
| if (receiveInstrumenter.shouldStart(parentContext, request)) { | ||
| InstrumenterUtil.startAndEnd( | ||
| receiveInstrumenter, | ||
| parentContext, | ||
| request, | ||
| null, | ||
| throwable, | ||
| timer.startTime(), | ||
| timer.now()); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.