-
Notifications
You must be signed in to change notification settings - Fork 356
Propagate trace context across Guidewire WSI SOAP worker threads #12125
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
Open
ValentinZakharov
wants to merge
2
commits into
master
Choose a base branch
from
vzakharov/guidewire
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+319
−0
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Guidewire is proprietary and not published to any repository, so this module has no | ||
| // compile dependency on it: the target classes are matched purely by name at runtime. | ||
| plugins { | ||
| id 'dd-trace-java.module.instrumentation' | ||
| } | ||
|
|
||
| muzzle { | ||
| pass { | ||
| coreJdk() | ||
| } | ||
| } | ||
|
|
||
| tasks.named("compileJava") { | ||
| configureCompiler(it, 8) | ||
| } | ||
|
|
||
| dependencies { | ||
| // Not required (the module self-activates in $Activate); kept so the test also exercises the | ||
| // default config where RunnableInstrumentation wraps run() too — the double activation is safe. | ||
| testImplementation project(':dd-java-agent:instrumentation:java:java-concurrent:java-concurrent-1.8') | ||
| // @Trace on fixture methods, to materialize the child span whose parent we assert. | ||
| testImplementation project(':dd-java-agent:instrumentation:datadog:tracing:trace-annotation') | ||
| } |
100 changes: 100 additions & 0 deletions
100
...rc/main/java/datadog/trace/instrumentation/guidewire/WsiAsyncResponseInstrumentation.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,100 @@ | ||
| package datadog.trace.instrumentation.guidewire; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.extendsClass; | ||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.nameStartsWith; | ||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
| import static datadog.trace.bootstrap.instrumentation.java.concurrent.AdviceUtils.capture; | ||
| import static datadog.trace.bootstrap.instrumentation.java.concurrent.AdviceUtils.endTaskScope; | ||
| import static datadog.trace.bootstrap.instrumentation.java.concurrent.AdviceUtils.startTaskScope; | ||
| import static java.util.Collections.singletonMap; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isConstructor; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import datadog.context.ContextScope; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.agent.tooling.InstrumenterModule; | ||
| import datadog.trace.bootstrap.InstrumentationContext; | ||
| import datadog.trace.bootstrap.instrumentation.java.concurrent.State; | ||
| import java.util.Map; | ||
| import net.bytebuddy.asm.Advice; | ||
| import net.bytebuddy.description.type.TypeDescription; | ||
| import net.bytebuddy.matcher.ElementMatcher; | ||
|
|
||
| /** | ||
| * Propagates trace context across the raw thread Guidewire's WSI layer spawns per outbound SOAP | ||
| * call ({@code AsyncResponseImpl$WebserviceInvocationThread}, seen at runtime as {@code | ||
| * "WSI-Invocation"}). | ||
| * | ||
| * <p>{@code java.lang.Thread} can't be instrumented (agent global-ignore + bootstrap), so we match | ||
| * the application-loaded worker subclass instead: capture the context in its {@code <init>} (still | ||
| * on the parent thread) and re-activate it in {@code run()}. Both halves live here so it works even | ||
| * if the runnable/executor integration is off; if that also wraps {@code run()}, the double | ||
| * activation is safe because the continuation is consumed once. | ||
| * | ||
| * <p>Known limitation: the context is captured in {@code <init>} and only released when {@code | ||
| * run()} executes. If a worker is constructed under an active span but never started or run (a rare | ||
| * caller error path), its continuation is not released and the enclosing trace stays pending until | ||
| * the tracer's timeout drops it — other traces are unaffected. Releasing it would need a "will not | ||
| * run" hook, which a raw thread does not expose, or coupling to Guidewire's closed-source, | ||
| * version-specific {@code AsyncResponseImpl} internals; so {@code <init>} remains the only reliable | ||
| * capture point. | ||
| */ | ||
| @AutoService(InstrumenterModule.class) | ||
| public final class WsiAsyncResponseInstrumentation extends InstrumenterModule.ContextTracking | ||
| implements Instrumenter.ForTypeHierarchy, Instrumenter.HasMethodAdvice { | ||
|
|
||
| private static final String ASYNC_RESPONSE = "gw.internal.xml.ws.AsyncResponseImpl"; | ||
|
|
||
| public WsiAsyncResponseInstrumentation() { | ||
| super("guidewire"); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean defaultEnabled() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public String hierarchyMarkerType() { | ||
| return ASYNC_RESPONSE; | ||
| } | ||
|
|
||
| @Override | ||
| public ElementMatcher<TypeDescription> hierarchyMatcher() { | ||
| // '$' matches only nested classes of AsyncResponseImpl, not top-level siblings. | ||
| return nameStartsWith(ASYNC_RESPONSE + "$").and(extendsClass(named("java.lang.Thread"))); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, String> contextStore() { | ||
| return singletonMap(Runnable.class.getName(), State.class.getName()); | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice(isConstructor(), getClass().getName() + "$Capture"); | ||
| transformer.applyAdvice( | ||
| named("run").and(takesArguments(0)).and(isPublic()), getClass().getName() + "$Activate"); | ||
| } | ||
|
|
||
| public static final class Capture { | ||
| @Advice.OnMethodExit(suppress = Throwable.class) | ||
| public static void onConstruct(@Advice.This final Runnable thiz) { | ||
| capture(InstrumentationContext.get(Runnable.class, State.class), thiz); | ||
|
ValentinZakharov marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| public static final class Activate { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static ContextScope enter(@Advice.This final Runnable thiz) { | ||
| return startTaskScope(InstrumentationContext.get(Runnable.class, State.class), thiz); | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void exit(@Advice.Enter final ContextScope scope) { | ||
| endTaskScope(scope); | ||
| } | ||
| } | ||
| } | ||
110 changes: 110 additions & 0 deletions
110
...est/java/datadog/trace/instrumentation/guidewire/WsiAsyncResponseInstrumentationTest.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,110 @@ | ||
| package datadog.trace.instrumentation.guidewire; | ||
|
|
||
| import static datadog.trace.agent.test.assertions.SpanMatcher.span; | ||
| import static datadog.trace.agent.test.assertions.TraceMatcher.SORT_BY_START_TIME; | ||
| import static datadog.trace.agent.test.assertions.TraceMatcher.trace; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan; | ||
|
|
||
| import datadog.context.ContextScope; | ||
| import datadog.trace.agent.test.AbstractInstrumentationTest; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import datadog.trace.test.junit.utils.config.WithConfig; | ||
| import gw.internal.xml.ws.AsyncResponseImpl; | ||
| import gw.internal.xml.ws.UnrelatedWorker; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| @WithConfig(key = "integration.guidewire.enabled", value = "true") | ||
| class WsiAsyncResponseInstrumentationTest extends AbstractInstrumentationTest { | ||
|
|
||
| @FunctionalInterface | ||
| interface Body { | ||
| void run() throws Exception; | ||
| } | ||
|
|
||
| private static void runUnderTrace(String operationName, Body body) throws Exception { | ||
| AgentSpan span = startSpan("guidewire-test", operationName); | ||
| try (ContextScope scope = activateSpan(span)) { | ||
| body.run(); | ||
| } finally { | ||
| span.finish(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void namedWorkerPropagatesContext() throws Exception { | ||
| // Constructed and run while the caller's span is active; invoke() blocks until the worker ends. | ||
| runUnderTrace("parent", () -> new AsyncResponseImpl().invoke()); | ||
|
|
||
| assertTraces( | ||
| trace( | ||
| SORT_BY_START_TIME, | ||
| span().root().operationName("parent"), | ||
| span().childOfPrevious().operationName("soap.call"))); | ||
| } | ||
|
|
||
| @Test | ||
| void anonymousWorkerPropagatesContext() throws Exception { | ||
| runUnderTrace("parent", () -> AsyncResponseImpl.anonymous().invoke()); | ||
|
|
||
| assertTraces( | ||
| trace( | ||
| SORT_BY_START_TIME, | ||
| span().root().operationName("parent"), | ||
| span().childOfPrevious().operationName("soap.call"))); | ||
| } | ||
|
|
||
| @Test | ||
| void synchronousRunPropagatesContext() throws Exception { | ||
| // callTimeout <= 0 path: AsyncResponseImpl.run() calls _thread.run() on the caller thread. | ||
| runUnderTrace("parent", () -> new AsyncResponseImpl().invokeSync()); | ||
|
|
||
| assertTraces( | ||
| trace( | ||
| SORT_BY_START_TIME, | ||
| span().root().operationName("parent"), | ||
| span().childOfPrevious().operationName("soap.call"))); | ||
| } | ||
|
|
||
| @Test | ||
| void unrelatedThreadIsNotInstrumented() throws Exception { | ||
| // Same construction pattern, but a class the narrow matcher must ignore. | ||
| runUnderTrace( | ||
| "parent", | ||
| () -> { | ||
| UnrelatedWorker worker = new UnrelatedWorker(); | ||
| worker.start(); | ||
| worker.join(); | ||
| }); | ||
|
|
||
| // No propagation: the worker's span starts its own trace instead of joining "parent". | ||
| assertTraces( | ||
| trace(span().root().operationName("parent")), | ||
| trace(span().root().operationName("unrelated.work"))); | ||
| } | ||
|
|
||
| @Test | ||
| void noContextLeakToSubsequentInvocation() throws Exception { | ||
| runUnderTrace("parent", () -> new AsyncResponseImpl().invoke()); | ||
| // Second invocation runs with no active span: capture is a no-op, so soap.call is its own root. | ||
| new AsyncResponseImpl().invoke(); | ||
|
|
||
| assertTraces( | ||
| trace( | ||
| SORT_BY_START_TIME, | ||
| span().root().operationName("parent"), | ||
| span().childOfPrevious().operationName("soap.call")), | ||
| trace(span().root().operationName("soap.call"))); | ||
| } | ||
|
|
||
| @Test | ||
| void workerConstructedButNeverRunDoesNotCorruptLaterTraces() throws Exception { | ||
| // Constructed under an active span but never run: capture happens but is never activated. | ||
| // Guards that the stranded continuation does not mis-attribute a later, unrelated trace. | ||
| runUnderTrace("outer", () -> new AsyncResponseImpl()); | ||
| runUnderTrace("independent", () -> {}); | ||
|
|
||
| // 'independent' is a clean, standalone root regardless of the stranded continuation. | ||
| assertTraces(trace(span().root().operationName("independent"))); | ||
| } | ||
| } |
59 changes: 59 additions & 0 deletions
59
...nt/instrumentation/guidewire-10.0/src/test/java/gw/internal/xml/ws/AsyncResponseImpl.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,59 @@ | ||
| package gw.internal.xml.ws; | ||
|
|
||
| import datadog.trace.api.Trace; | ||
|
|
||
| /** | ||
| * Test double for Guidewire's WSI worker; kept in package {@code gw.internal.xml.ws} so the matcher | ||
| * applies. | ||
| */ | ||
| public class AsyncResponseImpl { | ||
|
|
||
| private final Thread thread; | ||
|
|
||
| public AsyncResponseImpl() { | ||
| this.thread = new WebserviceInvocationThread(); | ||
| } | ||
|
|
||
| // The boolean only distinguishes this overload from the no-arg constructor; its value is unused. | ||
| private AsyncResponseImpl(boolean anonymous) { | ||
| this.thread = | ||
| new Thread() { | ||
| @Override | ||
| public void run() { | ||
| soapCall(); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| public static AsyncResponseImpl anonymous() { | ||
| return new AsyncResponseImpl(true); | ||
| } | ||
|
|
||
| public void invoke() throws InterruptedException { | ||
| thread.start(); | ||
| thread.join(); | ||
| } | ||
|
|
||
| public void invokeSync() { | ||
| thread.run(); | ||
| } | ||
|
|
||
| @Trace(operationName = "soap.call") | ||
| static void soapCall() {} | ||
|
|
||
| // Chained constructor: two <init> frames make capture fire twice, testing the State CAS dedup. | ||
| public static final class WebserviceInvocationThread extends Thread { | ||
| public WebserviceInvocationThread() { | ||
| this("WSI-Invocation"); | ||
| } | ||
|
|
||
| private WebserviceInvocationThread(String name) { | ||
| super(name); | ||
| } | ||
|
|
||
| @Override | ||
| public void run() { | ||
| soapCall(); | ||
| } | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
...gent/instrumentation/guidewire-10.0/src/test/java/gw/internal/xml/ws/UnrelatedWorker.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,17 @@ | ||
| package gw.internal.xml.ws; | ||
|
|
||
| import datadog.trace.api.Trace; | ||
|
|
||
| /** | ||
| * Negative control: a Thread subclass the matcher must ignore (not an {@code AsyncResponseImpl$…}). | ||
| */ | ||
| public class UnrelatedWorker extends Thread { | ||
|
|
||
| @Override | ||
| public void run() { | ||
| unrelatedWork(); | ||
| } | ||
|
|
||
| @Trace(operationName = "unrelated.work") | ||
| static void unrelatedWork() {} | ||
| } |
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
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.