17
17
import io .vertx .ext .web .impl .RouterImpl ;
18
18
19
19
public class RouteHandlerWrapper implements Handler <RoutingContext > {
20
+ static final String PARENT_SPAN_CONTEXT_KEY = AgentSpan .class .getName () + ".parent" ;
20
21
static final String HANDLER_SPAN_CONTEXT_KEY = AgentSpan .class .getName () + ".handler" ;
22
+ static final String ROUTE_CONTEXT_KEY = "dd." + Tags .HTTP_ROUTE ;
21
23
22
24
private final Handler <RoutingContext > actual ;
23
25
private final boolean spanStarter ;
@@ -40,16 +42,16 @@ public void handle(final RoutingContext routingContext) {
40
42
if (spanStarter ) {
41
43
if (span == null ) {
42
44
AgentSpan parentSpan = activeSpan ();
45
+ routingContext .put (PARENT_SPAN_CONTEXT_KEY , parentSpan );
43
46
44
47
span = startSpan (INSTRUMENTATION_NAME );
45
48
routingContext .put (HANDLER_SPAN_CONTEXT_KEY , span );
46
49
47
50
routingContext .response ().endHandler (new EndHandlerWrapper (routingContext ));
48
51
DECORATE .afterStart (span );
49
52
span .setResourceName (DECORATE .className (actual .getClass ()));
50
-
51
- setRoute (parentSpan , routingContext );
52
53
}
54
+ setRoute (routingContext );
53
55
}
54
56
try (final AgentScope scope = span != null ? activateSpan (span ) : noopScope ()) {
55
57
try {
@@ -61,7 +63,12 @@ public void handle(final RoutingContext routingContext) {
61
63
}
62
64
}
63
65
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
+
65
72
final String method = routingContext .request ().rawMethod ();
66
73
String mountPoint = routingContext .mountPoint ();
67
74
String path = routingContext .currentRoute ().getPath ();
@@ -74,15 +81,21 @@ private void setRoute(AgentSpan parentSpan, RoutingContext routingContext) {
74
81
}
75
82
path = mountPoint + path ;
76
83
}
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 );
78
86
HTTP_RESOURCE_DECORATOR .withRoute (parentSpan , method , path , true );
79
87
}
80
88
}
81
89
82
- static boolean shouldUpdateRoute (final AgentSpan span , final String path ) {
90
+ static boolean shouldUpdateRoute (
91
+ final RoutingContext routingContext , final AgentSpan span , final String path ) {
83
92
if (span == null ) {
84
93
return false ;
85
94
}
95
+ final String currentRoute = routingContext .get (ROUTE_CONTEXT_KEY );
96
+ if (currentRoute != null && currentRoute .equals (path )) {
97
+ return false ;
98
+ }
86
99
// do not override route with a "/" if it's already set (it's probably more meaningful)
87
100
return !path .equals ("/" ) || span .getTag (Tags .HTTP_ROUTE ) == null ;
88
101
}
0 commit comments