|
| 1 | +package datadog.trace.instrumentation.netty38.server.websocket; |
| 2 | + |
| 3 | +import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; |
| 4 | +import static datadog.trace.bootstrap.instrumentation.decorator.WebsocketDecorator.DECORATE; |
| 5 | +import static datadog.trace.bootstrap.instrumentation.websocket.HandlersExtractor.MESSAGE_TYPE_TEXT; |
| 6 | + |
| 7 | +import datadog.trace.bootstrap.ContextStore; |
| 8 | +import datadog.trace.bootstrap.instrumentation.api.AgentScope; |
| 9 | +import datadog.trace.bootstrap.instrumentation.api.AgentSpan; |
| 10 | +import datadog.trace.bootstrap.instrumentation.websocket.HandlerContext; |
| 11 | +import datadog.trace.instrumentation.netty38.ChannelTraceContext; |
| 12 | +import org.jboss.netty.channel.Channel; |
| 13 | +import org.jboss.netty.channel.ChannelHandlerContext; |
| 14 | +import org.jboss.netty.channel.MessageEvent; |
| 15 | +import org.jboss.netty.channel.SimpleChannelUpstreamHandler; |
| 16 | +import org.jboss.netty.handler.codec.http.websocketx.BinaryWebSocketFrame; |
| 17 | +import org.jboss.netty.handler.codec.http.websocketx.CloseWebSocketFrame; |
| 18 | +import org.jboss.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame; |
| 19 | +import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame; |
| 20 | +import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame; |
| 21 | + |
| 22 | +public class WebSocketServerRequestTracingHandler extends SimpleChannelUpstreamHandler { |
| 23 | + |
| 24 | + private final ContextStore<Channel, ChannelTraceContext> contextStore; |
| 25 | + |
| 26 | + public WebSocketServerRequestTracingHandler( |
| 27 | + final ContextStore<Channel, ChannelTraceContext> contextStore) { |
| 28 | + this.contextStore = contextStore; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) throws Exception { |
| 33 | + Object frame = event.getMessage(); |
| 34 | + if (frame instanceof WebSocketFrame) { |
| 35 | + Channel channel = ctx.getChannel(); |
| 36 | + |
| 37 | + ChannelTraceContext traceContext = this.contextStore.get(channel); |
| 38 | + if (traceContext != null) { |
| 39 | + |
| 40 | + HandlerContext.Receiver receiverContext = traceContext.getReceiverHandlerContext(); |
| 41 | + if (receiverContext == null) { |
| 42 | + HandlerContext.Sender sessionState = traceContext.getSenderHandlerContext(); |
| 43 | + if (sessionState != null) { |
| 44 | + receiverContext = |
| 45 | + new HandlerContext.Receiver( |
| 46 | + sessionState.getHandshakeSpan(), channel.getId().toString()); |
| 47 | + traceContext.setReceiverHandlerContext(receiverContext); |
| 48 | + } |
| 49 | + } |
| 50 | + if (receiverContext != null) { |
| 51 | + if (frame instanceof TextWebSocketFrame) { |
| 52 | + // WebSocket Read Text Start |
| 53 | + TextWebSocketFrame textFrame = (TextWebSocketFrame) frame; |
| 54 | + |
| 55 | + final AgentSpan span = |
| 56 | + DECORATE.onReceiveFrameStart( |
| 57 | + receiverContext, textFrame.getText(), textFrame.isFinalFragment()); |
| 58 | + try (final AgentScope scope = activateSpan(span)) { |
| 59 | + ctx.sendUpstream(event); |
| 60 | + // WebSocket Read Text Start |
| 61 | + } finally { |
| 62 | + if (textFrame.isFinalFragment()) { |
| 63 | + traceContext.setReceiverHandlerContext(null); |
| 64 | + DECORATE.onFrameEnd(receiverContext); |
| 65 | + } |
| 66 | + } |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if (frame instanceof BinaryWebSocketFrame) { |
| 71 | + // WebSocket Read Binary Start |
| 72 | + BinaryWebSocketFrame binaryFrame = (BinaryWebSocketFrame) frame; |
| 73 | + final AgentSpan span = |
| 74 | + DECORATE.onReceiveFrameStart( |
| 75 | + receiverContext, |
| 76 | + binaryFrame.getBinaryData().array(), |
| 77 | + binaryFrame.isFinalFragment()); |
| 78 | + try (final AgentScope scope = activateSpan(span)) { |
| 79 | + ctx.sendUpstream(event); |
| 80 | + } finally { |
| 81 | + // WebSocket Read Binary End |
| 82 | + if (binaryFrame.isFinalFragment()) { |
| 83 | + traceContext.setReceiverHandlerContext(null); |
| 84 | + DECORATE.onFrameEnd(receiverContext); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + if (frame instanceof ContinuationWebSocketFrame) { |
| 92 | + ContinuationWebSocketFrame continuationWebSocketFrame = |
| 93 | + (ContinuationWebSocketFrame) frame; |
| 94 | + final AgentSpan span = |
| 95 | + DECORATE.onReceiveFrameStart( |
| 96 | + receiverContext, |
| 97 | + MESSAGE_TYPE_TEXT.equals(receiverContext.getMessageType()) |
| 98 | + ? continuationWebSocketFrame.getText() |
| 99 | + : continuationWebSocketFrame.getBinaryData().array(), |
| 100 | + continuationWebSocketFrame.isFinalFragment()); |
| 101 | + try (final AgentScope scope = activateSpan(span)) { |
| 102 | + ctx.sendUpstream(event); |
| 103 | + } finally { |
| 104 | + if (continuationWebSocketFrame.isFinalFragment()) { |
| 105 | + traceContext.setReceiverHandlerContext(null); |
| 106 | + DECORATE.onFrameEnd(receiverContext); |
| 107 | + } |
| 108 | + } |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + if (frame instanceof CloseWebSocketFrame) { |
| 113 | + // WebSocket Closed by client |
| 114 | + CloseWebSocketFrame closeFrame = (CloseWebSocketFrame) frame; |
| 115 | + int statusCode = closeFrame.getStatusCode(); |
| 116 | + String reasonText = closeFrame.getReasonText(); |
| 117 | + traceContext.setSenderHandlerContext(null); |
| 118 | + traceContext.setReceiverHandlerContext(null); |
| 119 | + final AgentSpan span = |
| 120 | + DECORATE.onSessionCloseReceived(receiverContext, reasonText, statusCode); |
| 121 | + try (final AgentScope scope = activateSpan(span)) { |
| 122 | + ctx.sendUpstream(event); |
| 123 | + if (closeFrame.isFinalFragment()) { |
| 124 | + DECORATE.onFrameEnd(receiverContext); |
| 125 | + } |
| 126 | + } |
| 127 | + return; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + ctx.sendUpstream(event); // superclass does not throw |
| 134 | + } |
| 135 | +} |
0 commit comments