|
| 1 | +const { IncomingMessage, ClientRequest } = require('http'); |
| 2 | +const { HttpExtendedAttribute } = require('./constants'); |
| 3 | +const { shouldCaptureBodyByMimeType } = require('./mime-type'); |
| 4 | +const { StreamChunks } = require('./stream-chunks'); |
| 5 | + |
| 6 | +const streamChunksKey = Symbol('opentelemetry.instrumentation.http.StreamChunks'); |
| 7 | + |
| 8 | +const httpCustomAttributes = ( |
| 9 | + span, |
| 10 | + request, |
| 11 | + response |
| 12 | +) => { |
| 13 | + if (request instanceof ClientRequest) { |
| 14 | + const reqPath = request.path.split('?')[0]; |
| 15 | + span.setAttribute(HttpExtendedAttribute.HTTP_PATH, reqPath); |
| 16 | + span.setAttribute( |
| 17 | + HttpExtendedAttribute.HTTP_REQUEST_HEADERS, |
| 18 | + JSON.stringify((request).getHeaders()) |
| 19 | + ); |
| 20 | + } |
| 21 | + if (response instanceof IncomingMessage) { |
| 22 | + span.setAttribute( |
| 23 | + HttpExtendedAttribute.HTTP_RESPONSE_HEADERS, |
| 24 | + JSON.stringify((response).headers) |
| 25 | + ); |
| 26 | + } |
| 27 | + |
| 28 | + const requestBody = request[streamChunksKey]; |
| 29 | + if (requestBody) { |
| 30 | + span.setAttribute(HttpExtendedAttribute.HTTP_REQUEST_BODY, requestBody.getBody()); |
| 31 | + } |
| 32 | + |
| 33 | + const responseBody = response[streamChunksKey]; |
| 34 | + if (responseBody) { |
| 35 | + span.setAttribute(HttpExtendedAttribute.HTTP_RESPONSE_BODY, responseBody.getBody()); |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +const httpCustomAttributesOnRequest = (span, request) => { |
| 40 | + if (request instanceof ClientRequest) { |
| 41 | + const requestMimeType = request.getHeader('content-type'); |
| 42 | + if (!shouldCaptureBodyByMimeType(requestMimeType)) { |
| 43 | + span.setAttribute( |
| 44 | + HttpExtendedAttribute.HTTP_REQUEST_BODY, |
| 45 | + `Request body not collected due to unsupported mime type: ${requestMimeType}` |
| 46 | + ); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + let oldWrite = request.write; |
| 51 | + request[streamChunksKey] = new StreamChunks(); |
| 52 | + request.write = function (data) { |
| 53 | + const expectDevData = request[streamChunksKey]; |
| 54 | + expectDevData?.addChunk(data); |
| 55 | + return oldWrite.call(request, data); |
| 56 | + }; |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +const httpCustomAttributesOnResponse = (span, response) => { |
| 61 | + if (response instanceof IncomingMessage) { |
| 62 | + const responseMimeType = response.headers?.['content-type']; |
| 63 | + if (!shouldCaptureBodyByMimeType(responseMimeType)) { |
| 64 | + span.setAttribute( |
| 65 | + HttpExtendedAttribute.HTTP_RESPONSE_BODY, |
| 66 | + `Response body not collected due to unsupported mime type: ${responseMimeType}` |
| 67 | + ); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + response[streamChunksKey] = new StreamChunks(); |
| 72 | + const origPush = response.push; |
| 73 | + response.push = function (chunk) { |
| 74 | + if (chunk) { |
| 75 | + const expectDevData = response[streamChunksKey]; |
| 76 | + expectDevData?.addChunk(chunk); |
| 77 | + } |
| 78 | + return origPush.apply(this, arguments); |
| 79 | + }; |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +const httpInstrumentationConfig = { |
| 84 | + applyCustomAttributesOnSpan: httpCustomAttributes, |
| 85 | + requestHook: httpCustomAttributesOnRequest, |
| 86 | + responseHook: httpCustomAttributesOnResponse, |
| 87 | + headersToSpanAttributes: { |
| 88 | + client: { |
| 89 | + requestHeaders: ['traceloop_id'], |
| 90 | + responseHeaders: ['traceloop_id'], |
| 91 | + }, |
| 92 | + server: { |
| 93 | + requestHeaders: ['traceloop_id'], |
| 94 | + responseHeaders: ['traceloop_id'], |
| 95 | + }, |
| 96 | + }, |
| 97 | +}; |
| 98 | + |
| 99 | +module.exports = { |
| 100 | + httpInstrumentationConfig |
| 101 | +}; |
0 commit comments