Skip to main content
Loss of provenance

A previous release had provenance metadata, but this release does not. View details.

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 git+ssh://git@github.com/Ad-Leverage/self-sentry.git@v0.1.0

Pin to a tag, not a branch — that's what makes reproducible Lambda builds work.

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 (and optionally the email vars below), then:

self_sentry.init_from_env()

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

Email alerts (optional, via SendGrid)

Slack is always the primary destination. To also email an alert, configure SendGrid at init() and opt in per call with send_email=True:

self_sentry.init(
    token=os.environ["SLACK_BOT_TOKEN"],
    channel="#my-project-alerts",
    service_name="my-service",
    sendgrid_api_key=os.environ["SENDGRID_API_KEY"],
    email_from="alerts@my-co.com",
    email_to="oncall@my-co.com,backend@my-co.com",   # comma-separated string or a list
)

# Slack only (default):
self_sentry.notify("Heads up", status=3)

# Slack + email:
self_sentry.notify("Cookie expired", status=1, fields={"builder_id": 106609}, send_email=True)
self_sentry.report_exception(exc, send_email=True)

@self_sentry.report_errors(send_email=True)   # email uncaught exceptions too
def lambda_handler(event, context): ...

init_from_env() reads the same from SENDGRID_API_KEY, ALERT_EMAIL_FROM, and ALERT_EMAIL_TO. Email is sent through SendGrid's v3/mail/send REST API over stdlib urllib — no extra dependency. The email body mirrors the Slack alert's message + fields (and, for report_exception, the full traceback).

If send_email=True is requested but email creds are absent (or recipients parse to empty), nothing is emailed — the Slack post still goes out and a warning is logged. Email failures are swallowed like Slack failures and never break business code.

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-based; no PyPI.

git tag v0.1.0 && git push --tags

Bump the @vX.Y.Z 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

Metadata

Release files for self-sentry 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Loss of provenance

These files do not have provenance metadata, despite files in a prior release including this data.

We recommend further investigation before installing these files. View details.

Source distribution (sdist)

Source distribution for self-sentry 0.5.0
File Size Uploaded Provenance Actions
self_sentry-0.5.0.tar.gz 12.5 kB Without provenance Details for self_sentry-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for self-sentry 0.5.0
File Interpreter ABI Platform Actions
self_sentry-0.5.0-py3-none-any.whl Python 3 none any Details for self_sentry-0.5.0-py3-none-any.whl

Total release size: 26.9 kB

Release files / self_sentry-0.5.0.tar.gz

Loss of provenance

This file belongs to a release without provenance metadata, despite a prior release including this data.

We recommend further investigation before installing this file. View details.

Download URL self_sentry-0.5.0.tar.gz
Size 12.5 kB
Tags Source
SHA-256 checksum
How to use checksums
03e6ece44aefc14ed0fd6667c371886a42f3796b508741ee8b412e8f3b4f6e7a
BLAKE2b-256 checksum
How to use checksums
5d0bf72a3891af8f325183cd49ff8b112b972ddba5907ffa55f7bfca39264ecc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.9

Release files / self_sentry-0.5.0-py3-none-any.whl

Loss of provenance

This file belongs to a release without provenance metadata, despite a prior release including this data.

We recommend further investigation before installing this file. View details.

Download URL self_sentry-0.5.0-py3-none-any.whl
Size 14.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fafac9df3a0e954ac908c71d9e21f248aee278d50e71754f46fcb95cfa5adc74
BLAKE2b-256 checksum
How to use checksums
84ae98a50d50a73735924568b41d16c6ef58261257c9c1000f5896e63b89d665
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.9

Security for self-sentry 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Loss of provenance

A previous release of these files had provenance metadata, but this release does not.

While this could reflect a routine change by the project maintainers, it may also indicate that the release files have been compromised.

Release comparison

  • Removed 0.4.1 2 files With provenance
  • Added 0.5.0 2 files Without provenance

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files Loss of provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page