Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -14,10 +14,10 @@

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.undertow.server.HttpServerExchange;
import javax.annotation.Nullable;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand Down Expand Up @@ -46,45 +46,61 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class HandleRequestAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.Argument(0) HttpServerExchange exchange,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
Context attachedContext = helper().getServerContext(exchange);
if (attachedContext != null) {
if (!helper().sameTrace(Java8BytecodeBridge.currentContext(), attachedContext)) {
// request processing is dispatched to another thread
scope = attachedContext.makeCurrent();
context = attachedContext;
helper().handlerStarted(attachedContext);
}
return;
public static class AdviceScope {
private final Context context;
private final Scope scope;

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

Context parentContext = Java8BytecodeBridge.currentContext();
if (!helper().shouldStart(parentContext, exchange)) {
return;
@Nullable
public static AdviceScope start(HttpServerExchange exchange) {
Context attachedContext = helper().getServerContext(exchange);
if (attachedContext != null) {
if (!helper().sameTrace(Context.current(), attachedContext)) {
// request processing is dispatched to another thread
Scope scope = attachedContext.makeCurrent();
helper().handlerStarted(attachedContext);
return new AdviceScope(attachedContext, scope);
}
return null;
}

Context parentContext = Context.current();
if (!helper().shouldStart(parentContext, exchange)) {
return null;
}

Context context = helper().start(parentContext, exchange);
Scope scope = context.makeCurrent();

exchange.addExchangeCompleteListener(new EndSpanListener(context));
return new AdviceScope(context, scope);
}

context = helper().start(parentContext, exchange);
scope = context.makeCurrent();
public void end(HttpServerExchange exchange, @Nullable Throwable throwable) {
scope.close();

exchange.addExchangeCompleteListener(new EndSpanListener(context));
helper().handlerCompleted(context, throwable, exchange);
}
}

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AdviceScope onEnter(@Advice.Argument(0) HttpServerExchange exchange) {
return AdviceScope.start(exchange);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(
@Advice.Argument(0) HttpServerExchange exchange,
@Advice.Thrown Throwable throwable,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
return;
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter @Nullable AdviceScope adviceScope) {
if (adviceScope != null) {
adviceScope.end(exchange, throwable);
}
scope.close();

helper().handlerCompleted(context, throwable, exchange);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,20 @@ public void transform(TypeTransformer transformer) {
public static class ResponseAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.Argument(0) HttpServerExchange exchange,
@Advice.Local("otelCallDepth") CallDepth callDepth) {
callDepth = CallDepth.forClass(ServerConnection.class);
public static CallDepth onEnter(@Advice.Argument(0) HttpServerExchange exchange) {
CallDepth callDepth = CallDepth.forClass(ServerConnection.class);
if (callDepth.getAndIncrement() > 0) {
return;
return callDepth;
}

Context context = helper().getServerContext(exchange);
HttpServerResponseCustomizerHolder.getCustomizer()
.customize(context, exchange, UndertowHttpResponseMutator.INSTANCE);
return callDepth;
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(@Advice.Local("otelCallDepth") CallDepth callDepth) {
public static void onExit(@Advice.Enter CallDepth callDepth) {
callDepth.decrementAndGet();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

package io.opentelemetry.javaagent.instrumentation.undertow;

import static io.opentelemetry.javaagent.instrumentation.undertow.UndertowSingletons.PROPAGATED_CONTEXT;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.bootstrap.executors.ExecutorAdviceHelper;
import io.opentelemetry.javaagent.bootstrap.executors.PropagatedContext;
Expand Down Expand Up @@ -41,9 +41,7 @@ public static class DispatchAdvice {
public static PropagatedContext enterJobSubmit(@Advice.Argument(1) Runnable task) {
Context context = Java8BytecodeBridge.currentContext();
if (ExecutorAdviceHelper.shouldPropagateContext(context, task)) {
VirtualField<Runnable, PropagatedContext> virtualField =
VirtualField.find(Runnable.class, PropagatedContext.class);
return ExecutorAdviceHelper.attachContextToTask(context, virtualField, task);
return ExecutorAdviceHelper.attachContextToTask(context, PROPAGATED_CONTEXT, task);
}
return null;
}
Expand All @@ -53,9 +51,8 @@ public static void exitJobSubmit(
@Advice.Argument(1) Runnable task,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable) {
VirtualField<Runnable, PropagatedContext> virtualField =
VirtualField.find(Runnable.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, task);
ExecutorAdviceHelper.cleanUpAfterSubmit(
propagatedContext, throwable, PROPAGATED_CONTEXT, task);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
import java.util.List;
import net.bytebuddy.matcher.ElementMatcher;

@AutoService(InstrumentationModule.class)
public class UndertowInstrumentationModule extends InstrumentationModule {
public class UndertowInstrumentationModule extends InstrumentationModule
implements ExperimentalInstrumentationModule {

public UndertowInstrumentationModule() {
super("undertow", "undertow-1.4");
Expand All @@ -34,4 +36,9 @@ public List<TypeInstrumentation> typeInstrumentations() {
new HttpServerExchangeInstrumentation(),
new HttpServerConnectionInstrumentation());
}

@Override
public boolean isIndyReady() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package io.opentelemetry.javaagent.instrumentation.undertow;

import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.bootstrap.executors.PropagatedContext;
import io.opentelemetry.javaagent.bootstrap.internal.JavaagentHttpServerInstrumenters;
import io.opentelemetry.javaagent.bootstrap.servlet.AppServerBridge;
import io.opentelemetry.javaagent.bootstrap.undertow.UndertowActiveHandlers;
Expand All @@ -16,6 +18,9 @@ public final class UndertowSingletons {

private static final Instrumenter<HttpServerExchange, HttpServerExchange> INSTRUMENTER;

public static final VirtualField<Runnable, PropagatedContext> PROPAGATED_CONTEXT =
VirtualField.find(Runnable.class, PropagatedContext.class);

static {
INSTRUMENTER =
JavaagentHttpServerInstrumenters.create(
Expand Down