@@ -73,6 +73,14 @@ interface HttpOptions {
7373 */
7474 ignoreIncomingRequests ?: ( urlPath : string , request : IncomingMessage ) => boolean ;
7575
76+ /**
77+ * Do not capture spans for incoming HTTP requests with the given status codes.
78+ * By default, spans with 404 status code are ignored.
79+ *
80+ * @default `[404]`
81+ */
82+ dropSpansForIncomingRequestStatusCodes ?: ( number | RegExp ) [ ] ;
83+
7684 /**
7785 * Do not capture the request body for incoming HTTP requests to URLs where the given callback returns `true`.
7886 * This can be useful for long running requests where the body is not needed and we want to avoid capturing it.
@@ -148,6 +156,8 @@ export function _shouldInstrumentSpans(options: HttpOptions, clientOptions: Part
148156 * It creates breadcrumbs and spans for outgoing HTTP requests which will be attached to the currently active span.
149157 */
150158export const httpIntegration = defineIntegration ( ( options : HttpOptions = { } ) => {
159+ const dropSpansForIncomingRequestStatusCodes = options . dropSpansForIncomingRequestStatusCodes ?? [ 404 ] ;
160+
151161 return {
152162 name : INTEGRATION_NAME ,
153163 setupOnce ( ) {
@@ -180,6 +190,22 @@ export const httpIntegration = defineIntegration((options: HttpOptions = {}) =>
180190 instrumentOtelHttp ( instrumentationConfig ) ;
181191 }
182192 } ,
193+ processEvent ( event ) {
194+ // Drop transaction if it has a status code that should be ignored
195+ if ( event . type === 'transaction' ) {
196+ const statusCode = event . contexts ?. trace ?. data ?. [ 'http.response.status_code' ] ;
197+ if (
198+ typeof statusCode === 'number' &&
199+ dropSpansForIncomingRequestStatusCodes . some ( code =>
200+ typeof code === 'number' ? code === statusCode : code . test ( statusCode . toString ( ) ) ,
201+ )
202+ ) {
203+ return null ;
204+ }
205+ }
206+
207+ return event ;
208+ } ,
183209 } ;
184210} ) ;
185211
0 commit comments