TraceHawk // browser, PHP, Python, and Go errors Group · Count · File the ticket Ingest operational

Python

Python exception monitoring.

The TraceHawk package is a small Python 3.9 client. You point it at a project, capture the exception, and it keeps the stack, the cause, and a trace id.

What it does

A plain Python app calls the client itself. TraceHawk.init reads the DSN. TraceHawk.capture sends one exception. TraceHawk.track_request sends the HTTP request that just finished. TraceHawk.log sends one application log. Django and Flask call the first two for you.

The report includes the exception class and message, up to 40 frames, up to five causes, the release, the environment, a user id if you set one, and up to 30 breadcrumbs. Frames inside site-packages and the client itself are marked outside the application so grouping can prefer your code.

Install

pip install https://tracehawk.net/sdk/python/tracehawk-1.0.0-py3-none-any.whl
TRACEHAWK_DSN=https://tracehawk.net/api/<key>
import os
import sys
from tracehawk import TraceHawk

TraceHawk.init(os.environ.get("TRACEHAWK_DSN", ""))
TraceHawk.ignore(["/ping", "/health*"])
TraceHawk.set_environment("production")
TraceHawk.set_release("app@1.0.0")

def _report(exc_type, exc, tb):
    if exc is not None:
        TraceHawk.capture(exc)
    sys.__excepthook__(exc_type, exc, tb)

sys.excepthook = _report

Put that where the process boots, before your own code runs. ignore lists paths that are not sent. * is a wildcard. /ping matches that path only. Call it after init. A WSGI app should also call track_request when the request ends. Django and Flask do that.

Django

Add the middleware and set TRACEHAWK_DSN. The middleware reports an uncaught view exception and the request. HTTP 404 and other client errors are not reported. TRACEHAWK_IGNORE is the same comma-separated list as ignore.

MIDDLEWARE = [
    # ...
    "tracehawk.django.TraceHawkMiddleware",
]

Flask

One call after the app exists. The hook reports uncaught exceptions and each request. abort(404) is not reported.

from tracehawk.flask import install

install(app)

Application logs

Attach LogHandler to the logger you already use. Existing logger.info and logger.error calls stay as they are. A line does not open an issue. Each line counts as a request, on the same meter as an HTTP request.

import logging
from tracehawk.logging import LogHandler

logging.getLogger("app").addHandler(LogHandler())
logging.getLogger("app").setLevel(logging.INFO)

session_id, sessionId, session_uuid, user_id, and userId are copied out of the context so Logs can filter on them. TraceHawk.set_user fills user_id when the context has none. A plain call does the same thing when you are not using the logging module.

TraceHawk.log("error", "Convert failed", {"sessionId": "sess-1", "error": "no audio"})

A handled exception

try:
    payment.charge()
except Exception as error:
    TraceHawk.add_breadcrumb("payment", "Charge failed")
    TraceHawk.capture(error)
    raise

Supported features

The Python client does not collect SQL or cache spans on its own.

Troubleshooting

Questions

Do application logs count as errors?

No. A line sent through LogHandler or TraceHawk.log counts as a request, the same meter as an HTTP request. It does not open an issue.

Do I have to use Django or Flask?

No. The same package works from plain Python if you call init, capture, and track_request yourself. Django and Flask are shortcuts for those calls.

What Python version does the client require?

Python 3.9 or newer, as declared by tracehawk 1.0.0.

Will secrets in local variables be stored?

Argument names that look like a password, secret, token, credential, or API key are stored as [Filtered]. Do not put secrets in the exception message, tags, or user id.

Related

Ready to turn error noise into actionable issues?