@opentelemetry/instrumentation-xml-http-request 0.222.0 → 0.300.0-development.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.
Files changed (94) hide show
  1. package/README.md +8 -10
  2. package/dist/_virtual/_rolldown/runtime.cjs +23 -0
  3. package/dist/index.cjs +3 -0
  4. package/dist/index.d.cts +2 -0
  5. package/dist/index.d.mts +2 -0
  6. package/dist/index.mjs +2 -0
  7. package/dist/semconv.cjs +13 -0
  8. package/dist/semconv.cjs.map +1 -0
  9. package/dist/semconv.mjs +13 -0
  10. package/dist/semconv.mjs.map +1 -0
  11. package/dist/types.d.cts +31 -0
  12. package/dist/types.d.cts.map +1 -0
  13. package/dist/types.d.mts +31 -0
  14. package/dist/types.d.mts.map +1 -0
  15. package/dist/utils.cjs +86 -0
  16. package/dist/utils.cjs.map +1 -0
  17. package/dist/utils.mjs +82 -0
  18. package/dist/utils.mjs.map +1 -0
  19. package/dist/version.cjs +6 -0
  20. package/dist/version.cjs.map +1 -0
  21. package/dist/version.mjs +6 -0
  22. package/dist/version.mjs.map +1 -0
  23. package/dist/xhr.cjs +345 -0
  24. package/dist/xhr.cjs.map +1 -0
  25. package/dist/xhr.d.cts +133 -0
  26. package/dist/xhr.d.cts.map +1 -0
  27. package/dist/xhr.d.mts +133 -0
  28. package/dist/xhr.d.mts.map +1 -0
  29. package/dist/xhr.mjs +343 -0
  30. package/dist/xhr.mjs.map +1 -0
  31. package/package.json +34 -31
  32. package/build/esm/enums/EventNames.d.ts +0 -9
  33. package/build/esm/enums/EventNames.js +0 -14
  34. package/build/esm/enums/EventNames.js.map +0 -1
  35. package/build/esm/index.d.ts +0 -3
  36. package/build/esm/index.js +0 -6
  37. package/build/esm/index.js.map +0 -1
  38. package/build/esm/semconv.d.ts +0 -9
  39. package/build/esm/semconv.js +0 -18
  40. package/build/esm/semconv.js.map +0 -1
  41. package/build/esm/types.d.ts +0 -29
  42. package/build/esm/types.js +0 -6
  43. package/build/esm/types.js.map +0 -1
  44. package/build/esm/utils.d.ts +0 -14
  45. package/build/esm/utils.js +0 -118
  46. package/build/esm/utils.js.map +0 -1
  47. package/build/esm/version.d.ts +0 -2
  48. package/build/esm/version.js +0 -7
  49. package/build/esm/version.js.map +0 -1
  50. package/build/esm/xhr.d.ts +0 -131
  51. package/build/esm/xhr.js +0 -415
  52. package/build/esm/xhr.js.map +0 -1
  53. package/build/esnext/enums/EventNames.d.ts +0 -9
  54. package/build/esnext/enums/EventNames.js +0 -14
  55. package/build/esnext/enums/EventNames.js.map +0 -1
  56. package/build/esnext/index.d.ts +0 -3
  57. package/build/esnext/index.js +0 -6
  58. package/build/esnext/index.js.map +0 -1
  59. package/build/esnext/semconv.d.ts +0 -9
  60. package/build/esnext/semconv.js +0 -18
  61. package/build/esnext/semconv.js.map +0 -1
  62. package/build/esnext/types.d.ts +0 -29
  63. package/build/esnext/types.js +0 -6
  64. package/build/esnext/types.js.map +0 -1
  65. package/build/esnext/utils.d.ts +0 -14
  66. package/build/esnext/utils.js +0 -118
  67. package/build/esnext/utils.js.map +0 -1
  68. package/build/esnext/version.d.ts +0 -2
  69. package/build/esnext/version.js +0 -7
  70. package/build/esnext/version.js.map +0 -1
  71. package/build/esnext/xhr.d.ts +0 -131
  72. package/build/esnext/xhr.js +0 -415
  73. package/build/esnext/xhr.js.map +0 -1
  74. package/build/src/enums/EventNames.d.ts +0 -9
  75. package/build/src/enums/EventNames.js +0 -17
  76. package/build/src/enums/EventNames.js.map +0 -1
  77. package/build/src/index.d.ts +0 -3
  78. package/build/src/index.js +0 -10
  79. package/build/src/index.js.map +0 -1
  80. package/build/src/semconv.d.ts +0 -9
  81. package/build/src/semconv.js +0 -21
  82. package/build/src/semconv.js.map +0 -1
  83. package/build/src/types.d.ts +0 -29
  84. package/build/src/types.js +0 -7
  85. package/build/src/types.js.map +0 -1
  86. package/build/src/utils.d.ts +0 -14
  87. package/build/src/utils.js +0 -124
  88. package/build/src/utils.js.map +0 -1
  89. package/build/src/version.d.ts +0 -2
  90. package/build/src/version.js +0 -10
  91. package/build/src/version.js.map +0 -1
  92. package/build/src/xhr.d.ts +0 -131
  93. package/build/src/xhr.js +0 -419
  94. package/build/src/xhr.js.map +0 -1
package/dist/xhr.mjs ADDED
@@ -0,0 +1,343 @@
1
+ import { ATTR_HTTP_REQUEST_BODY_SIZE } from "./semconv.mjs";
2
+ import { getXHRBodyLength, normalizeHttpRequestMethod, serverPortFromUrl } from "./utils.mjs";
3
+ import { VERSION } from "./version.mjs";
4
+ import * as api from "@opentelemetry/api";
5
+ import { InstrumentationBase, safeExecuteInTheMiddle } from "@opentelemetry/instrumentation";
6
+ import { hrTime, isUrlIgnored } from "@opentelemetry/core";
7
+ import { PerformanceTimingNames, addSpanNetworkEvents, getResource, parseUrl, shouldPropagateTraceHeaders } from "@opentelemetry/web-common";
8
+ import { ATTR_ERROR_TYPE, ATTR_HTTP_REQUEST_METHOD, ATTR_HTTP_REQUEST_METHOD_ORIGINAL, ATTR_HTTP_RESPONSE_STATUS_CODE, ATTR_SERVER_ADDRESS, ATTR_SERVER_PORT, ATTR_URL_FULL } from "@opentelemetry/semantic-conventions";
9
+ //#region src/xhr.ts
10
+ const OBSERVER_WAIT_TIME_MS = 300;
11
+ /**
12
+ * This class represents a XMLHttpRequest plugin for auto instrumentation
13
+ */
14
+ var XMLHttpRequestInstrumentation = class extends InstrumentationBase {
15
+ component = "xml-http-request";
16
+ version = VERSION;
17
+ moduleName = this.component;
18
+ _tasksCount = 0;
19
+ _xhrMem = /* @__PURE__ */ new WeakMap();
20
+ _usedResources = /* @__PURE__ */ new WeakSet();
21
+ constructor(config = {}) {
22
+ super("@opentelemetry/instrumentation-xml-http-request", VERSION, config);
23
+ }
24
+ init() {}
25
+ /**
26
+ * Adds custom headers to XMLHttpRequest
27
+ * @param xhr
28
+ * @param spanUrl
29
+ * @private
30
+ */
31
+ _addHeaders(xhr, spanUrl) {
32
+ const url = parseUrl(spanUrl).href;
33
+ if (!shouldPropagateTraceHeaders(url, this.getConfig().propagateTraceHeaderCorsUrls)) {
34
+ const headers = {};
35
+ api.propagation.inject(api.context.active(), headers);
36
+ if (Object.keys(headers).length > 0) this._diag.debug("headers inject skipped due to CORS policy");
37
+ return;
38
+ }
39
+ const headers = {};
40
+ api.propagation.inject(api.context.active(), headers);
41
+ Object.keys(headers).forEach((key) => {
42
+ xhr.setRequestHeader(key, String(headers[key]));
43
+ });
44
+ }
45
+ /**
46
+ * Add cors pre flight child span
47
+ * @param span
48
+ * @param corsPreFlightRequest
49
+ * @private
50
+ */
51
+ _addChildSpan(span, corsPreFlightRequest) {
52
+ api.context.with(api.trace.setSpan(api.context.active(), span), () => {
53
+ const childSpan = this.tracer.startSpan("CORS Preflight", { startTime: corsPreFlightRequest[PerformanceTimingNames.FETCH_START] });
54
+ addSpanNetworkEvents(childSpan, corsPreFlightRequest, this.getConfig().ignoreNetworkEvents, void 0, true);
55
+ childSpan.end(corsPreFlightRequest[PerformanceTimingNames.RESPONSE_END]);
56
+ });
57
+ }
58
+ /**
59
+ * Add attributes when span is going to end
60
+ * @param span
61
+ * @param xhr
62
+ * @private
63
+ */
64
+ _addFinalSpanAttributes(span, xhrMem) {
65
+ if (xhrMem.status) span.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, xhrMem.status);
66
+ }
67
+ _applyAttributesAfterXHR(span, xhr) {
68
+ const applyCustomAttributesOnSpan = this.getConfig().applyCustomAttributesOnSpan;
69
+ if (typeof applyCustomAttributesOnSpan === "function") safeExecuteInTheMiddle(() => applyCustomAttributesOnSpan(span, xhr), (error) => {
70
+ if (!error) return;
71
+ this._diag.error("applyCustomAttributesOnSpan", error);
72
+ }, true);
73
+ }
74
+ /**
75
+ * will collect information about all resources created
76
+ * between "send" and "end" with additional waiting for main resource
77
+ * @param xhr
78
+ * @param spanUrl
79
+ * @private
80
+ */
81
+ _addResourceObserver(xhr, spanUrl) {
82
+ const xhrMem = this._xhrMem.get(xhr);
83
+ if (!xhrMem || typeof PerformanceObserver !== "function" || typeof PerformanceResourceTiming !== "function") return;
84
+ xhrMem.createdResources = {
85
+ observer: new PerformanceObserver((list) => {
86
+ const entries = list.getEntries();
87
+ const parsedUrl = parseUrl(spanUrl);
88
+ entries.forEach((entry) => {
89
+ if (entry.initiatorType === "xmlhttprequest" && entry.name === parsedUrl.href) {
90
+ if (xhrMem.createdResources) xhrMem.createdResources.entries.push(entry);
91
+ }
92
+ });
93
+ }),
94
+ entries: []
95
+ };
96
+ xhrMem.createdResources.observer.observe({ entryTypes: ["resource"] });
97
+ }
98
+ /**
99
+ * Clears the resource timings and all resources assigned with spans
100
+ * when {@link XMLHttpRequestInstrumentationConfig.clearTimingResources} is
101
+ * set to true (default false)
102
+ * @private
103
+ */
104
+ _clearResources() {
105
+ if (this._tasksCount === 0 && this.getConfig().clearTimingResources) {
106
+ performance.clearResourceTimings();
107
+ this._xhrMem = /* @__PURE__ */ new WeakMap();
108
+ this._usedResources = /* @__PURE__ */ new WeakSet();
109
+ }
110
+ }
111
+ /**
112
+ * Finds appropriate resource and add network events to the span
113
+ * @param span
114
+ */
115
+ _findResourceAndAddNetworkEvents(xhrMem, span, spanUrl, startTime, endTime) {
116
+ if (!spanUrl || !startTime || !endTime || !xhrMem.createdResources) return;
117
+ let resources = xhrMem.createdResources.entries;
118
+ if (!resources || !resources.length) resources = performance.getEntriesByType("resource");
119
+ const resource = getResource(parseUrl(spanUrl).href, startTime, endTime, resources, this._usedResources);
120
+ if (resource.mainRequest) {
121
+ const mainRequest = resource.mainRequest;
122
+ this._markResourceAsUsed(mainRequest);
123
+ const corsPreFlightRequest = resource.corsPreFlightRequest;
124
+ if (corsPreFlightRequest) {
125
+ this._addChildSpan(span, corsPreFlightRequest);
126
+ this._markResourceAsUsed(corsPreFlightRequest);
127
+ }
128
+ addSpanNetworkEvents(span, mainRequest, this.getConfig().ignoreNetworkEvents, void 0, true);
129
+ }
130
+ }
131
+ /**
132
+ * Removes the previous information about span.
133
+ * This might happened when the same xhr is used again.
134
+ * @param xhr
135
+ * @private
136
+ */
137
+ _cleanPreviousSpanInformation(xhr) {
138
+ const xhrMem = this._xhrMem.get(xhr);
139
+ if (xhrMem) {
140
+ const callbackToRemoveEvents = xhrMem.callbackToRemoveEvents;
141
+ if (callbackToRemoveEvents) callbackToRemoveEvents();
142
+ this._xhrMem.delete(xhr);
143
+ }
144
+ }
145
+ /**
146
+ * Creates a new span when method "open" is called
147
+ * @param xhr
148
+ * @param url
149
+ * @param method
150
+ * @private
151
+ */
152
+ _createSpan(xhr, url, method) {
153
+ const parsedUrl = parseUrl(url);
154
+ if (isUrlIgnored(parsedUrl.href, this.getConfig().ignoreUrls)) {
155
+ this._diag.debug("ignoring span as url matches ignored url");
156
+ return;
157
+ }
158
+ const attributes = {};
159
+ const origMethod = method;
160
+ const normMethod = normalizeHttpRequestMethod(method);
161
+ const name = normMethod;
162
+ attributes[ATTR_HTTP_REQUEST_METHOD] = normMethod;
163
+ if (normMethod !== origMethod) attributes[ATTR_HTTP_REQUEST_METHOD_ORIGINAL] = origMethod;
164
+ attributes[ATTR_URL_FULL] = parsedUrl.toString();
165
+ attributes[ATTR_SERVER_ADDRESS] = parsedUrl.hostname;
166
+ const serverPort = serverPortFromUrl(parsedUrl);
167
+ if (serverPort) attributes[ATTR_SERVER_PORT] = serverPort;
168
+ const currentSpan = this.tracer.startSpan(name, {
169
+ kind: api.SpanKind.CLIENT,
170
+ attributes
171
+ });
172
+ currentSpan.addEvent("open");
173
+ this._cleanPreviousSpanInformation(xhr);
174
+ this._xhrMem.set(xhr, {
175
+ span: currentSpan,
176
+ spanUrl: url
177
+ });
178
+ return currentSpan;
179
+ }
180
+ /**
181
+ * Marks certain [resource]{@link PerformanceResourceTiming} when information
182
+ * from this is used to add events to span.
183
+ * This is done to avoid reusing the same resource again for next span
184
+ * @param resource
185
+ * @private
186
+ */
187
+ _markResourceAsUsed(resource) {
188
+ this._usedResources.add(resource);
189
+ }
190
+ /**
191
+ * Patches the method open
192
+ * @private
193
+ */
194
+ _patchOpen() {
195
+ return (original) => {
196
+ const plugin = this;
197
+ return function patchOpen(...args) {
198
+ if (!plugin._isEnabled) return original.apply(this, args);
199
+ const method = args[0];
200
+ const url = args[1];
201
+ plugin._createSpan(this, url, method);
202
+ return original.apply(this, args);
203
+ };
204
+ };
205
+ }
206
+ /**
207
+ * Patches the method send
208
+ * @private
209
+ */
210
+ _patchSend() {
211
+ const plugin = this;
212
+ function endSpanTimeout(eventName, xhrMem, performanceEndTime, endTime) {
213
+ const callbackToRemoveEvents = xhrMem.callbackToRemoveEvents;
214
+ if (typeof callbackToRemoveEvents === "function") callbackToRemoveEvents();
215
+ const { span, spanUrl, sendStartTime } = xhrMem;
216
+ if (span) {
217
+ plugin._findResourceAndAddNetworkEvents(xhrMem, span, spanUrl, sendStartTime, performanceEndTime);
218
+ span.addEvent(eventName, endTime);
219
+ plugin._addFinalSpanAttributes(span, xhrMem);
220
+ span.end(endTime);
221
+ plugin._tasksCount--;
222
+ }
223
+ plugin._clearResources();
224
+ }
225
+ function endSpan(eventName, xhr, isError, errorType) {
226
+ const xhrMem = plugin._xhrMem.get(xhr);
227
+ if (!xhrMem) return;
228
+ xhrMem.status = xhr.status;
229
+ xhrMem.statusText = xhr.statusText;
230
+ plugin._xhrMem.delete(xhr);
231
+ if (xhrMem.span) {
232
+ const span = xhrMem.span;
233
+ plugin._applyAttributesAfterXHR(span, xhr);
234
+ if (isError) {
235
+ if (errorType) {
236
+ span.setStatus({
237
+ code: api.SpanStatusCode.ERROR,
238
+ message: errorType
239
+ });
240
+ span.setAttribute(ATTR_ERROR_TYPE, errorType);
241
+ }
242
+ } else if (xhrMem.status && xhrMem.status >= 400) {
243
+ span.setStatus({ code: api.SpanStatusCode.ERROR });
244
+ span.setAttribute(ATTR_ERROR_TYPE, String(xhrMem.status));
245
+ }
246
+ }
247
+ const performanceEndTime = hrTime();
248
+ const endTime = Date.now();
249
+ setTimeout(() => {
250
+ endSpanTimeout(eventName, xhrMem, performanceEndTime, endTime);
251
+ }, OBSERVER_WAIT_TIME_MS);
252
+ }
253
+ function onError() {
254
+ endSpan("error", this, true, "error");
255
+ }
256
+ function onAbort() {
257
+ endSpan("abort", this, false);
258
+ }
259
+ function onTimeout() {
260
+ endSpan("timeout", this, true, "timeout");
261
+ }
262
+ function onLoad() {
263
+ if (this.status < 299) endSpan("loaded", this, false);
264
+ else endSpan("error", this, false);
265
+ }
266
+ function unregister(xhr) {
267
+ xhr.removeEventListener("abort", onAbort);
268
+ xhr.removeEventListener("error", onError);
269
+ xhr.removeEventListener("load", onLoad);
270
+ xhr.removeEventListener("timeout", onTimeout);
271
+ const xhrMem = plugin._xhrMem.get(xhr);
272
+ if (xhrMem) xhrMem.callbackToRemoveEvents = void 0;
273
+ }
274
+ return (original) => {
275
+ return function patchSend(...args) {
276
+ if (!plugin._isEnabled) return original.apply(this, args);
277
+ const xhrMem = plugin._xhrMem.get(this);
278
+ if (!xhrMem) return original.apply(this, args);
279
+ const currentSpan = xhrMem.span;
280
+ const spanUrl = xhrMem.spanUrl;
281
+ if (currentSpan && spanUrl) {
282
+ if (plugin.getConfig().measureRequestSize && args?.[0]) {
283
+ const body = args[0];
284
+ const bodyLength = getXHRBodyLength(body);
285
+ if (bodyLength !== void 0) currentSpan.setAttribute(ATTR_HTTP_REQUEST_BODY_SIZE, bodyLength);
286
+ }
287
+ api.context.with(api.trace.setSpan(api.context.active(), currentSpan), () => {
288
+ plugin._tasksCount++;
289
+ xhrMem.sendStartTime = hrTime();
290
+ currentSpan.addEvent("send");
291
+ this.addEventListener("abort", onAbort);
292
+ this.addEventListener("error", onError);
293
+ this.addEventListener("load", onLoad);
294
+ this.addEventListener("timeout", onTimeout);
295
+ xhrMem.callbackToRemoveEvents = () => {
296
+ unregister(this);
297
+ if (xhrMem.createdResources) xhrMem.createdResources.observer.disconnect();
298
+ };
299
+ plugin._addHeaders(this, spanUrl);
300
+ plugin._addResourceObserver(this, spanUrl);
301
+ });
302
+ }
303
+ return original.apply(this, args);
304
+ };
305
+ };
306
+ }
307
+ /**
308
+ * implements enable function
309
+ */
310
+ enable() {
311
+ if (this._isEnabled) return;
312
+ if (this._isXhrPatched) {
313
+ this._diag.debug("reactivating existing patch on", this.moduleName, this.version);
314
+ this._isEnabled = true;
315
+ return;
316
+ }
317
+ try {
318
+ this._diag.debug("applying patch to", this.moduleName, this.version);
319
+ this._wrap(XMLHttpRequest.prototype, "open", this._patchOpen());
320
+ this._wrap(XMLHttpRequest.prototype, "send", this._patchSend());
321
+ this._isXhrPatched = true;
322
+ this._isEnabled = true;
323
+ } catch (err) {
324
+ this._unwrap(XMLHttpRequest.prototype, "open");
325
+ this._unwrap(XMLHttpRequest.prototype, "send");
326
+ this._diag.warn("Failed to patch globalThis.XMLHttpRequest; instrumentation will not be enabled. Another script may have locked globalThis.XMLHttpRequest via Object.defineProperty.", err);
327
+ }
328
+ }
329
+ /**
330
+ * implements disable function
331
+ */
332
+ disable() {
333
+ if (!this._isEnabled) return;
334
+ this._isEnabled = false;
335
+ this._tasksCount = 0;
336
+ this._xhrMem = /* @__PURE__ */ new WeakMap();
337
+ this._usedResources = /* @__PURE__ */ new WeakSet();
338
+ }
339
+ };
340
+ //#endregion
341
+ export { XMLHttpRequestInstrumentation };
342
+
343
+ //# sourceMappingURL=xhr.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xhr.mjs","names":["PTN"],"sources":["../src/xhr.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport * as api from '@opentelemetry/api';\nimport type { InstrumentationConfig } from '@opentelemetry/instrumentation';\nimport {\n InstrumentationBase,\n safeExecuteInTheMiddle,\n} from '@opentelemetry/instrumentation';\nimport { hrTime, isUrlIgnored } from '@opentelemetry/core';\nimport {\n addSpanNetworkEvents,\n getResource,\n PerformanceTimingNames as PTN,\n shouldPropagateTraceHeaders,\n parseUrl,\n} from '@opentelemetry/web-common';\nimport {\n ATTR_ERROR_TYPE,\n ATTR_HTTP_REQUEST_METHOD,\n ATTR_HTTP_REQUEST_METHOD_ORIGINAL,\n ATTR_HTTP_RESPONSE_STATUS_CODE,\n ATTR_SERVER_ADDRESS,\n ATTR_SERVER_PORT,\n ATTR_URL_FULL,\n} from '@opentelemetry/semantic-conventions';\nimport { ATTR_HTTP_REQUEST_BODY_SIZE } from './semconv';\nimport { EventNames } from './enums/EventNames';\nimport type {\n OpenFunction,\n PropagateTraceHeaderCorsUrls,\n SendFunction,\n XhrMem,\n} from './types';\nimport {\n normalizeHttpRequestMethod,\n serverPortFromUrl,\n getXHRBodyLength,\n} from './utils';\nimport { VERSION } from './version';\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 type XHRCustomAttributeFunction = (\n span: api.Span,\n xhr: XMLHttpRequest\n) => void;\n\n/**\n * XMLHttpRequest config\n */\nexport interface XMLHttpRequestInstrumentationConfig\n extends InstrumentationConfig {\n /**\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 */\n clearTimingResources?: boolean;\n /** URLs which should include trace headers when origin doesn't match */\n propagateTraceHeaderCorsUrls?: 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?: XHRCustomAttributeFunction;\n /** Ignore adding network events as span events */\n ignoreNetworkEvents?: boolean;\n /** Measure outgoing request size */\n measureRequestSize?: boolean;\n}\n\n/**\n * This class represents a XMLHttpRequest plugin for auto instrumentation\n */\nexport class XMLHttpRequestInstrumentation extends InstrumentationBase<XMLHttpRequestInstrumentationConfig> {\n readonly component: string = 'xml-http-request';\n readonly version: string = VERSION;\n moduleName = this.component;\n\n private _tasksCount = 0;\n private _xhrMem = new WeakMap<XMLHttpRequest, XhrMem>();\n private _usedResources = new WeakSet<PerformanceResourceTiming>();\n\n // Note: Intentionally *not* using `_enabled` as the field name to avoid\n // any possible confusion with the `_enabled` field used on the *Node.js*\n // InstrumentationBase class.\n // Also not initializing the fields to `false` because the base class\n // constructor already calls `enable` modifying their values and it will\n // set the instrumentations in a bad state (enabled, patched but with flags set to false)\n declare private _isEnabled: boolean;\n declare private _isXhrPatched: boolean;\n\n constructor(config: XMLHttpRequestInstrumentationConfig = {}) {\n super('@opentelemetry/instrumentation-xml-http-request', VERSION, config);\n }\n\n init() {}\n\n /**\n * Adds custom headers to XMLHttpRequest\n * @param xhr\n * @param spanUrl\n * @private\n */\n private _addHeaders(xhr: XMLHttpRequest, spanUrl: string) {\n const url = parseUrl(spanUrl).href;\n if (\n !shouldPropagateTraceHeaders(\n url,\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 const headers: { [key: string]: unknown } = {};\n api.propagation.inject(api.context.active(), headers);\n Object.keys(headers).forEach(key => {\n xhr.setRequestHeader(key, String(headers[key]));\n });\n }\n\n /**\n * Add cors pre flight child span\n * @param span\n * @param corsPreFlightRequest\n * @private\n */\n private _addChildSpan(\n span: api.Span,\n corsPreFlightRequest: PerformanceResourceTiming\n ): void {\n api.context.with(api.trace.setSpan(api.context.active(), span), () => {\n const childSpan = this.tracer.startSpan('CORS Preflight', {\n startTime: corsPreFlightRequest[PTN.FETCH_START],\n });\n addSpanNetworkEvents(\n childSpan,\n corsPreFlightRequest,\n this.getConfig().ignoreNetworkEvents,\n undefined,\n true\n );\n childSpan.end(corsPreFlightRequest[PTN.RESPONSE_END]);\n });\n }\n\n /**\n * Add attributes when span is going to end\n * @param span\n * @param xhr\n * @private\n */\n _addFinalSpanAttributes(span: api.Span, xhrMem: XhrMem) {\n if (xhrMem.status) {\n // Intentionally exclude status=0, because XHR uses 0 for before a\n // response is received and semconv says to only add the attribute if\n // received a response.\n span.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, xhrMem.status);\n }\n }\n\n private _applyAttributesAfterXHR(span: api.Span, xhr: XMLHttpRequest) {\n const applyCustomAttributesOnSpan =\n this.getConfig().applyCustomAttributesOnSpan;\n if (typeof applyCustomAttributesOnSpan === 'function') {\n safeExecuteInTheMiddle(\n () => applyCustomAttributesOnSpan(span, xhr),\n error => {\n if (!error) {\n return;\n }\n\n this._diag.error('applyCustomAttributesOnSpan', error);\n },\n true\n );\n }\n }\n\n /**\n * will collect information about all resources created\n * between \"send\" and \"end\" with additional waiting for main resource\n * @param xhr\n * @param spanUrl\n * @private\n */\n private _addResourceObserver(xhr: XMLHttpRequest, spanUrl: string) {\n const xhrMem = this._xhrMem.get(xhr);\n if (\n !xhrMem ||\n typeof PerformanceObserver !== 'function' ||\n typeof PerformanceResourceTiming !== 'function'\n ) {\n return;\n }\n xhrMem.createdResources = {\n observer: new PerformanceObserver(list => {\n const entries = list.getEntries() as PerformanceResourceTiming[];\n const parsedUrl = parseUrl(spanUrl);\n\n entries.forEach(entry => {\n if (\n entry.initiatorType === 'xmlhttprequest' &&\n entry.name === parsedUrl.href\n ) {\n if (xhrMem.createdResources) {\n xhrMem.createdResources.entries.push(entry);\n }\n }\n });\n }),\n entries: [],\n };\n xhrMem.createdResources.observer.observe({\n entryTypes: ['resource'],\n });\n }\n\n /**\n * Clears the resource timings and all resources assigned with spans\n * when {@link XMLHttpRequestInstrumentationConfig.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._xhrMem = new WeakMap<XMLHttpRequest, XhrMem>();\n this._usedResources = new WeakSet<PerformanceResourceTiming>();\n }\n }\n\n /**\n * Finds appropriate resource and add network events to the span\n * @param span\n */\n private _findResourceAndAddNetworkEvents(\n xhrMem: XhrMem,\n span: api.Span,\n spanUrl?: string,\n startTime?: api.HrTime,\n endTime?: api.HrTime\n ): void {\n if (!spanUrl || !startTime || !endTime || !xhrMem.createdResources) {\n return;\n }\n let resources: PerformanceResourceTiming[] =\n xhrMem.createdResources.entries;\n\n if (!resources || !resources.length) {\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 // ts thinks this is the perf_hooks module, but it is the browser performance api\n resources = performance.getEntriesByType(\n 'resource'\n ) as PerformanceResourceTiming[];\n }\n\n const resource = getResource(\n parseUrl(spanUrl).href,\n startTime,\n endTime,\n resources,\n this._usedResources\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 addSpanNetworkEvents(\n span,\n mainRequest,\n this.getConfig().ignoreNetworkEvents,\n undefined,\n true\n );\n }\n }\n\n /**\n * Removes the previous information about span.\n * This might happened when the same xhr is used again.\n * @param xhr\n * @private\n */\n private _cleanPreviousSpanInformation(xhr: XMLHttpRequest) {\n const xhrMem = this._xhrMem.get(xhr);\n if (xhrMem) {\n const callbackToRemoveEvents = xhrMem.callbackToRemoveEvents;\n if (callbackToRemoveEvents) {\n callbackToRemoveEvents();\n }\n this._xhrMem.delete(xhr);\n }\n }\n\n /**\n * Creates a new span when method \"open\" is called\n * @param xhr\n * @param url\n * @param method\n * @private\n */\n private _createSpan(\n xhr: XMLHttpRequest,\n url: string,\n method: string\n ): api.Span | undefined {\n const parsedUrl = parseUrl(url);\n if (isUrlIgnored(parsedUrl.href, this.getConfig().ignoreUrls)) {\n this._diag.debug('ignoring span as url matches ignored url');\n return;\n }\n const attributes = {} as api.Attributes;\n const origMethod = method;\n const normMethod = normalizeHttpRequestMethod(method);\n const name = normMethod;\n\n attributes[ATTR_HTTP_REQUEST_METHOD] = normMethod;\n if (normMethod !== origMethod) {\n attributes[ATTR_HTTP_REQUEST_METHOD_ORIGINAL] = origMethod;\n }\n attributes[ATTR_URL_FULL] = parsedUrl.toString();\n attributes[ATTR_SERVER_ADDRESS] = parsedUrl.hostname;\n const serverPort = serverPortFromUrl(parsedUrl);\n if (serverPort) {\n attributes[ATTR_SERVER_PORT] = serverPort;\n }\n\n const currentSpan = this.tracer.startSpan(name, {\n kind: api.SpanKind.CLIENT,\n attributes,\n });\n\n currentSpan.addEvent(EventNames.METHOD_OPEN);\n\n this._cleanPreviousSpanInformation(xhr);\n\n this._xhrMem.set(xhr, {\n span: currentSpan,\n spanUrl: url,\n });\n\n return currentSpan;\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 * @private\n */\n private _markResourceAsUsed(resource: PerformanceResourceTiming) {\n this._usedResources.add(resource);\n }\n\n /**\n * Patches the method open\n * @private\n */\n protected _patchOpen() {\n return (original: OpenFunction): OpenFunction => {\n const plugin = this;\n return function patchOpen(this: XMLHttpRequest, ...args): void {\n if (!plugin._isEnabled) {\n return original.apply(this, args);\n }\n const method: string = args[0];\n const url: string = args[1];\n plugin._createSpan(this, url, method);\n\n return original.apply(this, args);\n };\n };\n }\n\n /**\n * Patches the method send\n * @private\n */\n protected _patchSend() {\n const plugin = this;\n\n function endSpanTimeout(\n eventName: string,\n xhrMem: XhrMem,\n performanceEndTime: api.HrTime,\n endTime: number\n ) {\n const callbackToRemoveEvents = xhrMem.callbackToRemoveEvents;\n\n if (typeof callbackToRemoveEvents === 'function') {\n callbackToRemoveEvents();\n }\n\n const { span, spanUrl, sendStartTime } = xhrMem;\n\n if (span) {\n plugin._findResourceAndAddNetworkEvents(\n xhrMem,\n span,\n spanUrl,\n sendStartTime,\n performanceEndTime\n );\n span.addEvent(eventName, endTime);\n plugin._addFinalSpanAttributes(span, xhrMem);\n span.end(endTime);\n plugin._tasksCount--;\n }\n plugin._clearResources();\n }\n\n function endSpan(\n eventName: string,\n xhr: XMLHttpRequest,\n isError: boolean,\n errorType?: string\n ) {\n const xhrMem = plugin._xhrMem.get(xhr);\n if (!xhrMem) {\n return;\n }\n xhrMem.status = xhr.status;\n xhrMem.statusText = xhr.statusText;\n plugin._xhrMem.delete(xhr);\n\n if (xhrMem.span) {\n const span = xhrMem.span;\n plugin._applyAttributesAfterXHR(span, xhr);\n\n if (isError) {\n if (errorType) {\n span.setStatus({\n code: api.SpanStatusCode.ERROR,\n message: errorType,\n });\n span.setAttribute(ATTR_ERROR_TYPE, errorType);\n }\n } else if (xhrMem.status && xhrMem.status >= 400) {\n span.setStatus({ code: api.SpanStatusCode.ERROR });\n span.setAttribute(ATTR_ERROR_TYPE, String(xhrMem.status));\n }\n }\n\n const performanceEndTime = hrTime();\n const endTime = Date.now();\n\n // the timeout is needed as observer doesn't have yet information\n // when event \"load\" is called. Also the time may differ depends on\n // browser and speed of computer\n setTimeout(() => {\n endSpanTimeout(eventName, xhrMem, performanceEndTime, endTime);\n }, OBSERVER_WAIT_TIME_MS);\n }\n\n function onError(this: XMLHttpRequest) {\n endSpan(EventNames.EVENT_ERROR, this, true, 'error');\n }\n\n function onAbort(this: XMLHttpRequest) {\n endSpan(EventNames.EVENT_ABORT, this, false);\n }\n\n function onTimeout(this: XMLHttpRequest) {\n endSpan(EventNames.EVENT_TIMEOUT, this, true, 'timeout');\n }\n\n function onLoad(this: XMLHttpRequest) {\n if (this.status < 299) {\n endSpan(EventNames.EVENT_LOAD, this, false);\n } else {\n endSpan(EventNames.EVENT_ERROR, this, false);\n }\n }\n\n function unregister(xhr: XMLHttpRequest) {\n xhr.removeEventListener('abort', onAbort);\n xhr.removeEventListener('error', onError);\n xhr.removeEventListener('load', onLoad);\n xhr.removeEventListener('timeout', onTimeout);\n const xhrMem = plugin._xhrMem.get(xhr);\n if (xhrMem) {\n xhrMem.callbackToRemoveEvents = undefined;\n }\n }\n\n return (original: SendFunction): SendFunction => {\n return function patchSend(this: XMLHttpRequest, ...args): void {\n if (!plugin._isEnabled) {\n return original.apply(this, args);\n }\n const xhrMem = plugin._xhrMem.get(this);\n if (!xhrMem) {\n return original.apply(this, args);\n }\n const currentSpan = xhrMem.span;\n const spanUrl = xhrMem.spanUrl;\n\n if (currentSpan && spanUrl) {\n if (plugin.getConfig().measureRequestSize && args?.[0]) {\n const body = args[0];\n const bodyLength = getXHRBodyLength(body);\n if (bodyLength !== undefined) {\n currentSpan.setAttribute(ATTR_HTTP_REQUEST_BODY_SIZE, bodyLength);\n }\n }\n\n api.context.with(\n api.trace.setSpan(api.context.active(), currentSpan),\n () => {\n plugin._tasksCount++;\n xhrMem.sendStartTime = hrTime();\n currentSpan.addEvent(EventNames.METHOD_SEND);\n\n this.addEventListener('abort', onAbort);\n this.addEventListener('error', onError);\n this.addEventListener('load', onLoad);\n this.addEventListener('timeout', onTimeout);\n\n xhrMem.callbackToRemoveEvents = () => {\n unregister(this);\n if (xhrMem.createdResources) {\n xhrMem.createdResources.observer.disconnect();\n }\n };\n plugin._addHeaders(this, spanUrl);\n plugin._addResourceObserver(this, spanUrl);\n }\n );\n }\n return original.apply(this, args);\n };\n };\n }\n\n /**\n * implements enable function\n */\n override enable() {\n if (this._isEnabled) {\n return;\n }\n if (this._isXhrPatched) {\n this._diag.debug(\n 'reactivating existing patch on',\n this.moduleName,\n this.version\n );\n this._isEnabled = true;\n return;\n }\n\n try {\n this._diag.debug('applying patch to', this.moduleName, this.version);\n this._wrap(XMLHttpRequest.prototype, 'open', this._patchOpen());\n this._wrap(XMLHttpRequest.prototype, 'send', this._patchSend());\n this._isXhrPatched = true;\n this._isEnabled = true;\n } catch (err) {\n // make sure there is no wrapped functions\n this._unwrap(XMLHttpRequest.prototype, 'open');\n this._unwrap(XMLHttpRequest.prototype, 'send');\n this._diag.warn(\n 'Failed to patch globalThis.XMLHttpRequest; instrumentation will not be enabled. ' +\n 'Another script may have locked globalThis.XMLHttpRequest via Object.defineProperty.',\n err\n );\n }\n }\n\n /**\n * implements disable function\n */\n override disable() {\n if (!this._isEnabled) {\n return;\n }\n this._isEnabled = false;\n this._tasksCount = 0;\n this._xhrMem = new WeakMap<XMLHttpRequest, XhrMem>();\n this._usedResources = new WeakSet<PerformanceResourceTiming>();\n }\n}\n"],"mappings":";;;;;;;;;AA+CA,MAAM,wBAAwB;;;;AAuC9B,IAAa,gCAAb,cAAmD,oBAAyD;CAC1G,YAA6B;CAC7B,UAA2B;CAC3B,aAAa,KAAK;CAElB,cAAsB;CACtB,0BAAkB,IAAI,QAAgC;CACtD,iCAAyB,IAAI,QAAmC;CAWhE,YAAY,SAA8C,CAAC,GAAG;EAC5D,MAAM,mDAAmD,SAAS,MAAM;CAC1E;CAEA,OAAO,CAAC;;;;;;;CAQR,YAAoB,KAAqB,SAAiB;EACxD,MAAM,MAAM,SAAS,OAAO,CAAC,CAAC;EAC9B,IACE,CAAC,4BACC,KACA,KAAK,UAAU,CAAC,CAAC,4BACnB,GACA;GACA,MAAM,UAA4C,CAAC;GACnD,IAAI,YAAY,OAAO,IAAI,QAAQ,OAAO,GAAG,OAAO;GACpD,IAAI,OAAO,KAAK,OAAO,CAAC,CAAC,SAAS,GAChC,KAAK,MAAM,MAAM,2CAA2C;GAE9D;EACF;EACA,MAAM,UAAsC,CAAC;EAC7C,IAAI,YAAY,OAAO,IAAI,QAAQ,OAAO,GAAG,OAAO;EACpD,OAAO,KAAK,OAAO,CAAC,CAAC,SAAQ,QAAO;GAClC,IAAI,iBAAiB,KAAK,OAAO,QAAQ,IAAI,CAAC;EAChD,CAAC;CACH;;;;;;;CAQA,cACE,MACA,sBACM;EACN,IAAI,QAAQ,KAAK,IAAI,MAAM,QAAQ,IAAI,QAAQ,OAAO,GAAG,IAAI,SAAS;GACpE,MAAM,YAAY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ACnB,KAAK,0BAAU,IAAI,QAAgC;EACnD,KAAK,iCAAiB,IAAI,QAAmC;CAC/D;AACF"}
package/package.json CHANGED
@@ -1,25 +1,36 @@
1
1
  {
2
2
  "name": "@opentelemetry/instrumentation-xml-http-request",
3
- "version": "0.222.0",
3
+ "version": "0.300.0-development.0",
4
4
  "description": "OpenTelemetry instrumentation for XMLHttpRequest http client in web browsers",
5
- "main": "build/src/index.js",
6
- "module": "build/esm/index.js",
7
- "esnext": "build/esnext/index.js",
8
- "types": "build/src/index.d.ts",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.cts",
8
+ "exports": {
9
+ ".": {
10
+ "import": {
11
+ "types": "./dist/index.d.mts",
12
+ "default": "./dist/index.mjs"
13
+ },
14
+ "require": {
15
+ "types": "./dist/index.d.cts",
16
+ "default": "./dist/index.cjs"
17
+ }
18
+ }
19
+ },
9
20
  "repository": "open-telemetry/opentelemetry-js",
10
21
  "scripts": {
11
22
  "prepublishOnly": "npm run compile",
12
- "compile": "tsc --build tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
13
- "clean": "tsc --build --clean tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
23
+ "compile": "tsdown",
24
+ "clean": "rimraf dist build",
14
25
  "lint": "eslint .",
15
26
  "lint:fix": "eslint . --fix",
16
27
  "version": "node ../../../scripts/version-update.js",
17
28
  "tdd": "karma start",
18
29
  "test:browser": "karma start --single-run",
19
- "watch": "tsc --build --watch tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
30
+ "watch": "tsdown --watch --no-clean",
20
31
  "prewatch": "node ../../../scripts/version-update.js",
21
- "peer-api-check": "node ../../../scripts/peer-api-check.js",
22
- "align-api-deps": "node ../../../scripts/align-api-deps.js"
32
+ "peer-api-check": "node ../../../scripts/peer-api-check.mjs",
33
+ "align-api-deps": "node ../../../scripts/align-api-deps.mjs"
23
34
  },
24
35
  "keywords": [
25
36
  "opentelemetry",
@@ -33,21 +44,13 @@
33
44
  "author": "OpenTelemetry Authors",
34
45
  "license": "Apache-2.0",
35
46
  "engines": {
36
- "node": "^18.19.0 || >=20.6.0"
47
+ "node": ">=22.15.0"
37
48
  },
38
49
  "files": [
39
- "build/esm/**/*.js",
40
- "build/esm/**/*.js.map",
41
- "build/esm/**/*.d.ts",
42
- "build/esnext/**/*.js",
43
- "build/esnext/**/*.js.map",
44
- "build/esnext/**/*.d.ts",
45
- "build/src/**/*.js",
46
- "build/src/**/*.js.map",
47
- "build/src/**/*.d.ts",
48
50
  "doc",
49
51
  "LICENSE",
50
- "README.md"
52
+ "README.md",
53
+ "dist"
51
54
  ],
52
55
  "publishConfig": {
53
56
  "access": "public"
@@ -56,9 +59,9 @@
56
59
  "@babel/core": "7.29.7",
57
60
  "@babel/preset-env": "7.29.7",
58
61
  "@opentelemetry/api": "1.9.1",
59
- "@opentelemetry/context-zone": "2.11.0",
60
- "@opentelemetry/propagator-b3": "2.11.0",
61
- "@opentelemetry/sdk-trace": "2.11.0",
62
+ "@opentelemetry/context-zone": "3.0.0-development.0",
63
+ "@opentelemetry/propagator-b3": "3.0.0-development.0",
64
+ "@opentelemetry/sdk-trace": "3.0.0-development.0",
62
65
  "@types/mocha": "10.0.10",
63
66
  "@types/node": "18.19.130",
64
67
  "@types/sinon": "17.0.4",
@@ -69,7 +72,7 @@
69
72
  "karma-chrome-launcher": "3.2.0",
70
73
  "karma-coverage": "2.2.1",
71
74
  "karma-mocha": "2.0.1",
72
- "karma-spec-reporter": "0.0.36",
75
+ "karma-spec-reporter": "0.0.37",
73
76
  "karma-webpack": "5.0.1",
74
77
  "mocha": "11.8.0",
75
78
  "nyc": "17.1.0",
@@ -77,18 +80,18 @@
77
80
  "ts-loader": "9.5.7",
78
81
  "typescript": "5.2.2",
79
82
  "webpack": "5.108.4",
80
- "webpack-cli": "7.2.2"
83
+ "webpack-cli": "7.2.3"
81
84
  },
82
85
  "peerDependencies": {
83
86
  "@opentelemetry/api": "^1.3.0"
84
87
  },
85
88
  "dependencies": {
86
- "@opentelemetry/core": "2.11.0",
87
- "@opentelemetry/instrumentation": "0.222.0",
88
- "@opentelemetry/sdk-trace-web": "2.11.0",
89
- "@opentelemetry/semantic-conventions": "^1.29.0"
89
+ "@opentelemetry/core": "3.0.0-development.0",
90
+ "@opentelemetry/instrumentation": "0.300.0-development.0",
91
+ "@opentelemetry/semantic-conventions": "^1.29.0",
92
+ "@opentelemetry/web-common": "0.300.0-development.0"
90
93
  },
91
94
  "homepage": "https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/opentelemetry-instrumentation-xml-http-request",
92
95
  "sideEffects": false,
93
- "gitHead": "0b72a81636fa476e8f1f1afd2ae0c90a1362194c"
96
+ "gitHead": "ad3499bf70ee996f5565e5cd00f2c5fcb2e87042"
94
97
  }
@@ -1,9 +0,0 @@
1
- export declare enum EventNames {
2
- METHOD_OPEN = "open",
3
- METHOD_SEND = "send",
4
- EVENT_ABORT = "abort",
5
- EVENT_ERROR = "error",
6
- EVENT_LOAD = "loaded",
7
- EVENT_TIMEOUT = "timeout"
8
- }
9
- //# sourceMappingURL=EventNames.d.ts.map
@@ -1,14 +0,0 @@
1
- /*
2
- * Copyright The OpenTelemetry Authors
3
- * SPDX-License-Identifier: Apache-2.0
4
- */
5
- export var EventNames;
6
- (function (EventNames) {
7
- EventNames["METHOD_OPEN"] = "open";
8
- EventNames["METHOD_SEND"] = "send";
9
- EventNames["EVENT_ABORT"] = "abort";
10
- EventNames["EVENT_ERROR"] = "error";
11
- EventNames["EVENT_LOAD"] = "loaded";
12
- EventNames["EVENT_TIMEOUT"] = "timeout";
13
- })(EventNames || (EventNames = {}));
14
- //# sourceMappingURL=EventNames.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"EventNames.js","sourceRoot":"","sources":["../../../src/enums/EventNames.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,CAAN,IAAY,UAOX;AAPD,WAAY,UAAU;IACpB,kCAAoB,CAAA;IACpB,kCAAoB,CAAA;IACpB,mCAAqB,CAAA;IACrB,mCAAqB,CAAA;IACrB,mCAAqB,CAAA;IACrB,uCAAyB,CAAA;AAC3B,CAAC,EAPW,UAAU,KAAV,UAAU,QAOrB","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nexport enum EventNames {\n METHOD_OPEN = 'open',\n METHOD_SEND = 'send',\n EVENT_ABORT = 'abort',\n EVENT_ERROR = 'error',\n EVENT_LOAD = 'loaded',\n EVENT_TIMEOUT = 'timeout',\n}\n"]}
@@ -1,3 +0,0 @@
1
- export { XMLHttpRequestInstrumentation } from './xhr';
2
- export type { XHRCustomAttributeFunction, XMLHttpRequestInstrumentationConfig, } from './xhr';
3
- //# sourceMappingURL=index.d.ts.map
@@ -1,6 +0,0 @@
1
- /*
2
- * Copyright The OpenTelemetry Authors
3
- * SPDX-License-Identifier: Apache-2.0
4
- */
5
- export { XMLHttpRequestInstrumentation } from './xhr';
6
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,6BAA6B,EAAE,MAAM,OAAO,CAAC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nexport { XMLHttpRequestInstrumentation } from './xhr';\nexport type {\n XHRCustomAttributeFunction,\n XMLHttpRequestInstrumentationConfig,\n} from './xhr';\n"]}
@@ -1,9 +0,0 @@
1
- /**
2
- * The size of the request payload body in bytes. This is the number of bytes transferred excluding headers and is often, but not always, present as the [Content-Length](https://www.rfc-editor.org/rfc/rfc9110.html#field.content-length) header. For requests using transport encoding, this should be the compressed size.
3
- *
4
- * @example 3495
5
- *
6
- * @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
7
- */
8
- export declare const ATTR_HTTP_REQUEST_BODY_SIZE: "http.request.body.size";
9
- //# sourceMappingURL=semconv.d.ts.map
@@ -1,18 +0,0 @@
1
- /*
2
- * Copyright The OpenTelemetry Authors
3
- * SPDX-License-Identifier: Apache-2.0
4
- */
5
- /*
6
- * This file contains a copy of unstable semantic convention definitions
7
- * used by this package.
8
- * @see https://github.com/open-telemetry/opentelemetry-js/tree/main/semantic-conventions#unstable-semconv
9
- */
10
- /**
11
- * The size of the request payload body in bytes. This is the number of bytes transferred excluding headers and is often, but not always, present as the [Content-Length](https://www.rfc-editor.org/rfc/rfc9110.html#field.content-length) header. For requests using transport encoding, this should be the compressed size.
12
- *
13
- * @example 3495
14
- *
15
- * @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
16
- */
17
- export const ATTR_HTTP_REQUEST_BODY_SIZE = 'http.request.body.size';
18
- //# sourceMappingURL=semconv.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"semconv.js","sourceRoot":"","sources":["../../src/semconv.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;GAIG;AAEH;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG,wBAAiC,CAAC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\n/*\n * This file contains a copy of unstable semantic convention definitions\n * used by this package.\n * @see https://github.com/open-telemetry/opentelemetry-js/tree/main/semantic-conventions#unstable-semconv\n */\n\n/**\n * The size of the request payload body in bytes. This is the number of bytes transferred excluding headers and is often, but not always, present as the [Content-Length](https://www.rfc-editor.org/rfc/rfc9110.html#field.content-length) header. For requests using transport encoding, this should be the compressed size.\n *\n * @example 3495\n *\n * @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.\n */\nexport const ATTR_HTTP_REQUEST_BODY_SIZE = 'http.request.body.size' as const;\n"]}
@@ -1,29 +0,0 @@
1
- import type * as api from '@opentelemetry/api';
2
- /**
3
- * method "open" from XMLHttpRequest
4
- */
5
- export type OpenFunction = (method: string, url: string, async?: boolean, user?: string | null, pass?: string | null) => void;
6
- /**
7
- * method "send" from XMLHttpRequest
8
- */
9
- export type SendFunction = typeof XMLHttpRequest.prototype.send;
10
- export type SendBody = string | Document | Blob | ArrayBufferView | ArrayBuffer | FormData | URLSearchParams | ReadableStream<Uint8Array> | null;
11
- /**
12
- * interface to store information in weak map about spans, resources and
13
- * callbacks
14
- */
15
- export interface XhrMem {
16
- status?: number;
17
- statusText?: string;
18
- span: api.Span;
19
- spanUrl?: string;
20
- sendStartTime?: api.HrTime;
21
- createdResources?: {
22
- observer: PerformanceObserver;
23
- entries: PerformanceResourceTiming[];
24
- };
25
- callbackToRemoveEvents?: () => void;
26
- }
27
- export type PropagateTraceHeaderCorsUrl = string | RegExp;
28
- export type PropagateTraceHeaderCorsUrls = PropagateTraceHeaderCorsUrl | PropagateTraceHeaderCorsUrl[];
29
- //# sourceMappingURL=types.d.ts.map
@@ -1,6 +0,0 @@
1
- /*
2
- * Copyright The OpenTelemetry Authors
3
- * SPDX-License-Identifier: Apache-2.0
4
- */
5
- export {};
6
- //# sourceMappingURL=types.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type * as api from '@opentelemetry/api';\n\n/**\n * method \"open\" from XMLHttpRequest\n */\nexport type OpenFunction = (\n method: string,\n url: string,\n async?: boolean,\n user?: string | null,\n pass?: string | null\n) => void;\n\n/**\n * method \"send\" from XMLHttpRequest\n */\nexport type SendFunction = typeof XMLHttpRequest.prototype.send;\n\nexport type SendBody =\n | string\n | Document\n | Blob\n | ArrayBufferView\n | ArrayBuffer\n | FormData\n | URLSearchParams\n | ReadableStream<Uint8Array>\n | null;\n\n/**\n * interface to store information in weak map about spans, resources and\n * callbacks\n */\nexport interface XhrMem {\n status?: number;\n statusText?: string;\n // span assigned to xhr\n span: api.Span;\n // span url - not available on types.Span\n spanUrl?: string;\n // startTime of send function - used to filter cors preflight requests\n sendStartTime?: api.HrTime;\n // resources created between send and end plus some additional timeout\n createdResources?: {\n observer: PerformanceObserver;\n entries: PerformanceResourceTiming[];\n };\n // callback to remove events from xhr once the span ends\n callbackToRemoveEvents?: () => void;\n}\n\nexport type PropagateTraceHeaderCorsUrl = string | RegExp;\n\nexport type PropagateTraceHeaderCorsUrls =\n | PropagateTraceHeaderCorsUrl\n | PropagateTraceHeaderCorsUrl[];\n"]}