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 26a5d3f83e..b58b1c3dc8 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 @@ -61,16 +61,19 @@ public final class JaxRsAnnotationsInstrumentation extends Instrumenter.Default public static Scope nameSpan(@Advice.Origin final Method method) { // TODO: do we need caching for this? - final LinkedList classPaths = new LinkedList<>(); + final LinkedList paths = new LinkedList<>(); Class target = method.getDeclaringClass(); while (target != Object.class) { final Path annotation = target.getAnnotation(Path.class); if (annotation != null) { - classPaths.push(annotation); + paths.push(annotation); } target = target.getSuperclass(); } final Path methodPath = method.getAnnotation(Path.class); + if (methodPath != null) { + paths.add(methodPath); + } String httpMethod = null; for (final Annotation ann : method.getDeclaredAnnotations()) { if (ann.annotationType().getAnnotation(HttpMethod.class) != null) { @@ -83,11 +86,15 @@ public final class JaxRsAnnotationsInstrumentation extends Instrumenter.Default resourceNameBuilder.append(httpMethod); resourceNameBuilder.append(" "); } - for (final Path classPath : classPaths) { - resourceNameBuilder.append(classPath.value()); - } - if (methodPath != null) { - resourceNameBuilder.append(methodPath.value()); + Path last = null; + for (final Path path : paths) { + if (path.value().startsWith("/") || (last != null && last.value().endsWith("/"))) { + resourceNameBuilder.append(path.value()); + } else { + resourceNameBuilder.append("/"); + resourceNameBuilder.append(path.value()); + } + last = path; } final String resourceName = resourceNameBuilder.toString().trim(); 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 c70fa796dc..db65f2398e 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 @@ -82,7 +82,7 @@ class JaxRsAnnotationsInstrumentationTest extends AgentTestRunner { @DELETE void call() {} } - "POST /abstract/child" | new ChildClassWithPath() + "POST /abstract/child/call" | new ChildClassWithPath() className = getName(obj.class) } @@ -138,8 +138,9 @@ class JaxRsAnnotationsInstrumentationTest extends AgentTestRunner { abstract void call() } - @Path("/child") + @Path("child") class ChildClassWithPath extends AbstractClassWithPath { + @Path("call") @POST void call() {} }