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
Expand Up @@ -42,7 +42,7 @@ dependencies {
testImplementation group: 'io.vertx', name: 'vertx-web', version: '3.4.0'
testImplementation group: 'io.vertx', name: 'vertx-web-client', version: '3.4.0'

testImplementation group: 'org.mockito', name: 'mockito-inline', version: '4.11.0'
testImplementation libs.bundles.mockito

testImplementation project(':dd-java-agent:appsec:appsec-test-fixtures')

Expand Down
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 All @@ -39,9 +36,12 @@ public RouteHandlerWrapper(final Handler<RoutingContext> handler) {
@Override
public void handle(final RoutingContext routingContext) {
AgentSpan span = routingContext.get(HANDLER_SPAN_CONTEXT_KEY);
AgentSpan parentSpan = null;
if (spanStarter) {
if (span == null) {
AgentSpan parentSpan = activeSpan();
if (span != null) {
parentSpan = routingContext.get(PARENT_SPAN_CONTEXT_KEY);
} else {
parentSpan = activeSpan();
routingContext.put(PARENT_SPAN_CONTEXT_KEY, parentSpan);

span = startSpan(INSTRUMENTATION_NAME);
Expand All @@ -51,52 +51,18 @@ public void handle(final RoutingContext routingContext) {
DECORATE.afterStart(span);
span.setResourceName(DECORATE.className(actual.getClass()));
}
setRoute(routingContext);
}
try (final AgentScope scope = span != null ? activateSpan(span) : noopScope()) {
try {
actual.handle(routingContext);
} catch (final Throwable t) {
DECORATE.onError(span, t);
throw t;
} finally {
if (spanStarter) {
updateRouteFromContext(routingContext, parentSpan, span);
}
}
}
}

private void setRoute(RoutingContext routingContext) {
final AgentSpan parentSpan = routingContext.get(PARENT_SPAN_CONTEXT_KEY);
if (parentSpan == 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;
}
}
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.updateRouteFromMatchedRoute;

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 @@ -11,37 +16,43 @@ class RouteMatchesAdvice {
@Source(SourceTypes.REQUEST_PATH_PARAMETER)
static void after(
@Advice.Return int ret,
@Advice.This final Object route,
@Advice.Argument(0) final RoutingContext ctx,
@Advice.Thrown(readOnly = false) Throwable t) {
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);
updateRouteFromMatchedRoute(ctx, route, 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 {
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
@Source(SourceTypes.REQUEST_PATH_PARAMETER)
static void after(
@Advice.Return boolean ret,
@Advice.This final Object route,
@Advice.Argument(0) final RoutingContext ctx,
@Advice.Thrown(readOnly = false) Throwable t) {
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);
updateRouteFromMatchedRoute(ctx, route, 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,147 @@
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.Route;
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 MATCHED_ROUTE_CONTEXT_KEY = "dd.vertx.matched_route";
private static final String VERTX_ROUTE_HANDLER_SPAN_NAME = "vertx.route-handler";

private RouteUpdateHelper() {}

public static void updateRouteFromContext(
final RoutingContext routingContext,
final AgentSpan parentSpan,
final AgentSpan handlerSpan) {
Route currentRoute;
try {
currentRoute = routingContext.currentRoute();
if (currentRoute == null) {
return;
}
} catch (final RuntimeException ignored) {
// Vert.x can call RouteState.matches before currentRoute is set on the context.
return;
}
final String contextRoute = routePath(routingContext, currentRoute.getPath());
final String matchedRoute = routingContext.get(MATCHED_ROUTE_CONTEXT_KEY);
updateRoute(
routingContext,
matchedRoute != null ? matchedRoute : contextRoute,
parentSpan,
handlerSpan);
}

public static void updateRouteFromMatchedRoute(
final RoutingContext routingContext,
final Object route,
final AgentSpan parentSpan,
final AgentSpan handlerSpan) {
// try to get the route from the object, else, fallback to context
if (route instanceof Route) {
final String matchedRoute = routePath(routingContext, ((Route) route).getPath());
if (isConcreteRoute(routingContext, matchedRoute)) {
// Keep the route found for potential later handler/finally code that would only see a
// broader route
routingContext.put(MATCHED_ROUTE_CONTEXT_KEY, matchedRoute);
updateRoute(routingContext, matchedRoute, parentSpan, handlerSpan);
return;
}
}
updateRouteFromContext(routingContext, parentSpan, handlerSpan);
}

private static void updateRoute(
final RoutingContext routingContext,
final String path,
final AgentSpan parentSpan,
final AgentSpan handlerSpan) {
final String method = routingContext.request().rawMethod();
if ((method == null || path == null)
|| (parentSpan == null && handlerSpan == null)
|| !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);
}
}

private static String routePath(final RoutingContext routingContext, final String path) {
if (path == null) {
return null;
}
final String mountPoint = routingContext.mountPoint();
if (mountPoint == null || mountPoint.isEmpty()) {
return path;
}
return trimTrailingSlash(mountPoint) + withLeadingSlash(path);
}

private static String trimTrailingSlash(final String path) {
if (path.charAt(path.length() - 1) == '/') {
return path.substring(0, path.length() - 1);
}
return path;
}

private static String withLeadingSlash(final String path) {
if (!path.isEmpty() && path.charAt(0) == '/') {
return path;
}
return "/" + path;
}

private static boolean isConcreteRoute(final RoutingContext routingContext, final String path) {
if (path == null || path.indexOf('*') >= 0) {
return false;
}
final String requestPath = routingContext.request().path();
return requestPath == null
|| !path.endsWith("/")
|| path.length() >= requestPath.length()
|| !requestPath.startsWith(path);
}

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