Skip to main content

ForgeSight FastAPI integration — ASGI middleware + lifespan flush; request↔run correlation.

Project description

forgesight-fastapi

The FastAPI integration for ForgeSight. Three lines wire request↔run correlation, incoming-trace continuation, and flush-on-shutdown into any FastAPI / Starlette app — no per-handler instrumentation.

pip install forgesight-fastapi
from fastapi import FastAPI
from forgesight_fastapi import AgentForgeMiddleware, sdk_lifespan

app = FastAPI(lifespan=sdk_lifespan)        # configure() on startup, flush on shutdown
app.add_middleware(AgentForgeMiddleware)     # request → agent_run span, correlation

@app.post("/agents/pr-reviewer/run")
async def run(req: ReviewRequest):
    # Unchanged agent code. The run span is already open and bound to this request;
    # the agent's llm/tool/mcp calls nest under it automatically.
    return await pr_reviewer.run(req.task)

Compose with an existing lifespan:

from contextlib import asynccontextmanager
from forgesight_fastapi import sdk_lifespan

@asynccontextmanager
async def lifespan(app):
    async with sdk_lifespan(app):
        await connect_db()
        yield
        await close_db()

app = FastAPI(lifespan=lifespan)

What you get

  • Request↔run link. The HTTP request and the agent run share a trace_id; the response carries the run_id (header x-agentforge-run-id), so "request X was slow" jumps straight to the run's span tree and cost.
  • Distributed traces just work. An upstream traceparent is continued automatically — the agent service is a child span, not a new root. No propagation code in the app.
  • Zero lost telemetry on deploy. sdk_lifespan calls force_flush() + shutdown() on the shutdown phase (which ASGI servers run on SIGTERM), with a bounded timeout so a wedged backend can't hang the deploy.
  • Route-level metadata for free. The matched route template (/agents/{id}/run, not the raw path — bounded cardinality), method, and status land as span metadata (FR-5).
  • Correct error mapping. 5xx ⇒ span ERROR + error.type; an unhandled exception is recorded and re-raised (FR-7); 4xx is recorded without erroring the span.

Implemented as pure ASGI (not BaseHTTPMiddleware) to avoid streaming/lifespan pitfalls. Request/response bodies are captured only when capture_content resolves true (P7).

Configuration

Key Env Default
span_kind FORGESIGHT_FASTAPI_SPAN_KIND agent_run (or workflow_run)
exclude_paths FORGESIGHT_FASTAPI_EXCLUDE /health,/healthz,/metrics,/docs,/openapi.json
capture_content FORGESIGHT_FASTAPI_CAPTURE_CONTENT false
run_id_header FORGESIGHT_FASTAPI_RUN_ID_HEADER x-agentforge-run-id

Constructor kwargs win over env / forgesight.yaml (integrations.fastapi).

License

Apache-2.0

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

forgesight_fastapi-0.1.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

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

forgesight_fastapi-0.1.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file forgesight_fastapi-0.1.0.tar.gz.

File metadata

  • Download URL: forgesight_fastapi-0.1.0.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for forgesight_fastapi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f1e385e1c23c038b15102056b9fbd2c8325b28752b79d88377bfad581f31f7a5
MD5 cd60460e257ec215c42cb8009c596f71
BLAKE2b-256 d2bee2845aa87123b26fceadab2fc6fc90893fae4e88c411e3dc0f7c8d5ec980

See more details on using hashes here.

File details

Details for the file forgesight_fastapi-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for forgesight_fastapi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 421f500c90b784f5ee4ea5d8e0d0f48d1276b163b6dfe112eecd585de8525dde
MD5 3331965054cf7add6a0b6d6829682cc5
BLAKE2b-256 5baab1a49d64e008290439099cf5c5a6ca178a3c11a4e1be243f56ce649ce5f0

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