Skip to content

Commit

Permalink
Add instrumentation for JAX-RS 3.0 (#6136)
Browse files Browse the repository at this point in the history
* Add instrumentation for JAX-RS 3.0

* set min java version to 11 for jax-rs 3.0

* exclude broken version

* fix muzzle range

* include correct api

* fix muzzle

* fix muzzle

* remove generics

* share test code
  • Loading branch information
laurit committed Jun 3, 2022
1 parent 157ab7f commit cd2e11a
Show file tree
Hide file tree
Showing 88 changed files with 3,311 additions and 717 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.jaxrs.JaxrsConstants;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import net.bytebuddy.asm.Advice;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpRouteHolder;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpRouteSource;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.instrumentation.jaxrs.JaxrsConstants;
import io.opentelemetry.javaagent.instrumentation.jaxrs.JaxrsServerSpanNaming;
import java.lang.reflect.Method;
import javax.ws.rs.container.ContainerRequestContext;
import net.bytebuddy.asm.Advice;
Expand All @@ -38,7 +40,7 @@ public static class ContainerRequestContextAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void createGenericSpan(
@Advice.This ContainerRequestContext requestContext,
@Local("otelHandlerData") HandlerData handlerData,
@Local("otelHandlerData") Jaxrs2HandlerData handlerData,
@Local("otelContext") Context context,
@Local("otelScope") Scope scope) {
if (requestContext.getProperty(JaxrsConstants.ABORT_HANDLED) != null) {
Expand All @@ -60,7 +62,7 @@ public static void createGenericSpan(
}

Context parentContext = Java8BytecodeBridge.currentContext();
handlerData = new HandlerData(filterClass, method);
handlerData = new Jaxrs2HandlerData(filterClass, method);

HttpRouteHolder.updateHttpRoute(
parentContext,
Expand All @@ -78,7 +80,7 @@ public static void createGenericSpan(

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Local("otelHandlerData") HandlerData handlerData,
@Local("otelHandlerData") Jaxrs2HandlerData handlerData,
@Local("otelContext") Context context,
@Local("otelScope") Scope scope,
@Advice.Thrown Throwable throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.jaxrs.AsyncResponseData;
import io.opentelemetry.javaagent.instrumentation.jaxrs.CompletionStageFinishCallback;
import io.opentelemetry.javaagent.instrumentation.jaxrs.JaxrsServerSpanNaming;
import java.lang.reflect.Method;
import java.util.concurrent.CompletionStage;
import javax.ws.rs.Path;
Expand Down Expand Up @@ -74,7 +77,7 @@ public static void nameSpan(
@Advice.Origin Method method,
@Advice.AllArguments Object[] args,
@Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelHandlerData") HandlerData handlerData,
@Advice.Local("otelHandlerData") Jaxrs2HandlerData handlerData,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope,
@Advice.Local("otelAsyncResponse") AsyncResponse asyncResponse) {
Expand Down Expand Up @@ -102,7 +105,7 @@ public static void nameSpan(
}

Context parentContext = Java8BytecodeBridge.currentContext();
handlerData = new HandlerData(target.getClass(), method);
handlerData = new Jaxrs2HandlerData(target.getClass(), method);

HttpRouteHolder.updateHttpRoute(
parentContext,
Expand All @@ -127,7 +130,7 @@ public static void stopSpan(
@Advice.Return(readOnly = false, typing = Typing.DYNAMIC) Object returnValue,
@Advice.Thrown Throwable throwable,
@Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelHandlerData") HandlerData handlerData,
@Advice.Local("otelHandlerData") Jaxrs2HandlerData handlerData,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope,
@Advice.Local("otelAsyncResponse") AsyncResponse asyncResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0;

import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.javaagent.instrumentation.jaxrs.HandlerData;
import io.opentelemetry.javaagent.instrumentation.jaxrs.JaxrsInstrumenterFactory;

public final class JaxrsAnnotationsSingletons {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.jaxrs.AsyncResponseData;
import io.opentelemetry.javaagent.instrumentation.jaxrs.JaxrsConfig;
import javax.ws.rs.container.AsyncResponse;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ plugins {

dependencies {
bootstrap(project(":instrumentation:jaxrs:jaxrs-common:bootstrap"))
api(project(":instrumentation:jaxrs:jaxrs-common:javaagent"))

compileOnly("javax.ws.rs:javax.ws.rs-api:2.0")

compileOnly("com.google.auto.value:auto-value-annotations")
annotationProcessor("com.google.auto.value:auto-value")
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0;

import io.opentelemetry.javaagent.instrumentation.jaxrs.HandlerData;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.Path;

public class Jaxrs2HandlerData extends HandlerData {

private static final ClassValue<Map<Method, String>> serverSpanNames =
new ClassValue<Map<Method, String>>() {
@Override
protected Map<Method, String> computeValue(Class<?> type) {
return new ConcurrentHashMap<>();
}
};

public Jaxrs2HandlerData(Class<?> target, Method method) {
super(target, method);
}

/**
* Returns the span 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}.
*/
@Override
public String getServerSpanName() {
Map<Method, String> classMap = serverSpanNames.get(target);
String spanName = classMap.get(method);
if (spanName == null) {
spanName = super.getServerSpanName();
classMap.put(method, spanName);
}

return spanName;
}

@Override
protected Class<? extends Annotation> getHttpMethodAnnotation() {
return HttpMethod.class;
}

@Override
protected Supplier<String> getPathAnnotation(AnnotatedElement annotatedElement) {
Path path = annotatedElement.getAnnotation(Path.class);
return path != null ? path::value : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0;

import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.javaagent.instrumentation.jaxrs.HandlerData;
import io.opentelemetry.javaagent.instrumentation.jaxrs.JaxrsConstants;
import io.opentelemetry.javaagent.instrumentation.jaxrs.RequestContextHelper;
import javax.ws.rs.container.ContainerRequestContext;

public final class Jaxrs2RequestContextHelper {
public static Context createOrUpdateAbortSpan(
Instrumenter<HandlerData, Void> instrumenter,
ContainerRequestContext requestContext,
HandlerData handlerData) {

if (handlerData == null) {
return null;
}

requestContext.setProperty(JaxrsConstants.ABORT_HANDLED, true);
return RequestContextHelper.createOrUpdateAbortSpan(instrumenter, handlerData);
}

private Jaxrs2RequestContextHelper() {}
}

This file was deleted.

Loading

0 comments on commit cd2e11a

Please sign in to comment.