Sentinel Python SDK — automatic exception capture and reporting for Python services.
Project description
sentinel-python-sdk
Automatic exception capture for Python services. When an unhandled exception escapes your application, this SDK captures it, scrubs PII, and reports it to the Sentinel gateway — without adding required dependencies or interfering with your existing error handling.
Requires Python 3.10+
Installation
# Core — stdlib only, zero required dependencies
pip install sentinel-python-sdk
# For FastAPI / async services
pip install "sentinel-python-sdk[fastapi]"
# For async dispatch only (without FastAPI)
pip install "sentinel-python-sdk[async]"
Quick start
Synchronous services (scripts, workers, Flask, Django, etc.)
import sentinel_sdk
sentinel_sdk.configure(
api_key="your-api-key",
github_repo="owner/repo",
)
sentinel_sdk.install_hook()
# Any unhandled exception from this point is automatically reported.
FastAPI
from fastapi import FastAPI
import sentinel_sdk
from sentinel_sdk import SentinelMiddleware
sentinel_sdk.configure(
api_key="your-api-key",
github_repo="owner/repo",
)
app = FastAPI()
app.add_middleware(SentinelMiddleware)
Each request path (e.g. /api/orders) is used as the endpoint field in the reported exception.
Configuration
sentinel_sdk.configure(
api_key="your-api-key", # Required. Bearer token for the gateway.
github_repo="owner/repo", # Required. Repository to associate exceptions with.
gateway_url="https://nexuspartner.dev", # Default gateway endpoint.
timeout_seconds=5.0, # HTTP request timeout.
max_retries=3, # Retry attempts on 5xx and network errors.
retry_backoff_base=0.5, # Base for exponential backoff (seconds).
endpoint_label="<unknown>", # Fallback endpoint for hook-captured exceptions.
enabled=True, # Set False to disable all I/O (also reads SENTINEL_ENABLED env var).
)
configure() can be called once at startup. Calling it again overwrites the singleton — do not rely on this in production.
Environment variable: Set SENTINEL_ENABLED=false (or 0, no, off) to disable the SDK without changing code. Useful in local development or CI.
How it works
Global exception hook
install_hook() replaces sys.excepthook and threading.excepthook. When an unhandled exception reaches either hook:
- The SDK builds a payload from the exception type, message, and traceback.
- PII is scrubbed from the message and traceback.
- The payload is dispatched to the gateway in a background daemon thread (non-blocking).
- The original hook is called — Python's default traceback printing is preserved.
sentinel_sdk.uninstall_hook() # Restores original hooks (safe to call at any time).
sentinel_sdk.hook.is_installed() # True if hooks are active.
ASGI middleware
SentinelMiddleware wraps each request handler. On an unhandled exception:
- The exception is captured and dispatched asynchronously.
- The exception is re-raised — your existing FastAPI error handlers and 500 responses are unaffected.
The SDK is an observer. It never swallows exceptions.
PII scrubbing
All exception messages and tracebacks are scrubbed before leaving the process. The following patterns are redacted:
| Data type | Replacement |
|---|---|
| Email addresses | [EMAIL] |
| Credit card numbers (Luhn-validated) | [CREDIT_CARD] |
| IPv4 addresses | [IP_ADDRESS] |
IPv6 addresses (2001:db8::1, ::1) |
[IP_ADDRESS] |
| Bearer / API tokens | [REDACTED_TOKEN] |
password=, api_key=, secret=, token=, etc. |
<key>=[REDACTED_SECRET] |
Scrubbing is stateless and never raises — if a pattern fails, the original text is returned unchanged. Credit card patterns are validated with a Luhn checksum before redaction to eliminate false positives on order IDs, timestamps, and other long numeric strings.
Retry behaviour
The dispatcher retries on:
- Network errors (connection refused, timeout, DNS failure)
- HTTP 5xx responses
- HTTP 429 (rate limited)
It does not retry on HTTP 4xx responses (authentication failures, bad payloads). These indicate a configuration problem and retrying would not help.
Backoff formula: backoff_base × 2^attempt + random_jitter
Dispatch errors are logged at WARNING level and never propagate to the caller.
Payload structure
Exceptions are reported as JSON matching the Sentinel gateway contract:
{
"exceptionType": "module.ClassName",
"errorMessage": "...",
"stackTrace": "...",
"endpoint": "/api/orders",
"timestamp": "2026-04-11T14:23:01.123456+00:00",
"githubRepo": "owner/repo",
"language": "python"
}
Development
git clone https://github.com/nicolaemorcov/sentinel-platform
cd sentinel-python-sdk
pip install -e ".[dev]"
python -m pytest
The test suite has 38 scrubber tests + additional module tests covering all modules. They run in under one second with no network calls.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sentinel_python_sdk-1.0.0.tar.gz.
File metadata
- Download URL: sentinel_python_sdk-1.0.0.tar.gz
- Upload date:
- Size: 21.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ac6d2bb4e708df18146168606521684f9562b82fb555baec8fb359dc7a715bc
|
|
| MD5 |
53087fa432bf826ca832df939b37396f
|
|
| BLAKE2b-256 |
4c52c2c1344f1fe8a6886997462fda4cd24466e0078dff865154dd6ce528d16c
|
File details
Details for the file sentinel_python_sdk-1.0.0-py3-none-any.whl.
File metadata
- Download URL: sentinel_python_sdk-1.0.0-py3-none-any.whl
- Upload date:
- Size: 18.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4f891fccb64604cd9fd2fff44d0e00e8e8291e29658c1f241beceecffabf0000
|
|
| MD5 |
097b6c7b18ca7cf0db1a575bd80d5d91
|
|
| BLAKE2b-256 |
ccf9a421190273c0bd8cceea3e9b26840a18cfe4b4d21817de6d42851783aaae
|