diff --git a/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/types.ts b/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/types.ts index 430e9e217..8014e71e9 100644 --- a/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/types.ts +++ b/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/types.ts @@ -63,7 +63,7 @@ export interface XhrMem { entries: PerformanceResourceTiming[]; }; // callback to remove events from xhr once the span ends - callbackToRemoveEvents?: Function; + callbackToRemoveEvents?: () => void; } export type PropagateTraceHeaderCorsUrl = string | RegExp; diff --git a/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/utils.ts b/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/utils.ts index c15e84efd..b4884bb89 100644 --- a/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/utils.ts +++ b/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/utils.ts @@ -24,6 +24,10 @@ const DIAG_LOGGER = api.diag.createComponentLogger({ '@opentelemetry/opentelemetry-instrumentation-xml-http-request/utils', }); +function isDocument(value: unknown): value is Document { + return typeof Document !== 'undefined' && value instanceof Document; +} + /** * Helper function to determine payload content length for XHR requests * @param body @@ -32,17 +36,17 @@ const DIAG_LOGGER = api.diag.createComponentLogger({ export function getXHRBodyLength( body: Document | XMLHttpRequestBodyInit ): number | undefined { - if (typeof Document !== 'undefined' && body instanceof Document) { + if (isDocument(body)) { return new XMLSerializer().serializeToString(document).length; } + // XMLHttpRequestBodyInit expands to the following: - if (body instanceof Blob) { - return body.size; + if (typeof body === 'string') { + return getByteLength(body); } - // ArrayBuffer | ArrayBufferView - if ((body as any).byteLength !== undefined) { - return (body as any).byteLength as number; + if (body instanceof Blob) { + return body.size; } if (body instanceof FormData) { @@ -53,8 +57,9 @@ export function getXHRBodyLength( return getByteLength(body.toString()); } - if (typeof body === 'string') { - return getByteLength(body); + // ArrayBuffer | ArrayBufferView + if (body.byteLength !== undefined) { + return body.byteLength; } DIAG_LOGGER.warn('unknown body type'); diff --git a/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts b/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts index 823d6f942..e2f74d48b 100644 --- a/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts @@ -66,7 +66,7 @@ const XHR_TIMEOUT = 2000; const getData = ( req: XMLHttpRequest, url: string, - callbackAfterSend: Function, + callbackAfterSend: () => void, async?: boolean ) => { // eslint-disable-next-line no-async-promise-executor @@ -101,7 +101,7 @@ const postData = ( req: XMLHttpRequest, url: string, data: Document | XMLHttpRequestBodyInit, - callbackAfterSend: Function, + callbackAfterSend: () => void, async?: boolean ) => { // eslint-disable-next-line no-async-promise-executor