OpenTelemetry Replicate Instrumentation

0.58.0 · active · verified Fri Apr 10

OpenTelemetry Replicate instrumentation provides automatic tracing for applications using the Replicate Python client, allowing developers to monitor and observe AI/ML model inferences. It is part of the `openllmetry` project and is currently at version 0.58.0, with frequent releases to keep up with OpenTelemetry semantic conventions and new LLM integrations, typically on a weekly or bi-weekly cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up the OpenTelemetry SDK with a console exporter, instrument the Replicate client, and then make calls to Replicate. The console will display the generated traces for the Replicate operations.

import os
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from opentelemetry.instrumentation.replicate import ReplicateInstrumentor

# 1. Setup OpenTelemetry SDK (for demonstration, using ConsoleSpanExporter)
resource = Resource.create({"service.name": "replicate-app"})
provider = TracerProvider(resource=resource)
processor = SimpleSpanProcessor(ConsoleSpanExporter())
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)

# 2. Instrument Replicate
ReplicateInstrumentor().instrument()

# 3. Use Replicate client (ensure REPLICATE_API_TOKEN is set in environment)
# For local testing, you might set a dummy value, but real use requires a valid token.
os.environ["REPLICATE_API_TOKEN"] = os.environ.get("REPLICATE_API_TOKEN", "r8_DUMMY_TOKEN_FOR_TESTING")

import replicate

# Example: List models (this operation will be traced)
models = replicate.models.list()
print(f"Found {len(models)} Replicate models. First one: {models[0].id if models else 'N/A'}")

# Example: Create a prediction (this operation will be traced)
# Note: This requires a valid Replicate API token and potentially an actual model ID/version
try:
    model_version_id = "stability-ai/sdxl:39edb22c277335607b222fbc6e31ddadead4ad77a102713f01b34e53d5d71c1f"
    prediction = replicate.predictions.create(
        version=model_version_id,
        input={"prompt": "a photo of an astronaut riding a horse on mars"}
    )
    print(f"Replicate prediction created: {prediction.id}")
except Exception as e:
    print(f"Could not create Replicate prediction (check API token and model ID): {e}")

print("Traces should be visible in the console.")

view raw JSON →