Prerequisites
- Python 3.9+
- OpenAI API key
- Phoenix or another OpenTelemetry collector running
Installation
1
Install dependencies
2
Set environment variables
Complete Example
Key Features
Automatic Instrumentation
TheOpenAIInstrumentor automatically traces all OpenAI API calls without requiring code changes to your OpenAI usage.
Context Attributes
Useusing_attributes() context manager to add:
- Session tracking:
session_idanduser_id - Metadata: Custom key-value pairs for filtering and analysis
- Tags: Labels for categorization
- Prompt templates: Track template versions and variables
Multiple Exporters
This example uses both:OTLPSpanExporter: Send traces to Phoenix or any OTLP-compatible backendConsoleSpanExporter: Print traces to console for debugging
Next Steps
- Explore streaming responses
- Learn about function calling
- See LangChain integration