-
Notifications
You must be signed in to change notification settings - Fork 845
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jersey and cxf server span naming (#2919)
- Loading branch information
Showing
15 changed files
with
250 additions
and
36 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...nt/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/JaxRsPathUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0; | ||
|
||
public final class JaxRsPathUtil { | ||
private JaxRsPathUtil() {} | ||
|
||
public static String normalizePath(String path) { | ||
// ensure that non-empty path starts with / | ||
if (path == null || "/".equals(path)) { | ||
path = ""; | ||
} else if (!path.startsWith("/")) { | ||
path = "/" + path; | ||
} | ||
// remove trailing / | ||
if (path.endsWith("/")) { | ||
path = path.substring(0, path.length() - 1); | ||
} | ||
|
||
return path; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/CxfJaxRsInvokerInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0; | ||
|
||
import static java.util.Collections.singletonMap; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.javaagent.tooling.TypeInstrumentation; | ||
import java.util.Map; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.method.MethodDescription; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.apache.cxf.message.Exchange; | ||
|
||
public class CxfJaxRsInvokerInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("org.apache.cxf.jaxrs.JAXRSInvoker"); | ||
} | ||
|
||
@Override | ||
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() { | ||
return singletonMap( | ||
named("invoke") | ||
.and(takesArgument(0, named("org.apache.cxf.message.Exchange"))) | ||
.and(takesArgument(1, Object.class)) | ||
.and(takesArgument(2, Object.class)), | ||
CxfJaxRsInvokerInstrumentation.class.getName() + "$InvokeAdvice"); | ||
} | ||
|
||
public static class InvokeAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.Argument(0) Exchange exchange, @Advice.Local("otelScope") Scope scope) { | ||
Context context = CxfTracingUtil.updateServerSpanName(exchange); | ||
if (context != null) { | ||
scope = context.makeCurrent(); | ||
} | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void stopSpan(@Advice.Local("otelScope") Scope scope) { | ||
if (scope != null) { | ||
scope.close(); | ||
} | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...t/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/CxfTracingUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0; | ||
|
||
import static io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0.JaxRsPathUtil.normalizePath; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming; | ||
import io.opentelemetry.instrumentation.api.servlet.ServletContextPath; | ||
import io.opentelemetry.instrumentation.api.tracer.ServerSpan; | ||
import io.opentelemetry.javaagent.instrumentation.api.jaxrs.JaxrsContextPath; | ||
import org.apache.cxf.jaxrs.model.ClassResourceInfo; | ||
import org.apache.cxf.jaxrs.model.OperationResourceInfo; | ||
import org.apache.cxf.jaxrs.model.URITemplate; | ||
import org.apache.cxf.message.Exchange; | ||
|
||
public final class CxfTracingUtil { | ||
|
||
private CxfTracingUtil() {} | ||
|
||
public static Context updateServerSpanName(Exchange exchange) { | ||
Context context = Context.current(); | ||
Span serverSpan = ServerSpan.fromContextOrNull(context); | ||
if (serverSpan == null) { | ||
return null; | ||
} | ||
|
||
OperationResourceInfo ori = exchange.get(OperationResourceInfo.class); | ||
ClassResourceInfo cri = ori.getClassResourceInfo(); | ||
String name = getName(cri.getURITemplate(), ori.getURITemplate()); | ||
if (name.isEmpty()) { | ||
return null; | ||
} | ||
|
||
serverSpan.updateName( | ||
ServletContextPath.prepend(context, JaxrsContextPath.prepend(context, name))); | ||
// mark span name as updated from controller to avoid JaxRsAnnotationsTracer updating it | ||
ServerSpanNaming.updateSource(context, ServerSpanNaming.Source.CONTROLLER); | ||
|
||
return JaxrsContextPath.init(context, JaxrsContextPath.prepend(context, name)); | ||
} | ||
|
||
private static String getName(URITemplate classTemplate, URITemplate operationTemplate) { | ||
String classPath = normalize(classTemplate); | ||
String operationPath = normalize(operationTemplate); | ||
|
||
return classPath + operationPath; | ||
} | ||
|
||
private static String normalize(URITemplate uriTemplate) { | ||
if (uriTemplate == null) { | ||
return ""; | ||
} | ||
|
||
return normalizePath(uriTemplate.getValue()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...y/javaagent/instrumentation/jaxrs/v2_0/JerseyResourceMethodDispatcherInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0; | ||
|
||
import static java.util.Collections.singletonMap; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import io.opentelemetry.javaagent.tooling.TypeInstrumentation; | ||
import java.util.Map; | ||
import javax.ws.rs.core.Request; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.method.MethodDescription; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class JerseyResourceMethodDispatcherInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher"); | ||
} | ||
|
||
@Override | ||
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() { | ||
return singletonMap( | ||
named("dispatch") | ||
.and( | ||
takesArgument( | ||
1, | ||
named("javax.ws.rs.core.Request") | ||
.or(named("org.glassfish.jersey.server.ContainerRequest")))), | ||
JerseyResourceMethodDispatcherInstrumentation.class.getName() + "$DispatchAdvice"); | ||
} | ||
|
||
public static class DispatchAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter(@Advice.Argument(1) Request request) { | ||
JerseyTracingUtil.updateServerSpanName(request); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...rc/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/JerseyTracingUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0; | ||
|
||
import static io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0.JaxRsPathUtil.normalizePath; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming; | ||
import io.opentelemetry.instrumentation.api.servlet.ServletContextPath; | ||
import io.opentelemetry.instrumentation.api.tracer.ServerSpan; | ||
import io.opentelemetry.javaagent.instrumentation.api.jaxrs.JaxrsContextPath; | ||
import java.util.Optional; | ||
import javax.ws.rs.core.Request; | ||
import javax.ws.rs.core.UriInfo; | ||
import org.glassfish.jersey.server.ContainerRequest; | ||
import org.glassfish.jersey.server.ExtendedUriInfo; | ||
|
||
public class JerseyTracingUtil { | ||
|
||
public static void updateServerSpanName(Request request) { | ||
Context context = Context.current(); | ||
Span serverSpan = ServerSpan.fromContextOrNull(context); | ||
if (serverSpan == null) { | ||
return; | ||
} | ||
|
||
ContainerRequest containerRequest = (ContainerRequest) request; | ||
UriInfo uriInfo = containerRequest.getUriInfo(); | ||
ExtendedUriInfo extendedUriInfo = (ExtendedUriInfo) uriInfo; | ||
Optional<String> name = | ||
extendedUriInfo.getMatchedTemplates().stream() | ||
.map((uriTemplate) -> normalizePath(uriTemplate.getTemplate())) | ||
.reduce((a, b) -> b + a); | ||
if (!name.isPresent()) { | ||
return; | ||
} | ||
|
||
serverSpan.updateName( | ||
ServletContextPath.prepend(context, JaxrsContextPath.prepend(context, name.get()))); | ||
// mark span name as updated from controller to avoid JaxRsAnnotationsTracer updating it | ||
ServerSpanNaming.updateSource(context, ServerSpanNaming.Source.CONTROLLER); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters