Sentry Python SDK

repository·master·Indexed 24 days ago

https://github.com/getsentry/sentry-python

The official Sentry SDK for Python, used to monitor applications for exceptions and performance issues through real-time error reporting and tracing. It provides APIs for capturing exceptions and messages, enriching events with context and breadcrumbs, and monitoring performance via spans and transactions. The SDK includes logging integrations and tools for distributed tracing.

Tokens
6.1K
Snippets
12
Records
50
Agent score
81%

What's inside sentry-python

  1. Test AWS Lambda functions locally

    master

    The test-lambda-locally helper allows you to run an AWS Lambda function in a local environment that includes the currently checked-out version of the Sentry SDK. This is useful for verifying Sentry integration within a Lambda context before deploying to AWS.

    Limitations:

    • Only embedding the Sentry SDK directly into the Lambda function package is supported.
    • Adding the SDK as a Lambda Layer is not currently possible.
  2. Use new_scope and isolation_scope instead of push_scope and Hub cloning

    master

    To manage scope lifecycles in 2.x, use new_scope or isolation_scope instead of the deprecated push_scope or manual Hub cloning.

    • new_scope(): Use this to fork the current scope when you want to wrap a small unit of code with a temporary scope.
    • isolation_scope(): Use this to fork the isolation scope. This is typically used for a whole request-response cycle or a task execution (roughly matching the lifecycle of a transaction).

    Mapping for push_scope:

    • Instead of with push_scope() as scope:, use with sentry_sdk.new_scope() as scope: for localized changes, or with sentry_sdk.isolation_scope() as scope: for broader changes.
    import sentry_sdk
    
    # For a small unit of code
    with sentry_sdk.new_scope() as scope:
        # do something
    
    # For a whole request/task
    with sentry_sdk.isolation_scope() as scope:
        # do something
  3. Use logging integrations to capture logs as breadcrumbs

    master
    The sentry_sdk.integrations.logging module provides tools to integrate Python's standard logging library with Sentry. You can use BreadcrumbHandler to automatically turn log messages into Sentry breadcrumbs, which provides context leading up to an error. Additionally, you can use ignore_logger to prevent specific loggers from sending data to Sentry.
  4. Add a new test suite integration

    master

    To add a new integration to the automated test suite, follow these steps:

    1. Update Minimum Version: Add the minimum supported version of the framework/library to _MIN_VERSIONS in integrations/__init__.py. This should be the lowest version guaranteed to work with the SDK.
    2. Configure Test Suite: Add the integration and any necessary constraints to TEST_SUITE_CONFIG in scripts/populate_tox/config.py.
    3. Assign to Group: Add the integration to one of the groups in the GROUPS dictionary in scripts/split_tox_gh_actions/split_tox_gh_actions.py.
    4. Generate Files: Run scripts/generate-test-files.sh and commit the resulting changes.
  5. Run a local Lambda function

    master

    To execute your Lambda function locally with the Sentry SDK, follow these steps:

    1. Prepare your code: Update lambda_function.py with the code you wish to test.
    2. Execute the deployment script: Run ./deploy-lambda-locally.sh.

    What the script does:

    • Installs AWS SAM in a virtual Python environment.
    • Creates a Lambda function package in the package/ directory containing:
      • The currently checked-out Sentry SDK.
      • Sentry SDK dependencies (certifi and urllib3).
      • Your code from lambda_function.py.
    • Zips the package into lambda_deployment_package.zip.
    • Starts a local Lambda environment serving that function.
    1. Access the function: Once running, point your browser to http://127.0.0.1:3000. Currently, the environment supports GET and POST requests (as defined in template.yaml).
    ./deploy-lambda-locally.sh
  6. Configure the Sentry SDK

    master

    Initialize the Sentry SDK using sentry_sdk.init(). You must provide your project's DSN (Data Source Name).

    Key configuration options include:

    • traces_sample_rate: Set to 1.0 to capture 100% of performance traces.
    • data_collection: A dictionary used to control data privacy. For example, setting {"user_info": False, "http_bodies": []} disables sending user data and HTTP request/response bodies.
    import sentry_sdk
    
    sentry_sdk.init(
        "https://12927b5f211046b575ee51fd8b1ac34f@o1.ingest.sentry.io/1",  # Your DSN here
    
        # Set traces_sample_rate to 1.0 to capture 100%
        # of traces for performance monitoring.
        traces_sample_rate=1.0,
    
        # To disable sending user data and HTTP request/response bodies, uncomment
        # the line below.
        # data_collection={"user_info": False, "http_bodies": []},
    )
  7. Migrate from Sentry SDK 1.x to 2.x

    master

    When upgrading to Sentry SDK 2.x, several major changes apply:

    • Automatic Integrations: New packages like Ariadne, ARQ, asyncpg, Chalice, clickhouse-driver, GQL, Graphene, huey, Loguru, PyMongo, Quart, Starlite, and Strawberry will now have their integrations activated automatically if detected.
    • New Scope APIs: New top-level APIs new_scope and isolation_scope have been added for custom instrumentation.
    • Deprecation of Hub: Direct use of the Hub and hub-based APIs is deprecated. Use the top-level sentry_sdk API, sentry_sdk.scope, or sentry_sdk.client instead.
    • Python Support: Support for Python 2 and Python 3.5 has been removed. Minimum requirement is Python 3.6.
  8. Run the populate_tox.py script

    master

    The populate_tox.py script picks reasonable versions of frameworks/libraries to test automatically and generates parts of tox.ini. To run it, you require a free-threaded Python interpreter. You can run it via the toxgen dependency group using uv:

    UV_PROJECT_ENVIRONMENT=toxgen.venv \
      uv run --python 3.14t \
        --group toxgen \
        --with-editable . \
        python scripts/populate_tox/populate_tox.py

    Alternatively, you can run scripts/generate-test-files.sh, which is a wrapper that executes this script and also runs split_tox_gh_actions.