Skip to main content

A thin Python wrapper around the Sentry SDK providing a decorator for automatic initialization and standalone functions for the full event lifecycle.

Project description

core-sentry

A thin Python wrapper around the Sentry SDK providing a decorator for automatic initialization and standalone functions for the full event lifecycle: initialization, exception capture, message capture, and flush.


Python Versions License Pipeline Status Docs Status Security

Installation

pip install core-sentry
uv pip install core-sentry

Features

  • Decorator-based initialization: wrap any function with @with_sentry to initialize the SDK before it runs

  • Duplicate initialization guard: skips sentry_sdk.init if the client is already configured

  • Tag management: set global tags via SentryConfig and per-event tags via SentryRuntimeConfig

  • Exception capture: capture_exception sends errors with optional per-event tags and extra context using an isolated scope

  • Message capture: capture_message sends informational/warning events with configurable severity

  • Event flush: flush_sentry blocks until the event queue drains; essential for short-lived scripts and ETLs

  • Flexible decorator syntax: usable with or without parentheses

  • Fully typed: complete type annotations including Literal for severity levels

Quick Start

pip install core-sentry
pip install -e ".[dev]"    # For development

Decorator usage

The with_sentry decorator initializes Sentry before the wrapped function runs. For long-running services the defaults are sufficient:

from core_sentry.base import SentryConfig
from core_sentry.decorators.base import with_sentry

@with_sentry(config=SentryConfig(
    dsn="https://...",
    env="production",
    tags={"service": "my-api"},
))
def my_function():
    return "Hello World"

For ETLs and short-lived scripts running in containers, enable capture_exceptions and flush to guarantee delivery even when atexit handlers are skipped (e.g. SIGKILL or OOM):

from core_sentry.base import SentryConfig
from core_sentry.decorators.base import SentryRuntimeConfig, with_sentry

@with_sentry(
    config=SentryConfig(
        dsn="https://...",
        env="production",
        tags={"service": "etl-pipeline", "version": "1.0.0"},
        traces_sample_rate=0.5,
    ),
    runtime_config=SentryRuntimeConfig(
        capture_exceptions=True,
        flush=True,
        flush_timeout=5.0,
        tags={"job": "daily-sync"},
        extra={"rows_processed": 9999},
    ),
)
def run_etl():
    pass

Standalone functions

Use the standalone API when you need explicit control over initialization, capture, and flush within a single process:

from core_sentry.base import (
    SentryConfig,
    init_sentry,
    capture_exception,
    capture_message,
    flush_sentry,
)

init_sentry(SentryConfig(
    dsn="https://...",
    env="production",
    tags={"service": "etl-pipeline"},
))

# Informational breadcrumb
capture_message("ETL job started", level="info", tags={"job": "daily-sync"})

# Recoverable error mid-job
try:
    raise ValueError("Unexpected null value in column 'amount'")
except ValueError as exc:
    capture_exception(
        exc,
        tags={"job": "daily-sync", "stage": "transform"},
        extra={"row_index": 1042, "column": "amount"},
    )

# Guarantee delivery before process exit
flushed = flush_sentry(timeout=5.0)
if not flushed:
    print("WARNING: some Sentry events may not have been delivered.")

Development

# Create and activate virtual environment
virtualenv --python=python3.12 .venv
source .venv/bin/activate

# Install with dev dependencies
pip install -e ".[dev]"

# Run tests
python -m unittest discover -s tests -p "tests_*.py"
python manager.py run-tests
python manager.py run-coverage

# Lint and security scan
pylint core_sentry
bandit -r core_sentry

# Run across all supported Python versions
tox

Contributing

Contributions are welcome! Please:

  1. Fork the repository

  2. Create a feature branch

  3. Write tests for new functionality

  4. Ensure all tests pass: python -m unittest discover -s tests -p "tests_*.py"

  5. Run linting: pylint core_sentry

  6. Run security checks: bandit -r core_sentry

  7. Submit a pull request

License

This project is licensed under the MIT License. See the LICENSE file for details.

Support

For questions or support, please open an issue on GitLab or contact the maintainers.

Authors

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

core_sentry-2.1.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

core_sentry-2.1.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file core_sentry-2.1.0.tar.gz.

File metadata

  • Download URL: core_sentry-2.1.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for core_sentry-2.1.0.tar.gz
Algorithm Hash digest
SHA256 f13ba46d70a6d38b141e564dde53315c34cff2c83743b4021446137987d88dae
MD5 42bd88751b750afc8c6873d96221b54a
BLAKE2b-256 039e366bb65289bf3cb9660277deb9eb52cb3fc7b6e89f55279d37dee9b14a83

See more details on using hashes here.

File details

Details for the file core_sentry-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: core_sentry-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for core_sentry-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 011a994be84cf7148bbc076b0b01c8477e65bb48d0005b81811e8a770f17150f
MD5 ac3a2d7175379abec523e0414a26d98c
BLAKE2b-256 3c4b1e90c73b0c38d49a41d9aa1f05379ea4ba28e16d3da8db428a7b462fe64e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page