UK-1 Datacenter
|
Back to Knowledgebase
Py
PyPI trustport-beacon v1.8.0

Python & FastAPI SDK Guide

The TrustPort Beacon Python SDK provides ASGI & WSGI automatic trace propagation with SQLAlchemy query timing, Celery worker task telemetry, and zero async loop blocking.

1Install Package

Install via pip or poetry:

pip install trustport-beacon

2FastAPI ASGI Blueprint

main.pyPython 3.10+
from fastapi import FastAPI, Request
from trustport_beacon import BeaconMiddleware, beacon_span

app = FastAPI(title="Sovereign AI Engine")

# Register ASGI Telemetry Middleware
app.add_middleware(
    BeaconMiddleware,
    ingest_url="http://localhost:8443", # Or UK-1 Sovereign Zone Endpoint
    api_key="tp_live_9f8a3c4e12",
    service_name="python-ml-service",
    environment="production"
)

@app.get("/health")
async def health():
    return {"status": "healthy"}

@app.post("/api/v1/predict")
async def predict(request: Request):
    # Contextual child span for model inference
    with beacon_span("ml.inference.bert_classifier") as span:
        span.set_tag("model_version", "v2.4")
        # Perform computation...
        return {"label": "fintech_risk_low", "confidence": 0.98}

3Django & Celery Setup

Django settings.py

Add 'trustport_beacon.django.BeaconMiddleware' to your Django MIDDLEWARE array.

Questions about Gunicorn / Uvicorn worker settings?

Our engineers can help configure multi-process fork hooks.

Consult Python Architect