Repository navigation
Add Jetty 12.0 native WebSocket tracing and fix upgrade span completion #12674
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 25 commits into
master
from
ygree/jetty-native-websockets
Oct 20, 2026
+3,110
−69
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
2635b84
Add Jetty 12 native WebSocket tracing and fix upgrade span completion
ygree f8975b9
Convert Jetty 12 WebSocket tests to Java and JUnit 5
ygree c78ea6e
Refactor Jetty 12 WebSocket advice into instrumentation classes and s…
ygree e5375ba
Fix Jetty 12 WebSocket tracing for boxed-Void handlers
ygree 7c4584c
Move Jetty WebSocket upgrade test into client instrumentation module
ygree 6dacd01
Extract shared Jetty WebSocket endpoint factory and clarify test names
ygree af473db
Fix Jetty 12 WebSocket Java tests to target Java 17
ygree 63206fd
Validate Jetty WebSocket upgrade method compatibility with muzzle
ygree 46d005b
Trace messages sent through Jetty 12’s native WebSocket API
ygree de874e5
Fix lost WebSocket send errors after Jetty connection closure
ygree 333e301
Track Jetty 12.0 patch releases in latest-dependency tests and Muzzle.
ygree f8e0fdd
Trace Jetty 12 native streaming WebSocket handlers
ygree 3c32223
Fix Jetty 12 WebSocket handshake spans for swallowed upgrade failures
ygree 32b7140
Fix lost receive errors after Jetty WebSocket peer close
ygree 572a50e
Add Muzzle guard and tests to prevent partial instrumentation on Jett…
ygree 449cb65
Fix Jetty EE9 Muzzle selection and verify EE modules reject Jetty 12.1+
ygree 0e4b096
Add tests for native Jetty WebSocket client tracing
ygree 5e43379
Trace native Jetty WebSocket client receives
ygree 24c6afe
Reduce native Jetty WebSocket tracing overhead
ygree a66ef77
Simplify native Jetty WebSocket tracing tests
ygree a39a803
Fix Jetty 12 WebSocket text receive spans finishing before handler exit
ygree 66e7429
Align Jetty 12 WebSocket client send tracing
ygree 1e670c3
Fix SpotBugs warnings in Jetty 12 WebSocket receive tracing
ygree ea2e598
Merge master into
ygree 0b6a583
Merge master into
ygree 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
112 changes: 112 additions & 0 deletions
112
...va/datadog/trace/instrumentation/jetty_client12/JettyWebSocketUpgradeInstrumentation.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,112 @@ | ||
| package datadog.trace.instrumentation.jetty_client12; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; | ||
| import static datadog.trace.instrumentation.jetty_client12.JettyClientDecorator.DECORATE; | ||
| import static java.util.Collections.singletonMap; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
| 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.api.AgentSpan; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CancellationException; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.CompletionException; | ||
| import net.bytebuddy.asm.Advice; | ||
| import org.eclipse.jetty.client.Request; | ||
| import org.eclipse.jetty.client.Response; | ||
| import org.eclipse.jetty.io.EndPoint; | ||
| import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest; | ||
|
|
||
| @AutoService(InstrumenterModule.class) | ||
| public class JettyWebSocketUpgradeInstrumentation extends InstrumenterModule.Tracing | ||
| implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice { | ||
| public JettyWebSocketUpgradeInstrumentation() { | ||
| super("jetty-client"); | ||
| } | ||
|
|
||
| @Override | ||
| public String muzzleDirective() { | ||
| return "jetty-websocket-core-client"; | ||
| } | ||
|
|
||
| @Override | ||
| public String instrumentedType() { | ||
| return "org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest"; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] helperClassNames() { | ||
| return new String[] {packageName + ".JettyClientDecorator"}; | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, String> contextStore() { | ||
| return singletonMap("org.eclipse.jetty.client.Request", AgentSpan.class.getName()); | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| named("upgrade") | ||
| .and(takesArguments(2)) | ||
| .and(takesArgument(0, named("org.eclipse.jetty.client.Response"))) | ||
| .and(takesArgument(1, named("org.eclipse.jetty.io.EndPoint"))), | ||
| getClass().getName() + "$WebSocketUpgradeAdvice"); | ||
| } | ||
|
|
||
| public static class WebSocketUpgradeAdvice { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static ContextScope beforeUpgrade(@Advice.Argument(0) Response response) { | ||
| AgentSpan span = | ||
| InstrumentationContext.get(Request.class, AgentSpan.class).get(response.getRequest()); | ||
| return span == null ? null : activateSpan(span); | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void afterUpgrade( | ||
| @Advice.Argument(0) Response response, | ||
| @Advice.FieldValue("futureCoreSession") CompletableFuture<?> futureCoreSession, | ||
| @Advice.Enter ContextScope scope, | ||
| @Advice.Thrown Throwable failure) { | ||
| AgentSpan span = | ||
| InstrumentationContext.get(Request.class, AgentSpan.class).get(response.getRequest()); | ||
| try { | ||
| if (span != null && failure == null) { | ||
|
ygree marked this conversation as resolved.
|
||
| // Upgrades bypass response completion listeners even when endpoint failures are | ||
| // swallowed. | ||
| DECORATE.onResponse(span, response); | ||
| try { | ||
| futureCoreSession.getNow(null); | ||
| } catch (CompletionException e) { | ||
| DECORATE.onError(span, e.getCause()); | ||
| } catch (CancellationException e) { | ||
| DECORATE.onError(span, e); | ||
| } | ||
| DECORATE.beforeFinish(span); | ||
| } | ||
| } finally { | ||
| if (scope != null) { | ||
| scope.close(); | ||
| } | ||
| if (span != null && failure == null) { | ||
| span.finish(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Lets Muzzle fail CI if the upgrade method is removed or its signature changes, instead of | ||
| * silently skipping instrumentation. | ||
| */ | ||
| private void muzzleCheck( | ||
| CoreClientUpgradeRequest request, Response response, EndPoint endPoint) { | ||
| request.upgrade(response, endPoint); | ||
| } | ||
| } | ||
| } | ||
147 changes: 147 additions & 0 deletions
147
...ntation/jetty/jetty-client/jetty-client-12.0/src/test/java/JettyWebSocketUpgradeTest.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,147 @@ | ||
| import static datadog.trace.agent.test.assertions.SpanMatcher.span; | ||
| import static datadog.trace.agent.test.assertions.TraceMatcher.trace; | ||
| import static java.util.concurrent.TimeUnit.SECONDS; | ||
| import static java.util.regex.Pattern.compile; | ||
| import static java.util.regex.Pattern.quote; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.AdditionalAnswers.delegatesTo; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.doThrow; | ||
| import static org.mockito.Mockito.mock; | ||
|
|
||
| import datadog.trace.agent.test.AbstractInstrumentationTest; | ||
| import datadog.trace.api.DDSpanTypes; | ||
| import datadog.trace.core.DDSpan; | ||
| import java.net.URI; | ||
| import java.util.List; | ||
| import java.util.concurrent.ExecutionException; | ||
| import org.eclipse.jetty.client.Response; | ||
| import org.eclipse.jetty.io.Connection; | ||
| import org.eclipse.jetty.io.EndPoint; | ||
| import org.eclipse.jetty.server.Server; | ||
| import org.eclipse.jetty.server.ServerConnector; | ||
| import org.eclipse.jetty.server.handler.ContextHandler; | ||
| import org.eclipse.jetty.websocket.api.Session; | ||
| import org.eclipse.jetty.websocket.client.WebSocketClient; | ||
| import org.eclipse.jetty.websocket.core.FrameHandler; | ||
| import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest; | ||
| import org.eclipse.jetty.websocket.core.client.WebSocketCoreClient; | ||
| import org.eclipse.jetty.websocket.server.WebSocketUpgradeHandler; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class JettyWebSocketUpgradeTest extends AbstractInstrumentationTest { | ||
| @Test | ||
| void httpClientSpanFinishesWhenWebSocketUpgradeSucceeds() throws Exception { | ||
| Server server = new Server(0); | ||
| WebSocketClient client = new WebSocketClient(); | ||
| try { | ||
| URI uri = startServer(server); | ||
| client.start(); | ||
|
|
||
| Session session = client.connect(new Endpoint(), uri).get(5, SECONDS); | ||
|
|
||
| assertTrue(session.isOpen()); | ||
| // The HTTP handshake must be reported before the WebSocket connection closes. | ||
| assertTraces( | ||
| trace( | ||
| span() | ||
| .operationName(compile(quote("http.request"))) | ||
| .resourceName(compile(quote("GET /upgrade"))) | ||
| .type(DDSpanTypes.HTTP_CLIENT) | ||
| .root() | ||
| .error(false)), | ||
| trace(span().type(DDSpanTypes.HTTP_SERVER).error(false))); | ||
| DDSpan handshake = | ||
| writer.stream() | ||
| .flatMap(List::stream) | ||
| .filter(s -> "client".equals(s.getTag("span.kind"))) | ||
| .findFirst() | ||
| .orElseThrow(() -> new AssertionError("Missing client handshake span")); | ||
| assertEquals("jetty-client", handshake.getTag("component").toString()); | ||
| assertEquals("client", handshake.getTag("span.kind")); | ||
| assertEquals(101, handshake.getTag("http.status_code")); | ||
| } finally { | ||
| try { | ||
| client.stop(); | ||
| } finally { | ||
| server.stop(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void httpClientSpanRecordsFailureSwallowedByWebSocketUpgrade() throws Exception { | ||
| Server server = new Server(0); | ||
| WebSocketCoreClient client = new WebSocketCoreClient(); | ||
| try { | ||
| URI uri = startServer(server); | ||
| client.start(); | ||
| IllegalStateException upgradeFailure = new IllegalStateException("Endpoint upgrade failed"); | ||
| FrameHandler frameHandler = mock(FrameHandler.class); | ||
| CoreClientUpgradeRequest request = | ||
| new CoreClientUpgradeRequest(client, uri) { | ||
| @Override | ||
| public FrameHandler getFrameHandler() { | ||
| return frameHandler; | ||
| } | ||
|
|
||
| @Override | ||
| public void upgrade(Response response, EndPoint endPoint) { | ||
| EndPoint failingEndPoint = mock(EndPoint.class, delegatesTo(endPoint)); | ||
| doThrow(upgradeFailure).when(failingEndPoint).upgrade(any(Connection.class)); | ||
| super.upgrade(response, failingEndPoint); | ||
| } | ||
| }; | ||
|
|
||
| ExecutionException failure = | ||
| assertThrows(ExecutionException.class, () -> client.connect(request).get(5, SECONDS)); | ||
| assertSame(upgradeFailure, failure.getCause()); | ||
|
|
||
| assertTraces( | ||
| trace( | ||
| span() | ||
| .operationName(compile(quote("http.request"))) | ||
| .resourceName(compile(quote("GET /upgrade"))) | ||
| .type(DDSpanTypes.HTTP_CLIENT) | ||
| .root() | ||
| .error(true)), | ||
| trace(span().type(DDSpanTypes.HTTP_SERVER).error(false))); | ||
| DDSpan handshake = | ||
| writer.stream() | ||
| .flatMap(List::stream) | ||
| .filter(s -> "client".equals(s.getTag("span.kind"))) | ||
| .findFirst() | ||
| .orElseThrow(() -> new AssertionError("Missing client handshake span")); | ||
| assertEquals(101, handshake.getTag("http.status_code")); | ||
| assertEquals(IllegalStateException.class.getName(), handshake.getTag("error.type")); | ||
| assertEquals(upgradeFailure.getMessage(), handshake.getTag("error.message")); | ||
| } finally { | ||
| try { | ||
| client.stop(); | ||
| } finally { | ||
| server.stop(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static URI startServer(Server server) throws Exception { | ||
| ContextHandler context = new ContextHandler("/"); | ||
| server.setHandler(context); | ||
| context.setHandler( | ||
| WebSocketUpgradeHandler.from(server, context) | ||
| .configure( | ||
| container -> | ||
| container.addMapping( | ||
| "/upgrade", (request, response, callback) -> new Endpoint()))); | ||
| server.start(); | ||
| return URI.create( | ||
| "ws://localhost:" | ||
| + ((ServerConnector) server.getConnectors()[0]).getLocalPort() | ||
| + "/upgrade"); | ||
| } | ||
|
|
||
| public static class Endpoint implements Session.Listener.AutoDemanding {} | ||
| } |
Oops, something went wrong.
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.