> ## Documentation Index
> Fetch the complete documentation index at: https://www.truefoundry.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Add distributed Tracing to FastAPI applications

> Implement distributed tracing across multiple FastAPI microservices using the Traceloop SDK and OpenTelemetry.

This guide demonstrates how to use the TrueFoundry OtelCollector in combination with the Traceloop SDK to capture distributed traces in distributed FastAPI applications.

In this example, we’ll build two simple FastAPI services. The first service exposes a `/say-hello` endpoint, which internally calls the `/generate-greeting` endpoint on the second service.

<Steps>
  <Step title="Create Tracing Project, API Key and copy tracing code">
    Follow the instructions in [Getting Started](/docs/tracing/tracing-getting-started) to create a tracing project, generate API key and copy the
    tracing code.
  </Step>

  <Step title="Install Dependencies">
    First, you need to install the following

    ```shell lines theme={"dark"}
    pip install fastapi uvicorn dotenv opentelemetry-instrumentation-fastapi==0.55b0 traceloop-sdk opentelemetry-instrumentation-httpx
    ```
  </Step>

  <Step title="Add Tracing code to FastAPI application 1">
    For FastAPI applications, we need to add the `Traceloop.init()` call to initialize tracing and instrument both the FastAPI app and httpx client with OpenTelemetry to capture distributed traces.

    ```python FastAPI Code {3-4,10-11,15-23,40-45} lines theme={"dark"}
    from dotenv import load_dotenv
    from fastapi import FastAPI
    from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
    from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor

    import uvicorn
    import os
    import httpx

    # importing traceloop sdk
    from traceloop.sdk import Traceloop

    load_dotenv()

    # Add the traceloop init code to your application
    TFY_API_KEY = os.environ.get("TFY_API_KEY")
    Traceloop.init(
        api_endpoint="<enter_your_api_endpoint>",
        headers = {
            "Authorization": f"Bearer {TFY_API_KEY}",
            "TFY-Tracing-Project": "<enter_your_tracing_project_fqn>",
        },
    )

    app = FastAPI()

    # httpx client
    client = httpx.AsyncClient()

    @app.get("/say-hello")
    async def root():
        try:
            response = await client.get("http://localhost:8000/generate-greeting")
            return response.json()
        except httpx.HTTPStatusError as e:
            return {"error": f"Upstream error: {e.response.status_code}", "body": e.response.text}
        except ValueError:
            return {"error": "Invalid JSON response from upstream"}

    # Instrument the FastAPI app
    FastAPIInstrumentor.instrument_app(app)

    # Allows tracing Http requests made by the httpx library
    HTTPXClientInstrumentor().instrument()

    if __name__ == "__main__":
        uvicorn.run("main:app", host="0.0.0.0", port=8001, reload=True, log_level="debug")
    ```
  </Step>

  <Step title="Add Tracing code to FastAPI application 2">
    For FastAPI applications, we need to add the `Traceloop.init()` call to the application and instrument the FastAPI app with OpenTelemetry.

    ```python FastAPI Code {3,7-8,12-20,28-29} lines theme={"dark"}
    from dotenv import load_dotenv
    from fastapi import FastAPI
    from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
    import uvicorn
    import os

    # importing traceloop sdk
    from traceloop.sdk import Traceloop

    load_dotenv()

    # Add the traceloop init code to your application
    TFY_API_KEY = os.environ.get("TFY_API_KEY")
    Traceloop.init(
        api_endpoint="<enter_your_api_endpoint>",
        headers = {
            "Authorization": f"Bearer {TFY_API_KEY}",
            "TFY-Tracing-Project": "<enter_your_tracing_project_fqn>",
        },
    )

    app = FastAPI()

    @app.get("/generate-greeting")
    async def root():
        return {"message": "Hi there, wishing you a great day!"}

    # Instrument the FastAPI app
    FastAPIInstrumentor.instrument_app(app)

    if __name__ == "__main__":
        uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True, log_level="debug")
    ```

    <Note>
      Make sure to use the same tracing project fqn in the above 2 fastapi applications
    </Note>
  </Step>

  <Step title="View traces">
    1. Start both FastAPI applications
    2. Hit the endpoint `http://localhost:8081/say-hello`
    3. View the logged trace in your TrueFoundry dashboard

    <Frame caption="">
      <img src="https://mintcdn.com/truefoundry/PSBc0bX31_cIC7pm/images/distributed_tracing.png?fit=max&auto=format&n=PSBc0bX31_cIC7pm&q=85&s=7b44da37a2f926d38f12549e4247d132" width="3024" height="1714" data-path="images/distributed_tracing.png" />
    </Frame>
  </Step>
</Steps>
