@opentelemetry/otlp-exporter-base 0.300.0-development.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.
@@ -18,12 +18,15 @@ const MAX_KEEPALIVE_BODY_SIZE = 61440;
18
18
  */
19
19
  const MAX_KEEPALIVE_REQUESTS = 9;
20
20
  /**
21
- * Track cumulative pending body size across all in-flight keepalive requests.
21
+ * Track cumulative body size charged against the keepalive budget.
22
22
  * This is necessary because the 64KiB limit is cumulative, not per-request.
23
+ * Charged from request start until the response body drains, which is later
24
+ * than the request completing: that is when the browser gives the quota back.
23
25
  */
24
26
  let pendingBodySize = 0;
25
27
  /**
26
- * Track number of pending keepalive requests.
28
+ * Track number of requests holding keepalive budget, on the same terms as
29
+ * {@link pendingBodySize}.
27
30
  */
28
31
  let pendingKeepaliveCount = 0;
29
32
  var FetchTransport = class {
@@ -47,6 +50,17 @@ var FetchTransport = class {
47
50
  _opentelemetry_api.diag.debug(`keepalive disabled: ${(requestSize / 1024).toFixed(1)}KB payload, ${pendingKeepaliveCount} pending (${reason})`);
48
51
  }
49
52
  const suppressedContext = (0, _opentelemetry_core.suppressTracing)(_opentelemetry_api.context.active());
53
+ let released = false;
54
+ const release = () => {
55
+ if (released) return;
56
+ released = true;
57
+ if (useKeepalive) {
58
+ pendingBodySize -= requestSize;
59
+ pendingKeepaliveCount--;
60
+ }
61
+ clearTimeout(timeout);
62
+ };
63
+ let drainOwnsCleanup = false;
50
64
  try {
51
65
  const url = new URL(this._parameters.url);
52
66
  const headers = await this._parameters.headers();
@@ -58,7 +72,8 @@ var FetchTransport = class {
58
72
  keepalive: useKeepalive,
59
73
  mode: globalThis.location ? globalThis.location.origin === url.origin ? "same-origin" : "cors" : "no-cors"
60
74
  }));
61
- await drainResponseBody(response);
75
+ drainResponseBody(response).then(release, release);
76
+ drainOwnsCleanup = true;
62
77
  if (response.status >= 200 && response.status <= 299) {
63
78
  _opentelemetry_api.diag.debug(`export response success (status: ${response.status})`);
64
79
  return { status: "success" };
@@ -89,11 +104,7 @@ var FetchTransport = class {
89
104
  error: new Error("Fetch request errored", { cause: error })
90
105
  };
91
106
  } finally {
92
- clearTimeout(timeout);
93
- if (useKeepalive) {
94
- pendingBodySize -= requestSize;
95
- pendingKeepaliveCount--;
96
- }
107
+ if (!drainOwnsCleanup) release();
97
108
  }
98
109
  }
99
110
  shutdown() {}
@@ -120,7 +131,12 @@ function isFetchNetworkErrorRetryable(error) {
120
131
  * client-abort path and releases the quota far more slowly than reading it to
121
132
  * the end, so the body is read rather than cancelled.
122
133
  *
134
+ * Never rejects. Its settlement, not its outcome, is what releases the
135
+ * request's keepalive budget, so a caller that ignores it leaks that budget
136
+ * for the life of the document.
137
+ *
123
138
  * @see https://fetch.spec.whatwg.org/#fetch-processresponseendofbody
139
+ * @see https://issues.chromium.org/issues/546438373
124
140
  */
125
141
  async function drainResponseBody(response) {
126
142
  try {
@@ -1 +1 @@
1
- {"version":3,"file":"fetch-transport.cjs","names":["suppressTracing","context","isExportHTTPErrorRetryable","parseRetryAfterToMills"],"sources":["../../src/transport/fetch-transport.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type { IExporterTransport } from '../exporter-transport';\nimport type { ExportResponse } from '../export-response';\nimport { context, diag } from '@opentelemetry/api';\nimport { suppressTracing } from '@opentelemetry/core';\nimport {\n isExportHTTPErrorRetryable,\n parseRetryAfterToMills,\n} from '../is-export-retryable';\nimport type { HeadersFactory } from '../configuration/otlp-http-configuration';\n\n/**\n * Maximum total body size for concurrent keepalive requests.\n * Browsers enforce a 64KiB cumulative limit across all pending keepalive requests.\n * We use 60KB to leave headroom for headers.\n * @see https://github.com/whatwg/fetch/issues/679\n * @see https://blog.huli.tw/2025/01/06/en/navigator-sendbeacon-64kib-and-source-code/\n */\nconst MAX_KEEPALIVE_BODY_SIZE = 60 * 1024;\n\n/**\n * Maximum concurrent keepalive requests.\n * Chrome enforces 9 concurrent keepalive fetch requests per renderer process.\n * @see https://github.com/whatwg/fetch/issues/679\n * Quote: \"If the renderer process is processing more than 9 requests with keepalive set, we reject a new request\"\n */\nconst MAX_KEEPALIVE_REQUESTS = 9;\n\n/**\n * Track cumulative pending body size across all in-flight keepalive requests.\n * This is necessary because the 64KiB limit is cumulative, not per-request.\n */\nlet pendingBodySize = 0;\n\n/**\n * Track number of pending keepalive requests.\n */\nlet pendingKeepaliveCount = 0;\n\nexport interface FetchTransportParameters {\n url: string;\n headers: HeadersFactory;\n}\n\nclass FetchTransport implements IExporterTransport {\n private _parameters: FetchTransportParameters;\n\n constructor(parameters: FetchTransportParameters) {\n this._parameters = parameters;\n }\n\n async send(data: Uint8Array, timeoutMillis: number): Promise<ExportResponse> {\n const abortController = new AbortController();\n const timeout = setTimeout(() => abortController.abort(), timeoutMillis);\n // Fetch API may be wrapped by an instrumentation like `@opentelemetry/instrumentation-fetch`.\n // In that case the instrumentation would create a new Span for this request,\n // creating an indirect endless loop Export -> Span -> Export.\n // `__original` lets the call bypass one wrapper; the suppressed context\n // below covers a `fetch` that was wrapped more than once.\n let fetchApi = globalThis.fetch;\n // @ts-expect-error -- fetch could be wrapped\n if (typeof fetchApi.__original === 'function') {\n // @ts-expect-error -- fetch could be wrapped\n fetchApi = fetchApi.__original;\n }\n\n const requestSize = data.byteLength;\n\n // Determine if we can use keepalive based on cumulative browser limits.\n // We must check BEFORE adding to pending totals to avoid exceeding limits.\n const wouldExceedSize =\n pendingBodySize + requestSize > MAX_KEEPALIVE_BODY_SIZE;\n const wouldExceedCount = pendingKeepaliveCount >= MAX_KEEPALIVE_REQUESTS;\n const useKeepalive = !wouldExceedSize && !wouldExceedCount;\n\n if (useKeepalive) {\n pendingBodySize += requestSize;\n pendingKeepaliveCount++;\n } else {\n const reason = wouldExceedSize ? 'size limit' : 'count limit';\n diag.debug(\n `keepalive disabled: ${(requestSize / 1024).toFixed(1)}KB payload, ${pendingKeepaliveCount} pending (${reason})`\n );\n }\n\n // Captured before the first `await`, which a synchronous context manager\n // would not survive.\n const suppressedContext = suppressTracing(context.active());\n\n try {\n const url = new URL(this._parameters.url);\n // Resolve headers before entering the suppressed fetch context.\n const headers = await this._parameters.headers();\n const response = await context.with(suppressedContext, () =>\n fetchApi(url.href, {\n method: 'POST',\n headers,\n body: data,\n signal: abortController.signal,\n keepalive: useKeepalive,\n mode: globalThis.location\n ? globalThis.location.origin === url.origin\n ? 'same-origin'\n : 'cors'\n : 'no-cors',\n })\n );\n\n await drainResponseBody(response);\n\n if (response.status >= 200 && response.status <= 299) {\n diag.debug(`export response success (status: ${response.status})`);\n return { status: 'success' };\n } else if (isExportHTTPErrorRetryable(response.status)) {\n diag.warn(`export response retryable (status: ${response.status})`);\n const retryAfter = response.headers.get('Retry-After');\n const retryInMillis = parseRetryAfterToMills(retryAfter);\n return { status: 'retryable', retryInMillis };\n }\n diag.error(`export response failure (status: ${response.status})`);\n return {\n status: 'failure',\n error: new Error(\n `Fetch request failed with non-retryable status ${response.status}`\n ),\n };\n } catch (error) {\n if (isFetchNetworkErrorRetryable(error)) {\n diag.warn(`export request retryable (network error: ${error})`);\n return {\n status: 'retryable',\n error: new Error('Fetch request encountered a network error', {\n cause: error,\n }),\n };\n }\n diag.error(`export request failure (error: ${error})`);\n return {\n status: 'failure',\n error: new Error('Fetch request errored', { cause: error }),\n };\n } finally {\n clearTimeout(timeout);\n if (useKeepalive) {\n pendingBodySize -= requestSize;\n pendingKeepaliveCount--;\n }\n }\n }\n\n shutdown() {\n // Intentionally left empty, nothing to do.\n }\n}\n\n/**\n * Creates an exporter transport that uses `fetch` to send the data\n * @param parameters applied to each request made by transport\n */\nexport function createFetchTransport(\n parameters: FetchTransportParameters\n): IExporterTransport {\n return new FetchTransport(parameters);\n}\n\nfunction isFetchNetworkErrorRetryable(error: unknown): boolean {\n return error instanceof TypeError && !error.cause;\n}\n\n/**\n * Reads the response body to its end and discards it.\n *\n * Chromium gives the request's share of the keepalive quota back only once the\n * response body has been read to the end, and it skips the buffering consumer\n * that would otherwise drain the body on its own when the response carries a\n * `Cache-Control: no-store` header, which collectors commonly send. Leaving the\n * body unread then leaks the quota until the document goes away and every\n * following keepalive export stays pending forever. Cancelling the body is the\n * client-abort path and releases the quota far more slowly than reading it to\n * the end, so the body is read rather than cancelled.\n *\n * @see https://fetch.spec.whatwg.org/#fetch-processresponseendofbody\n */\nasync function drainResponseBody(response: Response): Promise<void> {\n try {\n // Empty and opaque responses have no body to read.\n const body = response.body;\n if (body == null) {\n return;\n }\n\n // Throws when the body is already locked to another reader, which happens\n // when the same response is handed to more than one export.\n const reader = body.getReader();\n try {\n // Chunks are dropped as they arrive: the payload is not used, and\n // buffering it - with `response.arrayBuffer()` for instance - would keep\n // a response of arbitrary size in memory.\n let chunk = await reader.read();\n while (!chunk.done) {\n chunk = await reader.read();\n }\n } finally {\n // The reader keeps the body locked until it is released, which would\n // make a later export handed the same response fail to acquire a reader\n // and skip the drain.\n reader.releaseLock();\n }\n } catch (error) {\n // The export outcome is decided by the response status, a body that cannot\n // be read must not change it.\n diag.debug(`error reading export response body: ${error}`);\n }\n}\n"],"mappings":";;;;;;;;;;;AAsBA,MAAM,0BAA0B;;;;;;;AAQhC,MAAM,yBAAyB;;;;;AAM/B,IAAI,kBAAkB;;;;AAKtB,IAAI,wBAAwB;AAO5B,IAAM,iBAAN,MAAmD;CACjD;CAEA,YAAY,YAAsC;EAChD,KAAK,cAAc;CACrB;CAEA,MAAM,KAAK,MAAkB,eAAgD;EAC3E,MAAM,kBAAkB,IAAI,gBAAgB;EAC5C,MAAM,UAAU,iBAAiB,gBAAgB,MAAM,GAAG,aAAa;EAMvE,IAAI,WAAW,WAAW;EAE1B,IAAI,OAAO,SAAS,eAAe,YAEjC,WAAW,SAAS;EAGtB,MAAM,cAAc,KAAK;EAIzB,MAAM,kBACJ,kBAAkB,cAAc;EAElC,MAAM,eAAe,CAAC,mBAAmB,EADhB,yBAAyB;EAGlD,IAAI,cAA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,OAAO,MACT,CAAC;IACH;GACF;GACA,mBAAA,KAAK,MAAM,kCAAkC,MAAM,EAAE;GACrD,OAAO;IACL,QAAQ;IACR,OAAO,IAAI,MAAM,yBAAyB,EAAE,OAAO,MAAM,CAAC;GAC5D;EACF,UAAU;GACR,aAAa,OAAO;GACpB,IAAI,cAAc;IAChB,mBAAmB;IACnB;GACF;EACF;CACF;CAEA,WAAW,CAEX;AACF;;;;;AAMA,SAAgB,qBACd,YACoB;CACpB,OAAO,IAAI,eAAe,UAAU;AACtC;AAEA,SAAS,6BAA6B,OAAyB;CAC7D,OAAO,iBAAiB,aAAa,CAAC,MAAM;AAC9C;;;;;;;;;;;;;;;AAgBA,eAAe,kBAAkB,UAAmC;CAClE,IAAI;EAEF,MAAM,OAAO,SAAS;EACtB,IAAI,QAAQ,MACV;EAKF,MAAM,SAAS,KAAK,UAAU;EAC9B,IAAI;GAIF,IAAI,QAAQ,MAAM,OAAO,KAAK;GAC9B,OAAO,CAAC,MAAM,MACZ,QAAQ,MAAM,OAAO,KAAK;EAE9B,UAAU;GAIR,OAAO,YAAY;EACrB;CACF,SAAS,OAAO;EAGd,mBAAA,KAAK,MAAM,uCAAuC,OAAO;CAC3D;AACF"}
1
+ {"version":3,"file":"fetch-transport.cjs","names":["suppressTracing","context","isExportHTTPErrorRetryable","parseRetryAfterToMills"],"sources":["../../src/transport/fetch-transport.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type { IExporterTransport } from '../exporter-transport';\nimport type { ExportResponse } from '../export-response';\nimport { context, diag } from '@opentelemetry/api';\nimport { suppressTracing } from '@opentelemetry/core';\nimport {\n isExportHTTPErrorRetryable,\n parseRetryAfterToMills,\n} from '../is-export-retryable';\nimport type { HeadersFactory } from '../configuration/otlp-http-configuration';\n\n/**\n * Maximum total body size for concurrent keepalive requests.\n * Browsers enforce a 64KiB cumulative limit across all pending keepalive requests.\n * We use 60KB to leave headroom for headers.\n * @see https://github.com/whatwg/fetch/issues/679\n * @see https://blog.huli.tw/2025/01/06/en/navigator-sendbeacon-64kib-and-source-code/\n */\nconst MAX_KEEPALIVE_BODY_SIZE = 60 * 1024;\n\n/**\n * Maximum concurrent keepalive requests.\n * Chrome enforces 9 concurrent keepalive fetch requests per renderer process.\n * @see https://github.com/whatwg/fetch/issues/679\n * Quote: \"If the renderer process is processing more than 9 requests with keepalive set, we reject a new request\"\n */\nconst MAX_KEEPALIVE_REQUESTS = 9;\n\n/**\n * Track cumulative body size charged against the keepalive budget.\n * This is necessary because the 64KiB limit is cumulative, not per-request.\n * Charged from request start until the response body drains, which is later\n * than the request completing: that is when the browser gives the quota back.\n */\nlet pendingBodySize = 0;\n\n/**\n * Track number of requests holding keepalive budget, on the same terms as\n * {@link pendingBodySize}.\n */\nlet pendingKeepaliveCount = 0;\n\nexport interface FetchTransportParameters {\n url: string;\n headers: HeadersFactory;\n}\n\nclass FetchTransport implements IExporterTransport {\n private _parameters: FetchTransportParameters;\n\n constructor(parameters: FetchTransportParameters) {\n this._parameters = parameters;\n }\n\n async send(data: Uint8Array, timeoutMillis: number): Promise<ExportResponse> {\n const abortController = new AbortController();\n const timeout = setTimeout(() => abortController.abort(), timeoutMillis);\n // Fetch API may be wrapped by an instrumentation like `@opentelemetry/instrumentation-fetch`.\n // In that case the instrumentation would create a new Span for this request,\n // creating an indirect endless loop Export -> Span -> Export.\n // `__original` lets the call bypass one wrapper; the suppressed context\n // below covers a `fetch` that was wrapped more than once.\n let fetchApi = globalThis.fetch;\n // @ts-expect-error -- fetch could be wrapped\n if (typeof fetchApi.__original === 'function') {\n // @ts-expect-error -- fetch could be wrapped\n fetchApi = fetchApi.__original;\n }\n\n const requestSize = data.byteLength;\n\n // Determine if we can use keepalive based on cumulative browser limits.\n // We must check BEFORE adding to pending totals to avoid exceeding limits.\n const wouldExceedSize =\n pendingBodySize + requestSize > MAX_KEEPALIVE_BODY_SIZE;\n const wouldExceedCount = pendingKeepaliveCount >= MAX_KEEPALIVE_REQUESTS;\n const useKeepalive = !wouldExceedSize && !wouldExceedCount;\n\n if (useKeepalive) {\n pendingBodySize += requestSize;\n pendingKeepaliveCount++;\n } else {\n const reason = wouldExceedSize ? 'size limit' : 'count limit';\n diag.debug(\n `keepalive disabled: ${(requestSize / 1024).toFixed(1)}KB payload, ${pendingKeepaliveCount} pending (${reason})`\n );\n }\n\n // Captured before the first `await`, which a synchronous context manager\n // would not survive.\n const suppressedContext = suppressTracing(context.active());\n\n // Idempotent so that a double call cannot drive the counters negative,\n // which would defeat the cap check above for every later request.\n let released = false;\n const release = () => {\n if (released) {\n return;\n }\n released = true;\n if (useKeepalive) {\n pendingBodySize -= requestSize;\n pendingKeepaliveCount--;\n }\n clearTimeout(timeout);\n };\n let drainOwnsCleanup = false;\n\n try {\n const url = new URL(this._parameters.url);\n // Resolve headers before entering the suppressed fetch context.\n const headers = await this._parameters.headers();\n const response = await context.with(suppressedContext, () =>\n fetchApi(url.href, {\n method: 'POST',\n headers,\n body: data,\n signal: abortController.signal,\n keepalive: useKeepalive,\n mode: globalThis.location\n ? globalThis.location.origin === url.origin\n ? 'same-origin'\n : 'cors'\n : 'no-cors',\n })\n );\n\n // Not awaited: the status already decides the export outcome, and a\n // collector that stalls mid-body must not hold up the caller.\n const drained = drainResponseBody(response);\n // Timer stays armed through the drain: an abort errors the body, so a\n // stalled drain still settles rather than holding the budget forever.\n // Released on rejection too, though the drain swallows its errors today:\n // a missed release would hold the keepalive budget for the page's life.\n void drained.then(release, release);\n // Set before any return, or `finally` frees the budget before it drains.\n drainOwnsCleanup = true;\n\n if (response.status >= 200 && response.status <= 299) {\n diag.debug(`export response success (status: ${response.status})`);\n return { status: 'success' };\n } else if (isExportHTTPErrorRetryable(response.status)) {\n diag.warn(`export response retryable (status: ${response.status})`);\n const retryAfter = response.headers.get('Retry-After');\n const retryInMillis = parseRetryAfterToMills(retryAfter);\n return { status: 'retryable', retryInMillis };\n }\n diag.error(`export response failure (status: ${response.status})`);\n return {\n status: 'failure',\n error: new Error(\n `Fetch request failed with non-retryable status ${response.status}`\n ),\n };\n } catch (error) {\n if (isFetchNetworkErrorRetryable(error)) {\n diag.warn(`export request retryable (network error: ${error})`);\n return {\n status: 'retryable',\n error: new Error('Fetch request encountered a network error', {\n cause: error,\n }),\n };\n }\n diag.error(`export request failure (error: ${error})`);\n return {\n status: 'failure',\n error: new Error('Fetch request errored', { cause: error }),\n };\n } finally {\n // Cleanup here only for a failure before the response arrived; past that\n // point the drain owns it.\n if (!drainOwnsCleanup) {\n release();\n }\n }\n }\n\n shutdown() {\n // Intentionally left empty, nothing to do.\n }\n}\n\n/**\n * Creates an exporter transport that uses `fetch` to send the data\n * @param parameters applied to each request made by transport\n */\nexport function createFetchTransport(\n parameters: FetchTransportParameters\n): IExporterTransport {\n return new FetchTransport(parameters);\n}\n\nfunction isFetchNetworkErrorRetryable(error: unknown): boolean {\n return error instanceof TypeError && !error.cause;\n}\n\n/**\n * Reads the response body to its end and discards it.\n *\n * Chromium gives the request's share of the keepalive quota back only once the\n * response body has been read to the end, and it skips the buffering consumer\n * that would otherwise drain the body on its own when the response carries a\n * `Cache-Control: no-store` header, which collectors commonly send. Leaving the\n * body unread then leaks the quota until the document goes away and every\n * following keepalive export stays pending forever. Cancelling the body is the\n * client-abort path and releases the quota far more slowly than reading it to\n * the end, so the body is read rather than cancelled.\n *\n * Never rejects. Its settlement, not its outcome, is what releases the\n * request's keepalive budget, so a caller that ignores it leaks that budget\n * for the life of the document.\n *\n * @see https://fetch.spec.whatwg.org/#fetch-processresponseendofbody\n * @see https://issues.chromium.org/issues/546438373\n */\nasync function drainResponseBody(response: Response): Promise<void> {\n try {\n // Empty and opaque responses have no body to read.\n const body = response.body;\n if (body == null) {\n return;\n }\n\n // Throws when the body is already locked to another reader, which happens\n // when the same response is handed to more than one export.\n const reader = body.getReader();\n try {\n // Chunks are dropped as they arrive: the payload is not used, and\n // buffering it - with `response.arrayBuffer()` for instance - would keep\n // a response of arbitrary size in memory.\n let chunk = await reader.read();\n while (!chunk.done) {\n chunk = await reader.read();\n }\n } finally {\n // The reader keeps the body locked until it is released, which would\n // make a later export handed the same response fail to acquire a reader\n // and skip the drain.\n reader.releaseLock();\n }\n } catch (error) {\n // The export outcome is decided by the response status, a body that cannot\n // be read must not change it.\n diag.debug(`error reading export response body: ${error}`);\n }\n}\n"],"mappings":";;;;;;;;;;;AAsBA,MAAM,0BAA0B;;;;;;;AAQhC,MAAM,yBAAyB;;;;;;;AAQ/B,IAAI,kBAAkB;;;;;AAMtB,IAAI,wBAAwB;AAO5B,IAAM,iBAAN,MAAmD;CACjD;CAEA,YAAY,YAAsC;EAChD,KAAK,cAAc;CACrB;CAEA,MAAM,KAAK,MAAkB,eAAgD;EAC3E,MAAM,kBAAkB,IAAI,gBAAgB;EAC5C,MAAM,UAAU,iBAAiB,gBAAgB,MAAM,GAAG,aAAa;EAMvE,IAAI,WAAW,WAAW;EAE1B,IAAI,OAAO,SAAS,eAAe,YAEjC,WAAW,SAAS;EAGtB,MAAM,cAAc,KAAK;EAIzB,MAAM,kBACJ,kBAAkB,cAAc;EAElC,MAAM,eAAe,CAAC,mBAAmB,EADhB,yBAAyB;EAGlD,IAAI,cAAc;GAChB,mBAAmB;GACnB;EACF,OAAO;GACL,MAAM,SAAS,kBAAkB,eAAe;GAChD,mBAAA,KAAK,MACH,wBAAwB,cAAc,KAAA,CAAM,QAAQ,CAAC,EAAE,cAAc,sBAAsB,YAAY,OAAO,EAChH;EACF;EAIA,MAAM,qBAAA,GAAoBA,oBAAAA,gBAAAA,CAAgBC,mBAAAA,QAAQ,OAAO,CAAC;EAI1D,IAAI,WAAW;EACf,MAAM,gBAAgB;GACpB,IAAI,UACF;GAEF,WAAW;GACX,IAAI,cAAc;IAChB,mBAAmB;IACnB;GACF;GACA,aAAa,OAAO;EACtB;EACA,IAAI,mBAAmB;EAEvB,IAAI;GACF,MAAM,MAAM,IAAI,IAAI,KAAK,YAAY,GAAG;GAExC,MAAM,UAA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AU;AACtC;AAEA,SAAS,6BAA6B,OAAyB;CAC7D,OAAO,iBAAiB,aAAa,CAAC,MAAM;AAC9C;;;;;;;;;;;;;;;;;;;;AAqBA,eAAe,kBAAkB,UAAmC;CAClE,IAAI;EAEF,MAAM,OAAO,SAAS;EACtB,IAAI,QAAQ,MACV;EAKF,MAAM,SAAS,KAAK,UAAU;EAC9B,IAAI;GAIF,IAAI,QAAQ,MAAM,OAAO,KAAK;GAC9B,OAAO,CAAC,MAAM,MACZ,QAAQ,MAAM,OAAO,KAAK;EAE9B,UAAU;GAIR,OAAO,YAAY;EACrB;CACF,SAAS,OAAO;EAGd,mBAAA,KAAK,MAAM,uCAAuC,OAAO;CAC3D;AACF"}
@@ -18,12 +18,15 @@ const MAX_KEEPALIVE_BODY_SIZE = 61440;
18
18
  */
19
19
  const MAX_KEEPALIVE_REQUESTS = 9;
20
20
  /**
21
- * Track cumulative pending body size across all in-flight keepalive requests.
21
+ * Track cumulative body size charged against the keepalive budget.
22
22
  * This is necessary because the 64KiB limit is cumulative, not per-request.
23
+ * Charged from request start until the response body drains, which is later
24
+ * than the request completing: that is when the browser gives the quota back.
23
25
  */
24
26
  let pendingBodySize = 0;
25
27
  /**
26
- * Track number of pending keepalive requests.
28
+ * Track number of requests holding keepalive budget, on the same terms as
29
+ * {@link pendingBodySize}.
27
30
  */
28
31
  let pendingKeepaliveCount = 0;
29
32
  var FetchTransport = class {
@@ -47,6 +50,17 @@ var FetchTransport = class {
47
50
  diag.debug(`keepalive disabled: ${(requestSize / 1024).toFixed(1)}KB payload, ${pendingKeepaliveCount} pending (${reason})`);
48
51
  }
49
52
  const suppressedContext = suppressTracing(context.active());
53
+ let released = false;
54
+ const release = () => {
55
+ if (released) return;
56
+ released = true;
57
+ if (useKeepalive) {
58
+ pendingBodySize -= requestSize;
59
+ pendingKeepaliveCount--;
60
+ }
61
+ clearTimeout(timeout);
62
+ };
63
+ let drainOwnsCleanup = false;
50
64
  try {
51
65
  const url = new URL(this._parameters.url);
52
66
  const headers = await this._parameters.headers();
@@ -58,7 +72,8 @@ var FetchTransport = class {
58
72
  keepalive: useKeepalive,
59
73
  mode: globalThis.location ? globalThis.location.origin === url.origin ? "same-origin" : "cors" : "no-cors"
60
74
  }));
61
- await drainResponseBody(response);
75
+ drainResponseBody(response).then(release, release);
76
+ drainOwnsCleanup = true;
62
77
  if (response.status >= 200 && response.status <= 299) {
63
78
  diag.debug(`export response success (status: ${response.status})`);
64
79
  return { status: "success" };
@@ -89,11 +104,7 @@ var FetchTransport = class {
89
104
  error: new Error("Fetch request errored", { cause: error })
90
105
  };
91
106
  } finally {
92
- clearTimeout(timeout);
93
- if (useKeepalive) {
94
- pendingBodySize -= requestSize;
95
- pendingKeepaliveCount--;
96
- }
107
+ if (!drainOwnsCleanup) release();
97
108
  }
98
109
  }
99
110
  shutdown() {}
@@ -120,7 +131,12 @@ function isFetchNetworkErrorRetryable(error) {
120
131
  * client-abort path and releases the quota far more slowly than reading it to
121
132
  * the end, so the body is read rather than cancelled.
122
133
  *
134
+ * Never rejects. Its settlement, not its outcome, is what releases the
135
+ * request's keepalive budget, so a caller that ignores it leaks that budget
136
+ * for the life of the document.
137
+ *
123
138
  * @see https://fetch.spec.whatwg.org/#fetch-processresponseendofbody
139
+ * @see https://issues.chromium.org/issues/546438373
124
140
  */
125
141
  async function drainResponseBody(response) {
126
142
  try {
@@ -1 +1 @@
1
- {"version":3,"file":"fetch-transport.mjs","names":[],"sources":["../../src/transport/fetch-transport.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type { IExporterTransport } from '../exporter-transport';\nimport type { ExportResponse } from '../export-response';\nimport { context, diag } from '@opentelemetry/api';\nimport { suppressTracing } from '@opentelemetry/core';\nimport {\n isExportHTTPErrorRetryable,\n parseRetryAfterToMills,\n} from '../is-export-retryable';\nimport type { HeadersFactory } from '../configuration/otlp-http-configuration';\n\n/**\n * Maximum total body size for concurrent keepalive requests.\n * Browsers enforce a 64KiB cumulative limit across all pending keepalive requests.\n * We use 60KB to leave headroom for headers.\n * @see https://github.com/whatwg/fetch/issues/679\n * @see https://blog.huli.tw/2025/01/06/en/navigator-sendbeacon-64kib-and-source-code/\n */\nconst MAX_KEEPALIVE_BODY_SIZE = 60 * 1024;\n\n/**\n * Maximum concurrent keepalive requests.\n * Chrome enforces 9 concurrent keepalive fetch requests per renderer process.\n * @see https://github.com/whatwg/fetch/issues/679\n * Quote: \"If the renderer process is processing more than 9 requests with keepalive set, we reject a new request\"\n */\nconst MAX_KEEPALIVE_REQUESTS = 9;\n\n/**\n * Track cumulative pending body size across all in-flight keepalive requests.\n * This is necessary because the 64KiB limit is cumulative, not per-request.\n */\nlet pendingBodySize = 0;\n\n/**\n * Track number of pending keepalive requests.\n */\nlet pendingKeepaliveCount = 0;\n\nexport interface FetchTransportParameters {\n url: string;\n headers: HeadersFactory;\n}\n\nclass FetchTransport implements IExporterTransport {\n private _parameters: FetchTransportParameters;\n\n constructor(parameters: FetchTransportParameters) {\n this._parameters = parameters;\n }\n\n async send(data: Uint8Array, timeoutMillis: number): Promise<ExportResponse> {\n const abortController = new AbortController();\n const timeout = setTimeout(() => abortController.abort(), timeoutMillis);\n // Fetch API may be wrapped by an instrumentation like `@opentelemetry/instrumentation-fetch`.\n // In that case the instrumentation would create a new Span for this request,\n // creating an indirect endless loop Export -> Span -> Export.\n // `__original` lets the call bypass one wrapper; the suppressed context\n // below covers a `fetch` that was wrapped more than once.\n let fetchApi = globalThis.fetch;\n // @ts-expect-error -- fetch could be wrapped\n if (typeof fetchApi.__original === 'function') {\n // @ts-expect-error -- fetch could be wrapped\n fetchApi = fetchApi.__original;\n }\n\n const requestSize = data.byteLength;\n\n // Determine if we can use keepalive based on cumulative browser limits.\n // We must check BEFORE adding to pending totals to avoid exceeding limits.\n const wouldExceedSize =\n pendingBodySize + requestSize > MAX_KEEPALIVE_BODY_SIZE;\n const wouldExceedCount = pendingKeepaliveCount >= MAX_KEEPALIVE_REQUESTS;\n const useKeepalive = !wouldExceedSize && !wouldExceedCount;\n\n if (useKeepalive) {\n pendingBodySize += requestSize;\n pendingKeepaliveCount++;\n } else {\n const reason = wouldExceedSize ? 'size limit' : 'count limit';\n diag.debug(\n `keepalive disabled: ${(requestSize / 1024).toFixed(1)}KB payload, ${pendingKeepaliveCount} pending (${reason})`\n );\n }\n\n // Captured before the first `await`, which a synchronous context manager\n // would not survive.\n const suppressedContext = suppressTracing(context.active());\n\n try {\n const url = new URL(this._parameters.url);\n // Resolve headers before entering the suppressed fetch context.\n const headers = await this._parameters.headers();\n const response = await context.with(suppressedContext, () =>\n fetchApi(url.href, {\n method: 'POST',\n headers,\n body: data,\n signal: abortController.signal,\n keepalive: useKeepalive,\n mode: globalThis.location\n ? globalThis.location.origin === url.origin\n ? 'same-origin'\n : 'cors'\n : 'no-cors',\n })\n );\n\n await drainResponseBody(response);\n\n if (response.status >= 200 && response.status <= 299) {\n diag.debug(`export response success (status: ${response.status})`);\n return { status: 'success' };\n } else if (isExportHTTPErrorRetryable(response.status)) {\n diag.warn(`export response retryable (status: ${response.status})`);\n const retryAfter = response.headers.get('Retry-After');\n const retryInMillis = parseRetryAfterToMills(retryAfter);\n return { status: 'retryable', retryInMillis };\n }\n diag.error(`export response failure (status: ${response.status})`);\n return {\n status: 'failure',\n error: new Error(\n `Fetch request failed with non-retryable status ${response.status}`\n ),\n };\n } catch (error) {\n if (isFetchNetworkErrorRetryable(error)) {\n diag.warn(`export request retryable (network error: ${error})`);\n return {\n status: 'retryable',\n error: new Error('Fetch request encountered a network error', {\n cause: error,\n }),\n };\n }\n diag.error(`export request failure (error: ${error})`);\n return {\n status: 'failure',\n error: new Error('Fetch request errored', { cause: error }),\n };\n } finally {\n clearTimeout(timeout);\n if (useKeepalive) {\n pendingBodySize -= requestSize;\n pendingKeepaliveCount--;\n }\n }\n }\n\n shutdown() {\n // Intentionally left empty, nothing to do.\n }\n}\n\n/**\n * Creates an exporter transport that uses `fetch` to send the data\n * @param parameters applied to each request made by transport\n */\nexport function createFetchTransport(\n parameters: FetchTransportParameters\n): IExporterTransport {\n return new FetchTransport(parameters);\n}\n\nfunction isFetchNetworkErrorRetryable(error: unknown): boolean {\n return error instanceof TypeError && !error.cause;\n}\n\n/**\n * Reads the response body to its end and discards it.\n *\n * Chromium gives the request's share of the keepalive quota back only once the\n * response body has been read to the end, and it skips the buffering consumer\n * that would otherwise drain the body on its own when the response carries a\n * `Cache-Control: no-store` header, which collectors commonly send. Leaving the\n * body unread then leaks the quota until the document goes away and every\n * following keepalive export stays pending forever. Cancelling the body is the\n * client-abort path and releases the quota far more slowly than reading it to\n * the end, so the body is read rather than cancelled.\n *\n * @see https://fetch.spec.whatwg.org/#fetch-processresponseendofbody\n */\nasync function drainResponseBody(response: Response): Promise<void> {\n try {\n // Empty and opaque responses have no body to read.\n const body = response.body;\n if (body == null) {\n return;\n }\n\n // Throws when the body is already locked to another reader, which happens\n // when the same response is handed to more than one export.\n const reader = body.getReader();\n try {\n // Chunks are dropped as they arrive: the payload is not used, and\n // buffering it - with `response.arrayBuffer()` for instance - would keep\n // a response of arbitrary size in memory.\n let chunk = await reader.read();\n while (!chunk.done) {\n chunk = await reader.read();\n }\n } finally {\n // The reader keeps the body locked until it is released, which would\n // make a later export handed the same response fail to acquire a reader\n // and skip the drain.\n reader.releaseLock();\n }\n } catch (error) {\n // The export outcome is decided by the response status, a body that cannot\n // be read must not change it.\n diag.debug(`error reading export response body: ${error}`);\n }\n}\n"],"mappings":";;;;;;;;;;;AAsBA,MAAM,0BAA0B;;;;;;;AAQhC,MAAM,yBAAyB;;;;;AAM/B,IAAI,kBAAkB;;;;AAKtB,IAAI,wBAAwB;AAO5B,IAAM,iBAAN,MAAmD;CACjD;CAEA,YAAY,YAAsC;EAChD,KAAK,cAAc;CACrB;CAEA,MAAM,KAAK,MAAkB,eAAgD;EAC3E,MAAM,kBAAkB,IAAI,gBAAgB;EAC5C,MAAM,UAAU,iBAAiB,gBAAgB,MAA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6B,KAAK,GAAG;IACvC,KAAK,KAAK,4CAA4C,MAAM,EAAE;IAC9D,OAAO;KACL,QAAQ;KACR,OAAO,IAAI,MAAM,6CAA6C,EAC5D,OAAO,MACT,CAAC;IACH;GACF;GACA,KAAK,MAAM,kCAAkC,MAAM,EAAE;GACrD,OAAO;IACL,QAAQ;IACR,OAAO,IAAI,MAAM,yBAAyB,EAAE,OAAO,MAAM,CAAC;GAC5D;EACF,UAAU;GACR,aAAa,OAAO;GACpB,IAAI,cAAc;IAChB,mBAAmB;IACnB;GACF;EACF;CACF;CAEA,WAAW,CAEX;AACF;;;;;AAMA,SAAgB,qBACd,YACoB;CACpB,OAAO,IAAI,eAAe,UAAU;AACtC;AAEA,SAAS,6BAA6B,OAAyB;CAC7D,OAAO,iBAAiB,aAAa,CAAC,MAAM;AAC9C;;;;;;;;;;;;;;;AAgBA,eAAe,kBAAkB,UAAmC;CAClE,IAAI;EAEF,MAAM,OAAO,SAAS;EACtB,IAAI,QAAQ,MACV;EAKF,MAAM,SAAS,KAAK,UAAU;EAC9B,IAAI;GAIF,IAAI,QAAQ,MAAM,OAAO,KAAK;GAC9B,OAAO,CAAC,MAAM,MACZ,QAAQ,MAAM,OAAO,KAAK;EAE9B,UAAU;GAIR,OAAO,YAAY;EACrB;CACF,SAAS,OAAO;EAGd,KAAK,MAAM,uCAAuC,OAAO;CAC3D;AACF"}
1
+ {"version":3,"file":"fetch-transport.mjs","names":[],"sources":["../../src/transport/fetch-transport.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type { IExporterTransport } from '../exporter-transport';\nimport type { ExportResponse } from '../export-response';\nimport { context, diag } from '@opentelemetry/api';\nimport { suppressTracing } from '@opentelemetry/core';\nimport {\n isExportHTTPErrorRetryable,\n parseRetryAfterToMills,\n} from '../is-export-retryable';\nimport type { HeadersFactory } from '../configuration/otlp-http-configuration';\n\n/**\n * Maximum total body size for concurrent keepalive requests.\n * Browsers enforce a 64KiB cumulative limit across all pending keepalive requests.\n * We use 60KB to leave headroom for headers.\n * @see https://github.com/whatwg/fetch/issues/679\n * @see https://blog.huli.tw/2025/01/06/en/navigator-sendbeacon-64kib-and-source-code/\n */\nconst MAX_KEEPALIVE_BODY_SIZE = 60 * 1024;\n\n/**\n * Maximum concurrent keepalive requests.\n * Chrome enforces 9 concurrent keepalive fetch requests per renderer process.\n * @see https://github.com/whatwg/fetch/issues/679\n * Quote: \"If the renderer process is processing more than 9 requests with keepalive set, we reject a new request\"\n */\nconst MAX_KEEPALIVE_REQUESTS = 9;\n\n/**\n * Track cumulative body size charged against the keepalive budget.\n * This is necessary because the 64KiB limit is cumulative, not per-request.\n * Charged from request start until the response body drains, which is later\n * than the request completing: that is when the browser gives the quota back.\n */\nlet pendingBodySize = 0;\n\n/**\n * Track number of requests holding keepalive budget, on the same terms as\n * {@link pendingBodySize}.\n */\nlet pendingKeepaliveCount = 0;\n\nexport interface FetchTransportParameters {\n url: string;\n headers: HeadersFactory;\n}\n\nclass FetchTransport implements IExporterTransport {\n private _parameters: FetchTransportParameters;\n\n constructor(parameters: FetchTransportParameters) {\n this._parameters = parameters;\n }\n\n async send(data: Uint8Array, timeoutMillis: number): Promise<ExportResponse> {\n const abortController = new AbortController();\n const timeout = setTimeout(() => abortController.abort(), timeoutMillis);\n // Fetch API may be wrapped by an instrumentation like `@opentelemetry/instrumentation-fetch`.\n // In that case the instrumentation would create a new Span for this request,\n // creating an indirect endless loop Export -> Span -> Export.\n // `__original` lets the call bypass one wrapper; the suppressed context\n // below covers a `fetch` that was wrapped more than once.\n let fetchApi = globalThis.fetch;\n // @ts-expect-error -- fetch could be wrapped\n if (typeof fetchApi.__original === 'function') {\n // @ts-expect-error -- fetch could be wrapped\n fetchApi = fetchApi.__original;\n }\n\n const requestSize = data.byteLength;\n\n // Determine if we can use keepalive based on cumulative browser limits.\n // We must check BEFORE adding to pending totals to avoid exceeding limits.\n const wouldExceedSize =\n pendingBodySize + requestSize > MAX_KEEPALIVE_BODY_SIZE;\n const wouldExceedCount = pendingKeepaliveCount >= MAX_KEEPALIVE_REQUESTS;\n const useKeepalive = !wouldExceedSize && !wouldExceedCount;\n\n if (useKeepalive) {\n pendingBodySize += requestSize;\n pendingKeepaliveCount++;\n } else {\n const reason = wouldExceedSize ? 'size limit' : 'count limit';\n diag.debug(\n `keepalive disabled: ${(requestSize / 1024).toFixed(1)}KB payload, ${pendingKeepaliveCount} pending (${reason})`\n );\n }\n\n // Captured before the first `await`, which a synchronous context manager\n // would not survive.\n const suppressedContext = suppressTracing(context.active());\n\n // Idempotent so that a double call cannot drive the counters negative,\n // which would defeat the cap check above for every later request.\n let released = false;\n const release = () => {\n if (released) {\n return;\n }\n released = true;\n if (useKeepalive) {\n pendingBodySize -= requestSize;\n pendingKeepaliveCount--;\n }\n clearTimeout(timeout);\n };\n let drainOwnsCleanup = false;\n\n try {\n const url = new URL(this._parameters.url);\n // Resolve headers before entering the suppressed fetch context.\n const headers = await this._parameters.headers();\n const response = await context.with(suppressedContext, () =>\n fetchApi(url.href, {\n method: 'POST',\n headers,\n body: data,\n signal: abortController.signal,\n keepalive: useKeepalive,\n mode: globalThis.location\n ? globalThis.location.origin === url.origin\n ? 'same-origin'\n : 'cors'\n : 'no-cors',\n })\n );\n\n // Not awaited: the status already decides the export outcome, and a\n // collector that stalls mid-body must not hold up the caller.\n const drained = drainResponseBody(response);\n // Timer stays armed through the drain: an abort errors the body, so a\n // stalled drain still settles rather than holding the budget forever.\n // Released on rejection too, though the drain swallows its errors today:\n // a missed release would hold the keepalive budget for the page's life.\n void drained.then(release, release);\n // Set before any return, or `finally` frees the budget before it drains.\n drainOwnsCleanup = true;\n\n if (response.status >= 200 && response.status <= 299) {\n diag.debug(`export response success (status: ${response.status})`);\n return { status: 'success' };\n } else if (isExportHTTPErrorRetryable(response.status)) {\n diag.warn(`export response retryable (status: ${response.status})`);\n const retryAfter = response.headers.get('Retry-After');\n const retryInMillis = parseRetryAfterToMills(retryAfter);\n return { status: 'retryable', retryInMillis };\n }\n diag.error(`export response failure (status: ${response.status})`);\n return {\n status: 'failure',\n error: new Error(\n `Fetch request failed with non-retryable status ${response.status}`\n ),\n };\n } catch (error) {\n if (isFetchNetworkErrorRetryable(error)) {\n diag.warn(`export request retryable (network error: ${error})`);\n return {\n status: 'retryable',\n error: new Error('Fetch request encountered a network error', {\n cause: error,\n }),\n };\n }\n diag.error(`export request failure (error: ${error})`);\n return {\n status: 'failure',\n error: new Error('Fetch request errored', { cause: error }),\n };\n } finally {\n // Cleanup here only for a failure before the response arrived; past that\n // point the drain owns it.\n if (!drainOwnsCleanup) {\n release();\n }\n }\n }\n\n shutdown() {\n // Intentionally left empty, nothing to do.\n }\n}\n\n/**\n * Creates an exporter transport that uses `fetch` to send the data\n * @param parameters applied to each request made by transport\n */\nexport function createFetchTransport(\n parameters: FetchTransportParameters\n): IExporterTransport {\n return new FetchTransport(parameters);\n}\n\nfunction isFetchNetworkErrorRetryable(error: unknown): boolean {\n return error instanceof TypeError && !error.cause;\n}\n\n/**\n * Reads the response body to its end and discards it.\n *\n * Chromium gives the request's share of the keepalive quota back only once the\n * response body has been read to the end, and it skips the buffering consumer\n * that would otherwise drain the body on its own when the response carries a\n * `Cache-Control: no-store` header, which collectors commonly send. Leaving the\n * body unread then leaks the quota until the document goes away and every\n * following keepalive export stays pending forever. Cancelling the body is the\n * client-abort path and releases the quota far more slowly than reading it to\n * the end, so the body is read rather than cancelled.\n *\n * Never rejects. Its settlement, not its outcome, is what releases the\n * request's keepalive budget, so a caller that ignores it leaks that budget\n * for the life of the document.\n *\n * @see https://fetch.spec.whatwg.org/#fetch-processresponseendofbody\n * @see https://issues.chromium.org/issues/546438373\n */\nasync function drainResponseBody(response: Response): Promise<void> {\n try {\n // Empty and opaque responses have no body to read.\n const body = response.body;\n if (body == null) {\n return;\n }\n\n // Throws when the body is already locked to another reader, which happens\n // when the same response is handed to more than one export.\n const reader = body.getReader();\n try {\n // Chunks are dropped as they arrive: the payload is not used, and\n // buffering it - with `response.arrayBuffer()` for instance - would keep\n // a response of arbitrary size in memory.\n let chunk = await reader.read();\n while (!chunk.done) {\n chunk = await reader.read();\n }\n } finally {\n // The reader keeps the body locked until it is released, which would\n // make a later export handed the same response fail to acquire a reader\n // and skip the drain.\n reader.releaseLock();\n }\n } catch (error) {\n // The export outcome is decided by the response status, a body that cannot\n // be read must not change it.\n diag.debug(`error reading export response body: ${error}`);\n }\n}\n"],"mappings":";;;;;;;;;;;AAsBA,MAAM,0BAA0B;;;;;;;AAQhC,MAAM,yBAAyB;;;;;;;AAQ/B,IAAI,kBAAkB;;;;;AAMtB,IAAI,wBAAwB;AAO5B,IAAM,iBAAN,MAAmD;CACjD;CAEA,YAAY,YAAsC;EAChD,KAAK,cAAc;CACrB;CAEA,MAAM,KAAK,MAAkB,eAAgD;EAC3E,MAAM,kBAAkB,IAAI,gBAAgB;EAC5C,MAAM,UAAU,iBAAiB,gBAAgB,MAAM,GAAG,aAAa;EAMvE,IAAI,WAAW,WAAW;EAE1B,IAAI,OAAO,SAAS,eAAe,YAEjC,WAAW,SAAS;EAGtB,MAAM,cAAc,KAAK;EAIzB,MAAM,kBACJ,kBAAkB,cAAc;EAElC,MAAM,eAAe,CAAC,mBAAmB,EADhB,yBAAyB;EAGlD,IAAI,cAAc;GAChB,mBAAmB;GACnB;EACF,OAAO;GACL,MAAM,SAAS,kBAAkB,eAAe;GAChD,KAAK,MACH,wBAAwB,cAAc,KAAA,CAAM,QAAQ,CAAC,EAAE,cAAc,sBAAsB,YAAY,OAAO,EAChH;EACF;EAIA,MAAM,oBAAoB,gBAAgB,QAAQ,OAAO,CAAC;EAI1D,IAAI,WAAW;EACf,MAAM,gBAAgB;GACpB,IAAI,UACF;GAEF,WAAW;GACX,IAAI,cAAc;IAChB,mBAAmB;IACnB;GACF;GACA,aAAa,OAAO;EACtB;EACA,IAAI,mBAAmB;EAEvB,IAAI;GACF,MAAM,MAAM,IAAI,IAAI,KAAK,YAAY,GA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AAO,iBAAiB,aAAa,CAAC,MAAM;AAC9C;;;;;;;;;;;;;;;;;;;;AAqBA,eAAe,kBAAkB,UAAmC;CAClE,IAAI;EAEF,MAAM,OAAO,SAAS;EACtB,IAAI,QAAQ,MACV;EAKF,MAAM,SAAS,KAAK,UAAU;EAC9B,IAAI;GAIF,IAAI,QAAQ,MAAM,OAAO,KAAK;GAC9B,OAAO,CAAC,MAAM,MACZ,QAAQ,MAAM,OAAO,KAAK;EAE9B,UAAU;GAIR,OAAO,YAAY;EACrB;CACF,SAAS,OAAO;EAGd,KAAK,MAAM,uCAAuC,OAAO;CAC3D;AACF"}
package/dist/version.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region src/version.ts
2
- const VERSION = "0.300.0-development.0";
2
+ const VERSION = "0.300.0-development.1";
3
3
  //#endregion
4
4
  exports.VERSION = VERSION;
5
5
 
@@ -1 +1 @@
1
- {"version":3,"file":"version.cjs","names":[],"sources":["../src/version.ts"],"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.300.0-development.0';\n"],"mappings":";AAMA,MAAa,UAAU"}
1
+ {"version":3,"file":"version.cjs","names":[],"sources":["../src/version.ts"],"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.300.0-development.1';\n"],"mappings":";AAMA,MAAa,UAAU"}
package/dist/version.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region src/version.ts
2
- const VERSION = "0.300.0-development.0";
2
+ const VERSION = "0.300.0-development.1";
3
3
  //#endregion
4
4
  export { VERSION };
5
5
 
@@ -1 +1 @@
1
- {"version":3,"file":"version.mjs","names":[],"sources":["../src/version.ts"],"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.300.0-development.0';\n"],"mappings":";AAMA,MAAa,UAAU"}
1
+ {"version":3,"file":"version.mjs","names":[],"sources":["../src/version.ts"],"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.300.0-development.1';\n"],"mappings":";AAMA,MAAa,UAAU"}
package/package.json CHANGED
@@ -1,39 +1,38 @@
1
1
  {
2
2
  "name": "@opentelemetry/otlp-exporter-base",
3
- "version": "0.300.0-development.0",
3
+ "version": "0.300.0-development.1",
4
4
  "description": "OpenTelemetry OTLP Exporter base (for internal use only)",
5
5
  "main": "./dist/index.cjs",
6
- "module": "./dist/index.mjs",
7
6
  "types": "./dist/index.d.cts",
8
7
  "exports": {
9
8
  ".": {
10
- "import": {
11
- "types": "./dist/index.d.mts",
12
- "default": "./dist/index.mjs"
13
- },
14
9
  "require": {
15
10
  "types": "./dist/index.d.cts",
16
11
  "default": "./dist/index.cjs"
12
+ },
13
+ "default": {
14
+ "types": "./dist/index.d.mts",
15
+ "default": "./dist/index.mjs"
17
16
  }
18
17
  },
19
18
  "./node-http": {
20
- "import": {
21
- "types": "./dist/index-node-http.d.mts",
22
- "default": "./dist/index-node-http.mjs"
23
- },
24
19
  "require": {
25
20
  "types": "./dist/index-node-http.d.cts",
26
21
  "default": "./dist/index-node-http.cjs"
22
+ },
23
+ "default": {
24
+ "types": "./dist/index-node-http.d.mts",
25
+ "default": "./dist/index-node-http.mjs"
27
26
  }
28
27
  },
29
28
  "./browser-http": {
30
- "import": {
31
- "types": "./dist/index-browser-http.d.mts",
32
- "default": "./dist/index-browser-http.mjs"
33
- },
34
29
  "require": {
35
30
  "types": "./dist/index-browser-http.d.cts",
36
31
  "default": "./dist/index-browser-http.cjs"
32
+ },
33
+ "default": {
34
+ "types": "./dist/index-browser-http.d.mts",
35
+ "default": "./dist/index-browser-http.mjs"
37
36
  }
38
37
  }
39
38
  },
@@ -76,14 +75,14 @@
76
75
  "access": "public"
77
76
  },
78
77
  "dependencies": {
79
- "@opentelemetry/core": "3.0.0-development.0",
80
- "@opentelemetry/otlp-transformer": "0.300.0-development.0"
78
+ "@opentelemetry/core": "3.0.0-development.1",
79
+ "@opentelemetry/otlp-transformer": "0.300.0-development.1"
81
80
  },
82
81
  "devDependencies": {
83
82
  "@babel/core": "7.29.7",
84
83
  "@babel/preset-env": "7.29.7",
85
- "@opentelemetry/api": "1.9.1",
86
- "@opentelemetry/sdk-trace": "3.0.0-development.0",
84
+ "@opentelemetry/api": "1.10.0-development.1",
85
+ "@opentelemetry/sdk-trace": "3.0.0-development.1",
87
86
  "@types/mocha": "10.0.10",
88
87
  "@types/node": "18.19.130",
89
88
  "@types/sinon": "17.0.4",
@@ -95,7 +94,7 @@
95
94
  "karma-spec-reporter": "0.0.37",
96
95
  "karma-webpack": "5.0.1",
97
96
  "mocha": "11.8.0",
98
- "nyc": "17.1.0",
97
+ "nyc": "18.0.0",
99
98
  "sinon": "18.0.1",
100
99
  "ts-loader": "9.5.7",
101
100
  "typescript": "5.2.2",
@@ -103,9 +102,9 @@
103
102
  "webpack-cli": "7.2.3"
104
103
  },
105
104
  "peerDependencies": {
106
- "@opentelemetry/api": "^1.3.0"
105
+ "@opentelemetry/api": "^1.3.0 || 1.10.0-development.1"
107
106
  },
108
107
  "homepage": "https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/otlp-exporter-base",
109
108
  "sideEffects": false,
110
- "gitHead": "ad3499bf70ee996f5565e5cd00f2c5fcb2e87042"
109
+ "gitHead": "83d76963961161511591d5585407d23b5f035a37"
111
110
  }