-
Notifications
You must be signed in to change notification settings - Fork 156
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
Fetch #590
Merged
Merged
Fetch #590
Changes from 8 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
2f3efc8
Add support for including sql query in sql subsegment for MySQL
a8228e5
Update createSqlData to accept values and sql as arguments, and the c…
08ad919
Add function comments to mysqL_p.createSqlData
9c1758e
Merge remote-tracking branch 'upstream/master'
3f53f08
Merge remote-tracking branch 'upstream/master'
9855c14
Working Node 14 and 18 tests for fetch
ef5108a
Auto/manual fetch support
02248f3
Add typescript defs for fetch, insure node-fetch tests run in NodeJS 14
1b96361
Merge remote-tracking branch 'upstream/master' into fetch
8ecfd6f
Moved fetch patcher to sdk_contrib
2b42c8f
Added integration testing for captureFetch
925ea85
Revert tests to Javascript
9de92ee
Add chai-as-promised to base package.json dev dependencies, because y…
5d0c457
Try fixing package*.json
4097c51
Merge remote-tracking branch 'upstream/master' into fetch
8a1eb78
Change var to const
49951cb
Removed captureFetch per @jj22ee
675921f
Removed chai-as-promised and tsconfig.debug.json
92ea416
wip
941fc47
wip
002defc
remove docker files used for diag
dfbef99
Updates per jj22ee
6d3644a
Housekeeping
d2117b0
More housekeeping
b23bf44
Fix typescript type test
32651cb
Merge branch 'master' into fetch-pr
jj22ee 9fb28d2
update version and package-lock, fix tests
jj22ee d7713c3
remove duplicate code
jj22ee f5f6fdc
Record fetch info in subsegment http property
dd7f457
Add type def file for subsegment addFetchRequestData method; if url o…
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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import * as fetch from 'node-fetch'; | ||
import { Subsegment } from '../aws-xray'; | ||
|
||
// Need to have this here to get "clean" response from captureFetch since | ||
// type file for node-fetch export both namespace and fetch function as default | ||
export declare function fetchFunction( | ||
url: fetch.RequestInfo, | ||
init: fetch.RequestInit | undefined | ||
): Promise<fetch.Response>; | ||
|
||
type fetchSubsegmentCallback = (subsegment: Subsegment, req: fetch.Request, res: fetch.Response | null, error: Error) => void | ||
|
||
export function captureFetch(downstreamXRayEnabled?: boolean, subsegmentCallback?: fetchSubsegmentCallback): typeof fetchFunction; |
This file contains 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 |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/** | ||
* @module fetch_p | ||
*/ | ||
|
||
/** | ||
* This module patches the global fetch instance for NodeJS 18+ | ||
*/ | ||
|
||
var contextUtils = require('../context_utils'); | ||
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. nit: use const instead of var |
||
var Utils = require('../utils'); | ||
|
||
var logger = require('../logger'); | ||
|
||
/** | ||
* Wrap fetch either in global instance for recent NodeJS or the node-fetch module for older versions, | ||
* to automatically capture information for the segment. | ||
* This patches the built-in fetch function globally. | ||
* @param {boolean} downstreamXRayEnabled - when true, adds a "traced:true" property to the subsegment | ||
* so the AWS X-Ray service expects a corresponding segment from the downstream service. | ||
* @param {function} subsegmentCallback - a callback that is called with the subsegment, the fetch request, | ||
* the fetch response and any error issued, allowing custom annotations and metadata to be added. | ||
* @alias module:fetch_p.captureFetch | ||
*/ | ||
function captureFetch(downstreamXRayEnabled, subsegmentCallback) { | ||
if ('fetch' in globalThis) { | ||
if (!globalThis.__fetch) { | ||
globalThis.__fetch = globalThis.fetch; | ||
globalThis.fetch = exports._fetchEnableCapture(globalThis.__fetch, globalThis.Request, | ||
downstreamXRayEnabled, subsegmentCallback); | ||
} | ||
return globalThis.fetch; | ||
} | ||
return exports.captureFetchModule(require('node-fetch'), downstreamXRayEnabled, subsegmentCallback); | ||
} | ||
|
||
/** | ||
* Wrap fetch module to capture information for the segment. | ||
* This patches the fetch function distributed in node-fetch package. | ||
* @param {fetch} module - The fetch module | ||
* @param {boolean} downstreamXRayEnabled - when true, adds a "traced:true" property to the subsegment | ||
* so the AWS X-Ray service expects a corresponding segment from the downstream service. | ||
* @param {function} subsegmentCallback - a callback that is called with the subsegment, the Node.js | ||
* http.ClientRequest, the Node.js http.IncomingMessage (if a response was received) and any error issued, | ||
* allowing custom annotations and metadata to be added. | ||
* to be added to the subsegment. | ||
* @alias module:fetch_p.captureFetchModule | ||
*/ | ||
function captureFetchModule(module, downstreamXRayEnabled, subsegmentCallback) { | ||
if (!module.default) { | ||
logger.getLogger().warn('X-ray capture did not find fetch function in module'); | ||
return null; | ||
} | ||
if (!module.__fetch) { | ||
module.__fetch = module.default; | ||
module.default = exports._fetchEnableCapture(module.__fetch, module.Request, | ||
downstreamXRayEnabled, subsegmentCallback); | ||
} | ||
return module.default; | ||
} | ||
|
||
const enableCapture = function enableCapture(baseFetchFunction, requestClass, downstreamXRayEnabled, subsegmentCallback) { | ||
// fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>; | ||
|
||
var overridenFetchAsync = async (...args) => { | ||
this.downstreamXRayEnabled = !!downstreamXRayEnabled; | ||
this.subsegmentCallback = subsegmentCallback; | ||
// Standardize request information | ||
const request = typeof args[0] === 'object' ? | ||
args[0] : | ||
new requestClass(...args); | ||
|
||
// Facilitate the addition of Segment information via the request arguments | ||
const params = args.length > 1 ? args[1] : {}; | ||
|
||
// Short circuit if the HTTP is already being captured | ||
if (request.headers.has('X-Amzn-Trace-Id')) { | ||
|
||
return await baseFetchFunction(...args); | ||
} | ||
|
||
const url = new URL(request.url); | ||
const isAutomaticMode = contextUtils.isAutomaticMode(); | ||
|
||
const parent = contextUtils.resolveSegment(contextUtils.resolveManualSegmentParams(params)); | ||
const hostname = url.hostname || url.host || 'Unknown host'; | ||
|
||
if (!parent) { | ||
let output = '[ host: ' + hostname + | ||
(request.method ? (', method: ' + request.method) : '') + | ||
', path: ' + url.pathname + ' ]'; | ||
|
||
if (isAutomaticMode) { | ||
logger.getLogger().info('RequestInit for request ' + output + | ||
' is missing the sub/segment context for automatic mode. Ignoring.'); | ||
} else { | ||
logger.getLogger().info('RequestInit for request ' + output + | ||
' requires a segment object on the options params as "XRaySegment" for tracing in manual mode. Ignoring.'); | ||
} | ||
|
||
// Options are not modified, only parsed for logging. We can pass in the original arguments. | ||
return await baseFetchFunction(...args); | ||
} | ||
|
||
let subsegment; | ||
if (parent.notTraced) { | ||
subsegment = parent.addNewSubsegmentWithoutSampling(hostname); | ||
} else { | ||
subsegment = parent.addNewSubsegment(hostname); | ||
} | ||
|
||
subsegment.namespace = 'remote'; | ||
|
||
request.headers.set('X-Amzn-Trace-Id', | ||
'Root=' + (parent.segment ? parent.segment : parent).trace_id + | ||
';Parent=' + subsegment.id + | ||
';Sampled=' + (subsegment.notTraced ? '0' : '1')); | ||
|
||
// Set up fetch call and capture any thrown errors | ||
const capturedFetch = async () => { | ||
const requestClone = request.clone(); | ||
let response; | ||
try { | ||
response = await baseFetchFunction(requestClone); | ||
|
||
if (this.subsegmentCallback) { | ||
this.subsegmentCallback(subsegment, requestClone, response); | ||
} | ||
|
||
const statusCode = response.status; | ||
if (statusCode === 429) { | ||
subsegment.addThrottleFlag(); | ||
} | ||
|
||
const cause = Utils.getCauseTypeFromHttpStatus(statusCode); | ||
if (cause) { | ||
subsegment[cause] = true; | ||
} | ||
|
||
subsegment.addRemoteRequestData(requestClone, response, this.downstreamXRayEnabled); | ||
subsegment.close(); | ||
return response; | ||
} catch (e) { | ||
if (this.subsegmentCallback) { | ||
this.subsegmentCallback(subsegment, requestClone, response, e); | ||
} | ||
const madeItToDownstream = (e.code !== 'ECONNREFUSED'); | ||
subsegment.addErrorFlag(); | ||
subsegment.addRemoteRequestData(requestClone, response, madeItToDownstream && this.downstreamXRayEnabled); | ||
subsegment.close(e); | ||
throw (e); | ||
} | ||
}; | ||
|
||
if (isAutomaticMode) { | ||
const session = contextUtils.getNamespace(); | ||
return await session.runPromise(async () => { | ||
contextUtils.setSegment(subsegment); | ||
return await capturedFetch(); | ||
}); | ||
} else { | ||
return await capturedFetch(); | ||
} | ||
}; | ||
|
||
return overridenFetchAsync; | ||
}; | ||
|
||
module.exports.captureFetch = captureFetch; | ||
module.exports.captureFetchModule = captureFetchModule; | ||
module.exports._fetchEnableCapture = enableCapture; |
Oops, something went wrong.
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.
Following up to the comment on moving to sdk_contrib, the dependencies that are unique to this instrumentation would then be placed in the package.json of the new instrumentation package in sdk_contrib.
You can reference this fastify instrumentation PR as a guide to help