Sanic

The Sanic integration adds support for the Sanic Web Framework.

Install sentry-sdk from PyPI with the sanic extra:

Copied
pip install --upgrade sentry-sdk[sanic]

If you're on Python 3.6, you also need the aiocontextvars package:

Copied
pip install --upgrade aiocontextvars

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

To ensure that errors occuring in background tasks get reported to Sentry, we always recommend enabling the AsyncioIntegration when you are using the SanicIntegration. To enable the AsyncioIntegration, you must explicitly add the AsyncioIntegration to the integrations list and initialize the SDK within a before_server_start listener.

The example below demonstrates the simplest recommended setup.

Copied
from sanic import Sanic
import sentry_sdk
from sentry_sdk.integrations.asyncio import AsyncioIntegration

app = Sanic(__main__)

@app.listener("before_server_start")
async def init_sentry(_):
    sentry_sdk.init(
        dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
        integrations=[AsyncioIntegration()],
    )

If you're using performance monitoring, you can configure the Sanic integration to not send transactions for requests that resulted in certain HTTP statuses using the unsampled_statuses option.

By default, the Sanic integration generates transactions for all requests, except requests that result in a 404 HTTP status. To generate transactions for all HTTP statuses, including 404, or change which HTTP statuses don't generate transactions, you can manually instantiate the Sanic integration when initializing the Sentry SDK, like so:

Copied
from sanic import Sanic
import sentry_sdk
from sentry_sdk.integrations.asyncio import AsyncioIntegration
from sentry_sdk.integrations.sanic import SanicIntegration

@app.listener("before_server_start")
async def init_sentry(_):
    sentry_sdk.init(
        dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
        enable_tracing=True,
        integrations=[
            AsyncioIntegration(),
            SanicIntegration(
                # Configure the Sanic integration so that we generate
                # transactions for all HTTP status codes, including 404
                unsampled_statuses=None,
            ),
        ],
    )

To specify a different set of HTTP statuses for which no transactions should be generated, set unsampled_statuses to a set containing the HTTP statuses that don't generate transactions. For example, if you wish to have no transactions for requests resulting in a 200 or 404 HTTP status, use SanicIntegration(unsampled_statuses={200, 400}).

Copied
from sanic import Sanic
from sanic.response import text
import sentry_sdk
from sentry_sdk.integrations.asyncio import AsyncioIntegration

app = Sanic(__name__)

@app.listener("before_server_start")
async def init_sentry(_):
    sentry_sdk.init(...)  # same as above

@app.get("/")
async def hello_world(request):
    1 / 0  # raises an error
    return text("Hello, world.")`

When you point your browser to http://localhost:8000/ an error event will be sent to sentry.io.

  • The Sentry Python SDK will install the Sanic integration for all of your apps.

  • All exceptions leading to an Internal Server Error are reported.

  • Request data is attached to all events: HTTP method, URL, headers, form data, JSON payloads. Sentry excludes raw bodies and multipart file uploads. Sentry also excludes personally identifiable information (such as user ids, usernames, cookies, authorization headers, IP addresses) unless you set send_default_pii to True.

  • Each request has a separate scope. Changes to the scope within a view, for example setting a tag, will only apply to events sent as part of the request being handled.

  • Logging with any logger will create breadcrumbs when the Logging integration is enabled (done by default).

  • Sanic: 0.8+
  • Python: 3.6+ (Sanic 0.8+), 3.7+ (Sanic 21.0+)
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").