Skip to content
Draft
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
@@ -0,0 +1,25 @@
package datadog.trace.instrumentation.vertx_3_4.server;

import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;

public class CloseHandlerWrapper implements Handler<Void> {
private final RoutingContext routingContext;

public Handler<Void> actual;

CloseHandlerWrapper(RoutingContext routingContext) {
this.routingContext = routingContext;
}

@Override
public void handle(final Void event) {
try {
if (actual != null) {
actual.handle(event);
}
} finally {
RouteHandlerWrapper.finishHandlerSpan(routingContext);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package datadog.trace.instrumentation.vertx_3_4.server;

import io.vertx.core.Handler;
import net.bytebuddy.asm.Advice;

public class CloseHandlerWrapperAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void wrapHandler(
@Advice.FieldValue(value = "closeHandler", readOnly = false) final Handler<Void> closeHandler,
@Advice.Argument(value = 0, readOnly = false) Handler<Void> handler) {
if (closeHandler instanceof CloseHandlerWrapper && handler instanceof CloseHandlerWrapper) {
return;
}
if (handler instanceof CloseHandlerWrapper && closeHandler != null) {
((CloseHandlerWrapper) handler).actual = closeHandler;
} else if (closeHandler instanceof CloseHandlerWrapper) {
((CloseHandlerWrapper) closeHandler).actual = handler;
handler = closeHandler;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package datadog.trace.instrumentation.vertx_3_4.server;

import static datadog.trace.instrumentation.vertx_3_4.server.RouteHandlerWrapper.HANDLER_SPAN_CONTEXT_KEY;
import static datadog.trace.instrumentation.vertx_3_4.server.VertxDecorator.DECORATE;

import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;

Expand All @@ -18,16 +14,12 @@ public class EndHandlerWrapper implements Handler<Void> {

@Override
public void handle(final Void event) {
AgentSpan span = routingContext.get(HANDLER_SPAN_CONTEXT_KEY);
try {
if (actual != null) {
actual.handle(event);
}
} finally {
if (span != null) {
DECORATE.onResponse(span, routingContext.response());
span.finish();
}
RouteHandlerWrapper.finishHandlerSpan(routingContext);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package datadog.trace.instrumentation.vertx_3_4.server;

import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;

public class ExceptionHandlerWrapper implements Handler<Throwable> {
private final RoutingContext routingContext;

public Handler<Throwable> actual;

ExceptionHandlerWrapper(RoutingContext routingContext) {
this.routingContext = routingContext;
}

@Override
public void handle(final Throwable event) {
try {
if (actual != null) {
actual.handle(event);
}
} finally {
RouteHandlerWrapper.finishHandlerSpan(routingContext);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package datadog.trace.instrumentation.vertx_3_4.server;

import io.vertx.core.Handler;
import net.bytebuddy.asm.Advice;

public class ExceptionHandlerWrapperAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void wrapHandler(
@Advice.FieldValue(value = "exceptionHandler", readOnly = false)
final Handler<Throwable> exceptionHandler,
@Advice.Argument(value = 0, readOnly = false) Handler<Throwable> handler) {
if (exceptionHandler instanceof ExceptionHandlerWrapper
&& handler instanceof ExceptionHandlerWrapper) {
return;
}
if (handler instanceof ExceptionHandlerWrapper && exceptionHandler != null) {
((ExceptionHandlerWrapper) handler).actual = exceptionHandler;
} else if (exceptionHandler instanceof ExceptionHandlerWrapper) {
((ExceptionHandlerWrapper) exceptionHandler).actual = handler;
handler = exceptionHandler;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public HttpServerResponseEndHandlerInstrumentation() {
@Override
public String[] helperClassNames() {
return new String[] {
packageName + ".CloseHandlerWrapper",
packageName + ".EndHandlerWrapper",
packageName + ".ExceptionHandlerWrapper",
packageName + ".RouteHandlerWrapper",
packageName + ".VertxDecorator",
packageName + ".VertxDecorator$VertxURIDataAdapter",
Expand All @@ -39,5 +41,17 @@ public void methodAdvice(MethodTransformer transformer) {
.and(isPublic())
.and(takesArgument(0, named("io.vertx.core.Handler"))),
packageName + ".EndHandlerWrapperAdvice");
transformer.applyAdvice(
isMethod()
.and(named("closeHandler"))
.and(isPublic())
.and(takesArgument(0, named("io.vertx.core.Handler"))),
packageName + ".CloseHandlerWrapperAdvice");
transformer.applyAdvice(
isMethod()
.and(named("exceptionHandler"))
.and(isPublic())
.and(takesArgument(0, named("io.vertx.core.Handler"))),
packageName + ".ExceptionHandlerWrapperAdvice");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public RouteHandlerInstrumentation() {
@Override
public String[] helperClassNames() {
return new String[] {
packageName + ".CloseHandlerWrapper",
packageName + ".EndHandlerWrapper",
packageName + ".ExceptionHandlerWrapper",
packageName + ".RouteHandlerWrapper",
packageName + ".VertxDecorator",
packageName + ".VertxDecorator$VertxURIDataAdapter",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,32 @@
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.Tags;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.impl.RouteImpl;
import io.vertx.ext.web.impl.RouterImpl;
import java.lang.reflect.Method;

public class RouteHandlerWrapper implements Handler<RoutingContext> {
static final String PARENT_SPAN_CONTEXT_KEY = AgentSpan.class.getName() + ".parent";
static final String HANDLER_SPAN_CONTEXT_KEY = AgentSpan.class.getName() + ".handler";
static final String ROUTE_CONTEXT_KEY = "dd." + Tags.HTTP_ROUTE;

private static final Object NO_ADD_END_HANDLER = new Object();

private static final ClassValue<Object> ADD_END_HANDLER_METHOD =
new ClassValue<Object>() {
@Override
protected Object computeValue(final Class<?> type) {
try {
return type.getMethod("addEndHandler", Handler.class);
} catch (final NoSuchMethodException e) {
return NO_ADD_END_HANDLER;
}
}
};

private final Handler<RoutingContext> actual;
private final boolean spanStarter;

Expand Down Expand Up @@ -48,6 +64,12 @@ public void handle(final RoutingContext routingContext) {
routingContext.put(HANDLER_SPAN_CONTEXT_KEY, span);

routingContext.response().endHandler(new EndHandlerWrapper(routingContext));
// Same intent as vertx-web-4.0: finish the route-handler span when the response
// endHandler is skipped (e.g. connection already closed). Prefer
// RoutingContext#addEndHandler
// when present (vertx-web 3.8+); otherwise register close/exception handlers, coordinated
// by HttpServerResponse*WrapperAdvice like EndHandlerWrapperAdvice.
registerSpanCompletionFallback(routingContext);
DECORATE.afterStart(span);
span.setResourceName(DECORATE.className(actual.getClass()));
}
Expand All @@ -63,6 +85,34 @@ public void handle(final RoutingContext routingContext) {
}
}

private static void registerSpanCompletionFallback(final RoutingContext routingContext) {
final Object methodOrAbsent = ADD_END_HANDLER_METHOD.get(routingContext.getClass());
if (methodOrAbsent instanceof Method) {
try {
@SuppressWarnings("unchecked")
final Handler<AsyncResult<Void>> end = ar -> finishHandlerSpan(routingContext);
((Method) methodOrAbsent).invoke(routingContext, end);
return;
} catch (final Throwable ignored) {
// Fall through to legacy response hooks (vertx-web 3.4.x without addEndHandler).
}
}
routingContext.response().closeHandler(new CloseHandlerWrapper(routingContext));
routingContext.response().exceptionHandler(new ExceptionHandlerWrapper(routingContext));
}

// Idempotently finish the route-handler span. EndHandlerWrapper, RoutingContext#addEndHandler
// (when used), and CloseHandlerWrapper / ExceptionHandlerWrapper may each call this.
static void finishHandlerSpan(final RoutingContext routingContext) {
final AgentSpan span = routingContext.get(HANDLER_SPAN_CONTEXT_KEY);
if (span == null) {
return;
}
routingContext.put(HANDLER_SPAN_CONTEXT_KEY, null);
DECORATE.onResponse(span, routingContext.response());
span.finish();
}

private void setRoute(RoutingContext routingContext) {
final AgentSpan parentSpan = routingContext.get(PARENT_SPAN_CONTEXT_KEY);
if (parentSpan == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package datadog.trace.instrumentation.vertx_4_0.server;

import static datadog.trace.instrumentation.vertx_4_0.server.RouteHandlerWrapper.HANDLER_SPAN_CONTEXT_KEY;
import static datadog.trace.instrumentation.vertx_4_0.server.VertxDecorator.DECORATE;

import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;

Expand All @@ -18,16 +14,12 @@ public class EndHandlerWrapper implements Handler<Void> {

@Override
public void handle(final Void event) {
AgentSpan span = routingContext.get(HANDLER_SPAN_CONTEXT_KEY);
try {
if (actual != null) {
actual.handle(event);
}
} finally {
if (span != null) {
DECORATE.onResponse(span, routingContext.response());
span.finish();
}
RouteHandlerWrapper.finishHandlerSpan(routingContext);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ public void handle(final RoutingContext routingContext) {
routingContext.put(HANDLER_SPAN_CONTEXT_KEY, span);

routingContext.response().endHandler(new EndHandlerWrapper(routingContext));
// Fallback finish path: HttpServerResponse.endHandler is silently skipped
// by Vert.x's Http1xServerResponse.end() when the underlying connection
// has already closed (Http1xServerResponse#end gates `endHandler.handle()`
// behind `!closed`). This happens in synthetic transports such as
// quarkus-amazon-lambda-rest's virtual Netty channel, where writes and
// close are synchronous in-memory, leaving the route-handler span unfinished
// and orphaning all jakarta-rs.request / aws.http child spans in the trace.
// RoutingContext#addEndHandler fires on routing-context completion regardless
// of underlying connection state and on both success and failure.
routingContext.addEndHandler(ar -> finishHandlerSpan(routingContext));
DECORATE.afterStart(span);
span.setResourceName(DECORATE.className(actual.getClass()));
}
Expand All @@ -60,6 +70,19 @@ public void handle(final RoutingContext routingContext) {
}
}

// Idempotently finish the route-handler span. Both EndHandlerWrapper (the
// response.endHandler path) and the routingContext.addEndHandler fallback may call
// this; the first one to win clears HANDLER_SPAN_CONTEXT_KEY so the second is a no-op.
static void finishHandlerSpan(final RoutingContext routingContext) {
final AgentSpan span = routingContext.get(HANDLER_SPAN_CONTEXT_KEY);
if (span == null) {
return;
}
routingContext.put(HANDLER_SPAN_CONTEXT_KEY, null);
DECORATE.onResponse(span, routingContext.response());
span.finish();
}

private void setRoute(RoutingContext routingContext) {
final AgentSpan parentSpan = routingContext.get(PARENT_SPAN_CONTEXT_KEY);
if (parentSpan == null) {
Expand Down
Loading