diff --git a/client/src/tracing.js b/client/src/tracing.js index 693a69b..fefee36 100644 --- a/client/src/tracing.js +++ b/client/src/tracing.js @@ -46,6 +46,18 @@ const startOtelInstrumentation = () => { enabled: true, propagateTraceHeaderCorsUrls: /.*/, clearTimingResources: true, + applyCustomAttributesOnSpan: (span, req) => { + if (req && req.headers) { + span.setAttribute( + "http.request.headers", + JSON.stringify(req.headers) + ); + } + if (req && req.body) { + const requestBody = req.body; + span.setAttribute("http.request.body", req.body); + } + }, }, }), ], diff --git a/server/src/instrumentation.ts b/server/src/instrumentation.ts index 447f508..7c8f4cd 100644 --- a/server/src/instrumentation.ts +++ b/server/src/instrumentation.ts @@ -89,6 +89,16 @@ class CustomSampler implements Sampler { : SamplingDecision.NOT_RECORD, }; } + if (attributes && attributes["http.method"] == "OPTIONS") { + return { + decision: SamplingDecision.NOT_RECORD, + }; + } + if (attributes && attributes["http.target"] == "/v1/logs") { + return { + decision: SamplingDecision.NOT_RECORD, + }; + } return { decision: SamplingDecision.RECORD_AND_SAMPLED, }; @@ -108,6 +118,29 @@ const sdk = new NodeSDK({ getNodeAutoInstrumentations({ "@opentelemetry/instrumentation-http": { enabled: true, + requestHook: (span, req) => { + let body = ""; + req.on("data", (chunk) => { + body += chunk; + }); + req.on("end", (chunk) => { + try { + span.setAttribute("http.request.body", body); + } catch (error) { + console.log(error); + } + }); + }, + responseHook: (span, response) => { + let body = ""; + response.on("data", (chunk) => { + body += chunk.toString(); + }); + response.on("end", () => { + span.setAttribute("http.response.body", body); + response.removeAllListeners(); + }); + }, }, "@opentelemetry/instrumentation-express": { enabled: true,