Arize Phoenix OpenTelemetry Wrapper

0.15.0 · active · verified Sat Apr 11

arize-phoenix-otel is a Python library that provides a lightweight wrapper around OpenTelemetry primitives, offering Phoenix-aware defaults and tracing decorators for common Generative AI patterns. It simplifies the setup of OpenTelemetry for sending traces to Arize Phoenix, an open-source AI observability platform. The library is actively maintained, with frequent updates released as part of the larger `arize-phoenix` monorepo, currently at version 0.15.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the OpenTelemetry tracer with Arize Phoenix using the `register()` function. It shows how to configure a project name, enable automatic instrumentation for common AI/ML libraries, and includes a manual span creation. Crucially, it highlights the need for `tracer_provider.shutdown()` when using batch processing to ensure all telemetry data is exported before the application terminates. Environment variables (`PHOENIX_COLLECTOR_ENDPOINT`, `PHOENIX_API_KEY`, `PHOENIX_PROJECT_NAME`) are automatically read for configuration.

import os
from phoenix.otel import register
from opentelemetry import trace

# Configure Phoenix collector endpoint and API key via environment variables
# PHOENIX_COLLECTOR_ENDPOINT defaults to http://localhost:4317 (gRPC) or http://localhost:6006/v1/traces (HTTP)
# PHOENIX_API_KEY for authentication if required by your Phoenix instance

# Example usage with explicit project name and auto-instrumentation
# For production, set PHOENIX_COLLECTOR_ENDPOINT and PHOENIX_API_KEY environment variables.
# If running locally without env vars, it defaults to localhost.
tracer_provider = register(
    project_name=os.environ.get('PHOENIX_PROJECT_NAME', 'my-llm-app'),
    auto_instrument=True, # Automatically instruments supported AI/ML libraries like OpenAI, LangChain
    batch=True # Enable batch processing for better performance, requires shutdown call
)

# Get a tracer
tracer = trace.get_tracer(__name__)

# Example of creating a span manually (if auto_instrument=False or for custom logic)
with tracer.start_as_current_span("my-manual-operation") as span:
    span.set_attribute("input", "test data")
    print("Performing a traced operation...")
    span.set_attribute("output", "result data")

# Important: If batch=True, ensure all spans are flushed before the process exits
tracer_provider.shutdown()

view raw JSON →