Pyramid

Learn about using Sentry with Pyramid.

The Pyramid integration adds support for the Pyramid Web Framework.

Install sentry-sdk from PyPI:

Copied
pip install "sentry-sdk" 

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

Copied
import sentry_sdk  sentry_sdk.init(  dsn="___PUBLIC_DSN___",  # 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,  # ___PRODUCT_OPTION_START___ performance  # Set traces_sample_rate to 1.0 to capture 100%  # of transactions for tracing.  traces_sample_rate=1.0,  # ___PRODUCT_OPTION_END___ performance  # ___PRODUCT_OPTION_START___ profiling  # To collect profiles for all profile sessions,  # set `profile_session_sample_rate` to 1.0.  profile_session_sample_rate=1.0,  # Profiles will be automatically collected while  # there is an active span.  profile_lifecycle="trace",  # ___PRODUCT_OPTION_END___ profiling  # ___PRODUCT_OPTION_START___ logs   # Enable logs to be sent to Sentry  enable_logs=True,  # ___PRODUCT_OPTION_END___ logs ) 

Copied
from wsgiref.simple_server import make_server from pyramid.config import Configurator from pyramid.response import Response  sentry_sdk.init(...) # same as above  def hello_world(request):  1 / 0 # raises an error  return Response('Hello World!')  if __name__ == '__main__':  with Configurator() as config:  config.add_route('hello', '/')  config.add_view(hello_world, route_name='hello')  app = config.make_wsgi_app()   server = make_server('0.0.0.0', 6543, app)  server.serve_forever() 

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

  • The Sentry Python SDK will install the Pyramid integration for all of your apps. The integration hooks into Pyramid itself, not any of your apps specifically.

  • The SDK will report all exceptions leading to an Internal Server Error. These two kinds of exceptions are:

    • exceptions that are not handled by any exception view
    • exceptions whose exception view returns a status code of 500 (Pyramid version 1.9+ only)
  • 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).

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

Copied
import sentry_sdk from sentry_sdk.integrations.pyramid import PyramidIntegration  sentry_sdk.init(  # ...  integrations=[  PyramidIntegration(  transaction_style="route_pattern",  )  ] ) 

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

  • transaction_style:

    Copied
    config.add_route("myroute", "/myurl/{id}") config.add_view(myfunction, route_name="myroute") 

    In the above code, you can set the transaction to:

    • /myurl/{id} if you set transaction_style="route_pattern"
    • myroute if you set transaction_style="route_name"

    The default is "route_name".

  • Pyramid: 1.6+
  • Python: 3.6+
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").