-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathfetch_p.js
177 lines (154 loc) · 6.65 KB
/
fetch_p.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* @module fetch_p
*/
/**
* This module patches the global fetch instance for NodeJS 18+
*/
const AWSXRay = require('aws-xray-sdk-core');
const utils = AWSXRay.utils;
const getLogger = AWSXRay.getLogger;
require('./subsegment_fetch');
/**
* Wrap fetch global instance for recent NodeJS 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.captureFetchGlobal
*/
function captureFetchGlobal(downstreamXRayEnabled, subsegmentCallback) {
if (globalThis.fetch === undefined) {
throw new Error('Global fetch is not available in NodeJS');
}
if (!globalThis.__fetch) {
globalThis.__fetch = globalThis.fetch;
globalThis.fetch = enableCapture(globalThis.__fetch, globalThis.Request,
downstreamXRayEnabled, subsegmentCallback);
}
return globalThis.fetch;
}
/**
* 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 fetch request,
* the fetch response and any error issued, allowing custom annotations and metadata to be added.
* @alias module:fetch_p.captureFetchModule
*/
function captureFetchModule(module, downstreamXRayEnabled, subsegmentCallback) {
if (!module.default) {
getLogger().warn('X-ray capture did not find fetch function in module');
return null;
}
if (!module.__fetch) {
module.__fetch = module.default;
module.default = enableCapture(module.__fetch, module.Request,
downstreamXRayEnabled, subsegmentCallback);
}
return module.default;
}
/**
* Return a fetch function that will pass segment information to the target host.
* This does not change any globals
* @param {function} baseFetchFunction fetch function to use as basis
* @param {function} requestClass Request class to use. This should correspond to the supplied fetch function.
* @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.
* @returns Response
*/
function enableCapture(baseFetchFunction, requestClass, downstreamXRayEnabled, subsegmentCallback) {
const overridenFetchAsync = async (...args) => {
const thisDownstreamXRayEnabled = !!downstreamXRayEnabled;
const thisSubsegmentCallback = 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 = AWSXRay.isAutomaticMode();
const parent = AWSXRay.resolveSegment(AWSXRay.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) {
getLogger().info('RequestInit for request ' + output +
' is missing the sub/segment context for automatic mode. Ignoring.');
} else {
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';
if (!parent.noOp) {
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 (thisSubsegmentCallback) {
thisSubsegmentCallback(subsegment, requestClone, response);
}
const statusCode = response.status;
if (statusCode === 429) {
subsegment.addThrottleFlag();
}
const cause = utils.getCauseTypeFromHttpStatus(statusCode);
if (cause) {
subsegment[cause] = true;
}
subsegment.addFetchRequestData(requestClone, response, thisDownstreamXRayEnabled);
subsegment.close();
return response;
} catch (e) {
if (thisSubsegmentCallback) {
thisSubsegmentCallback(subsegment, requestClone, response, e);
}
const madeItToDownstream = (e.code !== 'ECONNREFUSED');
subsegment.addErrorFlag();
subsegment.addFetchRequestData(requestClone, response, madeItToDownstream && thisDownstreamXRayEnabled);
subsegment.close(e);
throw (e);
}
};
if (isAutomaticMode) {
const session = AWSXRay.getNamespace();
return await session.runPromise(async () => {
AWSXRay.setSegment(subsegment);
return await capturedFetch();
});
} else {
return await capturedFetch();
}
};
return overridenFetchAsync;
}
module.exports.captureFetchGlobal = captureFetchGlobal;
module.exports.captureFetchModule = captureFetchModule;
module.exports.enableCapture = enableCapture;