FastAPI and Starlette Sentry Integrations Have Arrived
FastAPI is known for building REST APIs, middleware services, and simple integration for adding authentications and more. And it’s known for doing all of that…you guessed it, fast. It’s used by tech giants and scientists alike, and according to Stack Overflow’s Developer Survey 2022 more developers are using FastAPI than Ruby on Rails. With less than 4 years of being published, it’s impressive that it’s even creeping up on the traditional Python heavyweights Django and Flask.
So if you’re one of those developers, or maybe even one of the 240 (and counting) developers on this specific Python SDK GitHub issue, or just love all things Python, checkout our recently released FastAPI and Starlette integrations.
FastAPI Support
If you’re already using our Sentry Python SDK, with auto-enabling you shouldn’t have to change anything to begin using FastAPI Sentry support. The Sentry SDK will automatically detect your usage of FastAPI and set up everything for you.
from fastapi import FastAPI
import sentry_sdk
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
# We recommend adjusting this value in production,
traces_sample_rate=1.0,
)
app = FastAPI()
Starlette Support
FastAPI at its base is built around Starlette, another Python web framework with over 7.2k stars on GitHub. So along with our new FastAPI support, we released support for Starlette.
The support for Starlette is, again, the same as for FastAPI and all of our other support within our Python SDKs. With just a couple lines of code you can be up and running:
from starlette.applications import Starlette
import sentry_sdk
sentry_sdk.init(
dsn="https://examplePublicKey@o1.ingest.sentry.io/1",
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
# We recommend adjusting this value in production,
traces_sample_rate=1.0,
)
app = Starlette(routes=[...])
Leveraging Sentry
Support for FastAPI and Starlette opens the ability to use Sentry to improve your application monitoring. For example, you can use Sentry Performance with trace view to connect your FastAPI middleware to frontend, backend, and other middleware, microservices, etc. as was discussed on the Starlette repo.
You might also want to check out Sentry’s transaction focus/routes within Performance, because you’re working on building performant APIs or developing machine learning models, these “out of the box” Sentry features will reduce the complexity of your ecosystem and get more out of your application monitoring.
Building with @tiangolo (aka Sebastián) and Tom
What makes this release unique is that we collaborated with the FastAPI and Starlette creators; Sebastián Ramírez and Tom Christie, respectively. Sebastián and Tom are both major open source contributors, even beyond FastAPI and Starlette, and it was great getting the chance to work with them on this integration and to improve documentation of their frameworks.
Being able to get reviews from the creators of the frameworks that you’re building an integration for is a special experience. Thank you, Sebastián and Tom.
I’ve been using Sentry to help me with debugging for years. As a big fan of Sentry, I was thrilled to learn about their planned support for FastAPI. With major FastAPI features being available out-of-the-box through the Sentry integration, I’m excited to see how FastAPI developers will use the Sentry integration to debug and make their apps even faster.
-- Sebastián Ramírez
Have questions about the new release? Share your feedback on GitHub, join us on Twitter or in the Python channel in our Discord. And if you’re new to Sentry, you can try it for free today or request a demo to get started.