-
Notifications
You must be signed in to change notification settings - Fork 312
Support CompletableFuture on spring webmvc controllers #8659
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
206c03e
Support CompletableFuture on spring webmvc controllers
amarziali 48056e9
Update dd-java-agent/instrumentation/spring-webmvc-3.1/src/main/java/…
amarziali 5a31c34
Update dd-java-agent/instrumentation/spring-webmvc-6.0/src/main/java1…
amarziali 904f3aa
add suggestions
amarziali 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
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
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
76 changes: 76 additions & 0 deletions
76
...n/java/datadog/trace/instrumentation/springweb/InvocableHandlerMethodInstrumentation.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,76 @@ | ||
package datadog.trace.instrumentation.springweb; | ||
|
||
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
import static datadog.trace.instrumentation.springweb.SpringWebHttpServerDecorator.DD_HANDLER_SPAN_CONTINUE_SUFFIX; | ||
import static datadog.trace.instrumentation.springweb.SpringWebHttpServerDecorator.DD_HANDLER_SPAN_PREFIX_KEY; | ||
|
||
import com.google.auto.service.AutoService; | ||
import datadog.trace.agent.tooling.Instrumenter; | ||
import datadog.trace.agent.tooling.InstrumenterModule; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
import datadog.trace.bootstrap.instrumentation.java.concurrent.AsyncResultExtensions; | ||
import java.util.concurrent.CompletionStage; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.implementation.bytecode.assign.Assigner; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
import org.springframework.web.context.request.ServletWebRequest; | ||
import org.springframework.web.method.support.InvocableHandlerMethod; | ||
|
||
@AutoService(InstrumenterModule.class) | ||
public class InvocableHandlerMethodInstrumentation extends InstrumenterModule.Tracing | ||
implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice { | ||
public InvocableHandlerMethodInstrumentation() { | ||
super("spring-web"); | ||
} | ||
|
||
@Override | ||
public String instrumentedType() { | ||
return "org.springframework.web.method.support.InvocableHandlerMethod"; | ||
} | ||
|
||
@Override | ||
public void methodAdvice(MethodTransformer transformer) { | ||
transformer.applyAdvice( | ||
named("invokeForRequest"), getClass().getName() + "$WrapContinuableResultAdvice"); | ||
} | ||
|
||
@Override | ||
public String[] helperClassNames() { | ||
return new String[] { | ||
packageName + ".SpringWebHttpServerDecorator", packageName + ".ServletRequestURIAdapter", | ||
}; | ||
} | ||
|
||
public static class WrapContinuableResultAdvice { | ||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static void after( | ||
@Advice.Argument(value = 0, typing = Assigner.Typing.DYNAMIC) | ||
final NativeWebRequest nativeWebRequest, | ||
@Advice.Return(readOnly = false) Object result, | ||
@Advice.This final InvocableHandlerMethod self) { | ||
if (!(nativeWebRequest instanceof ServletWebRequest) | ||
|| !(result instanceof CompletionStage)) { | ||
return; | ||
} | ||
ServletWebRequest servletWebRequest = (ServletWebRequest) nativeWebRequest; | ||
final String handlerSpanKey = | ||
DD_HANDLER_SPAN_PREFIX_KEY + self.getBean().getClass().getName(); | ||
|
||
if (Boolean.TRUE.equals( | ||
servletWebRequest.getAttribute( | ||
handlerSpanKey + DD_HANDLER_SPAN_CONTINUE_SUFFIX, ServletWebRequest.SCOPE_REQUEST))) { | ||
return; | ||
} | ||
|
||
Object span = servletWebRequest.getAttribute(handlerSpanKey, ServletWebRequest.SCOPE_REQUEST); | ||
if (!(span instanceof AgentSpan)) { | ||
return; | ||
} | ||
servletWebRequest.setAttribute( | ||
handlerSpanKey + DD_HANDLER_SPAN_CONTINUE_SUFFIX, true, ServletWebRequest.SCOPE_REQUEST); | ||
result = | ||
((CompletionStage<?>) result) | ||
.whenComplete(AsyncResultExtensions.finishSpan((AgentSpan) span)); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
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.