UNPKG

@sentry/core

Version:
62 lines (61 loc) • 2.57 kB
import { DEBUG_BUILD } from "../../debug-build.js"; import { debug } from "../../utils/debug-logger.js"; import { HTTP_RESPONSE_STATUS_CODE } from "@sentry/conventions/attributes"; // src/integrations/http/server-transaction-event.ts /** * Shared post-processing for transaction events produced by server span instrumentation. * * Node's `httpServerSpansIntegration` and Deno's `denoHttpIntegration` both create their * server spans outside of the OTel SDK span exporter, so neither gets the exporter's * status code handling for free. Both run this from their `processEvent` hook instead. */ /** * Status codes for which server transactions are dropped unless `ignoreStatusCodes` says otherwise. * * 300 and 304 are possibly valid status codes we do not want to filter, hence the split ranges. */ const DEFAULT_IGNORE_STATUS_CODES = [ [401, 404], [301, 303], [305, 399] ]; /** * If the given status code should be filtered for the given list of status codes/ranges. */ function shouldFilterStatusCode(statusCode, dropForStatusCodes) { return dropForStatusCodes.some((code) => { if (typeof code === "number") return code === statusCode; const [min, max] = code; return statusCode >= min && statusCode <= max; }); } /** * Drop transaction events whose HTTP status code matches `ignoreStatusCodes`, and surface the * status as the top-level `response` context on the ones that are kept. * * Pass `spanOrigin` to only act on transactions produced by a specific instrumentation, so that * an integration owning this option does not filter transactions created by a different one. * When omitted, every transaction carrying an HTTP status code is considered. * * Returns `null` when the event should be dropped, otherwise the (possibly updated) event. */ function processHttpServerTransactionEvent(event, ignoreStatusCodes, spanOrigin) { if (event.type !== "transaction") return event; if (spanOrigin !== void 0 && event.contexts?.trace?.origin !== spanOrigin) return event; const statusCode = event.contexts?.trace?.data?.[HTTP_RESPONSE_STATUS_CODE]; if (typeof statusCode !== "number") return event; if (shouldFilterStatusCode(statusCode, ignoreStatusCodes)) { DEBUG_BUILD && debug.log("Dropping transaction due to status code", statusCode); return null; } event.contexts = { ...event.contexts, response: { ...event.contexts?.response, status_code: statusCode } }; return event; } export { DEFAULT_IGNORE_STATUS_CODES, processHttpServerTransactionEvent, shouldFilterStatusCode }; //# sourceMappingURL=server-transaction-event.js.map