Skip to content

Commit 85ab108

Browse files
committed
check type of handler span
1 parent a6a61be commit 85ab108

7 files changed

Lines changed: 72 additions & 2 deletions

File tree

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package datadog.trace.instrumentation.vertx_3_4.server;
22

33
import static datadog.trace.bootstrap.instrumentation.decorator.http.HttpResourceDecorator.HTTP_RESOURCE_DECORATOR;
4+
import static datadog.trace.instrumentation.vertx_3_4.server.VertxDecorator.INSTRUMENTATION_NAME;
45

56
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
67
import datadog.trace.bootstrap.instrumentation.api.ResourceNamePriorities;
@@ -31,7 +32,7 @@ public static void updateRoute(
3132
if (parentSpan != null) {
3233
HTTP_RESOURCE_DECORATOR.withRoute(parentSpan, method, path, true);
3334
}
34-
if (handlerSpan != null
35+
if (isVertxRouteHandlerSpan(handlerSpan)
3536
&& handlerSpan.getResourceNamePriority() < ResourceNamePriorities.HTTP_FRAMEWORK_ROUTE) {
3637
HTTP_RESOURCE_DECORATOR.withRoute(handlerSpan, method, path, true);
3738
}
@@ -82,4 +83,12 @@ private static boolean shouldUpdateRoute(
8283
public static boolean hasHttpRoute(final AgentSpan span) {
8384
return span != null && span.getTag(Tags.HTTP_ROUTE) != null;
8485
}
86+
87+
private static boolean isVertxRouteHandlerSpan(final AgentSpan span) {
88+
if (span == null) {
89+
return false;
90+
}
91+
final CharSequence spanName = span.getSpanName();
92+
return spanName != null && INSTRUMENTATION_NAME.toString().contentEquals(spanName);
93+
}
8594
}

dd-java-agent/instrumentation/vertx/vertx-web/vertx-web-3.4/src/test/groovy/server/RouteHandlerWrapperTest.groovy

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import datadog.trace.agent.test.InstrumentationSpecification
44
import datadog.trace.bootstrap.instrumentation.api.AgentSpan
55
import datadog.trace.bootstrap.instrumentation.api.Tags
66
import datadog.trace.instrumentation.vertx_3_4.server.RouteUpdateHelper
7+
import datadog.trace.instrumentation.vertx_3_4.server.VertxDecorator
78
import io.vertx.ext.web.RoutingContext
89

910
class RouteHandlerWrapperTest extends InstrumentationSpecification {
@@ -22,11 +23,30 @@ class RouteHandlerWrapperTest extends InstrumentationSpecification {
2223
1 * context.get("dd.${Tags.HTTP_ROUTE}") >> null
2324
1 * context.put("dd.${Tags.HTTP_ROUTE}", "/items/:id")
2425
1 * parentSpan.setTag(Tags.HTTP_ROUTE, "/items/:id")
26+
1 * handlerSpan.getSpanName() >> VertxDecorator.INSTRUMENTATION_NAME
2527
1 * handlerSpan.getResourceNamePriority() >> Byte.MIN_VALUE
2628
1 * handlerSpan.setTag(Tags.HTTP_ROUTE, "/items/:id")
2729
0 * _
2830
}
2931

32+
void "updateRoute does not write route to non vertx handler span"() {
33+
given:
34+
def context = Mock(RoutingContext)
35+
def parentSpan = Mock(AgentSpan)
36+
def handlerSpan = Mock(AgentSpan)
37+
38+
when:
39+
RouteUpdateHelper.updateRoute(context, "GET", "/items/:id", parentSpan, handlerSpan)
40+
41+
then:
42+
1 * context.get("dd.${Tags.HTTP_ROUTE}") >> null
43+
1 * context.put("dd.${Tags.HTTP_ROUTE}", "/items/:id")
44+
1 * parentSpan.setTag(Tags.HTTP_ROUTE, "/items/:id")
45+
1 * handlerSpan.getSpanName() >> "some.other.span"
46+
0 * handlerSpan.setTag(_, _)
47+
0 * _
48+
}
49+
3050
void "updateRoute does not replace root route when one exists"() {
3151
given:
3252
def context = Mock(RoutingContext)

dd-java-agent/instrumentation/vertx/vertx-web/vertx-web-3.4/src/test/groovy/server/VertxHttpServerForkedTest.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ class VertxHttpServerForkedTest extends HttpServerTest<Vertx> {
157157
"$Tags.COMPONENT" VertxDecorator.DECORATE.component()
158158
"$Tags.SPAN_KIND" Tags.SPAN_KIND_SERVER
159159
"$Tags.HTTP_STATUS" Integer
160+
def expectedRoute = expectedServerSpanRoute(endpoint)
161+
if (expectedRoute != null) {
162+
"$Tags.HTTP_ROUTE" expectedRoute
163+
}
160164
if (endpoint == EXCEPTION && this.testExceptionTag()) {
161165
errorTags(RuntimeException, EXCEPTION.body)
162166
}

dd-java-agent/instrumentation/vertx/vertx-web/vertx-web-4.0/src/latestDepTest/groovy/server/VertxHttpServerForkedTest.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ class VertxHttpServerForkedTest extends HttpServerTest<Vertx> {
163163
"$Tags.COMPONENT" VertxDecorator.DECORATE.component()
164164
"$Tags.SPAN_KIND" Tags.SPAN_KIND_SERVER
165165
"$Tags.HTTP_STATUS" Integer
166+
def expectedRoute = expectedServerSpanRoute(endpoint)
167+
if (expectedRoute != null) {
168+
"$Tags.HTTP_ROUTE" expectedRoute
169+
}
166170
if (endpoint == EXCEPTION && this.testExceptionTag()) {
167171
errorTags(RuntimeException, EXCEPTION.body)
168172
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package datadog.trace.instrumentation.vertx_4_0.server;
22

33
import static datadog.trace.bootstrap.instrumentation.decorator.http.HttpResourceDecorator.HTTP_RESOURCE_DECORATOR;
4+
import static datadog.trace.instrumentation.vertx_4_0.server.VertxDecorator.INSTRUMENTATION_NAME;
45

56
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
67
import datadog.trace.bootstrap.instrumentation.api.ResourceNamePriorities;
@@ -31,7 +32,7 @@ public static void updateRoute(
3132
if (parentSpan != null) {
3233
HTTP_RESOURCE_DECORATOR.withRoute(parentSpan, method, path, true);
3334
}
34-
if (handlerSpan != null
35+
if (isVertxRouteHandlerSpan(handlerSpan)
3536
&& handlerSpan.getResourceNamePriority() < ResourceNamePriorities.HTTP_FRAMEWORK_ROUTE) {
3637
HTTP_RESOURCE_DECORATOR.withRoute(handlerSpan, method, path, true);
3738
}
@@ -84,4 +85,12 @@ private static boolean shouldUpdateRoute(
8485
public static boolean hasHttpRoute(final AgentSpan span) {
8586
return span != null && span.getTag(Tags.HTTP_ROUTE) != null;
8687
}
88+
89+
private static boolean isVertxRouteHandlerSpan(final AgentSpan span) {
90+
if (span == null) {
91+
return false;
92+
}
93+
final CharSequence spanName = span.getSpanName();
94+
return spanName != null && INSTRUMENTATION_NAME.toString().contentEquals(spanName);
95+
}
8796
}

dd-java-agent/instrumentation/vertx/vertx-web/vertx-web-4.0/src/test/groovy/server/RouteHandlerWrapperTest.groovy

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import datadog.trace.agent.test.InstrumentationSpecification
44
import datadog.trace.bootstrap.instrumentation.api.AgentSpan
55
import datadog.trace.bootstrap.instrumentation.api.Tags
66
import datadog.trace.instrumentation.vertx_4_0.server.RouteUpdateHelper
7+
import datadog.trace.instrumentation.vertx_4_0.server.VertxDecorator
78
import io.vertx.ext.web.RoutingContext
89

910
class RouteHandlerWrapperTest extends InstrumentationSpecification {
@@ -22,11 +23,30 @@ class RouteHandlerWrapperTest extends InstrumentationSpecification {
2223
1 * context.get("dd.${Tags.HTTP_ROUTE}") >> null
2324
1 * context.put("dd.${Tags.HTTP_ROUTE}", "/items/:id")
2425
1 * parentSpan.setTag(Tags.HTTP_ROUTE, "/items/:id")
26+
1 * handlerSpan.getSpanName() >> VertxDecorator.INSTRUMENTATION_NAME
2527
1 * handlerSpan.getResourceNamePriority() >> Byte.MIN_VALUE
2628
1 * handlerSpan.setTag(Tags.HTTP_ROUTE, "/items/:id")
2729
0 * _
2830
}
2931

32+
void "updateRoute does not write route to non vertx handler span"() {
33+
given:
34+
def context = Mock(RoutingContext)
35+
def parentSpan = Mock(AgentSpan)
36+
def handlerSpan = Mock(AgentSpan)
37+
38+
when:
39+
RouteUpdateHelper.updateRoute(context, "GET", "/items/:id", parentSpan, handlerSpan)
40+
41+
then:
42+
1 * context.get("dd.${Tags.HTTP_ROUTE}") >> null
43+
1 * context.put("dd.${Tags.HTTP_ROUTE}", "/items/:id")
44+
1 * parentSpan.setTag(Tags.HTTP_ROUTE, "/items/:id")
45+
1 * handlerSpan.getSpanName() >> "some.other.span"
46+
0 * handlerSpan.setTag(_, _)
47+
0 * _
48+
}
49+
3050
void "updateRoute does not replace root route when one exists"() {
3151
given:
3252
def context = Mock(RoutingContext)

dd-java-agent/instrumentation/vertx/vertx-web/vertx-web-4.0/src/test/groovy/server/VertxHttpServerForkedTest.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ class VertxHttpServerForkedTest extends HttpServerTest<Vertx> {
168168
"$Tags.COMPONENT" VertxDecorator.DECORATE.component()
169169
"$Tags.SPAN_KIND" Tags.SPAN_KIND_SERVER
170170
"$Tags.HTTP_STATUS" Integer
171+
def expectedRoute = expectedServerSpanRoute(endpoint)
172+
if (expectedRoute != null) {
173+
"$Tags.HTTP_ROUTE" expectedRoute
174+
}
171175
if (endpoint == EXCEPTION && this.testExceptionTag()) {
172176
errorTags(RuntimeException, EXCEPTION.body)
173177
}

0 commit comments

Comments
 (0)