-
Notifications
You must be signed in to change notification settings - Fork 36
Fix requests patching edge case #146
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a4c3336
Fix requests patching edge case
DarcyRaynerDD c340370
Re-apploy _logHttpRequest
DarcyRaynerDD daceae0
Add command to log special case
DarcyRaynerDD cf3b026
Fix linting issues
DarcyRaynerDD 0634a4b
Merge branch 'main' into darcy.rayner/fix-requests-patching
DarcyRaynerDD 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,14 +35,13 @@ export function unpatchHttp() { | |
function patchMethod(mod: typeof http | typeof https, method: "get" | "request", contextService: TraceContextService) { | ||
shimmer.wrap(mod, method, (original) => { | ||
const fn = (arg1: any, arg2: any, arg3: any) => { | ||
const { options, callback } = normalizeArgs(arg1, arg2, arg3); | ||
const requestOpts = getRequestOptionsWithTraceContext(options, contextService); | ||
[arg1, arg2, arg3] = addTraceContextToArgs(contextService, arg1, arg2, arg3); | ||
|
||
if (isIntegrationTest()) { | ||
_logHttpRequest(requestOpts); | ||
if (arg3 === undefined || arg3 === null) { | ||
return original(arg1, arg2); | ||
} else { | ||
return original(arg1, arg2, arg3); | ||
} | ||
|
||
return original(requestOpts, callback); | ||
}; | ||
return fn as any; | ||
}); | ||
|
@@ -54,23 +53,37 @@ function unpatchMethod(mod: typeof http | typeof https, method: "get" | "request | |
} | ||
|
||
/** | ||
* The input into the http.request function has 6 different overloads. This method normalized the inputs | ||
* into a consistent format. | ||
* Finds the RequestOptions in the args and injects context into headers | ||
*/ | ||
function normalizeArgs( | ||
function addTraceContextToArgs( | ||
contextService: TraceContextService, | ||
arg1: string | URL | http.RequestOptions, | ||
arg2?: RequestCallback | http.RequestOptions, | ||
arg3?: RequestCallback, | ||
) { | ||
let options: http.RequestOptions = typeof arg1 === "string" ? parse(arg1) : { ...arg1 }; | ||
options.headers = options.headers || {}; | ||
let callback = arg3; | ||
if (typeof arg2 === "function") { | ||
callback = arg2; | ||
} else if (typeof arg2 === "object") { | ||
options = { ...options, ...arg2 }; | ||
let requestOpts: http.RequestOptions | undefined; | ||
if (typeof arg1 === "string" || arg1 instanceof URL) { | ||
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. Likewise I might add comments here to describe the different cases |
||
if (arg2 === undefined || arg2 === null) { | ||
requestOpts = { | ||
method: "GET", | ||
}; | ||
requestOpts = getRequestOptionsWithTraceContext(requestOpts, contextService); | ||
return [arg1, requestOpts, arg3]; | ||
} else if (typeof arg2 === "function") { | ||
requestOpts = { | ||
method: "GET", | ||
}; | ||
requestOpts = getRequestOptionsWithTraceContext(requestOpts, contextService); | ||
return [arg1, requestOpts, arg2]; | ||
} else { | ||
requestOpts = arg2 as http.RequestOptions; | ||
requestOpts = getRequestOptionsWithTraceContext(requestOpts, contextService); | ||
return [arg1, requestOpts, arg3]; | ||
} | ||
} else { | ||
requestOpts = getRequestOptionsWithTraceContext(arg1, contextService); | ||
return [requestOpts, arg2, arg3]; | ||
} | ||
return { options, callback }; | ||
} | ||
|
||
function getRequestOptionsWithTraceContext( | ||
|
@@ -86,10 +99,16 @@ function getRequestOptionsWithTraceContext( | |
...headers, | ||
...traceHeaders, | ||
}; | ||
return { | ||
const requestOpts = { | ||
...options, | ||
headers, | ||
}; | ||
// Logging all http requests during integration tests let's | ||
// us track traffic in our test snapshots | ||
if (isIntegrationTest()) { | ||
_logHttpRequest(requestOpts); | ||
} | ||
return requestOpts; | ||
} | ||
|
||
function isIntegrationTest() { | ||
|
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.
I would add a comment here explaining why this is necessary