Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions packages/node-core/src/integrations/http/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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])[];

Expand Down Expand Up @@ -115,7 +116,9 @@ export const instrumentSentryHttp = generateInstrumentOnce<SentryHttpInstrumenta
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 {
Expand All @@ -133,22 +136,30 @@ 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;
}
}
}

return event;
},
};
});

/**
* 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;
});
}
43 changes: 27 additions & 16 deletions packages/node/src/integrations/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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';
Expand Down Expand Up @@ -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])[];

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
}

Expand Down Expand Up @@ -282,3 +279,17 @@ function getConfigWithDefaults(options: Partial<HttpOptions> = {}): 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;
});
}
Loading