@opentelemetry/instrumentation-xml-http-request
Version:
OpenTelemetry instrumentation for XMLHttpRequest http client in web browsers
75 lines • 2.46 kB
JavaScript
;
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getXHRBodyLength = void 0;
// Much of the logic here overlaps with the same utils file in opentelemetry-instrumentation-fetch
// These may be unified in the future.
const api = require("@opentelemetry/api");
const DIAG_LOGGER = api.diag.createComponentLogger({
namespace: '@opentelemetry/opentelemetry-instrumentation-xml-http-request/utils',
});
function isDocument(value) {
return typeof Document !== 'undefined' && value instanceof Document;
}
/**
* Helper function to determine payload content length for XHR requests
* @param body
* @returns content length
*/
function getXHRBodyLength(body) {
if (isDocument(body)) {
return new XMLSerializer().serializeToString(document).length;
}
// XMLHttpRequestBodyInit expands to the following:
if (typeof body === 'string') {
return getByteLength(body);
}
if (body instanceof Blob) {
return body.size;
}
if (body instanceof FormData) {
return getFormDataSize(body);
}
if (body instanceof URLSearchParams) {
return getByteLength(body.toString());
}
// ArrayBuffer | ArrayBufferView
if (body.byteLength !== undefined) {
return body.byteLength;
}
DIAG_LOGGER.warn('unknown body type');
return undefined;
}
exports.getXHRBodyLength = getXHRBodyLength;
const TEXT_ENCODER = new TextEncoder();
function getByteLength(s) {
return TEXT_ENCODER.encode(s).byteLength;
}
function getFormDataSize(formData) {
let size = 0;
for (const [key, value] of formData.entries()) {
size += key.length;
if (value instanceof Blob) {
size += value.size;
}
else {
size += value.length;
}
}
return size;
}
//# sourceMappingURL=utils.js.map