Skip to content

Commit 7439134

Browse files
author
Luca Forstner
committed
Merge remote-tracking branch 'origin/develop' into lforst-body-capturing
2 parents 521824b + 1d10238 commit 7439134

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

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

Lines changed: 22 additions & 7 deletions
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

@@ -424,13 +431,21 @@ function patchRequestToCaptureBody(req: IncomingMessage, isolationScope: Scope):
424431
});
425432

426433
req.on('end', () => {
427-
const body = Buffer.concat(chunks).toString('utf-8');
428-
if (body) {
429-
isolationScope.setSDKProcessingMetadata({ normalizedRequest: { data: body } });
434+
try {
435+
const body = Buffer.concat(chunks).toString('utf-8');
436+
if (body) {
437+
isolationScope.setSDKProcessingMetadata({ normalizedRequest: { data: body } });
438+
}
439+
} catch (error) {
440+
if (DEBUG_BUILD) {
441+
logger.error(INSTRUMENTATION_NAME, 'Error building captured request body', error);
442+
}
430443
}
431444
});
432-
} catch {
433-
// ignore errors if we can't patch stuff
445+
} catch (error) {
446+
if (DEBUG_BUILD) {
447+
logger.error(INSTRUMENTATION_NAME, 'Error patching request to capture body', error);
448+
}
434449
}
435450
}
436451

0 commit comments

Comments
 (0)