Skip to content
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

Rabbitmq instrument consumer class correctly to get duration reported… #3761

Merged
merged 4 commits into from
Nov 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public class RabbitMQConsumerInterceptor implements InstanceMethodsAroundInterce
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
ContextCarrier contextCarrier = new ContextCarrier();
String url = (String) objInst.getSkyWalkingDynamicField();
Envelope envelope = (Envelope) allArguments[2];
AMQP.BasicProperties properties = (AMQP.BasicProperties) allArguments[3];
Envelope envelope = (Envelope) allArguments[1];
AMQP.BasicProperties properties = (AMQP.BasicProperties) allArguments[2];
AbstractSpan activeSpan = ContextManager.createEntrySpan(OPERATE_NAME_PREFIX + "Topic/" + envelope.getExchange() + "Queue/" + envelope.getRoutingKey() + CONSUMER_OPERATE_NAME_SUFFIX, null).start(System.currentTimeMillis());
Tags.MQ_BROKER.set(activeSpan,url);
Tags.MQ_TOPIC.set(activeSpan,envelope.getExchange());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.DeclaredInstanceMethodsInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
import org.apache.skywalking.apm.agent.core.plugin.match.MultiClassNameMatch;
import org.apache.skywalking.apm.agent.core.plugin.match.HierarchyMatch;

import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.apache.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType;

public class RabbitMQConsumerInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
public static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.rabbitmq.RabbitMQConsumerInterceptor";
public static final String ENHANCE_CLASS_PRODUCER = "com.rabbitmq.client.impl.ConsumerDispatcher";
public static final String ENHANCE_CLASS_PRODUCER = "com.rabbitmq.client.Consumer";
public static final String ENHANCE_METHOD_DISPATCH = "handleDelivery";
public static final String INTERCEPTOR_CONSTRUCTOR = "org.apache.skywalking.apm.plugin.rabbitmq.RabbitMQProducerAndConsumerConstructorInterceptor";
@Override
Expand All @@ -52,9 +53,9 @@ public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
@Override
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new InstanceMethodsInterceptPoint() {
new DeclaredInstanceMethodsInterceptPoint() {
@Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named(ENHANCE_METHOD_DISPATCH).and(takesArgumentWithType(3,"com.rabbitmq.client.AMQP$BasicProperties"));
return named(ENHANCE_METHOD_DISPATCH).and(takesArgumentWithType(2,"com.rabbitmq.client.AMQP$BasicProperties"));
}


Expand All @@ -63,14 +64,14 @@ public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
}

@Override public boolean isOverrideArgs() {
return true;
return false;
}
}
};
}

@Override
protected ClassMatch enhanceClass() {
return MultiClassNameMatch.byMultiClassMatch(ENHANCE_CLASS_PRODUCER);
return HierarchyMatch.byHierarchyMatch(new String[] {ENHANCE_CLASS_PRODUCER});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void TestRabbitMQConsumerInterceptor() throws Throwable {
Map<String, Object> headers = new HashMap<String, Object>();
headers.put("sw6","1-MS4xLjE1NDM5NzU1OTEwMTQwMDAx-MS4xLjE1NDM5NzU1OTA5OTcwMDAw-0-1-1-IzEyNy4wLjAuMTo1Mjcy-I1JhYmJpdE1RL1RvcGljL1F1ZXVlL3JhYmJpdG1xLXRlc3QvUHJvZHVjZXI=-I1JhYmJpdE1RL1RvcGljL1F1ZXVlL3JhYmJpdG1xLXRlc3QvUHJvZHVjZXI=");
AMQP.BasicProperties.Builder propsBuilder = new AMQP.BasicProperties.Builder();
Object[] arguments = new Object[] {0,0,envelope,propsBuilder.headers(headers).build()};
Object[] arguments = new Object[] {0,envelope,propsBuilder.headers(headers).build()};

rabbitMQConsumerInterceptor.beforeMethod(enhancedInstance,null,arguments,null,null);
rabbitMQConsumerInterceptor.afterMethod(enhancedInstance,null,arguments,null,null);
Expand All @@ -86,7 +86,7 @@ public void TestRabbitMQConsumerInterceptor() throws Throwable {
public void testRabbitMQConsumerInterceptorWithNilHeaders() throws Throwable {
Envelope envelope = new Envelope(1111,false,"","rabbitmq-test");
AMQP.BasicProperties.Builder propsBuilder = new AMQP.BasicProperties.Builder();
Object[] arguments = new Object[] {0,0,envelope,propsBuilder.headers(null).build()};
Object[] arguments = new Object[] {0,envelope,propsBuilder.headers(null).build()};

rabbitMQConsumerInterceptor.beforeMethod(enhancedInstance,null,arguments,null,null);
rabbitMQConsumerInterceptor.afterMethod(enhancedInstance,null,arguments,null,null);
Expand All @@ -99,7 +99,7 @@ public void testRabbitMQConsumerInterceptorWithEmptyHeaders() throws Throwable {
Envelope envelope = new Envelope(1111,false,"","rabbitmq-test");
Map<String, Object> headers = new HashMap<String, Object>();
AMQP.BasicProperties.Builder propsBuilder = new AMQP.BasicProperties.Builder();
Object[] arguments = new Object[] {0,0,envelope,propsBuilder.headers(headers).build()};
Object[] arguments = new Object[] {0,envelope,propsBuilder.headers(headers).build()};

rabbitMQConsumerInterceptor.beforeMethod(enhancedInstance,null,arguments,null,null);
rabbitMQConsumerInterceptor.afterMethod(enhancedInstance,null,arguments,null,null);
Expand Down