Skip to main content

Sentry-style Slack error reporting for Python — Lambda + servers + scripts.

Project description

self-sentry

Sentry-style Slack error reporting for Python — works in AWS Lambda, FastAPI/Django servers, plain scripts, anywhere.

One init() call per project, then:

  • @report_errors() decorator catches uncaught exceptions and posts to Slack.
  • Global hooks (sys.excepthook, threading.excepthook, asyncio loop handler) catch anything you forgot to wrap.
  • Lambda timeout watchdog posts a warning ~1s before your function times out (the silent-kill case Lambda gives you nothing for).
  • Manual notify(...) for ad-hoc info/success/error messages.

Install

pip install self-sentry

Or pin an exact version (recommended for reproducible Lambda builds):

pip install self-sentry==0.4.1

Fallback: install direct from git

Useful for testing an unreleased commit:

pip install "self-sentry @ git+https://github.com/Ad-Leverage/self-sentry.git@v0.4.1"

Quickstart

import os
import self_sentry

self_sentry.init(
    token=os.environ["SLACK_BOT_TOKEN"],
    channel="#my-project-alerts",
    service_name="my-service",
)

@self_sentry.report_errors()
def lambda_handler(event, context):
    ...

self_sentry.notify("Booking succeeded", fields={"booking_id": 42}, status=0)

Env-driven init (good for Lambdas that already hydrate secrets)

Set SLACK_BOT_TOKEN, SLACK_CHANNEL, and SERVICE_NAME, then:

self_sentry.init_from_env()

No-op (with a warning) if any of the three is missing — safe to call unconditionally at app startup.

What gets reported

Source Fires when
@report_errors() The wrapped function raises (re-raised after reporting).
sys.excepthook Uncaught exception kills the main thread.
threading.excepthook Uncaught exception in a threading.Thread.
asyncio loop handler Uncaught exception in a Task / call_soon callback. (Installed lazily on the first running loop.)
Lambda timeout watchdog context.get_remaining_time_in_millis() drops below timeout_warning_buffer_ms (default 1000).

Configuration

self_sentry.init(
    token="xoxb-...",                # required
    channel="#alerts",               # required
    service_name="my-service",       # shows up as Slack author_name
    install_excepthook=True,
    install_threading_hook=True,
    install_asyncio_hook=True,
    lambda_timeout_warning=True,
    timeout_warning_buffer_ms=1000,
)

One bot, one channel. If you need to post to multiple destinations, call notify(..., channel="#other") explicitly at the call site.

If init() is never called, every reporting function is a silent no-op — safe to leave decorators and notify() calls in code that runs locally without Slack creds.

Status codes (Slack color)

status Color Use
0 green success
1 orange error (default for report_exception)
2 grey debug
3 purple info (default for notify)

Local dev

pip install -e ".[dev]"
pytest -q
ruff check src tests
mypy src/self_sentry

Manual smoke test against a real Slack workspace:

LIVE_SLACK_TOKEN=xoxb-... LIVE_SLACK_CHANNEL='#test' python -m tests.smoke.send_real

Release

Tag-driven. Push a vX.Y.Z tag and CI builds the wheel + sdist, publishes to PyPI via Trusted Publishing (no API tokens), and attaches both artifacts to a GitHub Release as a secondary archive.

# Bump project.version in pyproject.toml first, then:
git tag v0.4.1 && git push --tags

The release workflow refuses to publish if the tag doesn't match project.version in pyproject.toml. Bump the version pin in consumer projects to upgrade.

Caveats

  • The asyncio handler is installed lazily on the first running loop observed (via @report_errors or report_exception). Exceptions on a loop that runs before that first call are not captured. Fine for typical FastAPI/Mangum/Lambda layouts.
  • The Lambda timeout watchdog uses a threading.Timer. It is always cancelled in the decorator's finally so it cannot leak across invocations — but if you reach inside and start a watchdog yourself, don't outlive the handler scope.

License

MIT

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

self_sentry-0.4.1.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

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

self_sentry-0.4.1-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file self_sentry-0.4.1.tar.gz.

File metadata

  • Download URL: self_sentry-0.4.1.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for self_sentry-0.4.1.tar.gz
Algorithm Hash digest
SHA256 1c7bc2721ac9fe37a8a3bc6510fb786315880bcec02d578a25de2f2626222c61
MD5 042f8499c90282e3e1929b989a9939ea
BLAKE2b-256 5938180772e06bf5ab172efc4a6e0a3f2cfd68aa5a473cce368cfb49c157a50f

See more details on using hashes here.

Provenance

The following attestation bundles were made for self_sentry-0.4.1.tar.gz:

Publisher: release.yml on Ad-Leverage/self-sentry

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file self_sentry-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: self_sentry-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for self_sentry-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e177389de18b98bcd834b13bfc3fc1244b639138c5f42696bd2cd09233b317a8
MD5 fc9056f603ade5f0f09fd9e1f4d40f25
BLAKE2b-256 143e121ed25096ddd33e7579f7a3b65a6dad606d24252d34dc52715f0a5b553a

See more details on using hashes here.

Provenance

The following attestation bundles were made for self_sentry-0.4.1-py3-none-any.whl:

Publisher: release.yml on Ad-Leverage/self-sentry

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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