Skip to content
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
09544aa
single returned scope
SylvainJuge Jul 1, 2025
def3f87
jaxrs-1.0
SylvainJuge Jul 1, 2025
0ff7fbb
jaxrs-2.0 annotations
SylvainJuge Jul 1, 2025
4e552d7
jaxrs-2.0-cxf
SylvainJuge Jul 1, 2025
c5dfc76
jaxrs-2.0-jersey
SylvainJuge Jul 1, 2025
f8dad3c
simplify a bit
SylvainJuge Jul 1, 2025
ef81cdd
jaxrs-2.0-resteasy
SylvainJuge Jul 1, 2025
2f2aca1
jaxrs-3.0-annotations
SylvainJuge Jul 1, 2025
31f606d
jaxrs-3.0-*
SylvainJuge Jul 1, 2025
cadca12
fix a few things
SylvainJuge Jul 1, 2025
d98dc33
add missing @Nullable
SylvainJuge Jul 4, 2025
4a11baf
fix pebkc
SylvainJuge Jul 4, 2025
907815a
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java-i…
SylvainJuge Jul 4, 2025
befadfa
virtualfield 1
SylvainJuge Jul 4, 2025
be189c5
virtualfield 2
SylvainJuge Jul 4, 2025
04c59a2
virtualfield 3
SylvainJuge Jul 4, 2025
5904cbd
virtualfield 4
SylvainJuge Jul 4, 2025
5850a34
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java-i…
SylvainJuge Jul 7, 2025
5776b56
post-review simplification
SylvainJuge Jul 7, 2025
24ffa66
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java-i…
SylvainJuge Jul 8, 2025
43bee25
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java-i…
SylvainJuge Jul 9, 2025
283f589
minor code changes for consistency
SylvainJuge Jul 9, 2025
6429d6f
post-review changes
SylvainJuge Jul 10, 2025
80f9671
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java-i…
SylvainJuge Jul 10, 2025
688600e
simplify conditions
SylvainJuge Jul 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.lang.reflect.Method;
import javax.annotation.Nullable;
import javax.ws.rs.Path;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
Expand Down Expand Up @@ -67,52 +68,62 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class JaxRsAnnotationsAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void nameSpan(
@Advice.This Object target,
@Advice.Origin Method method,
@Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelHandlerData") HandlerData handlerData,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
callDepth = CallDepth.forClass(Path.class);
if (callDepth.getAndIncrement() > 0) {
return;
}
public static class AdviceScope {
private final HandlerData handlerData;
private final CallDepth callDepth;
private final Context context;
private final Scope scope;

public AdviceScope(CallDepth callDepth, Class<?> type, Method method) {
this.callDepth = callDepth;
if (callDepth.getAndIncrement() > 0) {
handlerData = null;
context = null;
scope = null;
return;
}

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

HttpServerRoute.update(
parentContext,
HttpServerRouteSource.CONTROLLER,
JaxrsServerSpanNaming.SERVER_SPAN_NAME,
handlerData);

if (!instrumenter().shouldStart(parentContext, handlerData)) {
scope = null;
context = null;
return;
}

Context parentContext = Java8BytecodeBridge.currentContext();
handlerData = new HandlerData(target.getClass(), method);
context = instrumenter().start(parentContext, handlerData);
scope = context.makeCurrent();
}

HttpServerRoute.update(
parentContext,
HttpServerRouteSource.CONTROLLER,
JaxrsServerSpanNaming.SERVER_SPAN_NAME,
handlerData);
public void exit(@Nullable Throwable throwable) {
if (callDepth.decrementAndGet() > 0) {
return;
}

if (!instrumenter().shouldStart(parentContext, handlerData)) {
return;
if (scope == null) {
return;
}
scope.close();
instrumenter().end(context, handlerData, null, throwable);
}
}

context = instrumenter().start(parentContext, handlerData);
scope = context.makeCurrent();
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AdviceScope nameSpan(@Advice.This Object target, @Advice.Origin Method method) {
return new AdviceScope(CallDepth.forClass(Path.class), target.getClass(), method);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Thrown Throwable throwable,
@Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelHandlerData") HandlerData handlerData,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (callDepth.decrementAndGet() > 0) {
return;
}

if (scope == null) {
return;
}
scope.close();
instrumenter().end(context, handlerData, null, throwable);
@Advice.Thrown @Nullable Throwable throwable, @Advice.Enter AdviceScope adviceScope) {
adviceScope.exit(throwable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
import io.opentelemetry.javaagent.bootstrap.internal.ExperimentalConfig;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import java.util.List;
import net.bytebuddy.matcher.ElementMatcher;

@AutoService(InstrumentationModule.class)
public class JaxrsInstrumentationModule extends InstrumentationModule {
public class JaxrsInstrumentationModule extends InstrumentationModule
implements ExperimentalInstrumentationModule {
public JaxrsInstrumentationModule() {
super("jaxrs", "jaxrs-1.0");
}
Expand All @@ -41,4 +43,9 @@ public boolean defaultEnabled(ConfigProperties config) {
// This instrumentation uses complex type matcher, disabling it can improve startup performance.
return super.defaultEnabled(config) && ExperimentalConfig.get().controllerTelemetryEnabled();
}

@Override
public boolean isIndyReady() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import io.opentelemetry.javaagent.instrumentation.jaxrs.JaxrsConstants;
import io.opentelemetry.javaagent.instrumentation.jaxrs.JaxrsServerSpanNaming;
import java.lang.reflect.Method;
import javax.annotation.Nullable;
import javax.ws.rs.container.ContainerRequestContext;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.asm.Advice.Local;

/**
* Default context instrumentation.
Expand All @@ -37,18 +37,60 @@ protected String abortAdviceName() {
@SuppressWarnings("unused")
public static class ContainerRequestContextAdvice {

public static class AdviceScope {
private final Context context;
private final Scope scope;
private final Jaxrs2HandlerData handlerData;

private AdviceScope(Context context, Scope scope, Jaxrs2HandlerData handlerData) {
this.context = context;
this.scope = scope;
this.handlerData = handlerData;
}

@Nullable
public static AdviceScope enter(Class<?> filterClass, Method method) {

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

HttpServerRoute.update(
parentContext,
HttpServerRouteSource.CONTROLLER,
JaxrsServerSpanNaming.SERVER_SPAN_NAME,
handlerData);

if (!instrumenter().shouldStart(parentContext, handlerData)) {
return null;
}
Context context = instrumenter().start(parentContext, handlerData);
return new AdviceScope(context, context.makeCurrent(), handlerData);
}

public void exit(Throwable throwable) {
if (scope == null) {
return;
}
scope.close();
instrumenter().end(context, handlerData, null, throwable);
}
}

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void createGenericSpan(
@Advice.This ContainerRequestContext requestContext,
@Local("otelHandlerData") Jaxrs2HandlerData handlerData,
@Local("otelContext") Context context,
@Local("otelScope") Scope scope) {
public static AdviceScope createGenericSpan(
@Advice.This ContainerRequestContext requestContext) {

if (requestContext.getProperty(JaxrsConstants.ABORT_HANDLED) != null) {
return;
return null;
}

Class<?> filterClass =
(Class<?>) requestContext.getProperty(JaxrsConstants.ABORT_FILTER_CLASS);
if (filterClass == null) {
return null;
}

Method method = null;
try {
method = filterClass.getMethod("filter", ContainerRequestContext.class);
Expand All @@ -57,39 +99,20 @@ public static void createGenericSpan(
// can only be aborted inside the filter method
}

if (filterClass == null || method == null) {
return;
}

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

HttpServerRoute.update(
parentContext,
HttpServerRouteSource.CONTROLLER,
JaxrsServerSpanNaming.SERVER_SPAN_NAME,
handlerData);

if (!instrumenter().shouldStart(parentContext, handlerData)) {
return;
if (method == null) {
return null;
}

context = instrumenter().start(parentContext, handlerData);
scope = context.makeCurrent();
return AdviceScope.enter(filterClass, method);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Local("otelHandlerData") Jaxrs2HandlerData handlerData,
@Local("otelContext") Context context,
@Local("otelScope") Scope scope,
@Advice.Thrown Throwable throwable) {
if (scope == null) {
return;
}
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter @Nullable AdviceScope adviceScope) {

scope.close();
instrumenter().end(context, handlerData, null, throwable);
if (adviceScope != null) {
adviceScope.exit(throwable);
}
}
}
}
Loading
Loading