Better JMS span names (#626)

This commit is contained in:
Trask Stalnaker 2020-07-02 10:31:31 -07:00 committed by GitHub
parent dce1d4026e
commit 10e917bfa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 110 additions and 17 deletions

View File

@ -180,7 +180,7 @@ class JMS2Test extends AgentTestRunner {
trace(0, 1) { // Consumer trace
span(0) {
parent()
operationName "jms.receiveNoWait"
operationName expectedSpanName
spanKind CLIENT
errored false
tags {
@ -212,7 +212,7 @@ class JMS2Test extends AgentTestRunner {
trace(0, 1) { // Consumer trace
span(0) {
parent()
operationName "jms.receive"
operationName expectedSpanName
spanKind CLIENT
errored false
tags {

View File

@ -19,7 +19,6 @@ package io.opentelemetry.auto.instrumentation.jms;
import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.auto.bootstrap.instrumentation.decorator.ClientDecorator;
import io.opentelemetry.trace.Tracer;
import java.lang.reflect.Method;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.Queue;
@ -37,10 +36,6 @@ public class JMSDecorator extends ClientDecorator {
return toSpanName(message, null);
}
public String spanNameForReceive(final Method method) {
return "jms." + method.getName();
}
public String spanNameForConsumer(final Message message) {
return toSpanName(message, null);
}
@ -60,18 +55,22 @@ public class JMSDecorator extends ClientDecorator {
if (jmsDestination == null) {
jmsDestination = destination;
}
return toSpanName(jmsDestination);
}
public static String toSpanName(Destination destination) {
try {
if (jmsDestination instanceof Queue) {
final String queueName = ((Queue) jmsDestination).getQueueName();
if (jmsDestination instanceof TemporaryQueue || queueName.startsWith(TIBCO_TMP_PREFIX)) {
if (destination instanceof Queue) {
final String queueName = ((Queue) destination).getQueueName();
if (destination instanceof TemporaryQueue || queueName.startsWith(TIBCO_TMP_PREFIX)) {
return "queue/<temporary>";
} else {
return "queue/" + queueName;
}
}
if (jmsDestination instanceof Topic) {
final String topicName = ((Topic) jmsDestination).getTopicName();
if (jmsDestination instanceof TemporaryTopic || topicName.startsWith(TIBCO_TMP_PREFIX)) {
if (destination instanceof Topic) {
final String topicName = ((Topic) destination).getTopicName();
if (destination instanceof TemporaryTopic || topicName.startsWith(TIBCO_TMP_PREFIX)) {
return "topic/<temporary>";
} else {
return "topic/" + topicName;

View File

@ -24,11 +24,13 @@ import static io.opentelemetry.auto.tooling.ClassLoaderMatcher.hasClassesNamed;
import static io.opentelemetry.auto.tooling.bytebuddy.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.trace.Span.Kind.CLIENT;
import static io.opentelemetry.trace.TracingContextUtils.currentContextWith;
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.takesArguments;
import com.google.auto.service.AutoService;
import io.opentelemetry.auto.bootstrap.InstrumentationContext;
import io.opentelemetry.auto.tooling.Instrumenter;
import io.opentelemetry.context.Scope;
import io.opentelemetry.trace.Span;
@ -83,6 +85,11 @@ public final class JMSMessageConsumerInstrumentation extends Instrumenter.Defaul
return transformers;
}
@Override
public Map<String, String> contextStore() {
return singletonMap("javax.jms.MessageConsumer", "java.lang.String");
}
public static class ConsumerAdvice {
@Advice.OnMethodEnter
@ -97,9 +104,12 @@ public final class JMSMessageConsumerInstrumentation extends Instrumenter.Defaul
@Advice.Origin final Method method,
@Advice.Return final Message message,
@Advice.Thrown final Throwable throwable) {
final String spanName;
String spanName;
if (message == null) {
spanName = DECORATE.spanNameForReceive(method);
spanName = InstrumentationContext.get(MessageConsumer.class, String.class).get(consumer);
if (spanName == null) {
spanName = "destination";
}
} else {
spanName = DECORATE.spanNameForReceive(message);
}

View File

@ -0,0 +1,84 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.opentelemetry.auto.instrumentation.jms;
import static io.opentelemetry.auto.tooling.ClassLoaderMatcher.hasClassesNamed;
import static io.opentelemetry.auto.tooling.bytebuddy.matcher.AgentElementMatchers.implementsInterface;
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 com.google.auto.service.AutoService;
import io.opentelemetry.auto.bootstrap.InstrumentationContext;
import io.opentelemetry.auto.tooling.Instrumenter;
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;
@AutoService(Instrumenter.class)
public final class JMSSessionInstrumentation extends Instrumenter.Default {
public JMSSessionInstrumentation() {
super("jms", "jms-1", "jms-2");
}
@Override
public ElementMatcher<ClassLoader> classLoaderMatcher() {
// Optimization for expensive typeMatcher.
return hasClassesNamed("javax.jms.Session");
}
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return implementsInterface(named("javax.jms.Session"));
}
@Override
public String[] helperClassNames() {
return new String[] {packageName + ".JMSDecorator"};
}
@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");
}
@Override
public Map<String, String> contextStore() {
return singletonMap("javax.jms.MessageConsumer", "java.lang.String");
}
public static class ConsumerAdvice {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(
@Advice.Argument(0) final Destination destination,
@Advice.Return final MessageConsumer consumer) {
String spanName = JMSDecorator.toSpanName(destination);
InstrumentationContext.get(MessageConsumer.class, String.class).put(consumer, spanName);
}
}
}

View File

@ -143,7 +143,7 @@ class JMS1Test extends AgentTestRunner {
trace(0, 1) { // Consumer trace
span(0) {
parent()
operationName "jms.receiveNoWait"
operationName expectedSpanName
spanKind CLIENT
errored false
tags {
@ -175,7 +175,7 @@ class JMS1Test extends AgentTestRunner {
trace(0, 1) { // Consumer trace
span(0) {
parent()
operationName "jms.receive"
operationName expectedSpanName
spanKind CLIENT
errored false
tags {