From 1e7eecd94541e4580f9dd40881588ff8e8f8eab8 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Tue, 22 Apr 2025 10:53:36 +0200 Subject: [PATCH 1/2] ref(node): Log when incoming request bodies are being captured --- .../http/SentryHttpInstrumentation.ts | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/node/src/integrations/http/SentryHttpInstrumentation.ts b/packages/node/src/integrations/http/SentryHttpInstrumentation.ts index 0e0502b6fd1f..267588360f72 100644 --- a/packages/node/src/integrations/http/SentryHttpInstrumentation.ts +++ b/packages/node/src/integrations/http/SentryHttpInstrumentation.ts @@ -30,6 +30,8 @@ import { getRequestInfo } from './vendor/getRequestInfo'; type Http = typeof http; type Https = typeof https; +const INSTRUMENTATION_NAME = '@sentry/instrumentation-http'; + export type SentryHttpInstrumentationOptions = InstrumentationConfig & { /** * Whether breadcrumbs should be recorded for requests. @@ -101,7 +103,7 @@ const MAX_BODY_BYTE_LENGTH = 1024 * 1024; */ export class SentryHttpInstrumentation extends InstrumentationBase { public constructor(config: SentryHttpInstrumentationOptions = {}) { - super('@sentry/instrumentation-http', VERSION, config); + super(INSTRUMENTATION_NAME, VERSION, config); } /** @inheritdoc */ @@ -377,6 +379,10 @@ function patchRequestToCaptureBody(req: IncomingMessage, isolationScope: Scope): apply: (target, thisArg, args: Parameters) => { const [event, listener, ...restArgs] = args; + if (DEBUG_BUILD) { + logger.log(INSTRUMENTATION_NAME, 'Patching request.on', event); + } + if (event === 'data') { const callback = new Proxy(listener, { apply: (target, thisArg, args: Parameters) => { @@ -387,6 +393,7 @@ function patchRequestToCaptureBody(req: IncomingMessage, isolationScope: Scope): chunks.push(chunk); } else if (DEBUG_BUILD) { logger.log( + INSTRUMENTATION_NAME, `Dropping request body chunk because it maximum body length of ${MAX_BODY_BYTE_LENGTH}b is exceeded.`, ); } @@ -410,8 +417,10 @@ function patchRequestToCaptureBody(req: IncomingMessage, isolationScope: Scope): const normalizedRequest = { data: body } satisfies RequestEventData; isolationScope.setSDKProcessingMetadata({ normalizedRequest }); } - } catch { - // ignore errors here + } catch (error) { + if (DEBUG_BUILD) { + logger.error(INSTRUMENTATION_NAME, 'Error building captured request body', error); + } } return Reflect.apply(target, thisArg, args); @@ -445,8 +454,10 @@ function patchRequestToCaptureBody(req: IncomingMessage, isolationScope: Scope): return Reflect.apply(target, thisArg, args); }, }); - } catch { - // ignore errors if we can't patch stuff + } catch (error) { + if (DEBUG_BUILD) { + logger.error(INSTRUMENTATION_NAME, 'Error patching request to capture body', error); + } } } From d26edc0b9417d93bec0325cd9358e40708f2bb08 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Tue, 22 Apr 2025 10:56:07 +0200 Subject: [PATCH 2/2] fix typo --- .../node/src/integrations/http/SentryHttpInstrumentation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/node/src/integrations/http/SentryHttpInstrumentation.ts b/packages/node/src/integrations/http/SentryHttpInstrumentation.ts index 267588360f72..32b97e628d66 100644 --- a/packages/node/src/integrations/http/SentryHttpInstrumentation.ts +++ b/packages/node/src/integrations/http/SentryHttpInstrumentation.ts @@ -394,7 +394,7 @@ function patchRequestToCaptureBody(req: IncomingMessage, isolationScope: Scope): } else if (DEBUG_BUILD) { logger.log( INSTRUMENTATION_NAME, - `Dropping request body chunk because it maximum body length of ${MAX_BODY_BYTE_LENGTH}b is exceeded.`, + `Dropping request body chunk because maximum body length of ${MAX_BODY_BYTE_LENGTH}b is exceeded.`, ); }