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
@@ -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