OpenTelemetry Replicate Instrumentation
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
- breaking Starting from versions around 0.54.0, `openllmetry` (and thus `opentelemetry-instrumentation-replicate`) began conforming to the OpenTelemetry Generative AI Semantic Conventions (GenAI SemConv) version 0.5.0. This significantly changes the names and structure of attributes on spans related to LLM calls. If you have custom dashboards, alerts, or processing pipelines based on older attribute names, these will likely break.
- gotcha This instrumentation only tracks calls made via the `replicate` Python client. It does not automatically capture Replicate API calls made through other means (e.g., direct HTTP requests, other SDKs). Ensure all Replicate interactions you wish to trace go through the instrumented `replicate` package.
- gotcha The `opentelemetry-instrumentation-replicate` package requires the `replicate` library to be installed separately. It is not bundled as a direct dependency to allow for flexible dependency management.
- gotcha For `replicate.predictions.create` to function correctly and avoid API errors, a valid `REPLICATE_API_TOKEN` environment variable must be set. The instrumentation itself does not handle authentication, only tracing successful or failed API calls.
Install
-
pip install opentelemetry-instrumentation-replicate replicate opentelemetry-sdk
Imports
- ReplicateInstrumentor
from opentelemetry.instrumentation.replicate import ReplicateInstrumentor
Quickstart
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.")