Skip to content

Commit 1d10238

Browse files
authored
ref(node): Log when incoming request bodies are being captured (#16104)
We used to swallow most things when we try to capture request bodies of incoming requests in node. This can make it hard to debug this. This PR adds some log messages here: 1. Logs when we successfully patched `request.on` 2. Logs the errors that happen during patching ref #16090
1 parent e4f4597 commit 1d10238

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

packages/node/src/integrations/http/SentryHttpInstrumentation.ts

+17-6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import { getRequestInfo } from './vendor/getRequestInfo';
3030
type Http = typeof http;
3131
type Https = typeof https;
3232

33+
const INSTRUMENTATION_NAME = '@sentry/instrumentation-http';
34+
3335
export type SentryHttpInstrumentationOptions = InstrumentationConfig & {
3436
/**
3537
* Whether breadcrumbs should be recorded for requests.
@@ -101,7 +103,7 @@ const MAX_BODY_BYTE_LENGTH = 1024 * 1024;
101103
*/
102104
export class SentryHttpInstrumentation extends InstrumentationBase<SentryHttpInstrumentationOptions> {
103105
public constructor(config: SentryHttpInstrumentationOptions = {}) {
104-
super('@sentry/instrumentation-http', VERSION, config);
106+
super(INSTRUMENTATION_NAME, VERSION, config);
105107
}
106108

107109
/** @inheritdoc */
@@ -377,6 +379,10 @@ function patchRequestToCaptureBody(req: IncomingMessage, isolationScope: Scope):
377379
apply: (target, thisArg, args: Parameters<typeof req.on>) => {
378380
const [event, listener, ...restArgs] = args;
379381

382+
if (DEBUG_BUILD) {
383+
logger.log(INSTRUMENTATION_NAME, 'Patching request.on', event);
384+
}
385+
380386
if (event === 'data') {
381387
const callback = new Proxy(listener, {
382388
apply: (target, thisArg, args: Parameters<typeof listener>) => {
@@ -387,7 +393,8 @@ function patchRequestToCaptureBody(req: IncomingMessage, isolationScope: Scope):
387393
chunks.push(chunk);
388394
} else if (DEBUG_BUILD) {
389395
logger.log(
390-
`Dropping request body chunk because it maximum body length of ${MAX_BODY_BYTE_LENGTH}b is exceeded.`,
396+
INSTRUMENTATION_NAME,
397+
`Dropping request body chunk because maximum body length of ${MAX_BODY_BYTE_LENGTH}b is exceeded.`,
391398
);
392399
}
393400

@@ -410,8 +417,10 @@ function patchRequestToCaptureBody(req: IncomingMessage, isolationScope: Scope):
410417
const normalizedRequest = { data: body } satisfies RequestEventData;
411418
isolationScope.setSDKProcessingMetadata({ normalizedRequest });
412419
}
413-
} catch {
414-
// ignore errors here
420+
} catch (error) {
421+
if (DEBUG_BUILD) {
422+
logger.error(INSTRUMENTATION_NAME, 'Error building captured request body', error);
423+
}
415424
}
416425

417426
return Reflect.apply(target, thisArg, args);
@@ -445,8 +454,10 @@ function patchRequestToCaptureBody(req: IncomingMessage, isolationScope: Scope):
445454
return Reflect.apply(target, thisArg, args);
446455
},
447456
});
448-
} catch {
449-
// ignore errors if we can't patch stuff
457+
} catch (error) {
458+
if (DEBUG_BUILD) {
459+
logger.error(INSTRUMENTATION_NAME, 'Error patching request to capture body', error);
460+
}
450461
}
451462
}
452463

0 commit comments

Comments
 (0)