JMS instrumentation: do not create spans if no messages are received (#2091)

* #1989 JMS instrumentation: do not create spans if no messages are received

Signed-off-by: Sergei Malafeev <sergei@malafeev.org>

* remove the redundant else

Signed-off-by: Sergei Malafeev <sergei@malafeev.org>

* remove JmsSessionInstrumentation

Signed-off-by: Sergei Malafeev <sergei@malafeev.org>

Co-authored-by: Anuraag Agrawal <aanuraag@amazon.co.jp>
This commit is contained in:
Sergei Malafeev 2021-01-25 14:42:32 +08:00 committed by GitHub
parent 4652ce183d
commit 54d00026be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 134 deletions

View File

@ -161,22 +161,8 @@ class Jms2Test extends AgentTestRunner {
expect: expect:
receivedMessage == null receivedMessage == null
assertTraces(1) { // span is not created if no message is received
trace(0, 1) { // Consumer trace assertTraces(0,{})
span(0) {
hasNoParent()
name destinationName + " receive"
kind CONSUMER
errored false
attributes {
"${SemanticAttributes.MESSAGING_SYSTEM.key}" "jms"
"${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" destinationType
"${SemanticAttributes.MESSAGING_DESTINATION.key}" destinationName
"${SemanticAttributes.MESSAGING_OPERATION.key}" "receive"
}
}
}
}
cleanup: cleanup:
consumer.close() consumer.close()
@ -196,23 +182,8 @@ class Jms2Test extends AgentTestRunner {
expect: expect:
receivedMessage == null receivedMessage == null
assertTraces(1) { // span is not created if no message is received
trace(0, 1) { // Consumer trace assertTraces(0, {})
span(0) {
hasNoParent()
name destinationName + " receive"
kind CONSUMER
errored false
attributes {
"${SemanticAttributes.MESSAGING_SYSTEM.key}" "jms"
"${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" destinationType
"${SemanticAttributes.MESSAGING_DESTINATION.key}" destinationName
"${SemanticAttributes.MESSAGING_OPERATION.key}" "receive"
}
}
}
}
cleanup: cleanup:
consumer.close() consumer.close()

View File

@ -25,8 +25,7 @@ public class JmsInstrumentationModule extends InstrumentationModule {
return asList( return asList(
new JmsMessageConsumerInstrumentation(), new JmsMessageConsumerInstrumentation(),
new JmsMessageListenerInstrumentation(), new JmsMessageListenerInstrumentation(),
new JmsMessageProducerInstrumentation(), new JmsMessageProducerInstrumentation());
new JmsSessionInstrumentation());
} }
@Override @Override

View File

@ -13,12 +13,10 @@ import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.Span;
import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext;
import io.opentelemetry.javaagent.tooling.TypeInstrumentation; import io.opentelemetry.javaagent.tooling.TypeInstrumentation;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.jms.Message; import javax.jms.Message;
import javax.jms.MessageConsumer;
import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.description.type.TypeDescription;
@ -57,21 +55,15 @@ public class JmsMessageConsumerInstrumentation implements TypeInstrumentation {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan( public static void stopSpan(
@Advice.This MessageConsumer consumer,
@Advice.Enter long startTime, @Advice.Enter long startTime,
@Advice.Return Message message, @Advice.Return Message message,
@Advice.Thrown Throwable throwable) { @Advice.Thrown Throwable throwable) {
MessageDestination destination; MessageDestination destination;
if (message == null) { if (message == null) {
destination = // Do not create span when no message is received
InstrumentationContext.get(MessageConsumer.class, MessageDestination.class) return;
.get(consumer);
if (destination == null) {
destination = MessageDestination.UNKNOWN;
} }
} else {
destination = tracer().extractDestination(message, null); destination = tracer().extractDestination(message, null);
}
Span span = tracer().startConsumerSpan(destination, "receive", message, startTime); Span span = tracer().startConsumerSpan(destination, "receive", message, startTime);

View File

@ -1,56 +0,0 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.jms;
import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.ClassLoaderMatcher.hasClassesNamed;
import static java.util.Collections.singletonMap;
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.instrumentation.api.InstrumentationContext;
import io.opentelemetry.javaagent.tooling.TypeInstrumentation;
import java.util.Map;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
public class JmsSessionInstrumentation implements TypeInstrumentation {
@Override
public ElementMatcher<ClassLoader> classLoaderOptimization() {
return hasClassesNamed("javax.jms.Session");
}
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return implementsInterface(named("javax.jms.Session"));
}
@Override
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
return singletonMap(
named("createConsumer")
.and(takesArgument(0, named("javax.jms.Destination")))
.and(isPublic()),
JmsSessionInstrumentation.class.getName() + "$ConsumerAdvice");
}
public static class ConsumerAdvice {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(
@Advice.Argument(0) Destination destination, @Advice.Return MessageConsumer consumer) {
MessageDestination messageDestination = JmsTracer.extractMessageDestination(destination);
InstrumentationContext.get(MessageConsumer.class, MessageDestination.class)
.put(consumer, messageDestination);
}
}
}

View File

@ -134,22 +134,8 @@ class Jms1Test extends AgentTestRunner {
expect: expect:
receivedMessage == null receivedMessage == null
assertTraces(1) { // span is not created if no message is received
trace(0, 1) { // Consumer trace assertTraces(0, {})
span(0) {
hasNoParent()
name destinationName + " receive"
kind CONSUMER
errored false
attributes {
"${SemanticAttributes.MESSAGING_SYSTEM.key}" "jms"
"${SemanticAttributes.MESSAGING_DESTINATION.key}" destinationName
"${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" destinationType
"${SemanticAttributes.MESSAGING_OPERATION.key}" "receive"
}
}
}
}
cleanup: cleanup:
consumer.close() consumer.close()
@ -169,22 +155,8 @@ class Jms1Test extends AgentTestRunner {
expect: expect:
receivedMessage == null receivedMessage == null
assertTraces(1) { // span is not created if no message is received
trace(0, 1) { // Consumer trace assertTraces(0, {})
span(0) {
hasNoParent()
name destinationName + " receive"
kind CONSUMER
errored false
attributes {
"${SemanticAttributes.MESSAGING_SYSTEM.key}" "jms"
"${SemanticAttributes.MESSAGING_DESTINATION.key}" destinationName
"${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" destinationType
"${SemanticAttributes.MESSAGING_OPERATION.key}" "receive"
}
}
}
}
cleanup: cleanup:
consumer.close() consumer.close()