Skip to content

Commit 6d6c5e4

Browse files
Fix failing tests
1 parent 242c0b9 commit 6d6c5e4

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

dd-java-agent/instrumentation/vertx-web-3.4/src/main/java/datadog/trace/instrumentation/vertx_3_4/server/RouteHandlerWrapper.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
import io.vertx.ext.web.impl.RouterImpl;
1818

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

2224
private final Handler<RoutingContext> actual;
2325
private final boolean spanStarter;
@@ -40,16 +42,16 @@ public void handle(final RoutingContext routingContext) {
4042
if (spanStarter) {
4143
if (span == null) {
4244
AgentSpan parentSpan = activeSpan();
45+
routingContext.put(PARENT_SPAN_CONTEXT_KEY, parentSpan);
4346

4447
span = startSpan(INSTRUMENTATION_NAME);
4548
routingContext.put(HANDLER_SPAN_CONTEXT_KEY, span);
4649

4750
routingContext.response().endHandler(new EndHandlerWrapper(routingContext));
4851
DECORATE.afterStart(span);
4952
span.setResourceName(DECORATE.className(actual.getClass()));
50-
51-
setRoute(parentSpan, routingContext);
5253
}
54+
setRoute(routingContext);
5355
}
5456
try (final AgentScope scope = span != null ? activateSpan(span) : noopScope()) {
5557
try {
@@ -61,7 +63,12 @@ public void handle(final RoutingContext routingContext) {
6163
}
6264
}
6365

64-
private void setRoute(AgentSpan parentSpan, RoutingContext routingContext) {
66+
private void setRoute(RoutingContext routingContext) {
67+
final AgentSpan parentSpan = routingContext.get(PARENT_SPAN_CONTEXT_KEY);
68+
if (parentSpan == null) {
69+
return;
70+
}
71+
6572
final String method = routingContext.request().rawMethod();
6673
String mountPoint = routingContext.mountPoint();
6774
String path = routingContext.currentRoute().getPath();
@@ -74,15 +81,21 @@ private void setRoute(AgentSpan parentSpan, RoutingContext routingContext) {
7481
}
7582
path = mountPoint + path;
7683
}
77-
if (method != null && path != null && shouldUpdateRoute(parentSpan, path)) {
84+
if (method != null && path != null && shouldUpdateRoute(routingContext, parentSpan, path)) {
85+
routingContext.put(ROUTE_CONTEXT_KEY, path);
7886
HTTP_RESOURCE_DECORATOR.withRoute(parentSpan, method, path, true);
7987
}
8088
}
8189

82-
static boolean shouldUpdateRoute(final AgentSpan span, final String path) {
90+
static boolean shouldUpdateRoute(
91+
final RoutingContext routingContext, final AgentSpan span, final String path) {
8392
if (span == null) {
8493
return false;
8594
}
95+
final String currentRoute = routingContext.get(ROUTE_CONTEXT_KEY);
96+
if (currentRoute != null && currentRoute.equals(path)) {
97+
return false;
98+
}
8699
// do not override route with a "/" if it's already set (it's probably more meaningful)
87100
return !path.equals("/") || span.getTag(Tags.HTTP_ROUTE) == null;
88101
}

dd-java-agent/instrumentation/vertx-web-4.0/src/main/java/datadog/trace/instrumentation/vertx_4_0/server/RouteHandlerWrapper.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import io.vertx.ext.web.impl.RouteImpl;
1717

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

2123
private final Handler<RoutingContext> actual;
2224
private final boolean spanStarter;
@@ -36,16 +38,16 @@ public void handle(final RoutingContext routingContext) {
3638
if (spanStarter) {
3739
if (span == null) {
3840
AgentSpan parentSpan = activeSpan();
41+
routingContext.put(PARENT_SPAN_CONTEXT_KEY, parentSpan);
3942

4043
span = startSpan(INSTRUMENTATION_NAME);
4144
routingContext.put(HANDLER_SPAN_CONTEXT_KEY, span);
4245

4346
routingContext.response().endHandler(new EndHandlerWrapper(routingContext));
4447
DECORATE.afterStart(span);
4548
span.setResourceName(DECORATE.className(actual.getClass()));
46-
47-
setRoute(parentSpan, routingContext);
4849
}
50+
setRoute(routingContext);
4951
}
5052

5153
try (final AgentScope scope = span != null ? activateSpan(span) : noopScope()) {
@@ -58,7 +60,12 @@ public void handle(final RoutingContext routingContext) {
5860
}
5961
}
6062

61-
private void setRoute(AgentSpan parentSpan, RoutingContext routingContext) {
63+
private void setRoute(RoutingContext routingContext) {
64+
final AgentSpan parentSpan = routingContext.get(PARENT_SPAN_CONTEXT_KEY);
65+
if (parentSpan == null) {
66+
return;
67+
}
68+
6269
final String method = routingContext.request().method().name();
6370
final String mountPoint = routingContext.mountPoint();
6471

@@ -72,15 +79,21 @@ private void setRoute(AgentSpan parentSpan, RoutingContext routingContext) {
7279
: mountPoint;
7380
path = noBackslashhMountPoint + path;
7481
}
75-
if (method != null && path != null && shouldUpdateRoute(parentSpan, path)) {
82+
if (method != null && path != null && shouldUpdateRoute(routingContext, parentSpan, path)) {
83+
routingContext.put(ROUTE_CONTEXT_KEY, path);
7684
HTTP_RESOURCE_DECORATOR.withRoute(parentSpan, method, path, true);
7785
}
7886
}
7987

80-
static boolean shouldUpdateRoute(final AgentSpan span, final String path) {
88+
static boolean shouldUpdateRoute(
89+
final RoutingContext routingContext, final AgentSpan span, final String path) {
8190
if (span == null) {
8291
return false;
8392
}
93+
final String currentRoute = routingContext.get(ROUTE_CONTEXT_KEY);
94+
if (currentRoute != null && currentRoute.equals(path)) {
95+
return false;
96+
}
8497
// do not override route with a "/" if it's already set (it's probably more meaningful)
8598
return !path.equals("/") || span.getTag(Tags.HTTP_ROUTE) == null;
8699
}

0 commit comments

Comments
 (0)