Skip to main content

Drop-in error reporter for ghosttrap.io

Project description

ghosttrap-sdk

Error reporting for Python apps, built for AI agents. Part of ghosttrap.

Install

pip install ghosttrap-sdk

Use

import ghosttrap
ghosttrap.init("t_your_token_here")

Get your token by running ghosttrap setup from ghosttrap-cli.

Optional kwargs

ghosttrap.init(
    "t_your_token_here",
    server="https://ghosttrap.io",  # override only if self-hosting
    send_user=False,                # see "User context" below
)

What it hooks into

  • sys.excepthook — unhandled exceptions
  • Python logginglogger.exception() and logger.error(..., exc_info=True)
  • Celery — task failures via celery.signals.task_failure (auto-detected)

What it sends

Every report includes the exception type, message, traceback, frames (file/line/function/code), and the server's hostname (socket.gethostname()).

Django

INSTALLED_APPS = [
    ...
    "ghosttrap.django.GhostTrapApp",
]

MIDDLEWARE = [
    "ghosttrap.django.GhostTrapMiddleware",
    ...
]

Browser / JavaScript errors

Errors in your users' browsers never reach the Python hooks. The Django integration ships a small relay so they land in the same stream. Two lines:

# urls.py
path("ghosttrap/", include("ghosttrap.django.urls")),
<!-- base template -->
<script src="{% static 'ghosttrap/ghosttrap.js' %}" defer></script>

The script hooks uncaught errors and unhandled promise rejections, drops known junk (opaque cross-origin "Script error.", browser-extension noise), deduplicates per page, and caps itself at 10 reports per page load. It posts to your own domain — the ghosttrap token never appears in page source — and the relay view forwards through the endpoint you configured with init(). No CORS setup, and ad blockers don't see a third-party request.

Browser events arrive with the JS error type (e.g. TypeError), the page URL in the traceback header, and the parsed JS stack as frames. Minified bundles produce minified frames — there's no source-map support. If you mount the relay somewhere other than /ghosttrap/, point the script at it with data-endpoint="/your/path/js/".

Manually trap an event

For errors you catch and handle but still want logged, or for non-exception conditions worth flagging:

import ghosttrap

try:
    do_thing()
except ValueError as e:
    ghosttrap.trap(e)        # caught exception — sent with type, message, traceback
    fallback()

ghosttrap.trap("payment gateway returned 503")  # synthetic event labelled "TrappedEvent"

trap() accepts an exception instance or a string. Strings get the caller's stack attached and a TrappedEvent type so they're distinct from real exceptions in the CLI. Both go through the same 5-minute dedup window as the auto-hook.

User context

Off by default. Pass send_user=True to init() and the Django middleware will attach the authenticated user's id and username to each report. Has no effect outside Django.

ghosttrap.init("t_your_token_here", send_user=True)

Zero dependencies

Pure Python stdlib. No transitive dependencies in your production image.

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

ghosttrap_sdk-0.4.6.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

ghosttrap_sdk-0.4.6-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file ghosttrap_sdk-0.4.6.tar.gz.

File metadata

  • Download URL: ghosttrap_sdk-0.4.6.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ghosttrap_sdk-0.4.6.tar.gz
Algorithm Hash digest
SHA256 f5d2816d86567ac0a28547fed68131a50ca3ec4544c586da060159e8e54f1a51
MD5 dc7c0ec4cb2a63547729e64926907e0f
BLAKE2b-256 5d973ec3a7ec3718320acc1f161dbf3dcafd595afcf951cee76afbf1555b9bbb

See more details on using hashes here.

File details

Details for the file ghosttrap_sdk-0.4.6-py3-none-any.whl.

File metadata

  • Download URL: ghosttrap_sdk-0.4.6-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ghosttrap_sdk-0.4.6-py3-none-any.whl
Algorithm Hash digest
SHA256 fb2b92133da00f9137822bec918ca5f19e6084c1bacc21758447ab3ed2e6a19d
MD5 ff0c59e0c0e9362480d7940421f2713f
BLAKE2b-256 1af5ef0da2f766e72fb145d7b6520d3798896dfb68cacad08161bf9131f094e7

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