Skip to content

Commit

Permalink
Optionally prevent undertow from setting http.route
Browse files Browse the repository at this point in the history
  • Loading branch information
amarziali committed Mar 25, 2024
1 parent bfd3a15 commit 1ca6aa2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.InstrumenterModule;
import datadog.trace.api.Config;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import io.undertow.server.HttpServerExchange;
Expand Down Expand Up @@ -70,7 +71,8 @@ public static void enter(
String relativePath = exchange.getRelativePath();

ServletPathMatch servletPathMatch = servletRequestContext.getServletPathMatch();
if (servletPathMatch != null
if (!Config.get().isUndertowIgnoreHttpRouteEnabled()
&& servletPathMatch != null
&& servletPathMatch.getMappingMatch() != MappingMatch.DEFAULT) {
// Set the route unless the mapping match is default, this way we prevent setting route
// for a non-existing resource. Otherwise, it'd set a non-existing resource name with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ abstract class UndertowDispatcherTest extends HttpServerTest<Undertow> {
}
}

class UndertowDispatcherV0ForkedTest extends UndertowDispatcherTest {
class UndertowDispatcherV0Test extends UndertowDispatcherTest {
@Override
int version() {
return 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.undertow.servlet.api.DeploymentInfo
import io.undertow.servlet.api.DeploymentManager
import io.undertow.servlet.api.ServletContainer
import io.undertow.servlet.api.ServletInfo
import spock.lang.IgnoreIf

import javax.servlet.MultipartConfigElement

Expand Down Expand Up @@ -157,6 +158,9 @@ abstract class UndertowServletTest extends HttpServerTest<Undertow> {

@Override
String expectedResourceName(ServerEndpoint endpoint, String method, URI address) {
if (!generateHttpRoute()) {
return super.expectedResourceName(endpoint, method, address)
}
if (endpoint.status == 404 && endpoint.path == "/not-found") {
return "404"
} else if (endpoint.hasPathParam) {
Expand Down Expand Up @@ -198,8 +202,15 @@ abstract class UndertowServletTest extends HttpServerTest<Undertow> {
}
}

def generateHttpRoute() {
true
}

@Override
Serializable expectedServerSpanRoute(ServerEndpoint endpoint) {
if (!generateHttpRoute()) {
return null
}
switch (endpoint) {
case LOGIN:
case NOT_FOUND:
Expand All @@ -211,6 +222,7 @@ abstract class UndertowServletTest extends HttpServerTest<Undertow> {
}
}

@IgnoreIf({ !instance.generateHttpRoute() })
def "test not-here"() {
setup:
def request = request(NOT_HERE, method, body).build()
Expand Down Expand Up @@ -257,7 +269,19 @@ abstract class UndertowServletTest extends HttpServerTest<Undertow> {
}
}

class UndertowServletV0ForkedTest extends UndertowServletTest implements TestingGenericHttpNamingConventions.ServerV0 {
class UndertowServletV0Test extends UndertowServletTest implements TestingGenericHttpNamingConventions.ServerV0 {
}
class UndertowServletNoHttpRouteTest extends UndertowServletTest implements TestingGenericHttpNamingConventions.ServerV0 {
@Override
def generateHttpRoute() {
false
}

@Override
protected void configurePreAgent() {
super.configurePreAgent()
injectSysConfig("trace.undertow.ignore.http.route", "true")
}
}

class UndertowServletV1ForkedTest extends UndertowServletTest implements TestingGenericHttpNamingConventions.ServerV1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ public final class TraceInstrumentationConfig {
public static final String JAX_RS_ADDITIONAL_ANNOTATIONS = "trace.jax-rs.additional.annotations";
/** If set, the instrumentation will set its resource name on the local root too. */
public static final String AXIS_PROMOTE_RESOURCE_NAME = "trace.axis.promote.resource-name";
/** If explicitly set to true, undertow won't set the `http.route` tag. */
public static final String UNDERTOW_IGNORE_HTTP_ROUTE =
"trace.undertow.ignore.http.route";

private TraceInstrumentationConfig() {}
}
10 changes: 10 additions & 0 deletions internal-api/src/main/java/datadog/trace/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@
import static datadog.trace.api.config.TraceInstrumentationConfig.SPARK_APP_NAME_AS_SERVICE;
import static datadog.trace.api.config.TraceInstrumentationConfig.SPARK_TASK_HISTOGRAM_ENABLED;
import static datadog.trace.api.config.TraceInstrumentationConfig.SPRING_DATA_REPOSITORY_INTERFACE_RESOURCE_NAME;
import static datadog.trace.api.config.TraceInstrumentationConfig.UNDERTOW_IGNORE_HTTP_ROUTE;
import static datadog.trace.api.config.TracerConfig.AGENT_HOST;
import static datadog.trace.api.config.TracerConfig.AGENT_NAMED_PIPE;
import static datadog.trace.api.config.TracerConfig.AGENT_PORT_LEGACY;
Expand Down Expand Up @@ -929,6 +930,8 @@ static class HostNameHolder {

private final boolean telemetryDebugRequestsEnabled;

private final boolean undertowIgnoreHttpRouteEnabled;

// Read order: System Properties -> Env Variables, [-> properties file], [-> default value]
private Config() {
this(ConfigProvider.createDefault());
Expand Down Expand Up @@ -2032,6 +2035,9 @@ PROFILING_DATADOG_PROFILER_ENABLED, isDatadogProfilerSafeInCurrentEnvironment())

axisPromoteResourceName = configProvider.getBoolean(AXIS_PROMOTE_RESOURCE_NAME, false);

this.undertowIgnoreHttpRouteEnabled =
configProvider.getBoolean(UNDERTOW_IGNORE_HTTP_ROUTE, false);

this.traceFlushIntervalSeconds =
configProvider.getFloat(
TracerConfig.TRACE_FLUSH_INTERVAL, ConfigDefaults.DEFAULT_TRACE_FLUSH_INTERVAL);
Expand Down Expand Up @@ -3395,6 +3401,10 @@ public boolean isAxisPromoteResourceName() {
return axisPromoteResourceName;
}

public boolean isUndertowIgnoreHttpRouteEnabled() {
return undertowIgnoreHttpRouteEnabled;
}

/** @return A map of tags to be applied only to the local application root span. */
public Map<String, Object> getLocalRootSpanTags() {
final Map<String, String> runtimeTags = getRuntimeTags();
Expand Down

0 comments on commit 1ca6aa2

Please sign in to comment.