Example: OpenAI OpenTelemetrySDK Instrumentation
This guide demonstrates how to use OpenTelemetry SDK to instrument OpenAI API calls and send traces to TrueFoundry’s OtelCollector.
In this example, we’ll show how to instrument a Python application that makes calls to OpenAI’s API using OpenTelemetry’s context managers.
Install Dependencies
First, you need to install the following packages:
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.
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
Initialize OpenTelemetry and OpenAI Client
Instrument your OpenAI API calls
This section shows how to instrument your OpenAI API calls using OpenTelemetry’s context managers. The example demonstrates how to trace a chat completion request with proper attributes.
Run your application and view logged trace