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 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

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.1.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.1-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: forgesight_fastapi-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 a43176deb0a909f9ba55db85c958fcd5f818faefc0685757affafe200b79103a
MD5 ed57dc97e99f011e944e30f8e6fe99ce
BLAKE2b-256 eade4eef46c7bdc48a902b504d91a436d6182b7149addc8c2facb077c7831c5f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for forgesight_fastapi-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5c0a3df60892e59bc81ffc17a3ebda584842f0ac248a59871e5b237ab12df23f
MD5 61cbabb940bdf1a3f651b2bb9d4aef4f
BLAKE2b-256 6632da40153f999ab14701e930f721495e0e6c9929e7703c103b7b5514e000ad

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