OpenTelemetry Instrumentation for Hugging Face Transformers
The `opentelemetry-instrumentation-transformers` library provides OpenTelemetry instrumentation for the Hugging Face Transformers library. It automatically captures traces for operations performed using Transformers models, helping to monitor and observe AI/ML application performance and behavior. The current version is 0.58.0, and it follows a rapid release cadence, often aligning with updates to OpenTelemetry's Generative AI semantic conventions.
Warnings
- breaking Frequent updates to OpenTelemetry Generative AI semantic conventions (semconv) may introduce breaking changes in the names and structure of span attributes. Applications consuming these traces should be prepared for attribute schema changes.
- gotcha The `transformers` library must be installed and importable *before* `TransformersInstrumentor().instrument()` is called. If `transformers` is not present, instrumentation will fail silently or not apply.
- gotcha To observe traces, a complete OpenTelemetry SDK setup (including a `TracerProvider`, `Resource`, and `SpanProcessor` with an `Exporter`) is required. Without a configured exporter, traces will be generated but not sent anywhere.
Install
-
pip install opentelemetry-instrumentation-transformers transformers
Imports
- TransformersInstrumentor
from opentelemetry.instrumentation.transformers import TransformersInstrumentor
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.transformers import TransformersInstrumentor
from transformers import pipeline
# Configure OpenTelemetry SDK
# In production, use OTLPExporter to send traces to a collector.
resource = Resource.create({"service.name": "transformers-app"})
provider = TracerProvider(resource=resource)
processor = SimpleSpanProcessor(ConsoleSpanExporter())
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)
# Instrument the transformers library
TransformersInstrumentor().instrument()
# Use a transformers pipeline
print("Running transformers pipeline...")
pipe = pipeline("sentiment-analysis")
result = pipe("I love OpenTelemetry!")
print("Pipeline result:", result)
# Ensure traces are flushed before exit
provider.shutdown()