|
| 1 | +--- |
| 2 | +title: Litestar |
| 3 | +description: "Learn about using Sentry with Litestar." |
| 4 | +--- |
| 5 | + |
| 6 | +The Litestar integration adds support for the [Litestar framework](https://docs.litestar.dev/2/). |
| 7 | + |
| 8 | +## Install |
| 9 | + |
| 10 | +Install `sentry-sdk` from PyPI with the `litestar` extra: |
| 11 | + |
| 12 | +```bash |
| 13 | +pip install --upgrade 'sentry-sdk[litestar]' uvicorn |
| 14 | +``` |
| 15 | + |
| 16 | +## Configure |
| 17 | + |
| 18 | +Add `LitestarIntegration()` to your `integrations` list: |
| 19 | + |
| 20 | +<SignInNote /> |
| 21 | + |
| 22 | +<OnboardingOptionButtons |
| 23 | + options={[ |
| 24 | + 'error-monitoring', |
| 25 | + 'performance', |
| 26 | + 'profiling', |
| 27 | + ]} |
| 28 | +/> |
| 29 | + |
| 30 | +```python {"onboardingOptions": {"performance": "6-8", "profiling": "9-12"}} |
| 31 | +import sentry_sdk |
| 32 | +from sentry_sdk.integrations.litestar import LitestarIntegration |
| 33 | + |
| 34 | +sentry_sdk.init( |
| 35 | + dsn="___PUBLIC_DSN___", |
| 36 | + # Set traces_sample_rate to 1.0 to capture 100% |
| 37 | + # of transactions for tracing. |
| 38 | + traces_sample_rate=1.0, |
| 39 | + # Set profiles_sample_rate to 1.0 to profile 100% |
| 40 | + # of sampled transactions. |
| 41 | + # We recommend adjusting this value in production. |
| 42 | + profiles_sample_rate=1.0, |
| 43 | + integrations=[ |
| 44 | + LitestarIntegration(), |
| 45 | + ], |
| 46 | +) |
| 47 | +``` |
| 48 | + |
| 49 | +## Verify |
| 50 | + |
| 51 | +```python |
| 52 | +from litestar import Litestar, get |
| 53 | + |
| 54 | +sentry_sdk.init(...) # same as above |
| 55 | + |
| 56 | +@get("/hello") |
| 57 | +async def hello_world() -> str: |
| 58 | + 1 / 0 |
| 59 | + return "Hello!" |
| 60 | + |
| 61 | +app = Litestar(route_handlers=[hello_world]) |
| 62 | +``` |
| 63 | + |
| 64 | +Save the file above as `app.py` and start the development server with: |
| 65 | + |
| 66 | +```bash |
| 67 | +uvicorn app:app |
| 68 | +``` |
| 69 | + |
| 70 | +When you point your browser to [http://localhost:8000/hello](http://localhost:8000/hello) a transaction will be created in the Performance section of [sentry.io](https://sentry.io). Additionally, the `ZeroDivisionError` we've snuck into our `hello_world` handler will be sent to [sentry.io](https://sentry.io) and will be connected to the transaction. |
| 71 | + |
| 72 | +It takes a couple of moments for the data to appear in [sentry.io](https://sentry.io). |
| 73 | + |
| 74 | +## Supported Versions |
| 75 | + |
| 76 | +<Alert level="warning" title="Note"> |
| 77 | + |
| 78 | +Litestar was [renamed from Starlite](https://litestar.dev/about/organization.html#litestar-and-starlite) |
| 79 | +with the release of version 2.0. We support different integrations for each one. This guide applies to Litestar. |
| 80 | +See [Starlite integration](/platforms/python/integrations/starlite) for the guide that applies to Starlite. |
| 81 | + |
| 82 | +</Alert> |
| 83 | + |
| 84 | +- Litestar: 2.0.0+ |
| 85 | +- Python: 3.8+ |
0 commit comments