OpenTelemetry Milvus Instrumentation

0.58.0 · active · verified Fri Apr 10

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

Install

Imports

Quickstart

This quickstart demonstrates how to set up OpenTelemetry tracing for a Python application interacting with Milvus. It initializes the OpenTelemetry SDK with an OTLP exporter, instruments the Milvus client using `MilvusInstrumentor`, and then performs a basic Milvus operation. Ensure an OpenTelemetry Collector or Jaeger is running and accessible at the specified `OTEL_EXPORTER_OTLP_ENDPOINT` to receive traces. The `MilvusInstrumentor().instrument()` call should happen before importing `pymilvus`.

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']}")

view raw JSON →