This guide demonstrates how to use TrueFoundry OtelCollector along with the Traceloop SDK to instrument Python code.

In this example, we’ll show how to instrument a simple Python application with nested function calls using Traceloop’s task decorators.

1

Install Dependencies

First, you need to install the following

pip install traceloop-sdk==0.38.12 dotenv
2

Setup environment variables

To enable tracing, you’ll need to configure a few environment variables in your application.

Before proceeding, make sure you’ve, Created a tracing project and Generated an API token. If you haven’t done this yet, follow the instructions in Getting Started.

# Tracing configs
TRACELOOP_BASE_URL=<<control-plane-url>>/api/otel
TRACELOOP_HEADERS="Authorization=Bearer%20<<api-key>>,tfy-tracing-project=<<tracing-project-fqn>>"

Replace the placeholders above:

  • <<control-plane-url>>: Your actual TrueFoundry control plane URL
  • <<api-key>>: The API key associated with your tracing project
  • <<tracing-project-fqn>>: The fully qualified name of your tracing project
3

Initialise instrumentation

from traceloop.sdk import Traceloop
from dotenv import load_dotenv

# Load environment variables from a .env file
load_dotenv()

# Initialize Traceloop (this uses console exporter by default if not configured)
Traceloop.init(disable_batch=True)
4

Instrument your Python code

This section shows how to instrument your Python code using Traceloop’s task decorators. The example demonstrates nested function calls with proper tracing.

Tracing related code has been highlighted in the below code block.

from traceloop.sdk.decorators import task

# Child function — will create its own span
# Traceloop task decorator
@task(name="greet_user_task")
def greet_user(name):
    print(f"Hello, {name}!")

# Parent function — also creates a span and calls the child
# Traceloop task decorator
@task(name="main_task")
def main():
    user_name = "Sateesh"
    greet_user(user_name)
    print("Main task finished.")

# Run the main function
main()
5

Run your application and view logged trace