Litestar

Learn about using Sentry with Litestar.

The Litestar integration adds support for the Litestar framework.

Install sentry-sdk from PyPI with the litestar extra:

Copied
pip install --upgrade 'sentry-sdk[litestar]' uvicorn

If you have the litestar package in your dependencies, the Litestar integration will be enabled automatically when you initialize the Sentry SDK.

In addition to capturing errors, you can monitor interactions between multiple services or applications by enabling tracing. You can also collect and analyze performance profiles from real users with profiling.

Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.

Copied
import sentry_sdk

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    # Set profiles_sample_rate to 1.0 to profile 100%
    # of sampled transactions.
    # We recommend adjusting this value in production.
    profiles_sample_rate=1.0,
)

Copied
from litestar import Litestar, get

sentry_sdk.init(...)  # same as above

@get("/hello")
async def hello_world() -> str:
    1 / 0
    return "Hello!"

app = Litestar(route_handlers=[hello_world])

Save the file above as app.py and start the development server with:

Copied
uvicorn app:app

When you point your browser to http://localhost:8000/hello a transaction will be created in the Performance section of sentry.io. Additionally, the ZeroDivisionError we've snuck into our hello_world handler will be sent to sentry.io and will be connected to the transaction.

It takes a couple of moments for the data to appear in sentry.io.

By adding LitestarIntegration to your sentry_sdk.init() call explicitly, you can set options for LitestarIntegration to change its behavior:

Copied
import sentry_sdk
from sentry_sdk.integrations.litestar import LitestarIntegration

sentry_sdk.init(
    # ...
    integrations=[
        LitestarIntegration(
            failed_request_status_codes={403, *range(500, 599)},
        ),
    ],
)

You can pass the following keyword arguments to LitestarIntegration():

A set of integers that will determine when an HTTPException should be reported to Sentry. The HTTPException is reported to Sentry if its status code is contained in the failed_request_status_codes set.

Examples of valid failed_request_status_codes:

  • {500} will only report HTTPException with status 500.
  • {400, *range(500, 600)} will report HTTPException with status 400 as well as those in the 5xx range.
  • set() (the empty set) will not report any HTTPException to Sentry.

The default is {*range(500, 600)}, meaning that any HTTPException with a status in the 5xx range is reported to Sentry.

Regardless of how failed_request_status_codes is set, any exceptions raised by the handler, which are not of type HTTPException (or a subclass) are reported to Sentry. For example, if your request handler raises an unhandled AttributeError, the AttributeError gets reported to Sentry, even if you have set failed_request_status_codes=set().

  • Litestar: 2.0.0+
  • Python: 3.8+
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").