@opentelemetry/instrumentation-fetch 0.222.0 → 0.300.0-development.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.
Files changed (85) hide show
  1. package/README.md +14 -11
  2. package/dist/_virtual/_rolldown/runtime.cjs +23 -0
  3. package/dist/fetch.cjs +318 -0
  4. package/dist/fetch.cjs.map +1 -0
  5. package/dist/fetch.d.cts +121 -0
  6. package/dist/fetch.d.cts.map +1 -0
  7. package/dist/fetch.d.mts +121 -0
  8. package/dist/fetch.d.mts.map +1 -0
  9. package/dist/fetch.mjs +315 -0
  10. package/dist/fetch.mjs.map +1 -0
  11. package/dist/index.cjs +3 -0
  12. package/dist/index.d.cts +2 -0
  13. package/dist/index.d.mts +2 -0
  14. package/dist/index.mjs +2 -0
  15. package/dist/semconv.cjs +13 -0
  16. package/dist/semconv.cjs.map +1 -0
  17. package/dist/semconv.mjs +13 -0
  18. package/dist/semconv.mjs.map +1 -0
  19. package/dist/types.d.cts +12 -0
  20. package/dist/types.d.cts.map +1 -0
  21. package/dist/types.d.mts +12 -0
  22. package/dist/types.d.mts.map +1 -0
  23. package/dist/utils.cjs +157 -0
  24. package/dist/utils.cjs.map +1 -0
  25. package/dist/utils.mjs +154 -0
  26. package/dist/utils.mjs.map +1 -0
  27. package/dist/version.cjs +6 -0
  28. package/dist/version.cjs.map +1 -0
  29. package/dist/version.mjs +6 -0
  30. package/dist/version.mjs.map +1 -0
  31. package/package.json +36 -34
  32. package/build/esm/fetch.d.ts +0 -118
  33. package/build/esm/fetch.js +0 -412
  34. package/build/esm/fetch.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 -27
  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 -43
  45. package/build/esm/utils.js +0 -205
  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/esnext/fetch.d.ts +0 -118
  51. package/build/esnext/fetch.js +0 -412
  52. package/build/esnext/fetch.js.map +0 -1
  53. package/build/esnext/index.d.ts +0 -3
  54. package/build/esnext/index.js +0 -6
  55. package/build/esnext/index.js.map +0 -1
  56. package/build/esnext/semconv.d.ts +0 -9
  57. package/build/esnext/semconv.js +0 -18
  58. package/build/esnext/semconv.js.map +0 -1
  59. package/build/esnext/types.d.ts +0 -27
  60. package/build/esnext/types.js +0 -6
  61. package/build/esnext/types.js.map +0 -1
  62. package/build/esnext/utils.d.ts +0 -43
  63. package/build/esnext/utils.js +0 -205
  64. package/build/esnext/utils.js.map +0 -1
  65. package/build/esnext/version.d.ts +0 -2
  66. package/build/esnext/version.js +0 -7
  67. package/build/esnext/version.js.map +0 -1
  68. package/build/src/fetch.d.ts +0 -118
  69. package/build/src/fetch.js +0 -416
  70. package/build/src/fetch.js.map +0 -1
  71. package/build/src/index.d.ts +0 -3
  72. package/build/src/index.js +0 -10
  73. package/build/src/index.js.map +0 -1
  74. package/build/src/semconv.d.ts +0 -9
  75. package/build/src/semconv.js +0 -21
  76. package/build/src/semconv.js.map +0 -1
  77. package/build/src/types.d.ts +0 -27
  78. package/build/src/types.js +0 -7
  79. package/build/src/types.js.map +0 -1
  80. package/build/src/utils.d.ts +0 -43
  81. package/build/src/utils.js +0 -212
  82. package/build/src/utils.js.map +0 -1
  83. package/build/src/version.d.ts +0 -2
  84. package/build/src/version.js +0 -10
  85. package/build/src/version.js.map +0 -1
@@ -1,205 +0,0 @@
1
- /*
2
- * Copyright The OpenTelemetry Authors
3
- * SPDX-License-Identifier: Apache-2.0
4
- */
5
- // Much of the logic here overlaps with the same utils file in opentelemetry-instrumentation-xml-http-request
6
- // These may be unified in the future.
7
- import { diag } from '@opentelemetry/api';
8
- import { getStringListFromEnv } from '@opentelemetry/core';
9
- const DIAG_LOGGER = diag.createComponentLogger({
10
- namespace: '@opentelemetry/opentelemetry-instrumentation-fetch/utils',
11
- });
12
- /**
13
- * Helper function to determine payload content length for fetch requests
14
- *
15
- * The fetch API is kinda messy: there are a couple of ways the body can be passed in.
16
- *
17
- * In all cases, the body param can be some variation of ReadableStream,
18
- * and ReadableStreams can only be read once! We want to avoid consuming the body here,
19
- * because that would mean that the body never gets sent with the actual fetch request.
20
- *
21
- * Either the first arg is a Request object, which can be cloned
22
- * so we can clone that object and read the body of the clone
23
- * without disturbing the original argument
24
- * However, reading the body here can only be done async; the body() method returns a promise
25
- * this means this entire function has to return a promise
26
- *
27
- * OR the first arg is a url/string
28
- * in which case the second arg has type RequestInit
29
- * RequestInit is NOT cloneable, but RequestInit.body is writable
30
- * so we can chain it into ReadableStream.pipeThrough()
31
- *
32
- * ReadableStream.pipeThrough() lets us process a stream and returns a new stream
33
- * So we can measure the body length as it passes through the pie, but need to attach
34
- * the new stream to the original request
35
- * so that the browser still has access to the body.
36
- *
37
- * @param body
38
- * @returns promise that resolves to the content length of the body
39
- */
40
- export function getFetchBodyLength(...args) {
41
- if (args[0] instanceof URL || typeof args[0] === 'string') {
42
- const requestInit = args[1];
43
- if (!requestInit?.body) {
44
- return Promise.resolve();
45
- }
46
- if (requestInit.body instanceof ReadableStream) {
47
- const { body, length } = _getBodyNonDestructively(requestInit.body);
48
- requestInit.body = body;
49
- return length;
50
- }
51
- else {
52
- return Promise.resolve(getXHRBodyLength(requestInit.body));
53
- }
54
- }
55
- else {
56
- const info = args[0];
57
- if (!info?.body) {
58
- return Promise.resolve();
59
- }
60
- return info
61
- .clone()
62
- .text()
63
- .then(t => getByteLength(t));
64
- }
65
- }
66
- function _getBodyNonDestructively(body) {
67
- // can't read a ReadableStream without destroying it
68
- // but we CAN pipe it through and return a new ReadableStream
69
- // some (older) platforms don't expose the pipeThrough method and in that scenario, we're out of luck;
70
- // there's no way to read the stream without consuming it.
71
- if (!body.pipeThrough) {
72
- DIAG_LOGGER.warn('Platform has ReadableStream but not pipeThrough!');
73
- return {
74
- body,
75
- length: Promise.resolve(undefined),
76
- };
77
- }
78
- let length = 0;
79
- let resolveLength;
80
- const lengthPromise = new Promise(resolve => {
81
- resolveLength = resolve;
82
- });
83
- const transform = new TransformStream({
84
- start() { },
85
- async transform(chunk, controller) {
86
- const bytearray = (await chunk);
87
- length += bytearray.byteLength;
88
- controller.enqueue(chunk);
89
- },
90
- flush() {
91
- resolveLength(length);
92
- },
93
- });
94
- return {
95
- body: body.pipeThrough(transform),
96
- length: lengthPromise,
97
- };
98
- }
99
- function isDocument(value) {
100
- return typeof Document !== 'undefined' && value instanceof Document;
101
- }
102
- /**
103
- * Helper function to determine payload content length for XHR requests
104
- * @param body
105
- * @returns content length
106
- */
107
- export function getXHRBodyLength(body) {
108
- if (isDocument(body)) {
109
- return new XMLSerializer().serializeToString(document).length;
110
- }
111
- // XMLHttpRequestBodyInit expands to the following:
112
- if (typeof body === 'string') {
113
- return getByteLength(body);
114
- }
115
- if (body instanceof Blob) {
116
- return body.size;
117
- }
118
- if (body instanceof FormData) {
119
- return getFormDataSize(body);
120
- }
121
- if (body instanceof URLSearchParams) {
122
- return getByteLength(body.toString());
123
- }
124
- // ArrayBuffer | ArrayBufferView
125
- if (body.byteLength !== undefined) {
126
- return body.byteLength;
127
- }
128
- DIAG_LOGGER.warn('unknown body type');
129
- return undefined;
130
- }
131
- const TEXT_ENCODER = new TextEncoder();
132
- function getByteLength(s) {
133
- return TEXT_ENCODER.encode(s).byteLength;
134
- }
135
- function getFormDataSize(formData) {
136
- let size = 0;
137
- for (const [key, value] of formData.entries()) {
138
- size += key.length;
139
- if (value instanceof Blob) {
140
- size += value.size;
141
- }
142
- else {
143
- size += value.length;
144
- }
145
- }
146
- return size;
147
- }
148
- /**
149
- * Normalize an HTTP request method string per `http.request.method` spec
150
- * https://github.com/open-telemetry/semantic-conventions/blob/main/docs/http/http-spans.md#http-client-span
151
- */
152
- export function normalizeHttpRequestMethod(method) {
153
- const knownMethods = getKnownMethods();
154
- const methUpper = method.toUpperCase();
155
- if (methUpper in knownMethods) {
156
- return methUpper;
157
- }
158
- else {
159
- return '_OTHER';
160
- }
161
- }
162
- const DEFAULT_KNOWN_METHODS = {
163
- CONNECT: true,
164
- DELETE: true,
165
- GET: true,
166
- HEAD: true,
167
- OPTIONS: true,
168
- PATCH: true,
169
- POST: true,
170
- PUT: true,
171
- TRACE: true,
172
- // QUERY from https://datatracker.ietf.org/doc/draft-ietf-httpbis-safe-method-w-body/
173
- QUERY: true,
174
- };
175
- let knownMethods;
176
- function getKnownMethods() {
177
- if (knownMethods === undefined) {
178
- const cfgMethods = getStringListFromEnv('OTEL_INSTRUMENTATION_HTTP_KNOWN_METHODS');
179
- if (cfgMethods && cfgMethods.length > 0) {
180
- knownMethods = {};
181
- cfgMethods.forEach(m => {
182
- knownMethods[m] = true;
183
- });
184
- }
185
- else {
186
- knownMethods = DEFAULT_KNOWN_METHODS;
187
- }
188
- }
189
- return knownMethods;
190
- }
191
- const HTTP_PORT_FROM_PROTOCOL = {
192
- 'https:': '443',
193
- 'http:': '80',
194
- };
195
- export function serverPortFromUrl(url) {
196
- const serverPort = Number(url.port || HTTP_PORT_FROM_PROTOCOL[url.protocol]);
197
- // Guard with `if (serverPort)` because `Number('') === 0`.
198
- if (serverPort && !isNaN(serverPort)) {
199
- return serverPort;
200
- }
201
- else {
202
- return undefined;
203
- }
204
- }
205
- //# sourceMappingURL=utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,6GAA6G;AAC7G,sCAAsC;AAEtC,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC1C,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAG3D,MAAM,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IAAI,EAAE,IAAI;IACV,OAAO,EAAE,IAAI;IACb,KAAK,EAAE,IAAI;IACX,IAAI,EAAE,IAAI;IACV,GAAG,EAAE,IAAI;IACT,KAAK,EAAE,IAAI;IACX,qFAAqF;IACrF,KAAK,EAAE,IAAI;CACZ,CAAC;AACF,IAAI,YAAwC,CAAC;AAC7C,SAAS,eAAe;IACtB,IAAI,YAAY,KAAK,SAAS,EAAE;QAC9B,MAAM,UAAU,GAAG,oBAAoB,CACrC,yCAAyC,CAC1C,CAAC;QACF,IAAI,UAAU,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE;YACvC,YAAY,GAAG,EAAE,CAAC;YAClB,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE;gBACrB,YAAY,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;YACzB,CAAC,CAAC,CAAC;SACJ;aAAM;YACL,YAAY,GAAG,qBAAqB,CAAC;SACtC;KACF;IACD,OAAO,YAAY,CAAC;AACtB,CAAC;AAED,MAAM,uBAAuB,GAA8B;IACzD,QAAQ,EAAE,KAAK;IACf,OAAO,EAAE,IAAI;CACd,CAAC;AACF,MAAM,UAAU,iBAAiB,CAAC,GAAY;IAC5C,MAAM,UAAU,GAAG,MAAM,CAAC,GAAG,CAAC,IAAI,IAAI,uBAAuB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC7E,2DAA2D;IAC3D,IAAI,UAAU,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,EAAE;QACpC,OAAO,UAAU,CAAC;KACnB;SAAM;QACL,OAAO,SAAS,CAAC;KAClB;AACH,CAAC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\n// Much of the logic here overlaps with the same utils file in opentelemetry-instrumentation-xml-http-request\n// These may be unified in the future.\n\nimport { diag } from '@opentelemetry/api';\nimport { getStringListFromEnv } from '@opentelemetry/core';\nimport type { URLLike } from '@opentelemetry/sdk-trace-web';\n\nconst DIAG_LOGGER = diag.createComponentLogger({\n namespace: '@opentelemetry/opentelemetry-instrumentation-fetch/utils',\n});\n\n/**\n * Helper function to determine payload content length for fetch requests\n *\n * The fetch API is kinda messy: there are a couple of ways the body can be passed in.\n *\n * In all cases, the body param can be some variation of ReadableStream,\n * and ReadableStreams can only be read once! We want to avoid consuming the body here,\n * because that would mean that the body never gets sent with the actual fetch request.\n *\n * Either the first arg is a Request object, which can be cloned\n * so we can clone that object and read the body of the clone\n * without disturbing the original argument\n * However, reading the body here can only be done async; the body() method returns a promise\n * this means this entire function has to return a promise\n *\n * OR the first arg is a url/string\n * in which case the second arg has type RequestInit\n * RequestInit is NOT cloneable, but RequestInit.body is writable\n * so we can chain it into ReadableStream.pipeThrough()\n *\n * ReadableStream.pipeThrough() lets us process a stream and returns a new stream\n * So we can measure the body length as it passes through the pie, but need to attach\n * the new stream to the original request\n * so that the browser still has access to the body.\n *\n * @param body\n * @returns promise that resolves to the content length of the body\n */\nexport function getFetchBodyLength(...args: Parameters<typeof fetch>) {\n if (args[0] instanceof URL || typeof args[0] === 'string') {\n const requestInit = args[1];\n if (!requestInit?.body) {\n return Promise.resolve();\n }\n if (requestInit.body instanceof ReadableStream) {\n const { body, length } = _getBodyNonDestructively(requestInit.body);\n requestInit.body = body;\n\n return length;\n } else {\n return Promise.resolve(getXHRBodyLength(requestInit.body));\n }\n } else {\n const info = args[0];\n if (!info?.body) {\n return Promise.resolve();\n }\n\n return info\n .clone()\n .text()\n .then(t => getByteLength(t));\n }\n}\n\nfunction _getBodyNonDestructively(body: ReadableStream) {\n // can't read a ReadableStream without destroying it\n // but we CAN pipe it through and return a new ReadableStream\n\n // some (older) platforms don't expose the pipeThrough method and in that scenario, we're out of luck;\n // there's no way to read the stream without consuming it.\n if (!body.pipeThrough) {\n DIAG_LOGGER.warn('Platform has ReadableStream but not pipeThrough!');\n return {\n body,\n length: Promise.resolve(undefined),\n };\n }\n\n let length = 0;\n let resolveLength: (l: number) => void;\n const lengthPromise = new Promise<number>(resolve => {\n resolveLength = resolve;\n });\n\n const transform = new TransformStream({\n start() {},\n async transform(chunk, controller) {\n const bytearray = (await chunk) as Uint8Array;\n length += bytearray.byteLength;\n\n controller.enqueue(chunk);\n },\n flush() {\n resolveLength(length);\n },\n });\n\n return {\n body: body.pipeThrough(transform),\n length: lengthPromise,\n };\n}\n\nfunction isDocument(value: unknown): value is Document {\n return typeof Document !== 'undefined' && value instanceof Document;\n}\n\n/**\n * Helper function to determine payload content length for XHR requests\n * @param body\n * @returns content length\n */\nexport function getXHRBodyLength(\n body: Document | XMLHttpRequestBodyInit\n): number | undefined {\n if (isDocument(body)) {\n return new XMLSerializer().serializeToString(document).length;\n }\n\n // XMLHttpRequestBodyInit expands to the following:\n if (typeof body === 'string') {\n return getByteLength(body);\n }\n\n if (body instanceof Blob) {\n return body.size;\n }\n\n if (body instanceof FormData) {\n return getFormDataSize(body);\n }\n\n if (body instanceof URLSearchParams) {\n return getByteLength(body.toString());\n }\n\n // ArrayBuffer | ArrayBufferView\n if (body.byteLength !== undefined) {\n return body.byteLength;\n }\n\n DIAG_LOGGER.warn('unknown body type');\n return undefined;\n}\n\nconst TEXT_ENCODER = new TextEncoder();\nfunction getByteLength(s: string): number {\n return TEXT_ENCODER.encode(s).byteLength;\n}\n\nfunction getFormDataSize(formData: FormData): number {\n let size = 0;\n for (const [key, value] of formData.entries()) {\n size += key.length;\n if (value instanceof Blob) {\n size += value.size;\n } else {\n size += value.length;\n }\n }\n return size;\n}\n\n/**\n * Normalize an HTTP request method string per `http.request.method` spec\n * https://github.com/open-telemetry/semantic-conventions/blob/main/docs/http/http-spans.md#http-client-span\n */\nexport function normalizeHttpRequestMethod(method: string): string {\n const knownMethods = getKnownMethods();\n const methUpper = method.toUpperCase();\n if (methUpper in knownMethods) {\n return methUpper;\n } else {\n return '_OTHER';\n }\n}\n\nconst DEFAULT_KNOWN_METHODS = {\n CONNECT: true,\n DELETE: true,\n GET: true,\n HEAD: true,\n OPTIONS: true,\n PATCH: true,\n POST: true,\n PUT: true,\n TRACE: true,\n // QUERY from https://datatracker.ietf.org/doc/draft-ietf-httpbis-safe-method-w-body/\n QUERY: true,\n};\nlet knownMethods: { [key: string]: boolean };\nfunction getKnownMethods() {\n if (knownMethods === undefined) {\n const cfgMethods = getStringListFromEnv(\n 'OTEL_INSTRUMENTATION_HTTP_KNOWN_METHODS'\n );\n if (cfgMethods && cfgMethods.length > 0) {\n knownMethods = {};\n cfgMethods.forEach(m => {\n knownMethods[m] = true;\n });\n } else {\n knownMethods = DEFAULT_KNOWN_METHODS;\n }\n }\n return knownMethods;\n}\n\nconst HTTP_PORT_FROM_PROTOCOL: { [key: string]: string } = {\n 'https:': '443',\n 'http:': '80',\n};\nexport function serverPortFromUrl(url: URLLike): number | undefined {\n const serverPort = Number(url.port || HTTP_PORT_FROM_PROTOCOL[url.protocol]);\n // Guard with `if (serverPort)` because `Number('') === 0`.\n if (serverPort && !isNaN(serverPort)) {\n return serverPort;\n } else {\n return undefined;\n }\n}\n"]}
@@ -1,2 +0,0 @@
1
- export declare const VERSION = "0.222.0";
2
- //# sourceMappingURL=version.d.ts.map
@@ -1,7 +0,0 @@
1
- /*
2
- * Copyright The OpenTelemetry Authors
3
- * SPDX-License-Identifier: Apache-2.0
4
- */
5
- // this is autogenerated file, see scripts/version-update.js
6
- export const VERSION = '0.222.0';
7
- //# sourceMappingURL=version.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,4DAA4D;AAC5D,MAAM,CAAC,MAAM,OAAO,GAAG,SAAS,CAAC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\n// this is autogenerated file, see scripts/version-update.js\nexport const VERSION = '0.222.0';\n"]}
@@ -1,118 +0,0 @@
1
- import type { Span } from '@opentelemetry/api';
2
- import type { InstrumentationConfig } from '@opentelemetry/instrumentation';
3
- import { InstrumentationBase } from '@opentelemetry/instrumentation';
4
- import * as web from '@opentelemetry/sdk-trace-web';
5
- import type { FetchError } from './types';
6
- export interface FetchCustomAttributeFunction {
7
- (span: Span, request: Request | RequestInit, result: Response | FetchError): void;
8
- }
9
- export interface FetchRequestHookFunction {
10
- (span: Span, request: Request | RequestInit): void;
11
- }
12
- /**
13
- * FetchPlugin Config
14
- */
15
- export interface FetchInstrumentationConfig extends InstrumentationConfig {
16
- clearTimingResources?: boolean;
17
- propagateTraceHeaderCorsUrls?: web.PropagateTraceHeaderCorsUrls;
18
- /**
19
- * URLs that partially match any regex in ignoreUrls will not be traced.
20
- * In addition, URLs that are _exact matches_ of strings in ignoreUrls will
21
- * also not be traced.
22
- */
23
- ignoreUrls?: Array<string | RegExp>;
24
- /** Function for adding custom attributes on the span */
25
- applyCustomAttributesOnSpan?: FetchCustomAttributeFunction;
26
- /** Function for adding custom attributes or headers before the request is handled */
27
- requestHook?: FetchRequestHookFunction;
28
- ignoreNetworkEvents?: boolean;
29
- /** Measure outgoing request size */
30
- measureRequestSize?: boolean;
31
- }
32
- /**
33
- * This class represents a fetch plugin for auto instrumentation
34
- */
35
- export declare class FetchInstrumentation extends InstrumentationBase<FetchInstrumentationConfig> {
36
- readonly component: string;
37
- readonly version: string;
38
- moduleName: string;
39
- private _usedResources;
40
- private _tasksCount;
41
- private _isEnabled;
42
- private _isFetchPatched;
43
- constructor(config?: FetchInstrumentationConfig);
44
- init(): void;
45
- /**
46
- * Add cors pre flight child span
47
- * @param span
48
- * @param corsPreFlightRequest
49
- */
50
- private _addChildSpan;
51
- /**
52
- * Adds more attributes to span just before ending it
53
- * @param span
54
- * @param response
55
- */
56
- private _addFinalSpanAttributes;
57
- /**
58
- * Add headers
59
- * @param options
60
- * @param spanUrl
61
- */
62
- private _addHeaders;
63
- /**
64
- * Clears the resource timings and all resources assigned with spans
65
- * when {@link FetchPluginConfig.clearTimingResources} is
66
- * set to true (default false)
67
- * @private
68
- */
69
- private _clearResources;
70
- /**
71
- * Creates a new span
72
- * @param url
73
- * @param options
74
- */
75
- private _createSpan;
76
- /**
77
- * Finds appropriate resource and add network events to the span
78
- * @param span
79
- * @param resourcesObserver
80
- * @param endTime
81
- */
82
- private _findResourceAndAddNetworkEvents;
83
- /**
84
- * Marks certain [resource]{@link PerformanceResourceTiming} when information
85
- * from this is used to add events to span.
86
- * This is done to avoid reusing the same resource again for next span
87
- * @param resource
88
- */
89
- private _markResourceAsUsed;
90
- /**
91
- * Finish span, add attributes, network events etc.
92
- * @param span
93
- * @param spanData
94
- * @param response
95
- */
96
- private _endSpan;
97
- /**
98
- * Patches the constructor of fetch
99
- */
100
- private _patchConstructor;
101
- private _applyAttributesAfterFetch;
102
- private _callRequestHook;
103
- /**
104
- * Prepares a span data - needed later for matching appropriate network
105
- * resources
106
- * @param spanUrl
107
- */
108
- private _prepareSpanData;
109
- /**
110
- * implements enable function
111
- */
112
- enable(): void;
113
- /**
114
- * deactivates fetch instrumentation
115
- */
116
- disable(): void;
117
- }
118
- //# sourceMappingURL=fetch.d.ts.map