Metadata-Version: 2.4
Name: bugle-sdk
Version: 1.1.2
Summary: Automatic request reporting middleware for Bugle
Author: Joe Georgeo Slamie
Project-URL: Homepage, https://jgeeking.com/
Keywords: bugle,wsgi,middleware,monitoring,fly
Requires-Python: >=3.9
Description-Content-Type: text/markdown

# Bugle SDK

`bugle-sdk` is lightweight WSGI middleware that reports completed application
requests to a [Bugle](https://buggle.fly.dev) server. It works with Flask,
Django, and other WSGI applications. Reporting runs asynchronously and is
best-effort, so a reporting failure does not change the application's response.

## Install

```sh
pip install bugle-sdk
```

## Usage

Wrap your WSGI application once during startup. For Flask:

```python
from flask import Flask
from bugle_sdk import BugleMiddleware

app = Flask(__name__)
app.wsgi_app = BugleMiddleware(app.wsgi_app)
```

### Endpoint labels and pseudonymous user fingerprints

The SDK assigns every event a random `request_id` and a non-empty fingerprint.
For Flask route templates (for example, `/users/<user_id>` instead of
`/users/42`) and a stable pseudonymous user identifier, set request metadata in
an `after_request` hook:

```python
from flask import g, request

@app.after_request
def attach_bugle_context(response):
    request.environ["bugle.endpoint"] = (
        request.url_rule.rule if request.url_rule else request.path
    )

    # Adapt this lookup to the app's authentication system.
    user = getattr(g, "current_user", None)
    user_id = getattr(user, "id", None)
    if user_id is not None:
        request.environ["bugle.identity"] = str(user_id)
    return response
```

Set `BUGLE_FINGERPRINT_SECRET` to a long, random secret shared by the app's
instances. The SDK combines that secret with the app name and the identity
provided by the app, then sends only an HMAC-SHA256 `user_fingerprint` with
`fingerprint_scope: "user"`; it does not send the raw user ID, IP address, or
user-agent. If the app does not supply an identity or the secret is absent, the
SDK still sends a unique fingerprint for that request with
`fingerprint_scope: "request"`. That fallback does not identify the same
anonymous user across requests. For anonymous user tracking, an app may provide
a consented, first-party random device ID as `bugle.identity`.

To keep a real-time or other background polling route out of organic request
counts, mark it as browser traffic in the same hook. The SDK marks the standard
`/socket.io` path as browser traffic automatically; custom routes must be
identified by the app:

```python
if request.path.startswith(("/api/realtime/", "/api/notifications/poll")):
    request.environ["bugle.traffic_type"] = "browser"
```

Set `request.environ["bugle.traffic_type"] = "user"` only when an endpoint
should override the backend's browser-route fallback. The backend recognizes
`browser` and `user`; an unset value uses its safe-method route heuristics.

`request_id` identifies one request. `user_fingerprint` is always present;
`fingerprint_scope` says whether it identifies a pseudonymous app user or only
that request. Do not derive identity from IP address or browser/device
characteristics.

For Django, wrap the WSGI application in your WSGI entry point (commonly
`config/wsgi.py`):

```python
from django.core.wsgi import get_wsgi_application
from bugle_sdk import BugleMiddleware

application = get_wsgi_application()
application = BugleMiddleware(application)
```

## Configuration

On Fly.io, the SDK obtains a short-lived machine identity token automatically.
The app, machine, and organization are identified from that token.

For local testing or other non-Fly environments, configure the Bugle server URL,
the app name registered in Bugle, and the ingestion key using environment
variables:

```sh
export BUGLE_URL="http://localhost:8000"
export BUGLE_APP="my-app"
export BUGLE_INGESTION_KEY="your-local-ingestion-key"
```

`BUGLE_URL` is optional on Fly and defaults to the SDK's configured Bugle URL.
`BUGLE_APP` defaults to `FLY_APP_NAME`. `BUGLE_INGESTION_KEY` is intended only
as a shared-secret fallback outside Fly; keep it secret. Without Fly identity or
this key, the SDK does not send reports.

For direct configuration, `BugleMiddleware` also accepts `bugle_url`,
`app_name`, `machine`, and `ingestion_key` keyword arguments.

## Reported data

For each completed request, the SDK sends the app name, machine ID, HTTP method,
URL path, endpoint label, request ID, fingerprint and its scope, response status
code, duration in milliseconds, and an optional `traffic_type` label to the
Bugle `/api/hit` endpoint. It does not include request or response bodies.

## Links

- [Bugle](https://buggle.fly.dev)
- [Source code](https://github.com/Jslamie2/buggle-sdk)
- [Author website](https://jgeeking.com/)
