OpenTelemetry Milvus Instrumentation
This library provides client-side instrumentation for the Milvus vector database within the OpenTelemetry ecosystem. It enables automatic tracing of interactions with Milvus, allowing developers to monitor performance, debug distributed systems, and gain observability into their AI applications. Part of the OpenLLMetry project, it adheres to OpenTelemetry standards and is actively maintained with a frequent release cadence, as seen by multiple updates in recent months.
Warnings
- gotcha It is critical to initialize the OpenTelemetry instrumentation for Milvus (e.g., `MilvusInstrumentor().instrument()`) *before* importing the `pymilvus` library. Failure to do so may result in gRPC calls not being properly traced.
- gotcha This instrumentation primarily traces client-side interactions. For end-to-end observability, ensure your Milvus server instance is also configured to emit OpenTelemetry traces (Milvus 2.3.0+ supports this) and that an OpenTelemetry Collector or Jaeger is running to receive all telemetry data.
- breaking OpenTelemetry semantic conventions, especially in rapidly evolving areas like Generative AI (which OpenLLMetry targets), can change between versions. While the instrumentation API might remain stable, the names or structure of emitted span attributes could change, potentially breaking dashboards or alerts relying on older conventions.
- gotcha High CPU usage or unexpected behavior can occur if OpenTelemetry auto-instrumentation is enabled globally (e.g., via system-wide environment variables) and impacts unintended processes. This is a general risk with broad auto-instrumentation.
Install
-
pip install opentelemetry-instrumentation-milvus -
pip install pymilvus opentelemetry-sdk opentelemetry-exporter-otlp opentelemetry-instrumentation-grpc
Imports
- MilvusInstrumentor
from opentelemetry.instrumentation.milvus import MilvusInstrumentor
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 BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.grpc import GrpcInstrumentorClient
# IMPORTANT: Instrument Milvus BEFORE importing pymilvus
from opentelemetry.instrumentation.milvus import MilvusInstrumentor
MilvusInstrumentor().instrument()
# Configure OpenTelemetry (replace with your actual collector endpoint)
os.environ['OTEL_EXPORTER_OTLP_ENDPOINT'] = os.environ.get('OTEL_EXPORTER_OTLP_ENDPOINT', 'http://localhost:4317')
os.environ['OTEL_SERVICE_NAME'] = os.environ.get('OTEL_SERVICE_NAME', 'milvus-client-app')
resource = Resource.create({
"service.name": os.environ['OTEL_SERVICE_NAME'],
"application": "milvus-otel-test"
})
trace.set_tracer_provider(
TracerProvider(resource=resource)
)
otlp_exporter = OTLPSpanExporter()
span_processor = BatchSpanProcessor(otlp_exporter)
trace.get_tracer_provider().add_span_processor(span_processor)
# Also instrument gRPC client explicitly for broader gRPC tracing
grpc_client_instrumentor = GrpcInstrumentorClient()
grpc_client_instrumentor.instrument()
# Now import pymilvus
from pymilvus import Collection, connections
# Connect to Milvus (assuming a local Milvus instance)
connections.connect("default", host="localhost", port="19530")
# Perform a simple Milvus operation to generate a trace
collection_name = "hello_milvus_otel"
if collection_name in connections.list_collections():
Collection(collection_name).drop()
print(f"Milvus client instrumented. Check your OTLP collector/Jaeger at {os.environ['OTEL_EXPORTER_OTLP_ENDPOINT']}")