> ## 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 Tracing to FastAPI applications

> Add tracing instrumentation to FastAPI applications using the Traceloop SDK for request observability.

This guide demonstrates how to use TrueFoundry OtelCollector along with the Traceloop SDK to instrument FastAPI applications.
In this example, we'll create a simple FastAPI application with a `/greet` endpoint that demonstrates how to integrate TrueFoundry's tracing capabilities.

<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
    ```
  </Step>

  <Step title="Add Tracing code to FastAPI application">
    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-21,29-30} 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>",
        app_name="your tracing application name",
        headers = {
            "Authorization": f"Bearer {TFY_API_KEY}",
            "TFY-Tracing-Project": "<enter_your_tracing_project_fqn>",
        },
    )

    app = FastAPI()

    def get_random_greeting():
        greetings = ["Hello", "Hi", "Hey", "Good morning", "Good afternoon", "Good evening"]
        return random.choice(greetings)

    @app.get("/generate-greeting")
    async def root():
        greeting = get_random_greeting()
        return {"message": f"{greeting}, 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")
    ```
  </Step>

  <Step title="Run your application and view logged trace">
    <Frame caption="">
      <img src="https://mintcdn.com/truefoundry/yRoKH_fkKi2nPtuV/images/fastapi-tracing.png?fit=max&auto=format&n=yRoKH_fkKi2nPtuV&q=85&s=13b7e571dd4b0c28c98c0260e06b98f6" width="3012" height="1716" data-path="images/fastapi-tracing.png" />
    </Frame>
  </Step>

  <Step title="Trace custom internal functions">
    OpenTelemetry’s FastAPI instrumentation automatically handles tracing for incoming HTTP requests to your ASGI app. It creates and completes spans for the request/response lifecycle without any additional setup.

    However, it does not automatically trace other parts of your application, such as internal function calls, outgoing HTTP requests to other services, background tasks or database queries.

    To trace such operations, you can use OpenTelemetry's [existing instrumentation libraries](https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation) . For example, if your app makes HTTP requests using `httpx`, you can use the [`opentelemetry-instrumentation-httpx`](https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-httpx) package. The same GitHub repository provides instrumentation libraries for many popular Python frameworks and libraries.
    For a broader overview of how to integrate these libraries into your app, refer to our [Distributed Tracing](/docs/tracing/distributed-tracing) guide, which also includes examples with `httpx` instrumentation.

    If you're working with **custom internal logic** that isn't covered by existing instrumentation, you can use **Traceloop's decorators** to manually trace specific functions.

    Here’s an example of adding tracing to an internal function using the `@task` decorator:

    ```python FastAPI Code {3,8-10,14-23,27,37-38} lines theme={"dark"}
    from dotenv import load_dotenv
    from fastapi import FastAPI
    from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
    import uvicorn
    import os
    import random

    # importing traceloop sdk
    from traceloop.sdk import Traceloop
    from traceloop.sdk.decorators import task

    load_dotenv()

    # Add the traceloop init code to your application
    TFY_API_KEY = os.environ.get("TFY_API_KEY")
    Traceloop.init(
        api_endpoint="https://internal.devtest.truefoundry.tech/api/otel",
        app_name="your tracing application name",
        headers = {
            "Authorization": f"Bearer {TFY_API_KEY}",
            "TFY-Tracing-Project": "tracing-project:truefoundry/Tracing-test/fastapi",
        },
    )

    app = FastAPI()

    @task(name="get_random_greeting")
    def get_random_greeting():
        greetings = ["Hello", "Hi", "Hey", "Good morning", "Good afternoon", "Good evening"]
        return random.choice(greetings)

    @app.get("/generate-greeting")
    async def root():
        greeting = get_random_greeting()
        return {"message": f"{greeting}, 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")
    ```
  </Step>

  <Step title="Run your application and view traces, including custom function spans">
    <Frame caption="">
      <img src="https://mintcdn.com/truefoundry/yRoKH_fkKi2nPtuV/images/fastapi-tracing-custom-function.png?fit=max&auto=format&n=yRoKH_fkKi2nPtuV&q=85&s=fee59e2951bdc89ddc97ed8fbabe3810" width="3024" height="1714" data-path="images/fastapi-tracing-custom-function.png" />
    </Frame>
  </Step>
</Steps>
