-
Notifications
You must be signed in to change notification settings - Fork 872
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement consumer part of rocketmq new client instrumentation (#7019)
Fixes #6764 , This PR is about the consumer part. Co-authored-by: Trask Stalnaker <[email protected]>
- Loading branch information
Showing
23 changed files
with
1,048 additions
and
93 deletions.
There are no files selected for viewing
This file contains 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
49 changes: 49 additions & 0 deletions
49
...elemetry/javaagent/instrumentation/rocketmqclient/v5_0/ConsumeServiceInstrumentation.java
This file contains 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,49 @@ | ||
/* | ||
* 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 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() { | ||
return named("org.apache.rocketmq.client.java.impl.consumer.ConsumeService"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
isConstructor() | ||
.and( | ||
isPublic() | ||
.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 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()); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...o/opentelemetry/javaagent/instrumentation/rocketmqclient/v5_0/MessageListenerWrapper.java
This file contains 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,46 @@ | ||
/* | ||
* 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); | ||
ConsumeResult consumeResult = null; | ||
Throwable error = null; | ||
try (Scope ignored = context.makeCurrent()) { | ||
consumeResult = delegator.consume(messageView); | ||
return consumeResult; | ||
} catch (Throwable t) { | ||
error = t; | ||
throw t; | ||
} finally { | ||
processInstrumenter.end(context, messageView, consumeResult, error); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...java/io/opentelemetry/javaagent/instrumentation/rocketmqclient/v5_0/MessageMapGetter.java
This file contains 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 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 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 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 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.