@@ -3,7 +3,7 @@ import { context, propagation } from '@opentelemetry/api';
3
3
import { VERSION } from '@opentelemetry/core' ;
4
4
import type { InstrumentationConfig } from '@opentelemetry/instrumentation' ;
5
5
import { InstrumentationBase , InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation' ;
6
- import type { AggregationCounts , Client , RequestEventData , SanitizedRequestData , Scope } from '@sentry/core' ;
6
+ import type { AggregationCounts , Client , SanitizedRequestData , Scope } from '@sentry/core' ;
7
7
import {
8
8
addBreadcrumb ,
9
9
generateSpanId ,
@@ -360,12 +360,9 @@ function getBreadcrumbData(request: http.ClientRequest): Partial<SanitizedReques
360
360
* This way, we only read the body if the user also consumes the body, ensuring we do not change any behavior in unexpected ways.
361
361
*/
362
362
function patchRequestToCaptureBody ( req : IncomingMessage , isolationScope : Scope ) : void {
363
+ let bodyByteLength = 0 ;
363
364
const chunks : Buffer [ ] = [ ] ;
364
365
365
- function getChunksSize ( ) : number {
366
- return chunks . reduce ( ( acc , chunk ) => acc + chunk . byteLength , 0 ) ;
367
- }
368
-
369
366
/**
370
367
* We need to keep track of the original callbacks, in order to be able to remove listeners again.
371
368
* Since `off` depends on having the exact same function reference passed in, we need to be able to map
@@ -386,41 +383,21 @@ function patchRequestToCaptureBody(req: IncomingMessage, isolationScope: Scope):
386
383
if ( event === 'data' ) {
387
384
const callback = new Proxy ( listener , {
388
385
apply : ( target , thisArg , args : Parameters < typeof listener > ) => {
389
- // If we have already read more than the max body length, we stop adding chunks
390
- // To avoid growing the memory indefinitely if a response is e.g. streamed
391
- if ( getChunksSize ( ) < MAX_BODY_BYTE_LENGTH ) {
392
- const chunk = args [ 0 ] as Buffer ;
393
- chunks . push ( chunk ) ;
394
- } else if ( DEBUG_BUILD ) {
395
- logger . log (
396
- INSTRUMENTATION_NAME ,
397
- `Dropping request body chunk because maximum body length of ${ MAX_BODY_BYTE_LENGTH } b is exceeded.` ,
398
- ) ;
399
- }
400
-
401
- return Reflect . apply ( target , thisArg , args ) ;
402
- } ,
403
- } ) ;
404
-
405
- callbackMap . set ( listener , callback ) ;
406
-
407
- return Reflect . apply ( target , thisArg , [ event , callback , ...restArgs ] ) ;
408
- }
409
-
410
- if ( event === 'end' ) {
411
- const callback = new Proxy ( listener , {
412
- apply : ( target , thisArg , args ) => {
413
386
try {
414
- const body = Buffer . concat ( chunks ) . toString ( 'utf-8' ) ;
415
-
416
- if ( body ) {
417
- const normalizedRequest = { data : body } satisfies RequestEventData ;
418
- isolationScope . setSDKProcessingMetadata ( { normalizedRequest } ) ;
419
- }
420
- } catch ( error ) {
421
- if ( DEBUG_BUILD ) {
422
- logger . error ( INSTRUMENTATION_NAME , 'Error building captured request body' , error ) ;
387
+ const chunk = args [ 0 ] as Buffer | string ;
388
+ const bufferifiedChunk = Buffer . from ( chunk ) ;
389
+
390
+ if ( bodyByteLength < MAX_BODY_BYTE_LENGTH ) {
391
+ chunks . push ( bufferifiedChunk ) ;
392
+ bodyByteLength += bufferifiedChunk . byteLength ;
393
+ } else if ( DEBUG_BUILD ) {
394
+ logger . log (
395
+ INSTRUMENTATION_NAME ,
396
+ `Dropping request body chunk because maximum body length of ${ MAX_BODY_BYTE_LENGTH } b is exceeded.` ,
397
+ ) ;
423
398
}
399
+ } catch ( err ) {
400
+ DEBUG_BUILD && logger . error ( INSTRUMENTATION_NAME , 'Encountered error while storing body chunk.' ) ;
424
401
}
425
402
426
403
return Reflect . apply ( target , thisArg , args ) ;
@@ -454,6 +431,19 @@ function patchRequestToCaptureBody(req: IncomingMessage, isolationScope: Scope):
454
431
return Reflect . apply ( target , thisArg , args ) ;
455
432
} ,
456
433
} ) ;
434
+
435
+ req . on ( 'end' , ( ) => {
436
+ try {
437
+ const body = Buffer . concat ( chunks ) . toString ( 'utf-8' ) ;
438
+ if ( body ) {
439
+ isolationScope . setSDKProcessingMetadata ( { normalizedRequest : { data : body } } ) ;
440
+ }
441
+ } catch ( error ) {
442
+ if ( DEBUG_BUILD ) {
443
+ logger . error ( INSTRUMENTATION_NAME , 'Error building captured request body' , error ) ;
444
+ }
445
+ }
446
+ } ) ;
457
447
} catch ( error ) {
458
448
if ( DEBUG_BUILD ) {
459
449
logger . error ( INSTRUMENTATION_NAME , 'Error patching request to capture body' , error ) ;
0 commit comments