UNPKG

@grafana/faro-web-tracing

Version:
47 lines 2.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.xhrCustomAttributeFunctionWithDefaults = exports.fetchCustomAttributeFunctionWithDefaults = exports.setSpanStatusOnXMLHttpRequestError = exports.setSpanStatusOnFetchError = void 0; var api_1 = require("@opentelemetry/api"); /** * Adds HTTP status code to every span. * * The fetch instrumentation does not always set the span status to error as defined by the spec. * To work around that issue we manually set the span status. * * Issue: https://github.com/open-telemetry/opentelemetry-js/issues/3564 * Spec: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md#status */ function setSpanStatusOnFetchError(span, _request, result) { var httpStatusCode = result instanceof Error ? 0 : result.status; setSpanStatus(span, httpStatusCode); } exports.setSpanStatusOnFetchError = setSpanStatusOnFetchError; function setSpanStatusOnXMLHttpRequestError(span, xhr) { setSpanStatus(span, xhr.status); } exports.setSpanStatusOnXMLHttpRequestError = setSpanStatusOnXMLHttpRequestError; function setSpanStatus(span, httpStatusCode) { if (httpStatusCode == null) { return; } var isError = httpStatusCode === 0; var isClientOrServerError = httpStatusCode >= 400 && httpStatusCode < 600; if (isError || isClientOrServerError) { span.setStatus({ code: api_1.SpanStatusCode.ERROR }); } } function fetchCustomAttributeFunctionWithDefaults(callback) { return function (span, request, result) { setSpanStatusOnFetchError(span, request, result); callback === null || callback === void 0 ? void 0 : callback(span, request, result); }; } exports.fetchCustomAttributeFunctionWithDefaults = fetchCustomAttributeFunctionWithDefaults; function xhrCustomAttributeFunctionWithDefaults(callback) { return function (span, xhr) { setSpanStatusOnXMLHttpRequestError(span, xhr); callback === null || callback === void 0 ? void 0 : callback(span, xhr); }; } exports.xhrCustomAttributeFunctionWithDefaults = xhrCustomAttributeFunctionWithDefaults; //# sourceMappingURL=instrumentationUtils.js.map