@opentelemetry/instrumentation-fetch 0.26.0 → 0.29.0

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.
@@ -0,0 +1,340 @@
1
+ /*
2
+ * Copyright The OpenTelemetry Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import * as api from '@opentelemetry/api';
17
+ import { isWrapped, InstrumentationBase, safeExecuteInTheMiddle, } from '@opentelemetry/instrumentation';
18
+ import * as core from '@opentelemetry/core';
19
+ import * as web from '@opentelemetry/sdk-trace-web';
20
+ import { AttributeNames } from './enums/AttributeNames';
21
+ import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
22
+ import { VERSION } from './version';
23
+ import { _globalThis } from '@opentelemetry/core';
24
+ // how long to wait for observer to collect information about resources
25
+ // this is needed as event "load" is called before observer
26
+ // hard to say how long it should really wait, seems like 300ms is
27
+ // safe enough
28
+ const OBSERVER_WAIT_TIME_MS = 300;
29
+ /**
30
+ * This class represents a fetch plugin for auto instrumentation
31
+ */
32
+ export class FetchInstrumentation extends InstrumentationBase {
33
+ constructor(config) {
34
+ super('@opentelemetry/instrumentation-fetch', VERSION, config);
35
+ this.component = 'fetch';
36
+ this.version = VERSION;
37
+ this.moduleName = this.component;
38
+ this._usedResources = new WeakSet();
39
+ this._tasksCount = 0;
40
+ }
41
+ init() { }
42
+ _getConfig() {
43
+ return this._config;
44
+ }
45
+ /**
46
+ * Add cors pre flight child span
47
+ * @param span
48
+ * @param corsPreFlightRequest
49
+ */
50
+ _addChildSpan(span, corsPreFlightRequest) {
51
+ const childSpan = this.tracer.startSpan('CORS Preflight', {
52
+ startTime: corsPreFlightRequest[web.PerformanceTimingNames.FETCH_START],
53
+ }, api.trace.setSpan(api.context.active(), span));
54
+ web.addSpanNetworkEvents(childSpan, corsPreFlightRequest);
55
+ childSpan.end(corsPreFlightRequest[web.PerformanceTimingNames.RESPONSE_END]);
56
+ }
57
+ /**
58
+ * Adds more attributes to span just before ending it
59
+ * @param span
60
+ * @param response
61
+ */
62
+ _addFinalSpanAttributes(span, response) {
63
+ const parsedUrl = web.parseUrl(response.url);
64
+ span.setAttribute(SemanticAttributes.HTTP_STATUS_CODE, response.status);
65
+ if (response.statusText != null) {
66
+ span.setAttribute(AttributeNames.HTTP_STATUS_TEXT, response.statusText);
67
+ }
68
+ span.setAttribute(SemanticAttributes.HTTP_HOST, parsedUrl.host);
69
+ span.setAttribute(SemanticAttributes.HTTP_SCHEME, parsedUrl.protocol.replace(':', ''));
70
+ span.setAttribute(SemanticAttributes.HTTP_USER_AGENT, navigator.userAgent);
71
+ }
72
+ /**
73
+ * Add headers
74
+ * @param options
75
+ * @param spanUrl
76
+ */
77
+ _addHeaders(options, spanUrl) {
78
+ if (!web.shouldPropagateTraceHeaders(spanUrl, this._getConfig().propagateTraceHeaderCorsUrls)) {
79
+ const headers = {};
80
+ api.propagation.inject(api.context.active(), headers);
81
+ if (Object.keys(headers).length > 0) {
82
+ this._diag.debug('headers inject skipped due to CORS policy');
83
+ }
84
+ return;
85
+ }
86
+ if (options instanceof Request) {
87
+ api.propagation.inject(api.context.active(), options.headers, {
88
+ set: (h, k, v) => h.set(k, typeof v === 'string' ? v : String(v)),
89
+ });
90
+ }
91
+ else if (options.headers instanceof Headers) {
92
+ api.propagation.inject(api.context.active(), options.headers, {
93
+ set: (h, k, v) => h.set(k, typeof v === 'string' ? v : String(v)),
94
+ });
95
+ }
96
+ else {
97
+ const headers = {};
98
+ api.propagation.inject(api.context.active(), headers);
99
+ options.headers = Object.assign({}, headers, options.headers || {});
100
+ }
101
+ }
102
+ /**
103
+ * Clears the resource timings and all resources assigned with spans
104
+ * when {@link FetchPluginConfig.clearTimingResources} is
105
+ * set to true (default false)
106
+ * @private
107
+ */
108
+ _clearResources() {
109
+ if (this._tasksCount === 0 && this._getConfig().clearTimingResources) {
110
+ performance.clearResourceTimings();
111
+ this._usedResources = new WeakSet();
112
+ }
113
+ }
114
+ /**
115
+ * Creates a new span
116
+ * @param url
117
+ * @param options
118
+ */
119
+ _createSpan(url, options = {}) {
120
+ if (core.isUrlIgnored(url, this._getConfig().ignoreUrls)) {
121
+ this._diag.debug('ignoring span as url matches ignored url');
122
+ return;
123
+ }
124
+ const method = (options.method || 'GET').toUpperCase();
125
+ const spanName = `HTTP ${method}`;
126
+ return this.tracer.startSpan(spanName, {
127
+ kind: api.SpanKind.CLIENT,
128
+ attributes: {
129
+ [AttributeNames.COMPONENT]: this.moduleName,
130
+ [SemanticAttributes.HTTP_METHOD]: method,
131
+ [SemanticAttributes.HTTP_URL]: url,
132
+ },
133
+ });
134
+ }
135
+ /**
136
+ * Finds appropriate resource and add network events to the span
137
+ * @param span
138
+ * @param resourcesObserver
139
+ * @param endTime
140
+ */
141
+ _findResourceAndAddNetworkEvents(span, resourcesObserver, endTime) {
142
+ let resources = resourcesObserver.entries;
143
+ if (!resources.length) {
144
+ if (!performance.getEntriesByType) {
145
+ return;
146
+ }
147
+ // fallback - either Observer is not available or it took longer
148
+ // then OBSERVER_WAIT_TIME_MS and observer didn't collect enough
149
+ // information
150
+ resources = performance.getEntriesByType('resource');
151
+ }
152
+ const resource = web.getResource(resourcesObserver.spanUrl, resourcesObserver.startTime, endTime, resources, this._usedResources, 'fetch');
153
+ if (resource.mainRequest) {
154
+ const mainRequest = resource.mainRequest;
155
+ this._markResourceAsUsed(mainRequest);
156
+ const corsPreFlightRequest = resource.corsPreFlightRequest;
157
+ if (corsPreFlightRequest) {
158
+ this._addChildSpan(span, corsPreFlightRequest);
159
+ this._markResourceAsUsed(corsPreFlightRequest);
160
+ }
161
+ web.addSpanNetworkEvents(span, mainRequest);
162
+ }
163
+ }
164
+ /**
165
+ * Marks certain [resource]{@link PerformanceResourceTiming} when information
166
+ * from this is used to add events to span.
167
+ * This is done to avoid reusing the same resource again for next span
168
+ * @param resource
169
+ */
170
+ _markResourceAsUsed(resource) {
171
+ this._usedResources.add(resource);
172
+ }
173
+ /**
174
+ * Finish span, add attributes, network events etc.
175
+ * @param span
176
+ * @param spanData
177
+ * @param response
178
+ */
179
+ _endSpan(span, spanData, response) {
180
+ const endTime = core.hrTime();
181
+ this._addFinalSpanAttributes(span, response);
182
+ setTimeout(() => {
183
+ var _a;
184
+ (_a = spanData.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
185
+ this._findResourceAndAddNetworkEvents(span, spanData, endTime);
186
+ this._tasksCount--;
187
+ this._clearResources();
188
+ span.end(endTime);
189
+ }, OBSERVER_WAIT_TIME_MS);
190
+ }
191
+ /**
192
+ * Patches the constructor of fetch
193
+ */
194
+ _patchConstructor() {
195
+ return original => {
196
+ const plugin = this;
197
+ return function patchConstructor(...args) {
198
+ const self = this;
199
+ const url = web.parseUrl(args[0] instanceof Request ? args[0].url : args[0]).href;
200
+ const options = args[0] instanceof Request ? args[0] : args[1] || {};
201
+ const createdSpan = plugin._createSpan(url, options);
202
+ if (!createdSpan) {
203
+ return original.apply(this, args);
204
+ }
205
+ const spanData = plugin._prepareSpanData(url);
206
+ function endSpanOnError(span, error) {
207
+ plugin._applyAttributesAfterFetch(span, options, error);
208
+ plugin._endSpan(span, spanData, {
209
+ status: error.status || 0,
210
+ statusText: error.message,
211
+ url,
212
+ });
213
+ }
214
+ function endSpanOnSuccess(span, response) {
215
+ plugin._applyAttributesAfterFetch(span, options, response);
216
+ const spanResponse = {
217
+ status: response.status,
218
+ statusText: response.statusText,
219
+ headers: response.headers,
220
+ url
221
+ };
222
+ if (response.status >= 200 && response.status < 400) {
223
+ if (response.url != null && response.url !== '') {
224
+ spanResponse.url = url;
225
+ }
226
+ }
227
+ plugin._endSpan(span, spanData, {
228
+ status: response.status,
229
+ statusText: response.statusText,
230
+ url,
231
+ });
232
+ }
233
+ function onSuccess(span, resolve, response) {
234
+ try {
235
+ const resClone = response.clone();
236
+ const resClone4Hook = response.clone();
237
+ const body = resClone.body;
238
+ if (body) {
239
+ const reader = body.getReader();
240
+ const read = () => {
241
+ reader.read().then(({ done }) => {
242
+ if (done) {
243
+ endSpanOnSuccess(span, resClone4Hook);
244
+ }
245
+ else {
246
+ read();
247
+ }
248
+ }, error => {
249
+ endSpanOnError(span, error);
250
+ });
251
+ };
252
+ read();
253
+ }
254
+ else {
255
+ // some older browsers don't have .body implemented
256
+ endSpanOnSuccess(span, response);
257
+ }
258
+ }
259
+ finally {
260
+ resolve(response);
261
+ }
262
+ }
263
+ function onError(span, reject, error) {
264
+ try {
265
+ endSpanOnError(span, error);
266
+ }
267
+ finally {
268
+ reject(error);
269
+ }
270
+ }
271
+ return new Promise((resolve, reject) => {
272
+ return api.context.with(api.trace.setSpan(api.context.active(), createdSpan), () => {
273
+ plugin._addHeaders(options, url);
274
+ plugin._tasksCount++;
275
+ // TypeScript complains about arrow function captured a this typed as globalThis
276
+ // ts(7041)
277
+ return original
278
+ .apply(self, options instanceof Request ? [options] : [url, options])
279
+ .then(onSuccess.bind(self, createdSpan, resolve), onError.bind(self, createdSpan, reject));
280
+ });
281
+ });
282
+ };
283
+ };
284
+ }
285
+ _applyAttributesAfterFetch(span, request, result) {
286
+ const applyCustomAttributesOnSpan = this._getConfig()
287
+ .applyCustomAttributesOnSpan;
288
+ if (applyCustomAttributesOnSpan) {
289
+ safeExecuteInTheMiddle(() => applyCustomAttributesOnSpan(span, request, result), error => {
290
+ if (!error) {
291
+ return;
292
+ }
293
+ this._diag.error('applyCustomAttributesOnSpan', error);
294
+ }, true);
295
+ }
296
+ }
297
+ /**
298
+ * Prepares a span data - needed later for matching appropriate network
299
+ * resources
300
+ * @param spanUrl
301
+ */
302
+ _prepareSpanData(spanUrl) {
303
+ const startTime = core.hrTime();
304
+ const entries = [];
305
+ if (typeof PerformanceObserver !== 'function') {
306
+ return { entries, startTime, spanUrl };
307
+ }
308
+ const observer = new PerformanceObserver(list => {
309
+ const perfObsEntries = list.getEntries();
310
+ perfObsEntries.forEach(entry => {
311
+ if (entry.initiatorType === 'fetch' &&
312
+ entry.name === spanUrl) {
313
+ entries.push(entry);
314
+ }
315
+ });
316
+ });
317
+ observer.observe({
318
+ entryTypes: ['resource'],
319
+ });
320
+ return { entries, observer, startTime, spanUrl };
321
+ }
322
+ /**
323
+ * implements enable function
324
+ */
325
+ enable() {
326
+ if (isWrapped(fetch)) {
327
+ this._unwrap(_globalThis, 'fetch');
328
+ this._diag.debug('removing previous patch for constructor');
329
+ }
330
+ this._wrap(_globalThis, 'fetch', this._patchConstructor());
331
+ }
332
+ /**
333
+ * implements unpatch function
334
+ */
335
+ disable() {
336
+ this._unwrap(_globalThis, 'fetch');
337
+ this._usedResources = new WeakSet();
338
+ }
339
+ }
340
+ //# sourceMappingURL=fetch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch.js","sourceRoot":"","sources":["../../src/fetch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,GAAG,MAAM,oBAAoB,CAAC;AAC1C,OAAO,EACL,SAAS,EACT,mBAAmB,EAEnB,sBAAsB,GACvB,MAAM,gCAAgC,CAAC;AACxC,OAAO,KAAK,IAAI,MAAM,qBAAqB,CAAC;AAC5C,OAAO,KAAK,GAAG,MAAM,8BAA8B,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AAEzE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAElD,uEAAuE;AACvE,2DAA2D;AAC3D,kEAAkE;AACl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mB,KAAK,UAAU,EAAE;YAC7C,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC;SACxC;QAED,MAAM,QAAQ,GAAG,IAAI,mBAAmB,CAAC,IAAI,CAAC,EAAE;YAC9C,MAAM,cAAc,GAAG,IAAI,CAAC,UAAU,EAAiC,CAAC;YACxE,cAAc,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;gBAC7B,IACE,KAAK,CAAC,aAAa,KAAK,OAAO;oBAC/B,KAAK,CAAC,IAAI,KAAK,OAAO,EACtB;oBACA,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;iBACrB;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QACH,QAAQ,CAAC,OAAO,CAAC;YACf,UAAU,EAAE,CAAC,UAAU,CAAC;SACzB,CAAC,CAAC;QACH,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC;IACnD,CAAC;IAED;;OAEG;IACM,MAAM;QACb,IAAI,SAAS,CAAC,KAAK,CAAC,EAAE;YACpB,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;YACnC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,yCAAyC,CAAC,CAAC;SAC7D;QACD,IAAI,CAAC,KAAK,CAAC,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,iBAAiB,EAAE,CAAC,CAAC;IAC7D,CAAC;IAED;;OAEG;IACM,OAAO;QACd,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;QACnC,IAAI,CAAC,cAAc,GAAG,IAAI,OAAO,EAA6B,CAAC;IACjE,CAAC;CACF","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport * as api from '@opentelemetry/api';\nimport {\n isWrapped,\n InstrumentationBase,\n InstrumentationConfig,\n safeExecuteInTheMiddle,\n} from '@opentelemetry/instrumentation';\nimport * as core from '@opentelemetry/core';\nimport * as web from '@opentelemetry/sdk-trace-web';\nimport { AttributeNames } from './enums/AttributeNames';\nimport { SemanticAttributes } from '@opentelemetry/semantic-conventions';\nimport { FetchError, FetchResponse, SpanData } from './types';\nimport { VERSION } from './version';\nimport { _globalThis } from '@opentelemetry/core';\n\n// how long to wait for observer to collect information about resources\n// this is needed as event \"load\" is called before observer\n// hard to say how long it should really wait, seems like 300ms is\n// safe enough\nconst OBSERVER_WAIT_TIME_MS = 300;\n\nexport interface FetchCustomAttributeFunction {\n (\n span: api.Span,\n request: Request | RequestInit,\n result: Response | FetchError\n ): void;\n}\n\n/**\n * FetchPlugin Config\n */\nexport interface FetchInstrumentationConfig extends InstrumentationConfig {\n // the number of timing resources is limited, after the limit\n // (chrome 250, safari 150) the information is not collected anymore\n // the only way to prevent that is to regularly clean the resources\n // whenever it is possible, this is needed only when PerformanceObserver\n // is not available\n clearTimingResources?: boolean;\n // urls which should include trace headers when origin doesn't match\n propagateTraceHeaderCorsUrls?: web.PropagateTraceHeaderCorsUrls;\n /**\n * URLs that partially match any regex in ignoreUrls will not be traced.\n * In addition, URLs that are _exact matches_ of strings in ignoreUrls will\n * also not be traced.\n */\n ignoreUrls?: Array<string | RegExp>;\n /** Function for adding custom attributes on the span */\n applyCustomAttributesOnSpan?: FetchCustomAttributeFunction;\n}\n\n/**\n * This class represents a fetch plugin for auto instrumentation\n */\nexport class FetchInstrumentation extends InstrumentationBase<Promise<Response>> {\n readonly component: string = 'fetch';\n readonly version: string = VERSION;\n moduleName = this.component;\n private _usedResources = new WeakSet<PerformanceResourceTiming>();\n private _tasksCount = 0;\n\n constructor(config?: FetchInstrumentationConfig) {\n super(\n '@opentelemetry/instrumentation-fetch',\n VERSION,\n config\n );\n }\n\n init(): void {}\n\n private _getConfig(): FetchInstrumentationConfig {\n return this._config;\n }\n\n /**\n * Add cors pre flight child span\n * @param span\n * @param corsPreFlightRequest\n */\n private _addChildSpan(\n span: api.Span,\n corsPreFlightRequest: PerformanceResourceTiming\n ): void {\n const childSpan = this.tracer.startSpan(\n 'CORS Preflight',\n {\n startTime: corsPreFlightRequest[web.PerformanceTimingNames.FETCH_START],\n },\n api.trace.setSpan(api.context.active(), span)\n );\n web.addSpanNetworkEvents(childSpan, corsPreFlightRequest);\n childSpan.end(\n corsPreFlightRequest[web.PerformanceTimingNames.RESPONSE_END]\n );\n }\n\n /**\n * Adds more attributes to span just before ending it\n * @param span\n * @param response\n */\n private _addFinalSpanAttributes(\n span: api.Span,\n response: FetchResponse\n ): void {\n const parsedUrl = web.parseUrl(response.url);\n span.setAttribute(SemanticAttributes.HTTP_STATUS_CODE, response.status);\n if (response.statusText != null) {\n span.setAttribute(AttributeNames.HTTP_STATUS_TEXT, response.statusText);\n }\n span.setAttribute(SemanticAttributes.HTTP_HOST, parsedUrl.host);\n span.setAttribute(\n SemanticAttributes.HTTP_SCHEME,\n parsedUrl.protocol.replace(':', '')\n );\n span.setAttribute(SemanticAttributes.HTTP_USER_AGENT, navigator.userAgent);\n }\n\n /**\n * Add headers\n * @param options\n * @param spanUrl\n */\n private _addHeaders(options: Request | RequestInit, spanUrl: string): void {\n if (\n !web.shouldPropagateTraceHeaders(\n spanUrl,\n this._getConfig().propagateTraceHeaderCorsUrls\n )\n ) {\n const headers: Partial<Record<string, unknown>> = {};\n api.propagation.inject(api.context.active(), headers);\n if (Object.keys(headers).length > 0) {\n this._diag.debug('headers inject skipped due to CORS policy');\n }\n return;\n }\n\n if (options instanceof Request) {\n api.propagation.inject(api.context.active(), options.headers, {\n set: (h, k, v) => h.set(k, typeof v === 'string' ? v : String(v)),\n });\n } else if (options.headers instanceof Headers) {\n api.propagation.inject(api.context.active(), options.headers, {\n set: (h, k, v) => h.set(k, typeof v === 'string' ? v : String(v)),\n });\n } else {\n const headers: Partial<Record<string, unknown>> = {};\n api.propagation.inject(api.context.active(), headers);\n options.headers = Object.assign({}, headers, options.headers || {});\n }\n }\n\n /**\n * Clears the resource timings and all resources assigned with spans\n * when {@link FetchPluginConfig.clearTimingResources} is\n * set to true (default false)\n * @private\n */\n private _clearResources() {\n if (this._tasksCount === 0 && this._getConfig().clearTimingResources) {\n performance.clearResourceTimings();\n this._usedResources = new WeakSet<PerformanceResourceTiming>();\n }\n }\n\n /**\n * Creates a new span\n * @param url\n * @param options\n */\n private _createSpan(\n url: string,\n options: Partial<Request | RequestInit> = {}\n ): api.Span | undefined {\n if (core.isUrlIgnored(url, this._getConfig().ignoreUrls)) {\n this._diag.debug('ignoring span as url matches ignored url');\n return;\n }\n const method = (options.method || 'GET').toUpperCase();\n const spanName = `HTTP ${method}`;\n return this.tracer.startSpan(spanName, {\n kind: api.SpanKind.CLIENT,\n attributes: {\n [AttributeNames.COMPONENT]: this.moduleName,\n [SemanticAttributes.HTTP_METHOD]: method,\n [SemanticAttributes.HTTP_URL]: url,\n },\n });\n }\n\n /**\n * Finds appropriate resource and add network events to the span\n * @param span\n * @param resourcesObserver\n * @param endTime\n */\n private _findResourceAndAddNetworkEvents(\n span: api.Span,\n resourcesObserver: SpanData,\n endTime: api.HrTime\n ): void {\n let resources: PerformanceResourceTiming[] = resourcesObserver.entries;\n if (!resources.length) {\n if (!performance.getEntriesByType) {\n return;\n }\n // fallback - either Observer is not available or it took longer\n // then OBSERVER_WAIT_TIME_MS and observer didn't collect enough\n // information\n resources = performance.getEntriesByType(\n 'resource'\n ) as PerformanceResourceTiming[];\n }\n const resource = web.getResource(\n resourcesObserver.spanUrl,\n resourcesObserver.startTime,\n endTime,\n resources,\n this._usedResources,\n 'fetch'\n );\n\n if (resource.mainRequest) {\n const mainRequest = resource.mainRequest;\n this._markResourceAsUsed(mainRequest);\n\n const corsPreFlightRequest = resource.corsPreFlightRequest;\n if (corsPreFlightRequest) {\n this._addChildSpan(span, corsPreFlightRequest);\n this._markResourceAsUsed(corsPreFlightRequest);\n }\n web.addSpanNetworkEvents(span, mainRequest);\n }\n }\n\n /**\n * Marks certain [resource]{@link PerformanceResourceTiming} when information\n * from this is used to add events to span.\n * This is done to avoid reusing the same resource again for next span\n * @param resource\n */\n private _markResourceAsUsed(resource: PerformanceResourceTiming): void {\n this._usedResources.add(resource);\n }\n\n /**\n * Finish span, add attributes, network events etc.\n * @param span\n * @param spanData\n * @param response\n */\n private _endSpan(\n span: api.Span,\n spanData: SpanData,\n response: FetchResponse\n ) {\n const endTime = core.hrTime();\n this._addFinalSpanAttributes(span, response);\n\n setTimeout(() => {\n spanData.observer?.disconnect();\n this._findResourceAndAddNetworkEvents(span, spanData, endTime);\n this._tasksCount--;\n this._clearResources();\n span.end(endTime);\n }, OBSERVER_WAIT_TIME_MS);\n }\n\n /**\n * Patches the constructor of fetch\n */\n private _patchConstructor(): (original: typeof fetch) => typeof fetch {\n return original => {\n const plugin = this;\n return function patchConstructor(\n this: typeof globalThis,\n ...args: Parameters<typeof fetch>\n ): Promise<Response> {\n const self = this;\n const url = web.parseUrl(args[0] instanceof Request ? args[0].url : args[0]).href;\n\n const options = args[0] instanceof Request ? args[0] : args[1] || {};\n const createdSpan = plugin._createSpan(url, options);\n if (!createdSpan) {\n return original.apply(this, args);\n }\n const spanData = plugin._prepareSpanData(url);\n\n function endSpanOnError(span: api.Span, error: FetchError) {\n plugin._applyAttributesAfterFetch(span, options, error);\n plugin._endSpan(span, spanData, {\n status: error.status || 0,\n statusText: error.message,\n url,\n });\n }\n\n function endSpanOnSuccess(span: api.Span, response: Response) {\n plugin._applyAttributesAfterFetch(span, options, response);\n const spanResponse = {\n status: response.status,\n statusText: response.statusText,\n headers: response.headers,\n url\n };\n if (response.status >= 200 && response.status < 400) {\n if (response.url != null && response.url !== '') {\n spanResponse.url = url;\n }\n }\n plugin._endSpan(span, spanData, {\n status: response.status,\n statusText: response.statusText,\n url,\n });\n }\n\n function onSuccess(\n span: api.Span,\n resolve: (value: Response | PromiseLike<Response>) => void,\n response: Response\n ): void {\n try {\n const resClone = response.clone();\n const resClone4Hook = response.clone();\n const body = resClone.body;\n if (body) {\n const reader = body.getReader();\n const read = (): void => {\n reader.read().then(\n ({ done }) => {\n if (done) {\n endSpanOnSuccess(span, resClone4Hook);\n } else {\n read();\n }\n },\n error => {\n endSpanOnError(span, error);\n }\n );\n };\n read();\n } else {\n // some older browsers don't have .body implemented\n endSpanOnSuccess(span, response);\n }\n } finally {\n resolve(response);\n }\n }\n\n function onError(\n span: api.Span,\n reject: (reason?: unknown) => void,\n error: FetchError\n ) {\n try {\n endSpanOnError(span, error);\n } finally {\n reject(error);\n }\n }\n\n return new Promise((resolve, reject) => {\n return api.context.with(\n api.trace.setSpan(api.context.active(), createdSpan),\n () => {\n plugin._addHeaders(options, url);\n plugin._tasksCount++;\n // TypeScript complains about arrow function captured a this typed as globalThis\n // ts(7041)\n return original\n .apply(self, options instanceof Request ? [options] : [url, options])\n .then(\n onSuccess.bind(self, createdSpan, resolve),\n onError.bind(self, createdSpan, reject)\n );\n }\n );\n });\n };\n };\n }\n\n private _applyAttributesAfterFetch(\n span: api.Span,\n request: Request | RequestInit,\n result: Response | FetchError\n ) {\n const applyCustomAttributesOnSpan = this._getConfig()\n .applyCustomAttributesOnSpan;\n if (applyCustomAttributesOnSpan) {\n safeExecuteInTheMiddle(\n () => applyCustomAttributesOnSpan(span, request, result),\n error => {\n if (!error) {\n return;\n }\n\n this._diag.error('applyCustomAttributesOnSpan', error);\n },\n true\n );\n }\n }\n\n /**\n * Prepares a span data - needed later for matching appropriate network\n * resources\n * @param spanUrl\n */\n private _prepareSpanData(spanUrl: string): SpanData {\n const startTime = core.hrTime();\n const entries: PerformanceResourceTiming[] = [];\n if (typeof PerformanceObserver !== 'function') {\n return { entries, startTime, spanUrl };\n }\n\n const observer = new PerformanceObserver(list => {\n const perfObsEntries = list.getEntries() as PerformanceResourceTiming[];\n perfObsEntries.forEach(entry => {\n if (\n entry.initiatorType === 'fetch' &&\n entry.name === spanUrl\n ) {\n entries.push(entry);\n }\n });\n });\n observer.observe({\n entryTypes: ['resource'],\n });\n return { entries, observer, startTime, spanUrl };\n }\n\n /**\n * implements enable function\n */\n override enable(): void {\n if (isWrapped(fetch)) {\n this._unwrap(_globalThis, 'fetch');\n this._diag.debug('removing previous patch for constructor');\n }\n this._wrap(_globalThis, 'fetch', this._patchConstructor());\n }\n\n /**\n * implements unpatch function\n */\n override disable(): void {\n this._unwrap(_globalThis, 'fetch');\n this._usedResources = new WeakSet<PerformanceResourceTiming>();\n }\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export * from './fetch';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,17 @@
1
+ /*
2
+ * Copyright The OpenTelemetry Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ export * from './fetch';
17
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,cAAc,SAAS,CAAC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nexport * from './fetch';\n"]}
@@ -0,0 +1,27 @@
1
+ import * as api from '@opentelemetry/api';
2
+ /**
3
+ * Interface used to provide information to finish span on fetch response
4
+ */
5
+ export interface FetchResponse {
6
+ status: number;
7
+ statusText?: string;
8
+ url: string;
9
+ }
10
+ /**
11
+ * Interface used to provide information to finish span on fetch error
12
+ */
13
+ export interface FetchError {
14
+ status?: number;
15
+ message: string;
16
+ }
17
+ /**
18
+ * Interface used to keep information about span between creating and
19
+ * ending span
20
+ */
21
+ export interface SpanData {
22
+ entries: PerformanceResourceTiming[];
23
+ observer?: PerformanceObserver;
24
+ spanUrl: string;
25
+ startTime: api.HrTime;
26
+ }
27
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1,17 @@
1
+ /*
2
+ * Copyright The OpenTelemetry Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ export {};
17
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport * as api from '@opentelemetry/api';\n\n/**\n * Interface used to provide information to finish span on fetch response\n */\nexport interface FetchResponse {\n status: number;\n statusText?: string;\n url: string;\n}\n\n/**\n * Interface used to provide information to finish span on fetch error\n */\nexport interface FetchError {\n status?: number;\n message: string;\n}\n\n/**\n * Interface used to keep information about span between creating and\n * ending span\n */\nexport interface SpanData {\n entries: PerformanceResourceTiming[];\n observer?: PerformanceObserver;\n spanUrl: string;\n startTime: api.HrTime;\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export declare const VERSION = "0.29.0";
2
+ //# sourceMappingURL=version.d.ts.map
@@ -0,0 +1,18 @@
1
+ /*
2
+ * Copyright The OpenTelemetry Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ // this is autogenerated file, see scripts/version-update.js
17
+ export const VERSION = '0.29.0';
18
+ //# sourceMappingURL=version.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,4DAA4D;AAC5D,MAAM,CAAC,MAAM,OAAO,GAAG,QAAQ,CAAC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n// this is autogenerated file, see scripts/version-update.js\nexport const VERSION = '0.29.0';\n"]}
@@ -23,6 +23,7 @@ const web = require("@opentelemetry/sdk-trace-web");
23
23
  const AttributeNames_1 = require("./enums/AttributeNames");
24
24
  const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
25
25
  const version_1 = require("./version");
26
+ const core_1 = require("@opentelemetry/core");
26
27
  // how long to wait for observer to collect information about resources
27
28
  // this is needed as event "load" is called before observer
28
29
  // hard to say how long it should really wait, seems like 300ms is
@@ -32,8 +33,8 @@ const OBSERVER_WAIT_TIME_MS = 300;
32
33
  * This class represents a fetch plugin for auto instrumentation
33
34
  */
34
35
  class FetchInstrumentation extends instrumentation_1.InstrumentationBase {
35
- constructor(config = {}) {
36
- super('@opentelemetry/instrumentation-fetch', version_1.VERSION, Object.assign({}, config));
36
+ constructor(config) {
37
+ super('@opentelemetry/instrumentation-fetch', version_1.VERSION, config);
37
38
  this.component = 'fetch';
38
39
  this.version = version_1.VERSION;
39
40
  this.moduleName = this.component;
@@ -197,7 +198,8 @@ class FetchInstrumentation extends instrumentation_1.InstrumentationBase {
197
198
  return original => {
198
199
  const plugin = this;
199
200
  return function patchConstructor(...args) {
200
- const url = args[0] instanceof Request ? args[0].url : args[0];
201
+ const self = this;
202
+ const url = web.parseUrl(args[0] instanceof Request ? args[0].url : args[0]).href;
201
203
  const options = args[0] instanceof Request ? args[0] : args[1] || {};
202
204
  const createdSpan = plugin._createSpan(url, options);
203
205
  if (!createdSpan) {
@@ -214,27 +216,34 @@ class FetchInstrumentation extends instrumentation_1.InstrumentationBase {
214
216
  }
215
217
  function endSpanOnSuccess(span, response) {
216
218
  plugin._applyAttributesAfterFetch(span, options, response);
219
+ const spanResponse = {
220
+ status: response.status,
221
+ statusText: response.statusText,
222
+ headers: response.headers,
223
+ url
224
+ };
217
225
  if (response.status >= 200 && response.status < 400) {
218
- plugin._endSpan(span, spanData, response);
219
- }
220
- else {
221
- plugin._endSpan(span, spanData, {
222
- status: response.status,
223
- statusText: response.statusText,
224
- url,
225
- });
226
+ if (response.url != null && response.url !== '') {
227
+ spanResponse.url = url;
228
+ }
226
229
  }
230
+ plugin._endSpan(span, spanData, {
231
+ status: response.status,
232
+ statusText: response.statusText,
233
+ url,
234
+ });
227
235
  }
228
236
  function onSuccess(span, resolve, response) {
229
237
  try {
230
238
  const resClone = response.clone();
239
+ const resClone4Hook = response.clone();
231
240
  const body = resClone.body;
232
241
  if (body) {
233
242
  const reader = body.getReader();
234
243
  const read = () => {
235
244
  reader.read().then(({ done }) => {
236
245
  if (done) {
237
- endSpanOnSuccess(span, response);
246
+ endSpanOnSuccess(span, resClone4Hook);
238
247
  }
239
248
  else {
240
249
  read();
@@ -266,9 +275,11 @@ class FetchInstrumentation extends instrumentation_1.InstrumentationBase {
266
275
  return api.context.with(api.trace.setSpan(api.context.active(), createdSpan), () => {
267
276
  plugin._addHeaders(options, url);
268
277
  plugin._tasksCount++;
278
+ // TypeScript complains about arrow function captured a this typed as globalThis
279
+ // ts(7041)
269
280
  return original
270
- .apply(this, options instanceof Request ? [options] : [url, options])
271
- .then(onSuccess.bind(this, createdSpan, resolve), onError.bind(this, createdSpan, reject));
281
+ .apply(self, options instanceof Request ? [options] : [url, options])
282
+ .then(onSuccess.bind(self, createdSpan, resolve), onError.bind(self, createdSpan, reject));
272
283
  });
273
284
  });
274
285
  };
@@ -278,7 +289,7 @@ class FetchInstrumentation extends instrumentation_1.InstrumentationBase {
278
289
  const applyCustomAttributesOnSpan = this._getConfig()
279
290
  .applyCustomAttributesOnSpan;
280
291
  if (applyCustomAttributesOnSpan) {
281
- instrumentation_1.safeExecuteInTheMiddle(() => applyCustomAttributesOnSpan(span, request, result), error => {
292
+ (0, instrumentation_1.safeExecuteInTheMiddle)(() => applyCustomAttributesOnSpan(span, request, result), error => {
282
293
  if (!error) {
283
294
  return;
284
295
  }
@@ -294,16 +305,14 @@ class FetchInstrumentation extends instrumentation_1.InstrumentationBase {
294
305
  _prepareSpanData(spanUrl) {
295
306
  const startTime = core.hrTime();
296
307
  const entries = [];
297
- if (typeof window.PerformanceObserver === 'undefined') {
308
+ if (typeof PerformanceObserver !== 'function') {
298
309
  return { entries, startTime, spanUrl };
299
310
  }
300
311
  const observer = new PerformanceObserver(list => {
301
312
  const perfObsEntries = list.getEntries();
302
- const urlNormalizingAnchor = web.getUrlNormalizingAnchor();
303
- urlNormalizingAnchor.href = spanUrl;
304
313
  perfObsEntries.forEach(entry => {
305
314
  if (entry.initiatorType === 'fetch' &&
306
- entry.name === urlNormalizingAnchor.href) {
315
+ entry.name === spanUrl) {
307
316
  entries.push(entry);
308
317
  }
309
318
  });
@@ -317,17 +326,17 @@ class FetchInstrumentation extends instrumentation_1.InstrumentationBase {
317
326
  * implements enable function
318
327
  */
319
328
  enable() {
320
- if (instrumentation_1.isWrapped(window.fetch)) {
321
- this._unwrap(window, 'fetch');
329
+ if ((0, instrumentation_1.isWrapped)(fetch)) {
330
+ this._unwrap(core_1._globalThis, 'fetch');
322
331
  this._diag.debug('removing previous patch for constructor');
323
332
  }
324
- this._wrap(window, 'fetch', this._patchConstructor());
333
+ this._wrap(core_1._globalThis, 'fetch', this._patchConstructor());
325
334
  }
326
335
  /**
327
336
  * implements unpatch function
328
337
  */
329
338
  disable() {
330
- this._unwrap(window, 'fetch');
339
+ this._unwrap(core_1._globalThis, 'fetch');
331
340
  this._usedResources = new WeakSet();
332
341
  }
333
342
  }