@opentelemetry/instrumentation-fetch 0.57.2 → 0.200.0-dev.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -5
- package/build/esm/fetch.d.ts +6 -0
- package/build/esm/fetch.js +110 -154
- package/build/esm/fetch.js.map +1 -1
- package/build/esm/index.d.ts +1 -1
- package/build/esm/index.js.map +1 -1
- package/build/esm/utils.js +38 -123
- package/build/esm/utils.js.map +1 -1
- package/build/esm/version.d.ts +1 -1
- package/build/esm/version.js +1 -1
- package/build/esm/version.js.map +1 -1
- package/build/esnext/fetch.d.ts +6 -0
- package/build/esnext/fetch.js +21 -11
- package/build/esnext/fetch.js.map +1 -1
- package/build/esnext/index.d.ts +1 -1
- package/build/esnext/index.js.map +1 -1
- package/build/esnext/utils.js +12 -9
- package/build/esnext/utils.js.map +1 -1
- package/build/esnext/version.d.ts +1 -1
- package/build/esnext/version.js +1 -1
- package/build/esnext/version.js.map +1 -1
- package/build/src/fetch.d.ts +6 -0
- package/build/src/fetch.js +21 -11
- package/build/src/fetch.js.map +1 -1
- package/build/src/index.d.ts +1 -1
- package/build/src/index.js.map +1 -1
- package/build/src/utils.js +12 -9
- package/build/src/utils.js.map +1 -1
- package/build/src/version.d.ts +1 -1
- package/build/src/version.js +1 -1
- package/build/src/version.js.map +1 -1
- package/package.json +25 -20
package/README.md
CHANGED
|
@@ -66,11 +66,12 @@ See [examples/tracer-web/fetch](https://github.com/open-telemetry/opentelemetry-
|
|
|
66
66
|
|
|
67
67
|
Fetch instrumentation plugin has few options available to choose from. You can set the following:
|
|
68
68
|
|
|
69
|
-
| Options
|
|
70
|
-
|
|
71
|
-
| [`applyCustomAttributesOnSpan`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts#
|
|
72
|
-
| [`
|
|
73
|
-
| [`
|
|
69
|
+
| Options | Type | Description |
|
|
70
|
+
| ------- | ---- | ----------- |
|
|
71
|
+
| [`applyCustomAttributesOnSpan`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts#L83) | `FetchCustomAttributeFunction` | Function for adding custom attributes |
|
|
72
|
+
| [`requestHook`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts#L85) | `FetchRequestHookFunction` | Function for adding custom attributes or headers before the request is handled |
|
|
73
|
+
| [`ignoreNetworkEvents`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts#L87) | `boolean`| Disable network events being added as span events (network events are added by default) |
|
|
74
|
+
| [`measureRequestSize`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts#L89) | `boolean` | Measure outgoing request length (outgoing request length is not measured by default) |
|
|
74
75
|
|
|
75
76
|
## Semantic Conventions
|
|
76
77
|
|
package/build/esm/fetch.d.ts
CHANGED
|
@@ -5,6 +5,9 @@ import { FetchError } from './types';
|
|
|
5
5
|
export interface FetchCustomAttributeFunction {
|
|
6
6
|
(span: api.Span, request: Request | RequestInit, result: Response | FetchError): void;
|
|
7
7
|
}
|
|
8
|
+
export interface FetchRequestHookFunction {
|
|
9
|
+
(span: api.Span, request: Request | RequestInit): void;
|
|
10
|
+
}
|
|
8
11
|
/**
|
|
9
12
|
* FetchPlugin Config
|
|
10
13
|
*/
|
|
@@ -19,6 +22,8 @@ export interface FetchInstrumentationConfig extends InstrumentationConfig {
|
|
|
19
22
|
ignoreUrls?: Array<string | RegExp>;
|
|
20
23
|
/** Function for adding custom attributes on the span */
|
|
21
24
|
applyCustomAttributesOnSpan?: FetchCustomAttributeFunction;
|
|
25
|
+
/** Function for adding custom attributes or headers before the request is handled */
|
|
26
|
+
requestHook?: FetchRequestHookFunction;
|
|
22
27
|
ignoreNetworkEvents?: boolean;
|
|
23
28
|
/** Measure outgoing request size */
|
|
24
29
|
measureRequestSize?: boolean;
|
|
@@ -91,6 +96,7 @@ export declare class FetchInstrumentation extends InstrumentationBase<FetchInstr
|
|
|
91
96
|
*/
|
|
92
97
|
private _patchConstructor;
|
|
93
98
|
private _applyAttributesAfterFetch;
|
|
99
|
+
private _callRequestHook;
|
|
94
100
|
/**
|
|
95
101
|
* Prepares a span data - needed later for matching appropriate network
|
|
96
102
|
* resources
|
package/build/esm/fetch.js
CHANGED
|
@@ -13,47 +13,6 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
var __extends = (this && this.__extends) || (function () {
|
|
17
|
-
var extendStatics = function (d, b) {
|
|
18
|
-
extendStatics = Object.setPrototypeOf ||
|
|
19
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
20
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
21
|
-
return extendStatics(d, b);
|
|
22
|
-
};
|
|
23
|
-
return function (d, b) {
|
|
24
|
-
if (typeof b !== "function" && b !== null)
|
|
25
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
26
|
-
extendStatics(d, b);
|
|
27
|
-
function __() { this.constructor = d; }
|
|
28
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
29
|
-
};
|
|
30
|
-
})();
|
|
31
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
32
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
33
|
-
if (!m) return o;
|
|
34
|
-
var i = m.call(o), r, ar = [], e;
|
|
35
|
-
try {
|
|
36
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
37
|
-
}
|
|
38
|
-
catch (error) { e = { error: error }; }
|
|
39
|
-
finally {
|
|
40
|
-
try {
|
|
41
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
42
|
-
}
|
|
43
|
-
finally { if (e) throw e.error; }
|
|
44
|
-
}
|
|
45
|
-
return ar;
|
|
46
|
-
};
|
|
47
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
48
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
49
|
-
if (ar || !(i in from)) {
|
|
50
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
51
|
-
ar[i] = from[i];
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
55
|
-
};
|
|
56
|
-
var _a;
|
|
57
16
|
import * as api from '@opentelemetry/api';
|
|
58
17
|
import { isWrapped, InstrumentationBase, safeExecuteInTheMiddle, } from '@opentelemetry/instrumentation';
|
|
59
18
|
import * as core from '@opentelemetry/core';
|
|
@@ -67,43 +26,40 @@ import { _globalThis } from '@opentelemetry/core';
|
|
|
67
26
|
// this is needed as event "load" is called before observer
|
|
68
27
|
// hard to say how long it should really wait, seems like 300ms is
|
|
69
28
|
// safe enough
|
|
70
|
-
|
|
71
|
-
|
|
29
|
+
const OBSERVER_WAIT_TIME_MS = 300;
|
|
30
|
+
const isNode = typeof process === 'object' && process.release?.name === 'node';
|
|
72
31
|
/**
|
|
73
32
|
* This class represents a fetch plugin for auto instrumentation
|
|
74
33
|
*/
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
_this._usedResources = new WeakSet();
|
|
84
|
-
_this._tasksCount = 0;
|
|
85
|
-
return _this;
|
|
34
|
+
export class FetchInstrumentation extends InstrumentationBase {
|
|
35
|
+
component = 'fetch';
|
|
36
|
+
version = VERSION;
|
|
37
|
+
moduleName = this.component;
|
|
38
|
+
_usedResources = new WeakSet();
|
|
39
|
+
_tasksCount = 0;
|
|
40
|
+
constructor(config = {}) {
|
|
41
|
+
super('@opentelemetry/instrumentation-fetch', VERSION, config);
|
|
86
42
|
}
|
|
87
|
-
|
|
43
|
+
init() { }
|
|
88
44
|
/**
|
|
89
45
|
* Add cors pre flight child span
|
|
90
46
|
* @param span
|
|
91
47
|
* @param corsPreFlightRequest
|
|
92
48
|
*/
|
|
93
|
-
|
|
94
|
-
|
|
49
|
+
_addChildSpan(span, corsPreFlightRequest) {
|
|
50
|
+
const childSpan = this.tracer.startSpan('CORS Preflight', {
|
|
95
51
|
startTime: corsPreFlightRequest[web.PerformanceTimingNames.FETCH_START],
|
|
96
52
|
}, api.trace.setSpan(api.context.active(), span));
|
|
97
53
|
web.addSpanNetworkEvents(childSpan, corsPreFlightRequest, this.getConfig().ignoreNetworkEvents);
|
|
98
54
|
childSpan.end(corsPreFlightRequest[web.PerformanceTimingNames.RESPONSE_END]);
|
|
99
|
-
}
|
|
55
|
+
}
|
|
100
56
|
/**
|
|
101
57
|
* Adds more attributes to span just before ending it
|
|
102
58
|
* @param span
|
|
103
59
|
* @param response
|
|
104
60
|
*/
|
|
105
|
-
|
|
106
|
-
|
|
61
|
+
_addFinalSpanAttributes(span, response) {
|
|
62
|
+
const parsedUrl = web.parseUrl(response.url);
|
|
107
63
|
span.setAttribute(SEMATTRS_HTTP_STATUS_CODE, response.status);
|
|
108
64
|
if (response.statusText != null) {
|
|
109
65
|
span.setAttribute(AttributeNames.HTTP_STATUS_TEXT, response.statusText);
|
|
@@ -113,15 +69,15 @@ var FetchInstrumentation = /** @class */ (function (_super) {
|
|
|
113
69
|
if (typeof navigator !== 'undefined') {
|
|
114
70
|
span.setAttribute(SEMATTRS_HTTP_USER_AGENT, navigator.userAgent);
|
|
115
71
|
}
|
|
116
|
-
}
|
|
72
|
+
}
|
|
117
73
|
/**
|
|
118
74
|
* Add headers
|
|
119
75
|
* @param options
|
|
120
76
|
* @param spanUrl
|
|
121
77
|
*/
|
|
122
|
-
|
|
78
|
+
_addHeaders(options, spanUrl) {
|
|
123
79
|
if (!web.shouldPropagateTraceHeaders(spanUrl, this.getConfig().propagateTraceHeaderCorsUrls)) {
|
|
124
|
-
|
|
80
|
+
const headers = {};
|
|
125
81
|
api.propagation.inject(api.context.active(), headers);
|
|
126
82
|
if (Object.keys(headers).length > 0) {
|
|
127
83
|
this._diag.debug('headers inject skipped due to CORS policy');
|
|
@@ -130,68 +86,66 @@ var FetchInstrumentation = /** @class */ (function (_super) {
|
|
|
130
86
|
}
|
|
131
87
|
if (options instanceof Request) {
|
|
132
88
|
api.propagation.inject(api.context.active(), options.headers, {
|
|
133
|
-
set:
|
|
89
|
+
set: (h, k, v) => h.set(k, typeof v === 'string' ? v : String(v)),
|
|
134
90
|
});
|
|
135
91
|
}
|
|
136
92
|
else if (options.headers instanceof Headers) {
|
|
137
93
|
api.propagation.inject(api.context.active(), options.headers, {
|
|
138
|
-
set:
|
|
94
|
+
set: (h, k, v) => h.set(k, typeof v === 'string' ? v : String(v)),
|
|
139
95
|
});
|
|
140
96
|
}
|
|
141
97
|
else if (options.headers instanceof Map) {
|
|
142
98
|
api.propagation.inject(api.context.active(), options.headers, {
|
|
143
|
-
set:
|
|
99
|
+
set: (h, k, v) => h.set(k, typeof v === 'string' ? v : String(v)),
|
|
144
100
|
});
|
|
145
101
|
}
|
|
146
102
|
else {
|
|
147
|
-
|
|
103
|
+
const headers = {};
|
|
148
104
|
api.propagation.inject(api.context.active(), headers);
|
|
149
105
|
options.headers = Object.assign({}, headers, options.headers || {});
|
|
150
106
|
}
|
|
151
|
-
}
|
|
107
|
+
}
|
|
152
108
|
/**
|
|
153
109
|
* Clears the resource timings and all resources assigned with spans
|
|
154
110
|
* when {@link FetchPluginConfig.clearTimingResources} is
|
|
155
111
|
* set to true (default false)
|
|
156
112
|
* @private
|
|
157
113
|
*/
|
|
158
|
-
|
|
114
|
+
_clearResources() {
|
|
159
115
|
if (this._tasksCount === 0 && this.getConfig().clearTimingResources) {
|
|
160
116
|
performance.clearResourceTimings();
|
|
161
117
|
this._usedResources = new WeakSet();
|
|
162
118
|
}
|
|
163
|
-
}
|
|
119
|
+
}
|
|
164
120
|
/**
|
|
165
121
|
* Creates a new span
|
|
166
122
|
* @param url
|
|
167
123
|
* @param options
|
|
168
124
|
*/
|
|
169
|
-
|
|
170
|
-
var _a;
|
|
171
|
-
if (options === void 0) { options = {}; }
|
|
125
|
+
_createSpan(url, options = {}) {
|
|
172
126
|
if (core.isUrlIgnored(url, this.getConfig().ignoreUrls)) {
|
|
173
127
|
this._diag.debug('ignoring span as url matches ignored url');
|
|
174
128
|
return;
|
|
175
129
|
}
|
|
176
|
-
|
|
177
|
-
|
|
130
|
+
const method = (options.method || 'GET').toUpperCase();
|
|
131
|
+
const spanName = `HTTP ${method}`;
|
|
178
132
|
return this.tracer.startSpan(spanName, {
|
|
179
133
|
kind: api.SpanKind.CLIENT,
|
|
180
|
-
attributes:
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
134
|
+
attributes: {
|
|
135
|
+
[AttributeNames.COMPONENT]: this.moduleName,
|
|
136
|
+
[SEMATTRS_HTTP_METHOD]: method,
|
|
137
|
+
[SEMATTRS_HTTP_URL]: url,
|
|
138
|
+
},
|
|
185
139
|
});
|
|
186
|
-
}
|
|
140
|
+
}
|
|
187
141
|
/**
|
|
188
142
|
* Finds appropriate resource and add network events to the span
|
|
189
143
|
* @param span
|
|
190
144
|
* @param resourcesObserver
|
|
191
145
|
* @param endTime
|
|
192
146
|
*/
|
|
193
|
-
|
|
194
|
-
|
|
147
|
+
_findResourceAndAddNetworkEvents(span, resourcesObserver, endTime) {
|
|
148
|
+
let resources = resourcesObserver.entries;
|
|
195
149
|
if (!resources.length) {
|
|
196
150
|
if (!performance.getEntriesByType) {
|
|
197
151
|
return;
|
|
@@ -201,74 +155,68 @@ var FetchInstrumentation = /** @class */ (function (_super) {
|
|
|
201
155
|
// information
|
|
202
156
|
resources = performance.getEntriesByType('resource');
|
|
203
157
|
}
|
|
204
|
-
|
|
158
|
+
const resource = web.getResource(resourcesObserver.spanUrl, resourcesObserver.startTime, endTime, resources, this._usedResources, 'fetch');
|
|
205
159
|
if (resource.mainRequest) {
|
|
206
|
-
|
|
160
|
+
const mainRequest = resource.mainRequest;
|
|
207
161
|
this._markResourceAsUsed(mainRequest);
|
|
208
|
-
|
|
162
|
+
const corsPreFlightRequest = resource.corsPreFlightRequest;
|
|
209
163
|
if (corsPreFlightRequest) {
|
|
210
164
|
this._addChildSpan(span, corsPreFlightRequest);
|
|
211
165
|
this._markResourceAsUsed(corsPreFlightRequest);
|
|
212
166
|
}
|
|
213
167
|
web.addSpanNetworkEvents(span, mainRequest, this.getConfig().ignoreNetworkEvents);
|
|
214
168
|
}
|
|
215
|
-
}
|
|
169
|
+
}
|
|
216
170
|
/**
|
|
217
171
|
* Marks certain [resource]{@link PerformanceResourceTiming} when information
|
|
218
172
|
* from this is used to add events to span.
|
|
219
173
|
* This is done to avoid reusing the same resource again for next span
|
|
220
174
|
* @param resource
|
|
221
175
|
*/
|
|
222
|
-
|
|
176
|
+
_markResourceAsUsed(resource) {
|
|
223
177
|
this._usedResources.add(resource);
|
|
224
|
-
}
|
|
178
|
+
}
|
|
225
179
|
/**
|
|
226
180
|
* Finish span, add attributes, network events etc.
|
|
227
181
|
* @param span
|
|
228
182
|
* @param spanData
|
|
229
183
|
* @param response
|
|
230
184
|
*/
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
var performanceEndTime = core.hrTime();
|
|
185
|
+
_endSpan(span, spanData, response) {
|
|
186
|
+
const endTime = core.millisToHrTime(Date.now());
|
|
187
|
+
const performanceEndTime = core.hrTime();
|
|
235
188
|
this._addFinalSpanAttributes(span, response);
|
|
236
|
-
setTimeout(
|
|
237
|
-
|
|
238
|
-
(
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
_this._clearResources();
|
|
189
|
+
setTimeout(() => {
|
|
190
|
+
spanData.observer?.disconnect();
|
|
191
|
+
this._findResourceAndAddNetworkEvents(span, spanData, performanceEndTime);
|
|
192
|
+
this._tasksCount--;
|
|
193
|
+
this._clearResources();
|
|
242
194
|
span.end(endTime);
|
|
243
195
|
}, OBSERVER_WAIT_TIME_MS);
|
|
244
|
-
}
|
|
196
|
+
}
|
|
245
197
|
/**
|
|
246
198
|
* Patches the constructor of fetch
|
|
247
199
|
*/
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
257
|
-
var self = this;
|
|
258
|
-
var url = web.parseUrl(args[0] instanceof Request ? args[0].url : String(args[0])).href;
|
|
259
|
-
var options = args[0] instanceof Request ? args[0] : args[1] || {};
|
|
260
|
-
var createdSpan = plugin._createSpan(url, options);
|
|
200
|
+
_patchConstructor() {
|
|
201
|
+
return original => {
|
|
202
|
+
const plugin = this;
|
|
203
|
+
return function patchConstructor(...args) {
|
|
204
|
+
const self = this;
|
|
205
|
+
const url = web.parseUrl(args[0] instanceof Request ? args[0].url : String(args[0])).href;
|
|
206
|
+
const options = args[0] instanceof Request ? args[0] : args[1] || {};
|
|
207
|
+
const createdSpan = plugin._createSpan(url, options);
|
|
261
208
|
if (!createdSpan) {
|
|
262
209
|
return original.apply(this, args);
|
|
263
210
|
}
|
|
264
|
-
|
|
211
|
+
const spanData = plugin._prepareSpanData(url);
|
|
265
212
|
if (plugin.getConfig().measureRequestSize) {
|
|
266
|
-
getFetchBodyLength
|
|
213
|
+
getFetchBodyLength(...args)
|
|
214
|
+
.then(length => {
|
|
267
215
|
if (!length)
|
|
268
216
|
return;
|
|
269
217
|
createdSpan.setAttribute(SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED, length);
|
|
270
218
|
})
|
|
271
|
-
.catch(
|
|
219
|
+
.catch(error => {
|
|
272
220
|
plugin._diag.warn('getFetchBodyLength', error);
|
|
273
221
|
});
|
|
274
222
|
}
|
|
@@ -277,7 +225,7 @@ var FetchInstrumentation = /** @class */ (function (_super) {
|
|
|
277
225
|
plugin._endSpan(span, spanData, {
|
|
278
226
|
status: error.status || 0,
|
|
279
227
|
statusText: error.message,
|
|
280
|
-
url
|
|
228
|
+
url,
|
|
281
229
|
});
|
|
282
230
|
}
|
|
283
231
|
function endSpanOnSuccess(span, response) {
|
|
@@ -289,31 +237,29 @@ var FetchInstrumentation = /** @class */ (function (_super) {
|
|
|
289
237
|
plugin._endSpan(span, spanData, {
|
|
290
238
|
status: response.status,
|
|
291
239
|
statusText: response.statusText,
|
|
292
|
-
url
|
|
240
|
+
url,
|
|
293
241
|
});
|
|
294
242
|
}
|
|
295
243
|
}
|
|
296
244
|
function onSuccess(span, resolve, response) {
|
|
297
245
|
try {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
var body = resClone.body;
|
|
246
|
+
const resClone = response.clone();
|
|
247
|
+
const body = resClone.body;
|
|
301
248
|
if (body) {
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
var done = _a.done;
|
|
249
|
+
const reader = body.getReader();
|
|
250
|
+
const read = () => {
|
|
251
|
+
reader.read().then(({ done }) => {
|
|
306
252
|
if (done) {
|
|
307
|
-
endSpanOnSuccess(span,
|
|
253
|
+
endSpanOnSuccess(span, response);
|
|
308
254
|
}
|
|
309
255
|
else {
|
|
310
|
-
|
|
256
|
+
read();
|
|
311
257
|
}
|
|
312
|
-
},
|
|
258
|
+
}, error => {
|
|
313
259
|
endSpanOnError(span, error);
|
|
314
260
|
});
|
|
315
261
|
};
|
|
316
|
-
|
|
262
|
+
read();
|
|
317
263
|
}
|
|
318
264
|
else {
|
|
319
265
|
// some older browsers don't have .body implemented
|
|
@@ -332,9 +278,11 @@ var FetchInstrumentation = /** @class */ (function (_super) {
|
|
|
332
278
|
reject(error);
|
|
333
279
|
}
|
|
334
280
|
}
|
|
335
|
-
return new Promise(
|
|
336
|
-
return api.context.with(api.trace.setSpan(api.context.active(), createdSpan),
|
|
281
|
+
return new Promise((resolve, reject) => {
|
|
282
|
+
return api.context.with(api.trace.setSpan(api.context.active(), createdSpan), () => {
|
|
337
283
|
plugin._addHeaders(options, url);
|
|
284
|
+
// Important to execute "_callRequestHook" after "_addHeaders", allowing the consumer code to override the request headers.
|
|
285
|
+
plugin._callRequestHook(createdSpan, options);
|
|
338
286
|
plugin._tasksCount++;
|
|
339
287
|
// TypeScript complains about arrow function captured a this typed as globalThis
|
|
340
288
|
// ts(7041)
|
|
@@ -345,33 +293,43 @@ var FetchInstrumentation = /** @class */ (function (_super) {
|
|
|
345
293
|
});
|
|
346
294
|
};
|
|
347
295
|
};
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
var applyCustomAttributesOnSpan = this.getConfig().applyCustomAttributesOnSpan;
|
|
296
|
+
}
|
|
297
|
+
_applyAttributesAfterFetch(span, request, result) {
|
|
298
|
+
const applyCustomAttributesOnSpan = this.getConfig().applyCustomAttributesOnSpan;
|
|
352
299
|
if (applyCustomAttributesOnSpan) {
|
|
353
|
-
safeExecuteInTheMiddle(
|
|
300
|
+
safeExecuteInTheMiddle(() => applyCustomAttributesOnSpan(span, request, result), error => {
|
|
301
|
+
if (!error) {
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
this._diag.error('applyCustomAttributesOnSpan', error);
|
|
305
|
+
}, true);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
_callRequestHook(span, request) {
|
|
309
|
+
const requestHook = this.getConfig().requestHook;
|
|
310
|
+
if (requestHook) {
|
|
311
|
+
safeExecuteInTheMiddle(() => requestHook(span, request), error => {
|
|
354
312
|
if (!error) {
|
|
355
313
|
return;
|
|
356
314
|
}
|
|
357
|
-
|
|
315
|
+
this._diag.error('requestHook', error);
|
|
358
316
|
}, true);
|
|
359
317
|
}
|
|
360
|
-
}
|
|
318
|
+
}
|
|
361
319
|
/**
|
|
362
320
|
* Prepares a span data - needed later for matching appropriate network
|
|
363
321
|
* resources
|
|
364
322
|
* @param spanUrl
|
|
365
323
|
*/
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
324
|
+
_prepareSpanData(spanUrl) {
|
|
325
|
+
const startTime = core.hrTime();
|
|
326
|
+
const entries = [];
|
|
369
327
|
if (typeof PerformanceObserver !== 'function') {
|
|
370
|
-
return { entries
|
|
328
|
+
return { entries, startTime, spanUrl };
|
|
371
329
|
}
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
perfObsEntries.forEach(
|
|
330
|
+
const observer = new PerformanceObserver(list => {
|
|
331
|
+
const perfObsEntries = list.getEntries();
|
|
332
|
+
perfObsEntries.forEach(entry => {
|
|
375
333
|
if (entry.initiatorType === 'fetch' && entry.name === spanUrl) {
|
|
376
334
|
entries.push(entry);
|
|
377
335
|
}
|
|
@@ -380,12 +338,12 @@ var FetchInstrumentation = /** @class */ (function (_super) {
|
|
|
380
338
|
observer.observe({
|
|
381
339
|
entryTypes: ['resource'],
|
|
382
340
|
});
|
|
383
|
-
return { entries
|
|
384
|
-
}
|
|
341
|
+
return { entries, observer, startTime, spanUrl };
|
|
342
|
+
}
|
|
385
343
|
/**
|
|
386
344
|
* implements enable function
|
|
387
345
|
*/
|
|
388
|
-
|
|
346
|
+
enable() {
|
|
389
347
|
if (isNode) {
|
|
390
348
|
// Node.js v18+ *does* have a global `fetch()`, but this package does not
|
|
391
349
|
// support instrumenting it.
|
|
@@ -397,18 +355,16 @@ var FetchInstrumentation = /** @class */ (function (_super) {
|
|
|
397
355
|
this._diag.debug('removing previous patch for constructor');
|
|
398
356
|
}
|
|
399
357
|
this._wrap(_globalThis, 'fetch', this._patchConstructor());
|
|
400
|
-
}
|
|
358
|
+
}
|
|
401
359
|
/**
|
|
402
360
|
* implements unpatch function
|
|
403
361
|
*/
|
|
404
|
-
|
|
362
|
+
disable() {
|
|
405
363
|
if (isNode) {
|
|
406
364
|
return;
|
|
407
365
|
}
|
|
408
366
|
this._unwrap(_globalThis, 'fetch');
|
|
409
367
|
this._usedResources = new WeakSet();
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
}(InstrumentationBase));
|
|
413
|
-
export { FetchInstrumentation };
|
|
368
|
+
}
|
|
369
|
+
}
|
|
414
370
|
//# sourceMappingURL=fetch.js.map
|