SDK · Python

Python

Standalone Python client for Bugwatch. No third-party dependencies — standard library only. Non-blocking: network I/O runs in a daemon thread with an 8 s timeout.

Install

Shell
pip install bugwatch-sdk-python
# from source:
# git clone https://github.com/roomylabs/bugwatch && pip install ./sdk/python/

Quickstart

Python
import bugwatch_sdk as bugwatch

bugwatch.init(
    "https://bw-YOUR_KEY@bugwatch-api.loadmindx.com/api/1",
    environment="production",
    release="v1.2.3",
    sample_rate=1.0,
)

# Capture the active exception.
try:
    1 / 0
except ZeroDivisionError:
    event_id = bugwatch.capture_exception()

# Capture an explicit exception with tags.
try:
    raise ValueError("invalid amount")
except ValueError as exc:
    bugwatch.capture_exception(exc, level="warning", tags={"module": "billing"})

# Capture a message.
bugwatch.capture_message("Payment confirmed", level="info")

# Sends run in a daemon thread — flush before exit.
bugwatch.flush()

Global scope

User, tags and context set on the global scope are attached to every subsequent event. Use configure_scope() for a local override.

Python
bugwatch.set_user({"id": "42", "email": "user@example.com"})
bugwatch.set_tag("feature", "checkout")
bugwatch.set_context({
    "request": {"method": "POST", "url": "/api/checkout"}
})

# Local scope for a block of code:
with bugwatch.configure_scope() as scope:
    scope.set_tag("tenant", "acme")
    scope.set_user({"id": "43"})

API

APIDescription
init(dsn, environment=None, release=None, sample_rate=1.0)Configure the DSN and default context.
capture_exception(exc=None, level='error', tags=None, user=None)Capture an exception (active one if exc is None). Returns event_id or None.
capture_message(message, level='info', tags=None, user=None)Capture a message. Returns event_id or None.
set_user(user)Set the current user.
set_tag(key, value)Add a current tag.
set_context(context)Merge context (optional request key).
flush(timeout=5.0)Wait for in-flight sends.
configure_scope()Context manager for the current scope.

init options: debug=False, send_default_pii=False, allow_http=False.

DSN format

DSN
https://{public_key}@{host}/api/{project_id}

# POSTs JSON to {dsn}/store/ with header:
#   X-Bugwatch-Key: {public_key}