Skip to main content

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 ForgeSightMiddleware, sdk_lifespan

app = FastAPI(lifespan=sdk_lifespan)        # configure() on startup, flush on shutdown
app.add_middleware(ForgeSightMiddleware)     # 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-forgesight-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-forgesight-run-id

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

License

Apache-2.0

Metadata

Release files for forgesight-fastapi 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for forgesight-fastapi 0.1.1
File Size Uploaded
forgesight_fastapi-0.1.1.tar.gz 10.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for forgesight-fastapi 0.1.1
File Interpreter ABI Platform
forgesight_fastapi-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.6 kB

Release files / forgesight_fastapi-0.1.1.tar.gz

Download URL forgesight_fastapi-0.1.1.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a43176deb0a909f9ba55db85c958fcd5f818faefc0685757affafe200b79103a
BLAKE2b-256 checksum
How to use checksums
eade4eef46c7bdc48a902b504d91a436d6182b7149addc8c2facb077c7831c5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Release files / forgesight_fastapi-0.1.1-py3-none-any.whl

Download URL forgesight_fastapi-0.1.1-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5c0a3df60892e59bc81ffc17a3ebda584842f0ac248a59871e5b237ab12df23f
BLAKE2b-256 checksum
How to use checksums
6632da40153f999ab14701e930f721495e0e6c9929e7703c103b7b5514e000ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page