Skip to main content

Installation

Basic Example

Here’s a simple example showing how to use tracing with OpenAI:

Advanced Usage

Creating Spans

You can create custom spans using OpenTelemetry’s tracer:

Error Handling

Record exceptions in spans:

Adding Attributes

Add attributes to spans for better context:

Nested Spans

Create nested spans to represent complex operations:

Best Practices

  1. Span Naming
    • Use descriptive names that reflect the operation
    • Follow a consistent naming convention
    • Include the operation type in the name
  2. Attribute Management
    • Add relevant context to spans
    • Avoid logging sensitive data
    • Use consistent attribute names
  3. Error Handling
    • Always record exceptions
    • Set appropriate error status
    • Include error details in attributes
  4. Performance
    • Keep spans focused
    • Avoid excessive attributes
    • Use sampling for high-volume applications
  5. Async Operations
    • Properly handle async/await with spans
    • Ensure spans are ended in finally blocks
    • Use Promise.all for parallel operations
  6. TypeScript Integration
    • Use proper types for spans and attributes
    • Leverage TypeScript’s type system for safety
    • Document complex types and interfaces

Additional Resources

OpenTelemetry

OpenLLMetry

Next Steps