From 37dcfe5385bbe6319042e0098fdba443e1bba427 Mon Sep 17 00:00:00 2001 From: Luca Abbati Date: Fri, 30 Aug 2019 12:18:41 +0200 Subject: [PATCH 1/7] Use static operation names for JaxRS instrumentation --- .../jaxrs/JaxRsAnnotationsDecorator.java | 25 +++++++++++++-- .../JaxRsAnnotationsInstrumentation.java | 15 +++++---- ...JaxRsAnnotationsInstrumentationTest.groovy | 31 ++++++++++++++++--- 3 files changed, 58 insertions(+), 13 deletions(-) diff --git a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java index e6a68bfd79..566eda4702 100644 --- a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java +++ b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java @@ -43,6 +43,27 @@ public class JaxRsAnnotationsDecorator extends BaseDecorator { final Span span = scope.span(); Tags.COMPONENT.set(span, "jax-rs"); + String resourceName = getResourceName(method); + if (!resourceName.isEmpty()) { + span.setTag(DDTags.RESOURCE_NAME, resourceName); + } + } + + public void updateCurrentScope(final Scope scope, final Method method) { + String resourceName = getResourceName(method); + final Span span = scope.span(); + if (!resourceName.isEmpty()) { + span.setTag(DDTags.RESOURCE_NAME, resourceName); + } + } + + /** + * Returns the resource name given a JaxRS annotated method. Results are cached so this method can + * be called multiple times without significantly impacting on performance. + * + * @return The result can be an empty string but will never be {@code null}. + */ + private String getResourceName(final Method method) { final Class target = method.getDeclaringClass(); Map classMap = resourceNames.get(target); @@ -61,9 +82,7 @@ public class JaxRsAnnotationsDecorator extends BaseDecorator { classMap.put(method, resourceName); } - if (!resourceName.isEmpty()) { - span.setTag(DDTags.RESOURCE_NAME, resourceName); - } + return resourceName; } private String locateHttpMethod(final Method method) { diff --git a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java index cb3916cf58..12d30fa3a4 100644 --- a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java +++ b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java @@ -10,6 +10,7 @@ import static net.bytebuddy.matcher.ElementMatchers.named; import com.google.auto.service.AutoService; import datadog.trace.agent.tooling.Instrumenter; import io.opentracing.Scope; +import io.opentracing.Tracer; import io.opentracing.util.GlobalTracer; import java.lang.reflect.Method; import java.util.Map; @@ -21,6 +22,8 @@ import net.bytebuddy.matcher.ElementMatcher; @AutoService(Instrumenter.class) public final class JaxRsAnnotationsInstrumentation extends Instrumenter.Default { + private static final String JAX_ENDPOINT_OPERATION_NAME = "jax-rs.endpoint"; + public JaxRsAnnotationsInstrumentation() { super("jax-rs", "jaxrs", "jax-rs-annotations"); } @@ -58,12 +61,12 @@ public final class JaxRsAnnotationsInstrumentation extends Instrumenter.Default @Advice.OnMethodEnter(suppress = Throwable.class) public static Scope nameSpan(@Advice.Origin final Method method) { // Rename the parent span according to the path represented by these annotations. - final Scope scope = GlobalTracer.get().scopeManager().active(); - DECORATE.updateParent(scope, method); - - // Now create a span representing the method execution. - final String operationName = DECORATE.spanNameForMethod(method); - return DECORATE.afterStart(GlobalTracer.get().buildSpan(operationName).startActive(true)); + final Scope parent = GlobalTracer.get().scopeManager().active(); + Tracer.SpanBuilder span = GlobalTracer.get().buildSpan(JAX_ENDPOINT_OPERATION_NAME); + Scope scope = span.startActive(true); + DECORATE.updateParent(parent, method); + DECORATE.updateCurrentScope(scope, method); + return DECORATE.afterStart(scope); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) diff --git a/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy b/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy index 5233c940e4..7440b2527f 100644 --- a/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy +++ b/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy @@ -14,6 +14,29 @@ import static datadog.trace.instrumentation.jaxrs.JaxRsAnnotationsDecorator.DECO class JaxRsAnnotationsInstrumentationTest extends AgentTestRunner { + def "instrumentation can be used as root span and relevant tags are set"() { + setup: + new Jax() { + @POST + @Path("/a") + void call() {} + }.call() + + expect: + assertTraces(1) { + trace(0, 1) { + span(0) { + operationName "jax-rs.endpoint" + resourceName 'POST /a' + tags { + "$Tags.COMPONENT.key" "jax-rs-controller" + defaultTags() + } + } + } + } + } + def "span named '#name' from annotations on class"() { setup: def startingCacheSize = DECORATE.resourceNames.size() @@ -34,8 +57,8 @@ class JaxRsAnnotationsInstrumentationTest extends AgentTestRunner { } } span(1) { - operationName "${className}.call" - resourceName "${className}.call" + operationName "jax-rs.endpoint" + resourceName name childOf span(0) tags { "$Tags.COMPONENT.key" "jax-rs-controller" @@ -58,8 +81,8 @@ class JaxRsAnnotationsInstrumentationTest extends AgentTestRunner { DECORATE.resourceNames.get(obj.class).size() == 1 where: - name | obj - "/a" | new Jax() { + name | obj + "/a" | new Jax() { @Path("/a") void call() {} } From d9f09e6af5f8e05ae3f88c728da5ebf3bdcbe1da Mon Sep 17 00:00:00 2001 From: Luca Abbati Date: Fri, 30 Aug 2019 12:23:41 +0200 Subject: [PATCH 2/7] Minor refacotrings to JaxRsAnnotationsInstrumentation.java --- .../instrumentation/jaxrs/JaxRsAnnotationsDecorator.java | 2 +- .../jaxrs/JaxRsAnnotationsInstrumentation.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java index 566eda4702..0949e11af3 100644 --- a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java +++ b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java @@ -59,7 +59,7 @@ public class JaxRsAnnotationsDecorator extends BaseDecorator { /** * Returns the resource name given a JaxRS annotated method. Results are cached so this method can - * be called multiple times without significantly impacting on performance. + * be called multiple times without significantly impacting performance. * * @return The result can be an empty string but will never be {@code null}. */ diff --git a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java index 12d30fa3a4..988294bddf 100644 --- a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java +++ b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java @@ -60,10 +60,10 @@ public final class JaxRsAnnotationsInstrumentation extends Instrumenter.Default @Advice.OnMethodEnter(suppress = Throwable.class) public static Scope nameSpan(@Advice.Origin final Method method) { + Tracer tracer = GlobalTracer.get(); // Rename the parent span according to the path represented by these annotations. - final Scope parent = GlobalTracer.get().scopeManager().active(); - Tracer.SpanBuilder span = GlobalTracer.get().buildSpan(JAX_ENDPOINT_OPERATION_NAME); - Scope scope = span.startActive(true); + final Scope parent = tracer.scopeManager().active(); + Scope scope = tracer.buildSpan(JAX_ENDPOINT_OPERATION_NAME).startActive(true); DECORATE.updateParent(parent, method); DECORATE.updateCurrentScope(scope, method); return DECORATE.afterStart(scope); From 359ac9c190344c1610434fb3c27785fdc842fa7a Mon Sep 17 00:00:00 2001 From: Luca Abbati Date: Fri, 30 Aug 2019 13:05:24 +0200 Subject: [PATCH 3/7] Fix indentation in JaxRS instrumentation test --- .../test/groovy/JaxRsAnnotationsInstrumentationTest.groovy | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy b/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy index 7440b2527f..3cecca43e8 100644 --- a/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy +++ b/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy @@ -81,8 +81,8 @@ class JaxRsAnnotationsInstrumentationTest extends AgentTestRunner { DECORATE.resourceNames.get(obj.class).size() == 1 where: - name | obj - "/a" | new Jax() { + name | obj + "/a" | new Jax() { @Path("/a") void call() {} } From d877039fa77c95ce89c181e129beb7773ffc134a Mon Sep 17 00:00:00 2001 From: Luca Abbati Date: Fri, 30 Aug 2019 13:57:30 +0200 Subject: [PATCH 4/7] Set span.type = web on jax-rs endpoints --- .../instrumentation/jaxrs/JaxRsAnnotationsDecorator.java | 4 +++- .../test/groovy/JaxRsAnnotationsInstrumentationTest.groovy | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java index 0949e11af3..653a3f0220 100644 --- a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java +++ b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java @@ -3,6 +3,7 @@ package datadog.trace.instrumentation.jaxrs; import static datadog.trace.bootstrap.WeakMap.Provider.newWeakMap; import datadog.trace.agent.decorator.BaseDecorator; +import datadog.trace.api.DDSpanTypes; import datadog.trace.api.DDTags; import datadog.trace.bootstrap.WeakMap; import io.opentracing.Scope; @@ -50,8 +51,9 @@ public class JaxRsAnnotationsDecorator extends BaseDecorator { } public void updateCurrentScope(final Scope scope, final Method method) { - String resourceName = getResourceName(method); final Span span = scope.span(); + span.setTag(DDTags.SPAN_TYPE, DDSpanTypes.HTTP_SERVER); + String resourceName = getResourceName(method); if (!resourceName.isEmpty()) { span.setTag(DDTags.RESOURCE_NAME, resourceName); } diff --git a/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy b/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy index 3cecca43e8..e7cfa80b8e 100644 --- a/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy +++ b/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy @@ -27,7 +27,8 @@ class JaxRsAnnotationsInstrumentationTest extends AgentTestRunner { trace(0, 1) { span(0) { operationName "jax-rs.endpoint" - resourceName 'POST /a' + resourceName "POST /a" + spanType "web" tags { "$Tags.COMPONENT.key" "jax-rs-controller" defaultTags() @@ -59,6 +60,7 @@ class JaxRsAnnotationsInstrumentationTest extends AgentTestRunner { span(1) { operationName "jax-rs.endpoint" resourceName name + spanType "web" childOf span(0) tags { "$Tags.COMPONENT.key" "jax-rs-controller" From 0b71c6bbe54cda7bce86c31e3ad28c9253b87834 Mon Sep 17 00:00:00 2001 From: Luca Abbati Date: Fri, 30 Aug 2019 17:57:03 +0200 Subject: [PATCH 5/7] Rename operation jax-rs.endpoint to jax-rs.request --- .../jaxrs/JaxRsAnnotationsInstrumentation.java | 2 +- .../test/groovy/JaxRsAnnotationsInstrumentationTest.groovy | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java index 988294bddf..5cfde85b5b 100644 --- a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java +++ b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java @@ -22,7 +22,7 @@ import net.bytebuddy.matcher.ElementMatcher; @AutoService(Instrumenter.class) public final class JaxRsAnnotationsInstrumentation extends Instrumenter.Default { - private static final String JAX_ENDPOINT_OPERATION_NAME = "jax-rs.endpoint"; + private static final String JAX_ENDPOINT_OPERATION_NAME = "jax-rs.request"; public JaxRsAnnotationsInstrumentation() { super("jax-rs", "jaxrs", "jax-rs-annotations"); diff --git a/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy b/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy index e7cfa80b8e..6c475c2310 100644 --- a/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy +++ b/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy @@ -26,7 +26,7 @@ class JaxRsAnnotationsInstrumentationTest extends AgentTestRunner { assertTraces(1) { trace(0, 1) { span(0) { - operationName "jax-rs.endpoint" + operationName "jax-rs.request" resourceName "POST /a" spanType "web" tags { @@ -58,7 +58,7 @@ class JaxRsAnnotationsInstrumentationTest extends AgentTestRunner { } } span(1) { - operationName "jax-rs.endpoint" + operationName "jax-rs.request" resourceName name spanType "web" childOf span(0) From 0b0d75bcf2fdb35e4e7b88d9b088d3491d39b19f Mon Sep 17 00:00:00 2001 From: Luca Abbati Date: Fri, 30 Aug 2019 19:28:55 +0200 Subject: [PATCH 6/7] Jax-rs user METHOD PATH as resource only when root span --- .../jaxrs/JaxRsAnnotationsDecorator.java | 30 +++++++++++++++---- .../JaxRsAnnotationsInstrumentation.java | 3 +- ...JaxRsAnnotationsInstrumentationTest.groovy | 6 ++-- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java index 653a3f0220..b83bc7a023 100644 --- a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java +++ b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java @@ -37,25 +37,43 @@ public class JaxRsAnnotationsDecorator extends BaseDecorator { return "jax-rs-controller"; } - public void updateParent(final Scope scope, final Method method) { + public void updateScope(final Scope scope, final Scope parent, final Method method) { + String resourceName = getResourceName(method); + updateParent(parent, resourceName); + + final Span span = scope.span(); + span.setTag(DDTags.SPAN_TYPE, DDSpanTypes.HTTP_SERVER); + + // When jax-rs is the root scope, then we + boolean isRootScope = parent == null; + if (isRootScope && !resourceName.isEmpty()) { + span.setTag(DDTags.RESOURCE_NAME, resourceName); + } else { + span.setTag(DDTags.RESOURCE_NAME, DECORATE.spanNameForMethod(method)); + } + } + + private void updateParent(final Scope scope, final String resourceName) { if (scope == null) { return; } final Span span = scope.span(); Tags.COMPONENT.set(span, "jax-rs"); - String resourceName = getResourceName(method); if (!resourceName.isEmpty()) { span.setTag(DDTags.RESOURCE_NAME, resourceName); } } - public void updateCurrentScope(final Scope scope, final Method method) { + public void updateCurrentScope(final Scope scope, final Method method, boolean isRootScope) { final Span span = scope.span(); span.setTag(DDTags.SPAN_TYPE, DDSpanTypes.HTTP_SERVER); - String resourceName = getResourceName(method); - if (!resourceName.isEmpty()) { - span.setTag(DDTags.RESOURCE_NAME, resourceName); + + if (isRootScope) { + String resourceName = getResourceName(method); + if (!resourceName.isEmpty()) { + span.setTag(DDTags.RESOURCE_NAME, resourceName); + } } } diff --git a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java index 5cfde85b5b..220092185e 100644 --- a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java +++ b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java @@ -64,8 +64,7 @@ public final class JaxRsAnnotationsInstrumentation extends Instrumenter.Default // Rename the parent span according to the path represented by these annotations. final Scope parent = tracer.scopeManager().active(); Scope scope = tracer.buildSpan(JAX_ENDPOINT_OPERATION_NAME).startActive(true); - DECORATE.updateParent(parent, method); - DECORATE.updateCurrentScope(scope, method); + DECORATE.updateScope(scope, parent, method); return DECORATE.afterStart(scope); } diff --git a/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy b/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy index 6c475c2310..53a33d0980 100644 --- a/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy +++ b/dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JaxRsAnnotationsInstrumentationTest.groovy @@ -14,7 +14,7 @@ import static datadog.trace.instrumentation.jaxrs.JaxRsAnnotationsDecorator.DECO class JaxRsAnnotationsInstrumentationTest extends AgentTestRunner { - def "instrumentation can be used as root span and relevant tags are set"() { + def "instrumentation can be used as root span and resource is set to METHOD PATH"() { setup: new Jax() { @POST @@ -38,7 +38,7 @@ class JaxRsAnnotationsInstrumentationTest extends AgentTestRunner { } } - def "span named '#name' from annotations on class"() { + def "span named '#name' from annotations on class when is not root span"() { setup: def startingCacheSize = DECORATE.resourceNames.size() runUnderTrace("test") { @@ -59,7 +59,7 @@ class JaxRsAnnotationsInstrumentationTest extends AgentTestRunner { } span(1) { operationName "jax-rs.request" - resourceName name + resourceName "${className}.call" spanType "web" childOf span(0) tags { From aec22640f3b2f1e05da03340358bb24c0e113ea3 Mon Sep 17 00:00:00 2001 From: Tyler Benson Date: Fri, 30 Aug 2019 13:52:54 -0400 Subject: [PATCH 7/7] Remove unused method and finish comment Also adjust naming slightly. --- .../jaxrs/JaxRsAnnotationsDecorator.java | 22 +++++-------------- .../JaxRsAnnotationsInstrumentation.java | 6 ++--- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java index b83bc7a023..e271768488 100644 --- a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java +++ b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsDecorator.java @@ -37,15 +37,15 @@ public class JaxRsAnnotationsDecorator extends BaseDecorator { return "jax-rs-controller"; } - public void updateScope(final Scope scope, final Scope parent, final Method method) { - String resourceName = getResourceName(method); + public void onControllerStart(final Scope scope, final Scope parent, final Method method) { + final String resourceName = getPathResourceName(method); updateParent(parent, resourceName); final Span span = scope.span(); span.setTag(DDTags.SPAN_TYPE, DDSpanTypes.HTTP_SERVER); - // When jax-rs is the root scope, then we - boolean isRootScope = parent == null; + // When jax-rs is the root, we want to name using the path, otherwise use the class/method. + final boolean isRootScope = parent == null; if (isRootScope && !resourceName.isEmpty()) { span.setTag(DDTags.RESOURCE_NAME, resourceName); } else { @@ -65,25 +65,13 @@ public class JaxRsAnnotationsDecorator extends BaseDecorator { } } - public void updateCurrentScope(final Scope scope, final Method method, boolean isRootScope) { - final Span span = scope.span(); - span.setTag(DDTags.SPAN_TYPE, DDSpanTypes.HTTP_SERVER); - - if (isRootScope) { - String resourceName = getResourceName(method); - if (!resourceName.isEmpty()) { - span.setTag(DDTags.RESOURCE_NAME, resourceName); - } - } - } - /** * Returns the resource name given a JaxRS annotated method. Results are cached so this method can * be called multiple times without significantly impacting performance. * * @return The result can be an empty string but will never be {@code null}. */ - private String getResourceName(final Method method) { + private String getPathResourceName(final Method method) { final Class target = method.getDeclaringClass(); Map classMap = resourceNames.get(target); diff --git a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java index 220092185e..20aa86fda9 100644 --- a/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java +++ b/dd-java-agent/instrumentation/jax-rs-annotations/src/main/java/datadog/trace/instrumentation/jaxrs/JaxRsAnnotationsInstrumentation.java @@ -60,11 +60,11 @@ public final class JaxRsAnnotationsInstrumentation extends Instrumenter.Default @Advice.OnMethodEnter(suppress = Throwable.class) public static Scope nameSpan(@Advice.Origin final Method method) { - Tracer tracer = GlobalTracer.get(); + final Tracer tracer = GlobalTracer.get(); // Rename the parent span according to the path represented by these annotations. final Scope parent = tracer.scopeManager().active(); - Scope scope = tracer.buildSpan(JAX_ENDPOINT_OPERATION_NAME).startActive(true); - DECORATE.updateScope(scope, parent, method); + final Scope scope = tracer.buildSpan(JAX_ENDPOINT_OPERATION_NAME).startActive(true); + DECORATE.onControllerStart(scope, parent, method); return DECORATE.afterStart(scope); }