Traceloop instrumentation

To collect metrics, traces and logs from a Python application, Traceloop can be easily integrated by using its SDK or OpenTelemetry instrumentation. By adding a few lines of code, you can instrument a Python application to capture detailed traces of LLM interactions, API calls, and workflow performance.

Create a Python virtual environment

Make sure that Python 3.10 or later is installed. To check the Python version, run the following command:

python3 -V

Optional: Create a virtual environment for your applications to keep your dependencies consistent and prevent conflicts with other applications. To create a virtual environment, run the following command:

pip3 install virtualenv
virtualenv <virtual-env-name>
source <virtual-env-name>/bin/activate

Traceloop setup

  1. Install Traceloop SDK by running the following command:

    pip3 install traceloop-sdk==0.38.7
    
  2. To start instrumenting your code, add the following line to your code:

    from traceloop.sdk import Traceloop
    Traceloop.init()
    
  3. If you are running your application locally, you can disable the batch sending to see the traces immediately:

    Traceloop.init(disable_batch=True)
    
  4. Annotate your workflows:

    • If you have complex workflows or chains, you can annotate them to get a better understanding of what is going on. You can see the complete trace of your workflow on Traceloop or any other dashboard that you use. Use the @workflow decorator for synchronous functions or the @aworkflow decorator for asynchronous methods. For example, if you have a function that renders a prompt and calls an LLM, add @workflow. For more information about decorators, see Decorators.

    • If you use an LLM framework like Haystack, Langchain, or LlamaIndex, you do not need to add any annotations to your code.

      from traceloop.sdk.decorators import workflow
      @workflow(name="suggest_answers")
      def suggest_answers(question: str):
      

    For more information on annotations, see Annotations and Traceloop documentation.

Configuring the environment

Set up your environment to send traces and metrics to Instana by using either an agent or by connecting directly to the Instana backend in agentless mode. To locate the domain names for the Instana backend OTLP acceptor in different Instana SaaS environments, see Instana backend otlp-acceptor endpoints.

To export traces and metrics to Instana by using an Instana agent, run the following command:

export TRACELOOP_BASE_URL=<instana-agent-host>:4317
export OTEL_EXPORTER_OTLP_INSECURE=true

To export traces and metrics directly to the Instana backend (agentless mode), run the following command:

export TRACELOOP_BASE_URL=<instana-otlp-endpoint>:4317
export TRACELOOP_HEADERS="x-instana-key=<agent-key>,x-instana-host=<instana-host>"
export OTEL_EXPORTER_OTLP_INSECURE=false

The following environment variables apply to both agent-based and agentless modes:

export TRACELOOP_LOGGING_ENDPOINT=$TRACELOOP_BASE_URL
export TRACELOOP_LOGGING_ENABLED=true
export TRACELOOP_METRICS_ENDPOINT=<otel-dc-llm-host>:8000
export TRACELOOP_METRICS_ENABLED=true
export OTEL_EXPORTER_OTLP_METRICS_INSECURE=true
export OTEL_METRIC_EXPORT_INTERVAL=10000