-
Notifications
You must be signed in to change notification settings - Fork 337
Drop the duplicate handler resolution in Spring Web 6 instrumentation #11533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gh-worker-dd-mergequeue-cf854d
merged 2 commits into
master
from
andrea.marziali/spring-6-rework
Jun 3, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...va/datadog/trace/instrumentation/springweb6/ResourceNameFilterMappingInstrumentation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package datadog.trace.instrumentation.springweb6; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isProtected; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.agent.tooling.InstrumenterModule; | ||
|
|
||
| /** | ||
| * When the opt-in {@code spring-path-filter} integration is enabled, wires the {@code | ||
| * DispatcherServlet} handler mappings into {@link HandlerMappingResourceNameFilter} on context | ||
| * refresh. Disabled by default; the default route naming is handled by {@link HandlerMappingAdvice} | ||
| * ({@code getHandler}) and {@link ControllerAdvice}. | ||
| */ | ||
| @AutoService(InstrumenterModule.class) | ||
| public final class ResourceNameFilterMappingInstrumentation extends InstrumenterModule.Tracing | ||
| implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice { | ||
|
|
||
| public ResourceNameFilterMappingInstrumentation() { | ||
| super("spring-web", "spring-path-filter"); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean defaultEnabled() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public String instrumentedType() { | ||
| return "org.springframework.web.servlet.DispatcherServlet"; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] helperClassNames() { | ||
| return new String[] { | ||
| packageName + ".SpringWebHttpServerDecorator", | ||
| packageName + ".ServletRequestURIAdapter", | ||
| packageName + ".HandlerMappingResourceNameFilter", | ||
| packageName + ".HandlerMappingResourceNameFilter$BeanDefinition", | ||
| packageName + ".PathMatchingHttpServletRequestWrapper", | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| isMethod() | ||
| .and(isProtected()) | ||
| .and(named("onRefresh")) | ||
| .and(takesArgument(0, named("org.springframework.context.ApplicationContext"))) | ||
| .and(takesArguments(1)), | ||
| packageName + ".ResourceNameFilterMappingAdvice"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 28 additions & 15 deletions
43
...vc-6.0/src/main/java17/datadog/trace/instrumentation/springweb6/HandlerMappingAdvice.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...main/java17/datadog/trace/instrumentation/springweb6/ResourceNameFilterMappingAdvice.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package datadog.trace.instrumentation.springweb6; | ||
|
|
||
| import java.util.List; | ||
| import net.bytebuddy.asm.Advice; | ||
| import org.springframework.context.ApplicationContext; | ||
| import org.springframework.web.servlet.HandlerMapping; | ||
|
|
||
| /** | ||
| * Wires the {@code DispatcherServlet} handler mappings into the (opt-in) {@link | ||
| * HandlerMappingResourceNameFilter} after the context refreshes, so the filter can evaluate them at | ||
| * the start of the filter chain. Only active when the {@code spring-path-filter} integration is | ||
| * enabled (see {@code ResourceNameFilterMappingInstrumentation}). | ||
| */ | ||
| public class ResourceNameFilterMappingAdvice { | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void afterRefresh( | ||
| @Advice.Argument(0) final ApplicationContext springCtx, | ||
| @Advice.FieldValue("handlerMappings") final List<HandlerMapping> handlerMappings) { | ||
| if (handlerMappings != null && springCtx.containsBean("ddDispatcherFilter")) { | ||
| final HandlerMappingResourceNameFilter filter = | ||
| (HandlerMappingResourceNameFilter) springCtx.getBean("ddDispatcherFilter"); | ||
| filter.setHandlerMappings(handlerMappings); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...c-6.0/src/test/java/datadog/trace/instrumentation/springweb6/boot/FailOnHeaderConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package datadog.trace.instrumentation.springweb6.boot; | ||
|
|
||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
|
||
| @Configuration | ||
| public class FailOnHeaderConfig implements WebMvcConfigurer { | ||
| @Override | ||
| public void addInterceptors(final InterceptorRegistry registry) { | ||
| registry.addInterceptor(new FailOnHeaderInterceptor()); | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
.../src/test/java/datadog/trace/instrumentation/springweb6/boot/FailOnHeaderInterceptor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package datadog.trace.instrumentation.springweb6.boot; | ||
|
|
||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.springframework.web.servlet.HandlerInterceptor; | ||
|
|
||
| /** | ||
| * Aborts the request from {@code preHandle} when the {@code fail} header is set, so the controller | ||
| * is never invoked. Test to verify the route is still named on the server span in that case (see | ||
| * {@code HandlerMappingAdvice}). | ||
| */ | ||
| public class FailOnHeaderInterceptor implements HandlerInterceptor { | ||
| @Override | ||
| public boolean preHandle( | ||
| final HttpServletRequest request, final HttpServletResponse response, final Object handler) { | ||
| if ("true".equalsIgnoreCase(request.getHeader("fail"))) { | ||
| throw new RuntimeException("Stop here"); | ||
| } | ||
| return true; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.