|
| 1 | +'use strict' |
| 2 | + |
| 3 | +// WeakMap to store message counters per socket without mutating the socket object |
| 4 | +const socketCounters = new WeakMap() |
| 5 | + |
| 6 | +/** |
| 7 | + * Initializes WebSocket message counters for a socket. |
| 8 | + * @param {object} socket - The WebSocket socket object |
| 9 | + */ |
| 10 | +function initWebSocketMessageCounters (socket) { |
| 11 | + if (!socketCounters.has(socket)) { |
| 12 | + socketCounters.set(socket, { |
| 13 | + receiveCounter: 0, |
| 14 | + sendCounter: 0 |
| 15 | + }) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Increments and returns the WebSocket message counter. |
| 21 | + * @param {object} socket - The WebSocket socket object |
| 22 | + * @param {string} counterType - Either 'receiveCounter' or 'sendCounter' |
| 23 | + * @returns {number} The incremented counter value |
| 24 | + */ |
| 25 | +function incrementWebSocketCounter (socket, counterType) { |
| 26 | + if (!socketCounters.has(socket)) { |
| 27 | + initWebSocketMessageCounters(socket) |
| 28 | + } |
| 29 | + const counters = socketCounters.get(socket) |
| 30 | + counters[counterType]++ |
| 31 | + return counters[counterType] |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Builds a WebSocket span pointer hash. |
| 36 | + * |
| 37 | + * Format: <prefix><128 bit hex trace id><64 bit hex span id><32 bit hex counter> |
| 38 | + * Prefix: 'S' for server outgoing or client incoming, 'C' for server incoming or client outgoing |
| 39 | + * |
| 40 | + * @param {bigint} handshakeTraceId - The trace ID from the handshake span (as a BigInt) |
| 41 | + * @param {bigint} handshakeSpanId - The span ID from the handshake span (as a BigInt) |
| 42 | + * @param {number} counter - The message counter |
| 43 | + * @param {boolean} isServer - Whether this is a server (true) or client (false) |
| 44 | + * @param {boolean} isIncoming - Whether this is an incoming message (true) or outgoing (false) |
| 45 | + * @returns {string} The span pointer hash |
| 46 | + */ |
| 47 | +function buildWebSocketSpanPointerHash (handshakeTraceId, handshakeSpanId, counter, isServer, isIncoming) { |
| 48 | + // Determine prefix based on server/client and incoming/outgoing |
| 49 | + // Server outgoing or client incoming: 'S' |
| 50 | + // Server incoming or client outgoing: 'C' |
| 51 | + const prefix = (isServer && !isIncoming) || (!isServer && isIncoming) ? 'S' : 'C' |
| 52 | + |
| 53 | + // Pad trace ID to 32 hex chars (128 bits) |
| 54 | + const traceIdHex = handshakeTraceId.toString(16).padStart(32, '0') |
| 55 | + |
| 56 | + // Pad span ID to 16 hex chars (64 bits) |
| 57 | + const spanIdHex = handshakeSpanId.toString(16).padStart(16, '0') |
| 58 | + |
| 59 | + // Pad counter to 8 hex chars (32 bits) |
| 60 | + const counterHex = counter.toString(16).padStart(8, '0') |
| 61 | + |
| 62 | + return `${prefix}${traceIdHex}${spanIdHex}${counterHex}` |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Checks if the handshake span has extracted distributed tracing context. |
| 67 | + * A websocket server must not set the span pointer if the handshake has not extracted a context. |
| 68 | + * |
| 69 | + * A span has distributed tracing context if it has a parent context that was |
| 70 | + * extracted from headers (remote parent). |
| 71 | + * |
| 72 | + * @param {object} span - The handshake span |
| 73 | + * @param {object} socket - The WebSocket socket object |
| 74 | + * @returns {boolean} True if the span has distributed tracing context |
| 75 | + */ |
| 76 | +function hasDistributedTracingContext (span, socket) { |
| 77 | + if (!span) return false |
| 78 | + const context = span.context() |
| 79 | + if (!context) return false |
| 80 | + |
| 81 | + // Check if this span has a parent. If the parent was extracted from remote headers, |
| 82 | + // then this span is part of a distributed trace. |
| 83 | + // We check if the span has a parent by looking at _parentId. |
| 84 | + // In the JavaScript tracer, when a context is extracted from headers and a child span |
| 85 | + // is created, the child will have _parentId set to the extracted parent's span ID. |
| 86 | + // |
| 87 | + // For testing purposes, we also check if Datadog trace headers are present in the socket's |
| 88 | + // upgrade request, which indicates distributed tracing context was sent by the client. |
| 89 | + if (context._parentId !== null) { |
| 90 | + return true |
| 91 | + } |
| 92 | + |
| 93 | + // Fallback check: look for distributed tracing headers in the stored request headers |
| 94 | + if (socket && socket.requestHeaders) { |
| 95 | + const headers = socket.requestHeaders |
| 96 | + return !!(headers['x-datadog-trace-id'] || headers.traceparent) |
| 97 | + } |
| 98 | + |
| 99 | + return false |
| 100 | +} |
| 101 | + |
| 102 | +module.exports = { |
| 103 | + initWebSocketMessageCounters, |
| 104 | + incrementWebSocketCounter, |
| 105 | + buildWebSocketSpanPointerHash, |
| 106 | + hasDistributedTracingContext |
| 107 | +} |
0 commit comments