Metadata-Version: 2.4
Name: nolax-sdk
Version: 1.1.0
Summary: Nolax API observability SDK for Python
License-Expression: MIT
Keywords: nolax,observability,middleware,flask,django,fastapi
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Developers
Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: flask
Requires-Dist: flask>=2.0; extra == "flask"
Provides-Extra: django
Requires-Dist: django>=4.2; extra == "django"
Provides-Extra: fastapi
Requires-Dist: fastapi>=0.95; extra == "fastapi"
Requires-Dist: starlette>=0.27; extra == "fastapi"
Provides-Extra: dev
Requires-Dist: flask>=2.0; extra == "dev"
Requires-Dist: django>=4.2; extra == "dev"
Requires-Dist: fastapi>=0.95; extra == "dev"
Requires-Dist: httpx>=0.24; extra == "dev"
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-asyncio>=0.21; extra == "dev"
Requires-Dist: black>=23.0; extra == "dev"
Dynamic: license-file

# nolax-sdk (Python)

Python SDK for [Nolax](https://server.nolax.net), API observability for your applications. It installs as
middleware in Flask, Django or FastAPI, captures request metadata (method, route, status, latency, size) and
sends it to Nolax in batches from a background thread, without slowing your application.

- Python 3.8 or newer, no runtime dependencies.
- Flask 2.0+, Django 4.2+, FastAPI/Starlette 0.95+ (install whichever you use; none is required).

## Installation

```bash
pip install nolax-sdk
```

## Quick start

### Flask

```python
from flask import Flask
from nolax import NolaxFlask

app = Flask(__name__)
NolaxFlask(api_key="nlx_proj_xxx", environment="production").init_app(app)
```

### FastAPI

```python
from fastapi import FastAPI
from nolax import NolaxFastAPI

app = FastAPI()
app.add_middleware(NolaxFastAPI, api_key="nlx_proj_xxx", environment="production")
```

### Django

```python
# settings.py
MIDDLEWARE = [
    # ...
    "nolax.middleware.NolaxDjango",
]
NOLAX_API_KEY = "nlx_proj_xxx"
NOLAX_ENVIRONMENT = "production"
```

Every other option is a setting named `NOLAX_<OPTION>`, for example `NOLAX_BATCH_SIZE` or `NOLAX_IP_HEADER`.

## Options

Option names match case-insensitively and ignore underscores, so `flush_interval_ms`, `flushIntervalMs` and
`FlushIntervalMs` are the same option.

| Option | Default | Description |
|---|---|---|
| `api_key` | required | Your Nolax project API key |
| `environment` | required | `development`, `staging` or `production` (case-insensitive). Any other value logs one error and disables the SDK |
| `endpoint` | `https://server.nolax.net/ingest` | Override for staging and load tests |
| `batch_size` | `100` | Events that trigger a flush |
| `flush_interval_ms` | `2000` | Milliseconds between timer-based flushes. The old `flush_interval` (seconds) still works and is deprecated |
| `max_queue_size` | `10000` | Events held in memory; the oldest are dropped first |
| `max_in_flight` | `4` | Batches that may wait for the sender thread. Python sends one batch at a time from one thread over one persistent connection |
| `release` | none | Release identifier, sent once per batch (max 64 characters) |
| `service` | none | Service name, sent once per batch (max 64 characters) |
| `consumer` | none | `callable(request) -> str` returning a consumer id (max 64 characters). Receives the Flask or Django request, or the ASGI scope |
| `capture_ip` | `True` | When `False`, the client IP is never sent |
| `capture_user_agent` | `True` | When `False`, the User-Agent is never sent |
| `ip_header` | none | Header holding the client IP behind a proxy, for example `x-forwarded-for` (first entry) or `cf-connecting-ip`. Used only when set |
| `ignore_paths` | none | Strings or compiled patterns; matching requests are never recorded, for example `"/health"` |
| `ignore_methods` | none | Methods that are never recorded, for example `["OPTIONS"]` |
| `before_send` | none | `callable(event) -> event or None`. Modify an event, or return `None` to drop it. Must be synchronous. If it raises, the event is dropped |
| `dry_run` | `False` | Print each batch that would be sent (API key masked as `nlx_proj_****`) and send nothing. `NOLAX_DRY_RUN=1` does the same |
| `debug` | `False` | Verbose logging through the `nolax` logger. Never prints the API key |
| `on_error` | none | `callable(exception)` invoked when a batch is dropped |

## Behaviour worth knowing

- `payloadSize` is the response `Content-Length` header, or 0 when it is absent (for example streamed responses).
  The SDK never reads a response body, so streaming responses are not consumed.
- If your view raises, the event is recorded with status 500 and the exception class name (never the message).
  The exception is re-raised unchanged.
- On a rejected API key or a reached plan limit the SDK pauses sending for 60 seconds, doubling up to 15 minutes,
  and discards events meanwhile.
- The server can adjust batch size, flush interval and backoff through response hints, within fixed bounds.
- The flush timer and sender thread start on first use and start again in each forked worker, so gunicorn and
  uWSGI with preloaded apps work.
- On interpreter exit the SDK flushes with a 2 second limit and then stops waiting. It installs no signal
  handlers. Call `flush()` yourself if your process exits another way:

```python
nolax = NolaxFlask(api_key="nlx_proj_xxx", environment="production")
nolax.flush(2.0)
```

## Development

```bash
pip install -e ".[dev]"
pytest
```

## Ingest contract

Version 1.1 (additive over 1.0). Older servers keep working.
