diff --git a/packages/node-core/src/integrations/http/index.ts b/packages/node-core/src/integrations/http/index.ts index 8f81f28c8eb6..e89af730302d 100644 --- a/packages/node-core/src/integrations/http/index.ts +++ b/packages/node-core/src/integrations/http/index.ts @@ -1,5 +1,6 @@ import type { IncomingMessage, RequestOptions } from 'node:http'; -import { defineIntegration } from '@sentry/core'; +import { debug, defineIntegration } from '@sentry/core'; +import { DEBUG_BUILD } from '../../debug-build'; import { generateInstrumentOnce } from '../../otel/instrument'; import type { SentryHttpInstrumentationOptions } from './SentryHttpInstrumentation'; import { SentryHttpInstrumentation } from './SentryHttpInstrumentation'; @@ -62,10 +63,10 @@ interface HttpOptions { /** * Do not capture spans for incoming HTTP requests with the given status codes. - * By default, spans with 404 status code are ignored. + * By default, spans with some 3xx and 4xx status codes are ignored (see @default). * Expects an array of status codes or a range of status codes, e.g. [[300,399], 404] would ignore 3xx and 404 status codes. * - * @default `[[401, 404], [300, 399]]` + * @default `[[401, 404], [301, 303], [305, 399]]` */ dropSpansForIncomingRequestStatusCodes?: (number | [number, number])[]; @@ -115,7 +116,9 @@ export const instrumentSentryHttp = generateInstrumentOnce { const dropSpansForIncomingRequestStatusCodes = options.dropSpansForIncomingRequestStatusCodes ?? [ [401, 404], - [300, 399], + // 300 and 304 are possibly valid status codes we do not want to filter + [301, 303], + [305, 399], ]; return { @@ -133,18 +136,12 @@ export const httpIntegration = defineIntegration((options: HttpOptions = {}) => // Drop transaction if it has a status code that should be ignored if (event.type === 'transaction') { const statusCode = event.contexts?.trace?.data?.['http.response.status_code']; - if ( - typeof statusCode === 'number' && - dropSpansForIncomingRequestStatusCodes.some(code => { - if (typeof code === 'number') { - return code === statusCode; - } - - const [min, max] = code; - return statusCode >= min && statusCode <= max; - }) - ) { - return null; + if (typeof statusCode === 'number') { + const shouldDrop = shouldFilterStatusCode(statusCode, dropSpansForIncomingRequestStatusCodes); + if (shouldDrop) { + DEBUG_BUILD && debug.log('Dropping transaction due to status code', statusCode); + return null; + } } } @@ -152,3 +149,17 @@ export const httpIntegration = defineIntegration((options: HttpOptions = {}) => }, }; }); + +/** + * If the given status code should be filtered for the given list of status codes/ranges. + */ +function shouldFilterStatusCode(statusCode: number, dropForStatusCodes: (number | [number, number])[]): boolean { + return dropForStatusCodes.some(code => { + if (typeof code === 'number') { + return code === statusCode; + } + + const [min, max] = code; + return statusCode >= min && statusCode <= max; + }); +} diff --git a/packages/node/src/integrations/http.ts b/packages/node/src/integrations/http.ts index 60999b108872..dc7b48b4862c 100644 --- a/packages/node/src/integrations/http.ts +++ b/packages/node/src/integrations/http.ts @@ -3,7 +3,7 @@ import { diag } from '@opentelemetry/api'; import type { HttpInstrumentationConfig } from '@opentelemetry/instrumentation-http'; import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'; import type { Span } from '@sentry/core'; -import { defineIntegration, getClient, hasSpansEnabled } from '@sentry/core'; +import { debug, defineIntegration, getClient, hasSpansEnabled } from '@sentry/core'; import type { HTTPModuleRequestIncomingMessage, NodeClient } from '@sentry/node-core'; import { type SentryHttpInstrumentationOptions, @@ -13,6 +13,7 @@ import { NODE_VERSION, SentryHttpInstrumentation, } from '@sentry/node-core'; +import { DEBUG_BUILD } from '../debug-build'; import type { NodeClientOptions } from '../types'; const INTEGRATION_NAME = 'Http'; @@ -84,10 +85,10 @@ interface HttpOptions { /** * Do not capture spans for incoming HTTP requests with the given status codes. - * By default, spans with 404 status code are ignored. + * By default, spans with some 3xx and 4xx status codes are ignored (see @default). * Expects an array of status codes or a range of status codes, e.g. [[300,399], 404] would ignore 3xx and 404 status codes. * - * @default `[[401, 404], [300, 399]]` + * @default `[[401, 404], [301, 303], [305, 399]]` */ dropSpansForIncomingRequestStatusCodes?: (number | [number, number])[]; @@ -195,7 +196,9 @@ export function _shouldUseOtelHttpInstrumentation( export const httpIntegration = defineIntegration((options: HttpOptions = {}) => { const dropSpansForIncomingRequestStatusCodes = options.dropSpansForIncomingRequestStatusCodes ?? [ [401, 404], - [300, 399], + // 300 and 304 are possibly valid status codes we do not want to filter + [301, 303], + [305, 399], ]; return { @@ -225,18 +228,12 @@ export const httpIntegration = defineIntegration((options: HttpOptions = {}) => // Drop transaction if it has a status code that should be ignored if (event.type === 'transaction') { const statusCode = event.contexts?.trace?.data?.['http.response.status_code']; - if ( - typeof statusCode === 'number' && - dropSpansForIncomingRequestStatusCodes.some(code => { - if (typeof code === 'number') { - return code === statusCode; - } - - const [min, max] = code; - return statusCode >= min && statusCode <= max; - }) - ) { - return null; + if (typeof statusCode === 'number') { + const shouldDrop = shouldFilterStatusCode(statusCode, dropSpansForIncomingRequestStatusCodes); + if (shouldDrop) { + DEBUG_BUILD && debug.log('Dropping transaction due to status code', statusCode); + return null; + } } } @@ -282,3 +279,17 @@ function getConfigWithDefaults(options: Partial = {}): HttpInstrume return instrumentationConfig; } + +/** + * If the given status code should be filtered for the given list of status codes/ranges. + */ +function shouldFilterStatusCode(statusCode: number, dropForStatusCodes: (number | [number, number])[]): boolean { + return dropForStatusCodes.some(code => { + if (typeof code === 'number') { + return code === statusCode; + } + + const [min, max] = code; + return statusCode >= min && statusCode <= max; + }); +}