Example: Python OpenTelemetrySDK Instrumentation
This guide demonstrates how to use OpenTelemetry SDK to instrument Python code and send traces to TrueFoundry’s OtelCollector.
In this example, we’ll show how to instrument a simple Python application with nested function calls 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
Instrument your Python code
This section shows how to instrument your Python code using OpenTelemetry’s context managers. The example demonstrates nested function calls with proper tracing.
Run your application and view logged trace