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,6 +1,6 @@
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.RouteUpdateHelper.HANDLER_SPAN_CONTEXT_KEY;
import static datadog.trace.instrumentation.vertx_3_4.server.VertxDecorator.DECORATE;

import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public HttpServerResponseEndHandlerInstrumentation() {
public String[] helperClassNames() {
return new String[] {
packageName + ".EndHandlerWrapper",
packageName + ".RouteUpdateHelper",
packageName + ".RouteHandlerWrapper",
packageName + ".VertxDecorator",
packageName + ".VertxDecorator$VertxURIDataAdapter",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public RouteHandlerInstrumentation() {
public String[] helperClassNames() {
return new String[] {
packageName + ".EndHandlerWrapper",
packageName + ".RouteUpdateHelper",
packageName + ".RouteHandlerWrapper",
packageName + ".VertxDecorator",
packageName + ".VertxDecorator$VertxURIDataAdapter",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,20 @@
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopScope;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.bootstrap.instrumentation.decorator.http.HttpResourceDecorator.HTTP_RESOURCE_DECORATOR;
import static datadog.trace.instrumentation.vertx_3_4.server.RouteUpdateHelper.HANDLER_SPAN_CONTEXT_KEY;
import static datadog.trace.instrumentation.vertx_3_4.server.RouteUpdateHelper.PARENT_SPAN_CONTEXT_KEY;
import static datadog.trace.instrumentation.vertx_3_4.server.RouteUpdateHelper.updateRouteFromContext;
import static datadog.trace.instrumentation.vertx_3_4.server.VertxDecorator.DECORATE;
import static datadog.trace.instrumentation.vertx_3_4.server.VertxDecorator.INSTRUMENTATION_NAME;

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.Handler;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.impl.RouteImpl;
import io.vertx.ext.web.impl.RouterImpl;

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 final Handler<RoutingContext> actual;
private final boolean spanStarter;

Expand Down Expand Up @@ -57,6 +54,9 @@ public void handle(final RoutingContext routingContext) {
try {
actual.handle(routingContext);
} catch (final Throwable t) {
if (spanStarter) {
setRoute(routingContext);
}
DECORATE.onError(span, t);
throw t;
}
Expand All @@ -65,38 +65,10 @@ public void handle(final RoutingContext routingContext) {

private void setRoute(RoutingContext routingContext) {
final AgentSpan parentSpan = routingContext.get(PARENT_SPAN_CONTEXT_KEY);
if (parentSpan == null) {
final AgentSpan handlerSpan = routingContext.get(HANDLER_SPAN_CONTEXT_KEY);
if (parentSpan == null && handlerSpan == null) {
return;
}

final String method = routingContext.request().rawMethod();
String mountPoint = routingContext.mountPoint();
String path = routingContext.currentRoute().getPath();
if (mountPoint != null && !mountPoint.isEmpty()) {
if (mountPoint.charAt(mountPoint.length() - 1) == '/'
&& path != null
&& !path.isEmpty()
&& path.charAt(0) == '/') {
mountPoint = mountPoint.substring(0, mountPoint.length() - 1);
}
path = mountPoint + path;
}
if (method != null && path != null && shouldUpdateRoute(routingContext, parentSpan, path)) {
routingContext.put(ROUTE_CONTEXT_KEY, path);
HTTP_RESOURCE_DECORATOR.withRoute(parentSpan, method, path, true);
}
}

static boolean shouldUpdateRoute(
final RoutingContext routingContext, final AgentSpan span, final String path) {
if (span == null) {
return false;
}
final String currentRoute = routingContext.get(ROUTE_CONTEXT_KEY);
if (currentRoute != null && currentRoute.equals(path)) {
return false;
}
// do not override route with a "/" if it's already set (it's probably more meaningful)
return !path.equals("/") || span.getTag(Tags.HTTP_ROUTE) == null;
updateRouteFromContext(routingContext, parentSpan, handlerSpan);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public boolean isApplicable(Set<TargetSystem> enabledSystems) {
postProcessorFactory = IastPostProcessorFactory.INSTANCE;
return true;
}
return enabledSystems.contains(TargetSystem.APPSEC);
return enabledSystems.contains(TargetSystem.TRACING)
|| enabledSystems.contains(TargetSystem.APPSEC);
}

@Override
Expand All @@ -50,7 +51,7 @@ public String[] knownMatchingTypes() {
@Override
public String[] helperClassNames() {
return new String[] {
packageName + ".PathParameterPublishingHelper",
packageName + ".RouteUpdateHelper", packageName + ".PathParameterPublishingHelper",
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package datadog.trace.instrumentation.vertx_3_4.server;

import static datadog.trace.instrumentation.vertx_3_4.server.RouteUpdateHelper.HANDLER_SPAN_CONTEXT_KEY;
import static datadog.trace.instrumentation.vertx_3_4.server.RouteUpdateHelper.PARENT_SPAN_CONTEXT_KEY;
import static datadog.trace.instrumentation.vertx_3_4.server.RouteUpdateHelper.updateRouteFromContext;

import datadog.trace.api.iast.Source;
import datadog.trace.api.iast.SourceTypes;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import io.vertx.ext.web.RoutingContext;
import java.util.Map;
import net.bytebuddy.asm.Advice;
Expand All @@ -16,13 +21,15 @@ static void after(
if (ret != 0 || t != null) {
return;
}
Map<String, String> params = ctx.pathParams();
final AgentSpan parentSpan = ctx.get(PARENT_SPAN_CONTEXT_KEY);
final AgentSpan handlerSpan = ctx.get(HANDLER_SPAN_CONTEXT_KEY);
updateRouteFromContext(ctx, parentSpan, handlerSpan);

final Map<String, String> params = ctx.pathParams();
if (params.isEmpty()) {
return;
}

Throwable resThr = PathParameterPublishingHelper.publishParams(params);
t = resThr;
t = PathParameterPublishingHelper.publishParams(params);
}

static class BooleanReturnVariant {
Expand All @@ -35,13 +42,15 @@ static void after(
if (!ret || t != null) {
return;
}
Map<String, String> params = ctx.pathParams();
final AgentSpan parentSpan = ctx.get(PARENT_SPAN_CONTEXT_KEY);
final AgentSpan handlerSpan = ctx.get(HANDLER_SPAN_CONTEXT_KEY);
updateRouteFromContext(ctx, parentSpan, handlerSpan);

final Map<String, String> params = ctx.pathParams();
if (params.isEmpty()) {
return;
}

Throwable resThr = PathParameterPublishingHelper.publishParams(params);
t = resThr;
t = PathParameterPublishingHelper.publishParams(params);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package datadog.trace.instrumentation.vertx_3_4.server;

import static datadog.trace.bootstrap.instrumentation.decorator.http.HttpResourceDecorator.HTTP_RESOURCE_DECORATOR;

import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.ResourceNamePriorities;
import datadog.trace.bootstrap.instrumentation.api.Tags;
import io.vertx.ext.web.RoutingContext;

public final class RouteUpdateHelper {
public static final String PARENT_SPAN_CONTEXT_KEY = AgentSpan.class.getName() + ".parent";
public static final String HANDLER_SPAN_CONTEXT_KEY = AgentSpan.class.getName() + ".handler";
public static final String ROUTE_CONTEXT_KEY = "dd." + Tags.HTTP_ROUTE;
private static final String VERTX_ROUTE_HANDLER_SPAN_NAME = "vertx.route-handler";

private RouteUpdateHelper() {}

public static void updateRoute(
final RoutingContext routingContext,
final String method,
final String path,
final AgentSpan parentSpan,
final AgentSpan handlerSpan) {
if (method == null || path == null) {
return;
}
if (!shouldUpdateRoute(routingContext, parentSpan, handlerSpan, path)) {
return;
}

routingContext.put(ROUTE_CONTEXT_KEY, path);
if (parentSpan != null) {
HTTP_RESOURCE_DECORATOR.withRoute(parentSpan, method, path, true);
}
if (isVertxRouteHandlerSpan(handlerSpan)
&& handlerSpan.getResourceNamePriority() < ResourceNamePriorities.HTTP_FRAMEWORK_ROUTE) {
HTTP_RESOURCE_DECORATOR.withRoute(handlerSpan, method, path, true);
}
}

public static void updateRouteFromContext(
final RoutingContext routingContext,
final AgentSpan parentSpan,
final AgentSpan handlerSpan) {
if (parentSpan == null && handlerSpan == null) {
return;
}
if (routingContext.currentRoute() == null) {
return;
}

final String method = routingContext.request().rawMethod();
String mountPoint = routingContext.mountPoint();
String path = routingContext.currentRoute().getPath();
if (mountPoint != null && !mountPoint.isEmpty()) {
if (mountPoint.charAt(mountPoint.length() - 1) == '/'
&& path != null
&& !path.isEmpty()
&& path.charAt(0) == '/') {
mountPoint = mountPoint.substring(0, mountPoint.length() - 1);
}
path = mountPoint + path;
}
updateRoute(routingContext, method, path, parentSpan, handlerSpan);
}

private static boolean shouldUpdateRoute(
final RoutingContext routingContext,
final AgentSpan parentSpan,
final AgentSpan handlerSpan,
final String path) {
final String currentRoute = routingContext.get(ROUTE_CONTEXT_KEY);
if (currentRoute != null && currentRoute.equals(path)) {
return false;
}
// do not override route with a "/" if it's already set (it's probably more meaningful)
if (!path.equals("/")) {
return true;
}
return !hasHttpRoute(parentSpan) && !hasHttpRoute(handlerSpan);
}

public static boolean hasHttpRoute(final AgentSpan span) {
return span != null && span.getTag(Tags.HTTP_ROUTE) != null;
}

private static boolean isVertxRouteHandlerSpan(final AgentSpan span) {
if (span == null) {
return false;
}
final CharSequence spanName = span.getSpanName();
return spanName != null && VERTX_ROUTE_HANDLER_SPAN_NAME.contentEquals(spanName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package server

import datadog.trace.agent.test.InstrumentationSpecification
import datadog.trace.bootstrap.instrumentation.api.AgentSpan
import datadog.trace.bootstrap.instrumentation.api.Tags
import datadog.trace.instrumentation.vertx_3_4.server.RouteUpdateHelper
import io.vertx.ext.web.RoutingContext

class RouteHandlerWrapperTest extends InstrumentationSpecification {

void "updateRoute writes route to both spans"() {
given:
def context = Mock(RoutingContext)
def parentSpan = Mock(AgentSpan)
def handlerSpan = Mock(AgentSpan)

when:
RouteUpdateHelper.updateRoute(context, "GET", "/items/:id", parentSpan, handlerSpan)

then:
1 * context.get("dd.${Tags.HTTP_ROUTE}") >> null
1 * context.put("dd.${Tags.HTTP_ROUTE}", "/items/:id")
1 * parentSpan.setTag(Tags.HTTP_ROUTE, "/items/:id")
1 * parentSpan.setResourceName("GET /items/:id", _)
1 * handlerSpan.getSpanName() >> "vertx.route-handler"
1 * handlerSpan.getResourceNamePriority() >> Byte.MIN_VALUE
1 * handlerSpan.setTag(Tags.HTTP_ROUTE, "/items/:id")
1 * handlerSpan.setResourceName("GET /items/:id", _)
0 * _
}

void "updateRoute does not write route to non vertx handler span"() {
given:
def context = Mock(RoutingContext)
def parentSpan = Mock(AgentSpan)
def handlerSpan = Mock(AgentSpan)

when:
RouteUpdateHelper.updateRoute(context, "GET", "/items/:id", parentSpan, handlerSpan)

then:
1 * context.get("dd.${Tags.HTTP_ROUTE}") >> null
1 * context.put("dd.${Tags.HTTP_ROUTE}", "/items/:id")
1 * parentSpan.setTag(Tags.HTTP_ROUTE, "/items/:id")
1 * parentSpan.setResourceName("GET /items/:id", _)
1 * handlerSpan.getSpanName() >> "some.other.span"
0 * handlerSpan.setTag(_, _)
0 * _
}

void "updateRoute does not replace root route when one exists"() {
given:
def context = Mock(RoutingContext)
def parentSpan = Mock(AgentSpan)
def handlerSpan = Mock(AgentSpan)

when:
RouteUpdateHelper.updateRoute(context, "GET", "/", parentSpan, handlerSpan)

then:
1 * context.get("dd.${Tags.HTTP_ROUTE}") >> null
1 * parentSpan.getTag(Tags.HTTP_ROUTE) >> "/existing"
0 * context.put(_, _)
0 * parentSpan.setTag(_, _)
0 * handlerSpan.setTag(_, _)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ class VertxHttpServerForkedTest extends HttpServerTest<Vertx> {
"$Tags.COMPONENT" VertxDecorator.DECORATE.component()
"$Tags.SPAN_KIND" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_STATUS" Integer
def expectedRoute = this.expectedServerSpanRoute(endpoint)
if (expectedRoute != null) {
"$Tags.HTTP_ROUTE" expectedRoute
}
if (endpoint == EXCEPTION && this.testExceptionTag()) {
errorTags(RuntimeException, EXCEPTION.body)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class VertxMiddlewareHttpServerForkedTest extends VertxHttpServerForkedTest {
"$Tags.SPAN_KIND" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_STATUS" Integer
"before" true
def expectedRoute = this.expectedServerSpanRoute(endpoint)
if (expectedRoute != null) {
"$Tags.HTTP_ROUTE" expectedRoute
}
if (endpoint == EXCEPTION && this.testExceptionTag()) {
errorTags(RuntimeException, EXCEPTION.body)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ class VertxHttpServerForkedTest extends HttpServerTest<Vertx> {
"$Tags.COMPONENT" VertxDecorator.DECORATE.component()
"$Tags.SPAN_KIND" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_STATUS" Integer
def expectedRoute = this.expectedServerSpanRoute(endpoint)
if (expectedRoute != null) {
"$Tags.HTTP_ROUTE" expectedRoute
}
if (endpoint == EXCEPTION && this.testExceptionTag()) {
errorTags(RuntimeException, EXCEPTION.body)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class VertxMiddlewareHttpServerForkedTest extends VertxHttpServerForkedTest {
"$Tags.SPAN_KIND" Tags.SPAN_KIND_SERVER
"$Tags.HTTP_STATUS" Integer
"before" true
def expectedRoute = this.expectedServerSpanRoute(endpoint)
if (expectedRoute != null) {
"$Tags.HTTP_ROUTE" expectedRoute
}
if (endpoint == EXCEPTION && this.testExceptionTag()) {
errorTags(RuntimeException, EXCEPTION.body)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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.RouteUpdateHelper.HANDLER_SPAN_CONTEXT_KEY;
import static datadog.trace.instrumentation.vertx_4_0.server.VertxDecorator.DECORATE;

import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public Reference[] additionalMuzzleReferences() {
public String[] helperClassNames() {
return new String[] {
packageName + ".EndHandlerWrapper",
packageName + ".RouteUpdateHelper",
packageName + ".RouteHandlerWrapper",
packageName + ".VertxDecorator",
};
Expand Down
Loading
Loading