-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(node): Make body capturing more robust #16105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
521824b
fix(node): Make body capturing more robust
lforst 7439134
Merge remote-tracking branch 'origin/develop' into lforst-body-capturing
lforst 07d85ce
.
lforst 793812b
Bufferify
lforst de8072b
lint
lforst c60142d
test(fastify): Add test for request body parsing in fastify (#16107)
mydea fe7973a
bytelength
lforst 331bcab
msg
lforst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ import { context, propagation } from '@opentelemetry/api'; | |
import { VERSION } from '@opentelemetry/core'; | ||
import type { InstrumentationConfig } from '@opentelemetry/instrumentation'; | ||
import { InstrumentationBase, InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation'; | ||
import type { AggregationCounts, Client, RequestEventData, SanitizedRequestData, Scope } from '@sentry/core'; | ||
import type { AggregationCounts, Client, SanitizedRequestData, Scope } from '@sentry/core'; | ||
import { | ||
addBreadcrumb, | ||
generateSpanId, | ||
|
@@ -360,12 +360,9 @@ function getBreadcrumbData(request: http.ClientRequest): Partial<SanitizedReques | |
* This way, we only read the body if the user also consumes the body, ensuring we do not change any behavior in unexpected ways. | ||
*/ | ||
function patchRequestToCaptureBody(req: IncomingMessage, isolationScope: Scope): void { | ||
let bodyByteLength = 0; | ||
const chunks: Buffer[] = []; | ||
|
||
function getChunksSize(): number { | ||
return chunks.reduce((acc, chunk) => acc + chunk.byteLength, 0); | ||
} | ||
|
||
/** | ||
* We need to keep track of the original callbacks, in order to be able to remove listeners again. | ||
* 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): | |
if (event === 'data') { | ||
const callback = new Proxy(listener, { | ||
apply: (target, thisArg, args: Parameters<typeof listener>) => { | ||
// If we have already read more than the max body length, we stop adding chunks | ||
// To avoid growing the memory indefinitely if a response is e.g. streamed | ||
if (getChunksSize() < MAX_BODY_BYTE_LENGTH) { | ||
const chunk = args[0] as Buffer; | ||
chunks.push(chunk); | ||
} else if (DEBUG_BUILD) { | ||
logger.log( | ||
INSTRUMENTATION_NAME, | ||
`Dropping request body chunk because maximum body length of ${MAX_BODY_BYTE_LENGTH}b is exceeded.`, | ||
); | ||
} | ||
|
||
return Reflect.apply(target, thisArg, args); | ||
}, | ||
}); | ||
|
||
callbackMap.set(listener, callback); | ||
|
||
return Reflect.apply(target, thisArg, [event, callback, ...restArgs]); | ||
} | ||
|
||
if (event === 'end') { | ||
const callback = new Proxy(listener, { | ||
apply: (target, thisArg, args) => { | ||
try { | ||
const body = Buffer.concat(chunks).toString('utf-8'); | ||
|
||
if (body) { | ||
const normalizedRequest = { data: body } satisfies RequestEventData; | ||
isolationScope.setSDKProcessingMetadata({ normalizedRequest }); | ||
} | ||
} catch (error) { | ||
if (DEBUG_BUILD) { | ||
logger.error(INSTRUMENTATION_NAME, 'Error building captured request body', error); | ||
const chunk = args[0] as Buffer | string; | ||
const bufferifiedChunk = Buffer.from(chunk); | ||
|
||
if (bodyByteLength < MAX_BODY_BYTE_LENGTH) { | ||
chunks.push(bufferifiedChunk); | ||
bodyByteLength += bufferifiedChunk.length; | ||
} else if (DEBUG_BUILD) { | ||
logger.log( | ||
INSTRUMENTATION_NAME, | ||
`Dropping request body chunk because maximum body length of ${MAX_BODY_BYTE_LENGTH}b is exceeded.`, | ||
); | ||
} | ||
} catch { | ||
// noop | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a debug logger message here? |
||
} | ||
|
||
return Reflect.apply(target, thisArg, args); | ||
|
@@ -454,6 +431,19 @@ function patchRequestToCaptureBody(req: IncomingMessage, isolationScope: Scope): | |
return Reflect.apply(target, thisArg, args); | ||
}, | ||
}); | ||
|
||
req.on('end', () => { | ||
try { | ||
const body = Buffer.concat(chunks).toString('utf-8'); | ||
if (body) { | ||
isolationScope.setSDKProcessingMetadata({ normalizedRequest: { data: body } }); | ||
} | ||
} catch (error) { | ||
if (DEBUG_BUILD) { | ||
logger.error(INSTRUMENTATION_NAME, 'Error building captured request body', error); | ||
} | ||
} | ||
}); | ||
} catch (error) { | ||
if (DEBUG_BUILD) { | ||
logger.error(INSTRUMENTATION_NAME, 'Error patching request to capture body', error); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we use
length
orbyteLength
here? 🤔