Skip to content

WebSocket locally initiated spans ignore single-trace configuration #12390

Description

@lrwh

Tracer Version(s)

1.65.1 and current master (a1b6599a5f9f8d803cfac89fa1c3befe41b8c33f)

Java Version(s)

Oracle JDK 21.0.3

JVM Vendor

Oracle JDK

Bug Report

When DD_TRACE_WEBSOCKET_MESSAGES_SEPARATE_TRACES=false, received messages and peer-initiated close spans use the handshake span context, but locally initiated websocket.send and websocket.close spans still use the currently active context.

If there is no active span when Session.close() or RemoteEndpoint.send*() is called, those spans become roots of new traces. If another business span is active, they join that trace instead. In both cases they have a different trace ID from the WebSocket handshake.

This behavior comes from WebsocketDecorator.onFrameStart: the useDedicatedTraces setting is only checked when traceStarter is true. The traceStarter == false branch always calls startSpan(..., operationName) without the handshake context.

This was reproduced with Jakarta WebSocket and Tyrus 2.2.0, but the behavior is in the shared WebSocket decorator and is not Tyrus-specific.

The current result for one short-lived connection is:

websocket.open     trace=A
websocket.receive  trace=A
websocket.send     trace=B
websocket.close    trace=C

Expected Behavior

The Java configuration documentation says that setting DD_TRACE_WEBSOCKET_MESSAGES_SEPARATE_TRACES=false causes all spans captured during the session to be in the same trace. Locally initiated send and close spans should therefore use the handshake trace as well:

websocket.open     trace=A
websocket.receive  trace=A
websocket.send     trace=A
websocket.close    trace=A

When the setting remains at its default value of true, the existing behavior of outgoing operations following the current trace and linking to the handshake should remain unchanged.

Documentation: https://docs.datadoghq.com/tracing/trace_collection/library_config/java/#websocket_messages

Reproduction Code

Session session = client.connectToServer(endpoint, config, uri);
session.getBasicRemote().sendText("hello");
session.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "done"));

Run without an active application span around sendText and close, with:

DD_TRACE_WEBSOCKET_MESSAGES_SEPARATE_TRACES=false
DD_TRACE_WEBSOCKET_MESSAGES_ENABLED=true

Compare the trace IDs of the handshake, receive, send, and close spans.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions