Skip to content

Enable configuration of reported status codes for FastAPI/Starlette HTTPExceptions #2825

Closed
@PeterBaker0

Description

@PeterBaker0

Problem Statement

When instrumenting a FastAPI application with Sentry, I want to be able to configure the status codes for which a HTTPException is reported to Sentry, so that I can control what kinds of errors are reported in sentry in a way which makes sense in my application.

Solution Brainstorm

The code which informs the status code filtering appears to be here

and exp.status_code >= 500

 async def _sentry_patched_exception_handler(self, *args, **kwargs):
                # type: (Any, Any, Any) -> None
                exp = args[0]

                is_http_server_error = (
                    hasattr(exp, "status_code")
                    and isinstance(exp.status_code, int)
                    and exp.status_code >= 500
                )
                # ...

I don't know enough about the sentry SDK/integration architecture to understand where/how/if integration specific config can be injected to this context.

I'd assume that if this is possible, it would be an easy addition.

A (rough) interface like so might be helpful:

from typing import Callable

# This function takes the status code and returns True iff should be reported
StatusCodeCheckFunction  = Callable[[int], bool]

# Then can setup your preferred check function 
def check_function(status_code:int)-> bool:
    range_min
    return status_code == 401 or status_code > 500 or status_code in [429, 403]

# then include in sentry init somehow
sentry_init(
#... other stuff
fast_api_config=FastApiConfig(check_function=check_function)
#... other stuff

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions