Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

[Feat]: Refactor Milvus and AstraDB OpenTelemetry Monitoring Instrumentations (#804)

Patcher5031c741f48e
diff --git a/sdk/python/pyproject.toml b/sdk/python/pyproject.toml
index b87cd7ef..35d83c48 100644
--- a/sdk/python/pyproject.toml
+++ b/sdk/python/pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "openlit"
-version = "1.34.26"
+version = "1.34.27"
description = "OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications and GPUs, facilitating the integration of observability into your GenAI-driven projects"
authors = ["OpenLIT"]
license = "Apache-2.0"
diff --git a/sdk/python/src/openlit/instrumentation/astra/__init__.py b/sdk/python/src/openlit/instrumentation/astra/__init__.py
index baa798b8..39b6126c 100644
--- a/sdk/python/src/openlit/instrumentation/astra/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/astra/__init__.py
@@ -1,178 +1,90 @@
-"""Initializer of Auto Instrumentation of AstraDB Functions"""
+"""
+OpenLIT AstraDB Instrumentation
+"""
+
from typing import Collection
import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper
from openlit.instrumentation.astra.astra import general_wrap
-# from openlit.instrumentation.astra.async_astra import asyc_general_wrap
+from openlit.instrumentation.astra.async_astra import async_general_wrap
+
+_instruments = ("astrapy >= 1.5.2",)
+
+# AstraDB sync operations
+ASTRA_SYNC_OPERATIONS = [
+ # Database operations
+ ("astrapy.database", "Database.create_collection", "astra.create_collection"),
+ ("astrapy.database", "Database.drop_collection", "astra.drop_collection"),
+
+ # Collection operations
+ ("astrapy.collection", "Collection.insert_one", "astra.insert_one"),
+ ("astrapy.collection", "Collection.insert_many", "astra.insert_many"),
+ ("astrapy.collection", "Collection.update_one", "astra.update_one"),
+ ("astrapy.collection", "Collection.update_many", "astra.update_many"),
+ ("astrapy.collection", "Collection.find_one_and_update", "astra.find_one_and_update"),
+ ("astrapy.collection", "Collection.find", "astra.find"),
+ ("astrapy.collection", "Collection.replace_one", "astra.replace_one"),
+ ("astrapy.collection", "Collection.find_one_and_delete", "astra.find_one_and_delete"),
+ ("astrapy.collection", "Collection.delete_one", "astra.delete_one"),
+ ("astrapy.collection", "Collection.delete_many", "astra.delete_many"),
+]
+
+# AstraDB async operations
+ASTRA_ASYNC_OPERATIONS = [
+ # Async Database operations
+ ("astrapy.database", "AsyncDatabase.create_collection", "astra.create_collection"),
+ ("astrapy.database", "AsyncDatabase.drop_collection", "astra.drop_collection"),
-_instruments = ('astrapy >= 1.5.2',)
+ # Async Collection operations
+ ("astrapy.collection", "AsyncCollection.insert_one", "astra.insert_one"),
+ ("astrapy.collection", "AsyncCollection.insert_many", "astra.insert_many"),
+ ("astrapy.collection", "AsyncCollection.update_one", "astra.update_one"),
+ ("astrapy.collection", "AsyncCollection.update_many", "astra.update_many"),
+ ("astrapy.collection", "AsyncCollection.find_one_and_update", "astra.find_one_and_update"),
+ ("astrapy.collection", "AsyncCollection.find", "astra.find"),
+ ("astrapy.collection", "AsyncCollection.replace_one", "astra.replace_one"),
+ ("astrapy.collection", "AsyncCollection.find_one_and_delete", "astra.find_one_and_delete"),
+ ("astrapy.collection", "AsyncCollection.delete_one", "astra.delete_one"),
+ ("astrapy.collection", "AsyncCollection.delete_many", "astra.delete_many"),
+]
class AstraInstrumentor(BaseInstrumentor):
- """An instrumentor for AstraDB's client library."""
+ """
+ An instrumentor for AstraDB's client library.
+ """
def instrumentation_dependencies(self) -> Collection[str]:
return _instruments
def _instrument(self, **kwargs):
- application_name = kwargs.get('application_name')
- environment = kwargs.get('environment')
- tracer = kwargs.get('tracer')
- metrics = kwargs.get('metrics_dict')
- pricing_info = kwargs.get('pricing_info')
- capture_message_content = kwargs.get('capture_message_content')
- disable_metrics = kwargs.get('disable_metrics')
- version = importlib.metadata.version('astrapy')
+ version = importlib.metadata.version("astrapy")
+ environment = kwargs.get("environment", "default")
+ application_name = kwargs.get("application_name", "default")
+ tracer = kwargs.get("tracer")
+ pricing_info = kwargs.get("pricing_info", {})
+ capture_message_content = kwargs.get("capture_message_content", False)
+ metrics = kwargs.get("metrics_dict")
+ disable_metrics = kwargs.get("disable_metrics")
- # Sync
- wrap_function_wrapper(
- 'astrapy.database',
- 'Database.create_collection',
- general_wrap('astra.create_collection', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.database',
- 'Database.drop_collection',
- general_wrap('astra.drop_collection', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'Collection.insert_one',
- general_wrap('astra.insert', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'Collection.insert_many',
- general_wrap('astra.insert', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'Collection.update_one',
- general_wrap('astra.update', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'Collection.update_many',
- general_wrap('astra.update', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'Collection.find_one_and_update',
- general_wrap('astra.find_one_and_update', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'Collection.find',
- general_wrap('astra.find', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'Collection.replace_one',
- general_wrap('astra.replace_one', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'Collection.find_one_and_delete',
- general_wrap('astra.find_one_and_delete', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'Collection.delete_one',
- general_wrap('astra.delete', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'Collection.delete_many',
- general_wrap('astra.delete', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
+ # Wrap sync operations
+ for module, class_method, endpoint in ASTRA_SYNC_OPERATIONS:
+ wrap_function_wrapper(
+ module,
+ class_method,
+ general_wrap(endpoint, version, environment, application_name,
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ )
- # ASync
- wrap_function_wrapper(
- 'astrapy.database',
- 'AsyncDatabase.create_collection',
- general_wrap('astra.create_collection', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.database',
- 'AsyncDatabase.drop_collection',
- general_wrap('astra.drop_collection', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'AsyncCollection.insert_one',
- general_wrap('astra.insert_one', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'AsyncCollection.insert_many',
- general_wrap('astra.insert_many', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'AsyncCollection.update_one',
- general_wrap('astra.update_one', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'AsyncCollection.update_many',
- general_wrap('astra.update_many', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'AsyncCollection.find_one_and_update',
- general_wrap('astra.find_one_and_update', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'AsyncCollection.find',
- general_wrap('astra.find', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'AsyncCollection.replace_one',
- general_wrap('astra.replace_one', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'AsyncCollection.find_one_and_delete',
- general_wrap('astra.find_one_and_delete', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'AsyncCollection.delete_one',
- general_wrap('astra.delete_one', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
- wrap_function_wrapper(
- 'astrapy.collection',
- 'AsyncCollection.delete_many',
- general_wrap('astra.delete_many', version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
- )
+ # Wrap async operations
+ for module, class_method, endpoint in ASTRA_ASYNC_OPERATIONS:
+ wrap_function_wrapper(
+ module,
+ class_method,
+ async_general_wrap(endpoint, version, environment, application_name,
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ )
def _uninstrument(self, **kwargs):
- # Proper uninstrumentation logic to revert patched methods
pass
diff --git a/sdk/python/src/openlit/instrumentation/astra/astra.py b/sdk/python/src/openlit/instrumentation/astra/astra.py
index 5a0cbd74..fa0fa127 100644
--- a/sdk/python/src/openlit/instrumentation/astra/astra.py
+++ b/sdk/python/src/openlit/instrumentation/astra/astra.py
@@ -1,44 +1,54 @@
"""
-Module for monitoring AstraDB.
+Module for monitoring AstraDB API calls.
"""
import time
from opentelemetry.trace import SpanKind
+from opentelemetry import context as context_api
+from openlit.__helpers import handle_exception
from openlit.instrumentation.astra.utils import (
+ process_astra_response,
DB_OPERATION_MAP,
- process_db_operations
+ set_server_address_and_port,
)
-from openlit.semcov import SemanticConvention
def general_wrap(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics):
"""
- Generates a telemetry wrapper for VectorDB function call
+ Generates a telemetry wrapper for AstraDB function calls.
"""
-
def wrapper(wrapped, instance, args, kwargs):
- """
- Wraps the VectorDB function call.
- """
+ # CRITICAL: Suppression check
+ if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
+ return wrapped(*args, **kwargs)
+
+ # Get server address and port using the standard helper
+ server_address, server_port = set_server_address_and_port(instance)
+
+ db_operation = DB_OPERATION_MAP.get(gen_ai_endpoint, "unknown")
- db_operation = DB_OPERATION_MAP.get(gen_ai_endpoint, "UNKNOWN")
- if db_operation == SemanticConvention.DB_OPERATION_REPLACE and kwargs.get('upsert'):
- db_operation = SemanticConvention.DB_OPERATION_UPSERT
+ # Handle upsert detection for replace operations
+ if db_operation == "DB_OPERATION_REPLACE" and kwargs.get("upsert"):
+ db_operation = "DB_OPERATION_UPSERT"
- span_name = f"{db_operation} {instance.name}"
+ # Get collection name for span naming
+ collection_name = getattr(instance, "name", "unknown")
+ span_name = f"{db_operation} {collection_name}"
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
response = wrapped(*args, **kwargs)
- server_address = getattr(getattr(instance, 'database', instance), 'api_endpoint', '')
- server_port = 443
- collection_name = instance.name
- response = process_db_operations(
- response, span, start_time, gen_ai_endpoint,
- version, environment, application_name, capture_message_content,
- metrics, disable_metrics, server_address, server_port,
- collection_name, db_operation, kwargs, args
- )
+
+ try:
+ # Process response and generate telemetry
+ response = process_astra_response(
+ response, db_operation, server_address, server_port,
+ environment, application_name, metrics, start_time, span,
+ capture_message_content, disable_metrics, version, instance, args, **kwargs
+ )
+
+ except Exception as e:
+ handle_exception(span, e)
return response
diff --git a/sdk/python/src/openlit/instrumentation/astra/async_astra.py b/sdk/python/src/openlit/instrumentation/astra/async_astra.py
index 019cb1bf..3aa6f6c4 100644
--- a/sdk/python/src/openlit/instrumentation/astra/async_astra.py
+++ b/sdk/python/src/openlit/instrumentation/astra/async_astra.py
@@ -1,44 +1,54 @@
"""
-Module for monitoring AstraDB.
+Module for monitoring AstraDB async API calls.
"""
import time
from opentelemetry.trace import SpanKind
+from opentelemetry import context as context_api
+from openlit.__helpers import handle_exception
from openlit.instrumentation.astra.utils import (
+ process_astra_response,
DB_OPERATION_MAP,
- process_db_operations
+ set_server_address_and_port,
)
-from openlit.semcov import SemanticConvention
def async_general_wrap(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics):
"""
- Generates a telemetry wrapper for VectorDB function call
+ Generates a telemetry wrapper for AstraDB async function calls.
"""
-
async def wrapper(wrapped, instance, args, kwargs):
- """
- Wraps the VectorDB function call.
- """
+ # CRITICAL: Suppression check
+ if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
+ return wrapped(*args, **kwargs)
+
+ # Get server address and port using the standard helper
+ server_address, server_port = set_server_address_and_port(instance)
+
+ db_operation = DB_OPERATION_MAP.get(gen_ai_endpoint, "unknown")
- db_operation = DB_OPERATION_MAP.get(gen_ai_endpoint, "UNKNOWN")
- if db_operation == SemanticConvention.DB_OPERATION_REPLACE and kwargs.get('upsert'):
- db_operation = SemanticConvention.DB_OPERATION_UPSERT
+ # Handle upsert detection for replace operations
+ if db_operation == "DB_OPERATION_REPLACE" and kwargs.get("upsert"):
+ db_operation = "DB_OPERATION_UPSERT"
- span_name = f"{db_operation} {instance.name}"
+ # Get collection name for span naming
+ collection_name = getattr(instance, "name", "unknown")
+ span_name = f"{db_operation} {collection_name}"
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
response = await wrapped(*args, **kwargs)
- server_address = getattr(getattr(instance, 'database', instance), 'api_endpoint', '')
- server_port = 443
- collection_name = instance.name
- response = process_db_operations(
- response, span, start_time, gen_ai_endpoint,
- version, environment, application_name, capture_message_content,
- metrics, disable_metrics, server_address, server_port,
- collection_name, db_operation, kwargs, args
- )
+
+ try:
+ # Process response and generate telemetry
+ response = process_astra_response(
+ response, db_operation, server_address, server_port,
+ environment, application_name, metrics, start_time, span,
+ capture_message_content, disable_metrics, version, instance, args, **kwargs
+ )
+
+ except Exception as e:
+ handle_exception(span, e)
return response
diff --git a/sdk/python/src/openlit/instrumentation/astra/utils.py b/sdk/python/src/openlit/instrumentation/astra/utils.py
index bd217cff..850ea4f7 100644
--- a/sdk/python/src/openlit/instrumentation/astra/utils.py
+++ b/sdk/python/src/openlit/instrumentation/astra/utils.py
@@ -1,102 +1,277 @@
"""
-Astra OpenTelemetry instrumentation utility functions
+Utility functions for AstraDB instrumentation.
"""
import time
-import logging
+from urllib.parse import urlparse
from opentelemetry.trace import Status, StatusCode
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
-from openlit.__helpers import handle_exception
+from openlit.__helpers import (
+ common_db_span_attributes,
+ record_db_metrics,
+)
from openlit.semcov import SemanticConvention
-# Initialize logger for logging potential issues and operations
-logger = logging.getLogger(__name__)
+# Operation mapping for simple span naming
+DB_OPERATION_MAP = {
+ "astra.create_collection": SemanticConvention.DB_OPERATION_CREATE_COLLECTION,
+ "astra.drop_collection": SemanticConvention.DB_OPERATION_DELETE_COLLECTION,
+ "astra.insert": SemanticConvention.DB_OPERATION_INSERT,
+ "astra.insert_one": SemanticConvention.DB_OPERATION_INSERT,
+ "astra.insert_many": SemanticConvention.DB_OPERATION_INSERT,
+ "astra.update": SemanticConvention.DB_OPERATION_UPDATE,
+ "astra.update_one": SemanticConvention.DB_OPERATION_UPDATE,
+ "astra.update_many": SemanticConvention.DB_OPERATION_UPDATE,
+ "astra.find": SemanticConvention.DB_OPERATION_SELECT,
+ "astra.find_one_and_update": SemanticConvention.DB_OPERATION_REPLACE,
+ "astra.replace_one": SemanticConvention.DB_OPERATION_REPLACE,
+ "astra.find_one_and_delete": SemanticConvention.DB_OPERATION_FIND_AND_DELETE,
+ "astra.delete": SemanticConvention.DB_OPERATION_DELETE,
+ "astra.delete_one": SemanticConvention.DB_OPERATION_DELETE,
+ "astra.delete_many": SemanticConvention.DB_OPERATION_DELETE,
+}
def object_count(obj):
- """Counts Length of object if it exists, Else returns None."""
- return len(obj) if isinstance(obj, list) else 1
+ """
+ Counts length of object if it exists, else returns 0.
+ """
+ if isinstance(obj, list):
+ return len(obj)
+ elif obj is not None:
+ return 1
+ return 0
-DB_OPERATION_MAP = {
- 'astra.create_collection': SemanticConvention.DB_OPERATION_CREATE_COLLECTION,
- 'astra.drop_collection': SemanticConvention.DB_OPERATION_DELETE_COLLECTION,
- 'astra.insert': SemanticConvention.DB_OPERATION_INSERT,
- 'astra.update': SemanticConvention.DB_OPERATION_UPDATE,
- 'astra.find': SemanticConvention.DB_OPERATION_SELECT,
- 'astra.find_one_and_update': SemanticConvention.DB_OPERATION_REPLACE,
- 'astra.replace_one': SemanticConvention.DB_OPERATION_REPLACE,
- 'astra.delete': SemanticConvention.DB_OPERATION_DELETE,
- 'astra.find_one_and_delete': SemanticConvention.DB_OPERATION_FIND_AND_DELETE
-}
+def set_server_address_and_port(instance):
+ """
+ Extracts server address and port from AstraDB client instance.
+
+ Args:
+ instance: AstraDB client instance
+
+ Returns:
+ tuple: (server_address, server_port)
+ """
+ server_address = "astra.datastax.com"
+ server_port = 443
+
+ # Try getting api_endpoint from instance or its database
+ api_endpoint = getattr(instance, "api_endpoint", None)
+ if not api_endpoint:
+ # Try getting from database attribute
+ database = getattr(instance, "database", None)
+ if database:
+ api_endpoint = getattr(database, "api_endpoint", None)
+
+ if api_endpoint and isinstance(api_endpoint, str):
+ if api_endpoint.startswith(("http://", "https://")):
+ try:
+ parsed = urlparse(api_endpoint)
+ server_address = parsed.hostname or server_address
+ server_port = parsed.port or server_port
+ except Exception:
+ pass
+ else:
+ # Handle cases like "hostname:port" or just "hostname"
+ if ":" in api_endpoint:
+ parts = api_endpoint.split(":")
+ server_address = parts[0]
+ try:
+ server_port = int(parts[1])
+ except (ValueError, IndexError):
+ pass
+ else:
+ server_address = api_endpoint
+
+ return server_address, server_port
-def process_db_operations(response, span, start_time, gen_ai_endpoint,
- version, environment, application_name,
- capture_message_content, metrics, disable_metrics, server_address,
- server_port, collection_name, db_operation, kwargs, args):
+def common_astra_logic(scope, environment, application_name,
+ metrics, capture_message_content, disable_metrics, version,
+ instance=None, endpoint=None):
"""
- Process DB operation and generate Telemetry
+ Process AstraDB request and generate telemetry.
+
+ Args:
+ scope: Scope object containing span, response, and operation details
+ environment: Deployment environment
+ application_name: Name of the application
+ metrics: Metrics dictionary for recording telemetry
+ capture_message_content: Flag to capture message content
+ disable_metrics: Flag to disable metrics collection
+ version: Version of the AstraDB client
+ instance: AstraDB client instance
+ endpoint: Operation endpoint for differentiation
"""
+ scope._end_time = time.time()
+
+ # Set common database span attributes using helper
+ common_db_span_attributes(scope, SemanticConvention.DB_SYSTEM_ASTRA,
+ scope._server_address, scope._server_port, environment, application_name, version)
+
+ # Set DB operation specific attributes
+ scope._span.set_attribute(SemanticConvention.DB_OPERATION_NAME, scope._db_operation)
+ scope._span.set_attribute(SemanticConvention.DB_CLIENT_OPERATION_DURATION,
+ scope._end_time - scope._start_time)
+
+ # Get collection name from instance
+ collection_name = getattr(instance, "name", "unknown")
+ scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+
+ if scope._db_operation == SemanticConvention.DB_OPERATION_CREATE_COLLECTION:
+ # Handle create_collection operation
+ scope._span.set_attribute(SemanticConvention.DB_COLLECTION_DIMENSION,
+ scope._kwargs.get("dimension", -1))
+ scope._span.set_attribute(SemanticConvention.DB_INDEX_METRIC,
+ str(scope._kwargs.get("metric", "")))
+
+ # Set namespace if available in response
+ if scope._response and hasattr(scope._response, "keyspace"):
+ scope._span.set_attribute(SemanticConvention.DB_NAMESPACE, scope._response.keyspace)
+
+ if scope._response and hasattr(scope._response, "name"):
+ scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, scope._response.name)
+
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name} "
+ f"dimension={scope._kwargs.get('dimension', 'None')} "
+ f"metric={scope._kwargs.get('metric', 'None')}")
+
+ elif scope._db_operation == SemanticConvention.DB_OPERATION_DELETE_COLLECTION:
+ # Handle drop_collection operation
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name}")
+
+ elif scope._db_operation == SemanticConvention.DB_OPERATION_INSERT:
+ # Handle insert operations (insert_one, insert_many, regular insert)
+ documents = scope._args[0] if scope._args else scope._kwargs.get("documents", [])
+
+ scope._span.set_attribute(SemanticConvention.DB_DOCUMENTS_COUNT, object_count(documents))
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(documents))
+
+ # Response metrics
+ if scope._response and hasattr(scope._response, "inserted_ids"):
+ scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
+ len(scope._response.inserted_ids))
+
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name} "
+ f"documents_count={object_count(documents)}")
+
+ elif scope._db_operation == SemanticConvention.DB_OPERATION_UPDATE:
+ # Handle update operations (update_one, update_many, regular update)
+ update_query = scope._args[1] if len(scope._args) > 1 else scope._kwargs.get("update", {})
+ filter_query = scope._args[0] if scope._args else scope._kwargs.get("filter", {})
+
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(update_query))
+ scope._span.set_attribute(SemanticConvention.DB_FILTER, str(filter_query))
+
+ # Response metrics
+ if scope._response and hasattr(scope._response, "update_info"):
+ scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
+ scope._response.update_info.get("nModified", 0))
+
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name} "
+ f"filter={str(filter_query)[:100]}... "
+ f"update={str(update_query)[:100]}...")
+
+ elif scope._db_operation == SemanticConvention.DB_OPERATION_REPLACE:
+ # Handle replace operations (find_one_and_update, replace_one)
+ filter_query = scope._args[0] if scope._args else scope._kwargs.get("filter", {})
+
+ # Check if it's an upsert operation
+ if scope._kwargs.get("upsert"):
+ scope._db_operation = SemanticConvention.DB_OPERATION_UPSERT
+ scope._span.set_attribute(SemanticConvention.DB_OPERATION_NAME,
+ SemanticConvention.DB_OPERATION_UPSERT)
+
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(filter_query))
+ scope._span.set_attribute(SemanticConvention.DB_FILTER, str(filter_query))
+
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name} "
+ f"filter={str(filter_query)[:100]}... "
+ f"upsert={scope._kwargs.get('upsert', False)}")
+
+ elif scope._db_operation == SemanticConvention.DB_OPERATION_SELECT:
+ # Handle find operations
+ filter_query = scope._args[0] if scope._args else scope._kwargs.get("filter", {})
+
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(filter_query))
+ scope._span.set_attribute(SemanticConvention.DB_FILTER, str(filter_query))
+
+ # Response metrics
+ if scope._response and hasattr(scope._response, "__len__"):
+ scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
+ len(scope._response))
+
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name} "
+ f"filter={str(filter_query)[:100]}...")
+
+ elif scope._db_operation in [SemanticConvention.DB_OPERATION_DELETE,
+ SemanticConvention.DB_OPERATION_FIND_AND_DELETE]:
+ # Handle delete operations (delete_one, delete_many, find_one_and_delete)
+ filter_query = scope._args[0] if scope._args else scope._kwargs.get("filter", {})
+
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(filter_query))
+ scope._span.set_attribute(SemanticConvention.DB_FILTER, str(filter_query))
+
+ # Response metrics
+ if scope._response and hasattr(scope._response, "deleted_count"):
+ scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
+ scope._response.deleted_count)
+
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name} "
+ f"filter={str(filter_query)[:100]}...")
+
+ scope._span.set_status(Status(StatusCode.OK))
+
+ # Record metrics using helper
+ if not disable_metrics:
+ record_db_metrics(metrics, SemanticConvention.DB_SYSTEM_ASTRA,
+ scope._server_address, scope._server_port, environment, application_name,
+ scope._start_time, scope._end_time)
+
+def process_astra_response(response, db_operation, server_address, server_port,
+ environment, application_name, metrics, start_time, span,
+ capture_message_content, disable_metrics, version, instance, args, **kwargs):
+ """
+ Process AstraDB response and generate telemetry.
+
+ Args:
+ response: Response from AstraDB operation
+ db_operation: Database operation type
+ server_address: Server address
+ server_port: Server port
+ environment: Deployment environment
+ application_name: Application name
+ metrics: Metrics dictionary
+ start_time: Start time of the operation
+ span: OpenTelemetry span
+ capture_message_content: Flag to capture message content
+ disable_metrics: Flag to disable metrics
+ version: AstraDB client version
+ instance: AstraDB client instance
+ args: Positional arguments
+ **kwargs: Keyword arguments
+
+ Returns:
+ Original response
+ """
+
+ # Create a scope object to hold all the context
+ scope = type("GenericScope", (), {})()
+ scope._response = response
+ scope._db_operation = db_operation
+ scope._server_address = server_address
+ scope._server_port = server_port
+ scope._start_time = start_time
+ scope._span = span
+ scope._kwargs = kwargs
+ scope._args = args
+
+ # Process the response using common logic
+ common_astra_logic(scope, environment, application_name,
+ metrics, capture_message_content, disable_metrics, version, instance)
- end_time = time.time()
-
- try:
- span.set_attribute(TELEMETRY_SDK_NAME, 'openlit')
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION, SemanticConvention.GEN_AI_OPERATION_TYPE_VECTORDB)
- span.set_attribute(SemanticConvention.DB_SYSTEM_NAME, SemanticConvention.DB_SYSTEM_ASTRA)
- span.set_attribute(SemanticConvention.DB_CLIENT_OPERATION_DURATION, end_time - start_time)
- span.set_attribute(SemanticConvention.SERVER_ADDRESS, server_address)
- span.set_attribute(SemanticConvention.SERVER_PORT, server_port)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
- span.set_attribute(SERVICE_NAME, application_name)
- span.set_attribute(SemanticConvention.DB_OPERATION_NAME, db_operation)
- span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
- span.set_attribute(SemanticConvention.DB_SDK_VERSION, version)
-
- if db_operation == SemanticConvention.DB_OPERATION_CREATE_COLLECTION:
- span.set_attribute(SemanticConvention.DB_NAMESPACE, response.keyspace)
- span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, response.name)
- span.set_attribute(SemanticConvention.DB_INDEX_DIMENSION, kwargs.get('dimension', ''))
- span.set_attribute(SemanticConvention.DB_INDEX_METRIC, str(kwargs.get('metric', '')))
-
- if db_operation == SemanticConvention.DB_OPERATION_INSERT:
- span.set_attribute(SemanticConvention.DB_DOCUMENTS_COUNT, object_count(args[0]))
- span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(args[0] or kwargs.get('documents', {})))
-
- elif db_operation == SemanticConvention.DB_OPERATION_UPDATE:
- span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS, response.update_info.get('nModified', 0))
- span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(args[1] or kwargs.get('update', {})))
-
- elif db_operation == SemanticConvention.DB_OPERATION_DELETE:
- span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS, response.deleted_count)
- span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(args[0] or kwargs.get('filter', {})))
-
- elif db_operation in [
- SemanticConvention.DB_OPERATION_SELECT,
- SemanticConvention.DB_OPERATION_FIND_AND_DELETE,
- SemanticConvention.DB_OPERATION_REPLACE
- ]:
- span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(args or kwargs.get('filter', {})))
-
- span.set_status(Status(StatusCode.OK))
-
- if not disable_metrics:
- attributes = {
- TELEMETRY_SDK_NAME: 'openlit',
- SERVICE_NAME: application_name,
- SemanticConvention.DB_SYSTEM_NAME: SemanticConvention.DB_SYSTEM_ASTRA,
- DEPLOYMENT_ENVIRONMENT: environment,
- SemanticConvention.GEN_AI_OPERATION: SemanticConvention.GEN_AI_OPERATION_TYPE_VECTORDB,
- SemanticConvention.DB_OPERATION_NAME: db_operation
- }
-
- metrics['db_requests'].add(1, attributes)
- metrics['db_client_operation_duration'].record(end_time - start_time, attributes)
-
- # Return original response
- return response
-
- except Exception as e:
- handle_exception(span, e)
- logger.error('Error in trace creation: %s', e)
-
- # Return original response
- return response
+ return response
diff --git a/sdk/python/src/openlit/instrumentation/milvus/__init__.py b/sdk/python/src/openlit/instrumentation/milvus/__init__.py
index 66203730..8624b10d 100644
--- a/sdk/python/src/openlit/instrumentation/milvus/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/milvus/__init__.py
@@ -1,5 +1,7 @@
-# pylint: disable=useless-return, bad-staticmethod-argument, disable=duplicate-code
-"""Initializer of Auto Instrumentation of Milvus Functions"""
+"""
+OpenLIT Milvus Instrumentation
+"""
+
from typing import Collection
import importlib.metadata
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
@@ -9,86 +11,46 @@ from openlit.instrumentation.milvus.milvus import general_wrap
_instruments = ("pymilvus >= 2.4.3",)
-WRAPPED_METHODS = [
- {
- "package": "pymilvus",
- "object": "MilvusClient.create_collection",
- "endpoint": "milvus.create_collection",
- "wrapper": general_wrap,
- },
- {
- "package": "pymilvus",
- "object": "MilvusClient.drop_collection",
- "endpoint": "milvus.drop_collection",
- "wrapper": general_wrap,
- },
- {
- "package": "pymilvus",
- "object": "MilvusClient.insert",
- "endpoint": "milvus.insert",
- "wrapper": general_wrap,
- },
- {
- "package": "pymilvus",
- "object": "MilvusClient.upsert",
- "endpoint": "milvus.upsert",
- "wrapper": general_wrap,
- },
- {
- "package": "pymilvus",
- "object": "MilvusClient.search",
- "endpoint": "milvus.search",
- "wrapper": general_wrap,
- },
- {
- "package": "pymilvus",
- "object": "MilvusClient.query",
- "endpoint": "milvus.query",
- "wrapper": general_wrap,
- },
- {
- "package": "pymilvus",
- "object": "MilvusClient.get",
- "endpoint": "milvus.get",
- "wrapper": general_wrap,
- },
- {
- "package": "pymilvus",
- "object": "MilvusClient.delete",
- "endpoint": "milvus.delete",
- "wrapper": general_wrap,
- },
+# Operations to wrap for Milvus client
+MILVUS_OPERATIONS = [
+ ("create_collection", "milvus.create_collection"),
+ ("drop_collection", "milvus.drop_collection"),
+ ("insert", "milvus.insert"),
+ ("upsert", "milvus.upsert"),
+ ("search", "milvus.search"),
+ ("query", "milvus.query"),
+ ("get", "milvus.get"),
+ ("delete", "milvus.delete"),
]
class MilvusInstrumentor(BaseInstrumentor):
- """An instrumentor for Milvus's client library."""
+ """
+ An instrumentor for Milvus's client library.
+ """
def instrumentation_dependencies(self) -> Collection[str]:
return _instruments
def _instrument(self, **kwargs):
- application_name = kwargs.get("application_name")
- environment = kwargs.get("environment")
+ version = importlib.metadata.version("pymilvus")
+ environment = kwargs.get("environment", "default")
+ application_name = kwargs.get("application_name", "default")
tracer = kwargs.get("tracer")
+ pricing_info = kwargs.get("pricing_info", {})
+ capture_message_content = kwargs.get("capture_message_content", False)
metrics = kwargs.get("metrics_dict")
- pricing_info = kwargs.get("pricing_info")
- capture_message_content = kwargs.get("capture_message_content")
disable_metrics = kwargs.get("disable_metrics")
- version = importlib.metadata.version("pymilvus")
- for wrapped_method in WRAPPED_METHODS:
- wrap_package = wrapped_method.get("package")
- wrap_object = wrapped_method.get("object")
- gen_ai_endpoint = wrapped_method.get("endpoint")
- wrapper = wrapped_method.get("wrapper")
+ # Wrap operations
+ for method_name, endpoint in MILVUS_OPERATIONS:
wrap_function_wrapper(
- wrap_package,
- wrap_object,
- wrapper(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ "pymilvus",
+ f"MilvusClient.{method_name}",
+ general_wrap(
+ endpoint, version, environment, application_name, tracer,
+ pricing_info, capture_message_content, metrics, disable_metrics
+ ),
)
-
- @staticmethod
def _uninstrument(self, **kwargs):
pass
diff --git a/sdk/python/src/openlit/instrumentation/milvus/milvus.py b/sdk/python/src/openlit/instrumentation/milvus/milvus.py
index 226281fd..23140e39 100644
--- a/sdk/python/src/openlit/instrumentation/milvus/milvus.py
+++ b/sdk/python/src/openlit/instrumentation/milvus/milvus.py
@@ -1,179 +1,52 @@
-# pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument, possibly-used-before-assignment, too-many-branches
"""
-Module for monitoring Milvus.
+Module for monitoring Milvus API calls.
"""
-import logging
-from opentelemetry.trace import SpanKind, Status, StatusCode
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+import time
+from opentelemetry.trace import SpanKind
+from opentelemetry import context as context_api
from openlit.__helpers import handle_exception
-from openlit.semcov import SemanticConvention
-
-# Initialize logger for logging potential issues and operations
-logger = logging.getLogger(__name__)
-
-def object_count(obj):
- """
- Counts Length of object if it exists, Else returns None
- """
- try:
- cnt = len(obj)
- # pylint: disable=bare-except
- except:
- cnt = 0
-
- return cnt
+from openlit.instrumentation.milvus.utils import (
+ process_milvus_response,
+ DB_OPERATION_MAP,
+ set_server_address_and_port,
+)
def general_wrap(gen_ai_endpoint, version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics):
"""
- Creates a wrapper around a function call to trace and log its execution metrics.
-
- This function wraps any given function to measure its execution time,
- log its operation, and trace its execution using OpenTelemetry.
-
- Parameters:
- - gen_ai_endpoint (str): A descriptor or name for the endpoint being traced.
- - version (str): The version of the Langchain application.
- - environment (str): The deployment environment (e.g., 'production', 'development').
- - application_name (str): Name of the Langchain application.
- - tracer (opentelemetry.trace.Tracer): The tracer object used for OpenTelemetry tracing.
- - pricing_info (dict): Information about the pricing for internal metrics (currently not used).
- - capture_message_content (bool): Flag indicating whether to trace the content of the response.
-
- Returns:
- - function: A higher-order function that takes a function 'wrapped' and returns
- a new function that wraps 'wrapped' with additional tracing and logging.
+ Generates a telemetry wrapper for Milvus function calls.
"""
-
def wrapper(wrapped, instance, args, kwargs):
- """
- An inner wrapper function that executes the wrapped function, measures execution
- time, and records trace data using OpenTelemetry.
-
- Parameters:
- - wrapped (Callable): The original function that this wrapper will execute.
- - instance (object): The instance to which the wrapped function belongs. This
- is used for instance methods. For static and classmethods,
- this may be None.
- - args (tuple): Positional arguments passed to the wrapped function.
- - kwargs (dict): Keyword arguments passed to the wrapped function.
-
- Returns:
- - The result of the wrapped function call.
-
- The wrapper initiates a span with the provided tracer, sets various attributes
- on the span based on the function's execution and response, and ensures
- errors are handled and logged appropriately.
- """
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
+ # CRITICAL: Suppression check
+ if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
+ return wrapped(*args, **kwargs)
+
+ # Get server address and port using the standard helper
+ server_address, server_port = set_server_address_and_port(instance)
+
+ db_operation = DB_OPERATION_MAP.get(gen_ai_endpoint, "unknown")
+ if db_operation == "create_collection":
+ collection_name = kwargs.get("collection_name") or (args[0] if args else "unknown")
+ else:
+ collection_name = kwargs.get("collection_name", "unknown")
+ span_name = f"{db_operation} {collection_name}"
+
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
+ start_time = time.time()
response = wrapped(*args, **kwargs)
try:
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT,
- gen_ai_endpoint)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
- SemanticConvention.GEN_AI_OPERATION_TYPE_VECTORDB)
- span.set_attribute(SemanticConvention.DB_SYSTEM_NAME,
- SemanticConvention.DB_SYSTEM_MILVUS)
-
- if gen_ai_endpoint == "milvus.create_collection":
- db_operation = SemanticConvention.DB_OPERATION_CREATE_COLLECTION
- span.set_attribute(SemanticConvention.DB_OPERATION_NAME,
- SemanticConvention.DB_OPERATION_CREATE_COLLECTION)
- span.set_attribute(SemanticConvention.DB_COLLECTION_NAME,
- kwargs.get("collection_name", ""))
- span.set_attribute(SemanticConvention.DB_COLLECTION_DIMENSION,
- kwargs.get("dimension", ""))
-
- elif gen_ai_endpoint == "milvus.drop_collection":
- db_operation = SemanticConvention.DB_OPERATION_DELETE_COLLECTION
- span.set_attribute(SemanticConvention.DB_OPERATION_NAME,
- SemanticConvention.DB_OPERATION_DELETE_COLLECTION)
- span.set_attribute(SemanticConvention.DB_COLLECTION_NAME,
- kwargs.get("collection_name", ""))
-
- elif gen_ai_endpoint == "milvus.insert":
- db_operation = SemanticConvention.DB_OPERATION_ADD
- span.set_attribute(SemanticConvention.DB_OPERATION_NAME,
- SemanticConvention.DB_OPERATION_ADD)
- span.set_attribute(SemanticConvention.DB_COLLECTION_NAME,
- kwargs.get("collection_name", ""))
- span.set_attribute(SemanticConvention.DB_VECTOR_COUNT,
- object_count(kwargs.get("data")))
- span.set_attribute(SemanticConvention.DB_OPERATION_COST,
- response["cost"])
-
- elif gen_ai_endpoint == "milvus.search":
- db_operation = SemanticConvention.DB_OPERATION_QUERY
- span.set_attribute(SemanticConvention.DB_OPERATION_NAME,
- SemanticConvention.DB_OPERATION_QUERY)
- span.set_attribute(SemanticConvention.DB_COLLECTION_NAME,
- kwargs.get("collection_name", ""))
- span.set_attribute(SemanticConvention.DB_STATEMENT,
- str(kwargs.get("data")))
-
- elif gen_ai_endpoint in ["milvus.query", "milvus.get"]:
- db_operation = SemanticConvention.DB_OPERATION_QUERY
- span.set_attribute(SemanticConvention.DB_OPERATION_NAME,
- SemanticConvention.DB_OPERATION_QUERY)
- span.set_attribute(SemanticConvention.DB_COLLECTION_NAME,
- kwargs.get("collection_name", ""))
- span.set_attribute(SemanticConvention.DB_STATEMENT,
- str(kwargs.get("output_fields")))
-
- elif gen_ai_endpoint == "milvus.upsert":
- db_operation = SemanticConvention.DB_OPERATION_ADD
- span.set_attribute(SemanticConvention.DB_OPERATION_NAME,
- SemanticConvention.DB_OPERATION_UPSERT)
- span.set_attribute(SemanticConvention.DB_COLLECTION_NAME,
- kwargs.get("collection_name", ""))
- span.set_attribute(SemanticConvention.DB_VECTOR_COUNT,
- object_count(kwargs.get("data")))
- span.set_attribute(SemanticConvention.DB_OPERATION_COST,
- response["cost"])
-
- elif gen_ai_endpoint == "milvus.delete":
- db_operation = SemanticConvention.DB_OPERATION_DELETE
- span.set_attribute(SemanticConvention.DB_OPERATION_NAME,
- SemanticConvention.DB_OPERATION_DELETE)
- span.set_attribute(SemanticConvention.DB_COLLECTION_NAME,
- kwargs.get("collection_name", ""))
- span.set_attribute(SemanticConvention.DB_FILTER,
- str(kwargs.get("filter", "")))
-
- span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = {
- TELEMETRY_SDK_NAME:
- "openlit",
- SERVICE_NAME:
- application_name,
- SemanticConvention.DB_SYSTEM_NAME:
- SemanticConvention.DB_SYSTEM_MILVUS,
- DEPLOYMENT_ENVIRONMENT:
- environment,
- SemanticConvention.GEN_AI_OPERATION:
- SemanticConvention.GEN_AI_OPERATION_TYPE_VECTORDB,
- SemanticConvention.DB_OPERATION_NAME:
- db_operation
- }
-
- metrics["db_requests"].add(1, attributes)
-
- return response
+ # Process response and generate telemetry
+ response = process_milvus_response(
+ response, db_operation, server_address, server_port,
+ environment, application_name, metrics, start_time, span,
+ capture_message_content, disable_metrics, version, instance, args, **kwargs
+ )
except Exception as e:
handle_exception(span, e)
- logger.error("Error in trace creation: %s", e)
- # Return original response
- return response
+ return response
return wrapper
diff --git a/sdk/python/src/openlit/instrumentation/milvus/utils.py b/sdk/python/src/openlit/instrumentation/milvus/utils.py
new file mode 100644
index 00000000..b702c456
--- /dev/null
+++ b/sdk/python/src/openlit/instrumentation/milvus/utils.py
@@ -0,0 +1,276 @@
+"""
+Utility functions for Milvus instrumentation.
+"""
+
+import time
+from urllib.parse import urlparse
+from opentelemetry.trace import Status, StatusCode
+from openlit.__helpers import (
+ common_db_span_attributes,
+ record_db_metrics,
+)
+from openlit.semcov import SemanticConvention
+
+# Operation mapping for simple span naming
+DB_OPERATION_MAP = {
+ "milvus.create_collection": SemanticConvention.DB_OPERATION_CREATE_COLLECTION,
+ "milvus.drop_collection": SemanticConvention.DB_OPERATION_DELETE_COLLECTION,
+ "milvus.insert": SemanticConvention.DB_OPERATION_INSERT,
+ "milvus.upsert": SemanticConvention.DB_OPERATION_UPSERT,
+ "milvus.search": SemanticConvention.DB_OPERATION_SEARCH,
+ "milvus.query": SemanticConvention.DB_OPERATION_QUERY,
+ "milvus.get": SemanticConvention.DB_OPERATION_GET,
+ "milvus.delete": SemanticConvention.DB_OPERATION_DELETE,
+}
+
+def object_count(obj):
+ """
+ Counts length of object if it exists, else returns 0.
+ """
+ return len(obj) if obj else 0
+
+def set_server_address_and_port(instance):
+ """
+ Extracts server address and port from Milvus client instance.
+
+ Args:
+ instance: Milvus client instance
+
+ Returns:
+ tuple: (server_address, server_port)
+ """
+ server_address = "localhost"
+ server_port = 19530 # Default Milvus port
+
+ # Try getting uri from multiple potential attributes
+ client_config = getattr(instance, "_client_config", None)
+ if client_config:
+ uri = getattr(client_config, "uri", None)
+ if uri and isinstance(uri, str):
+ if uri.startswith(("http://", "https://")):
+ try:
+ parsed = urlparse(uri)
+ server_address = parsed.hostname or server_address
+ server_port = parsed.port or server_port
+ except Exception:
+ pass
+ else:
+ # Handle cases like "localhost:19530" or just "localhost"
+ if ":" in uri:
+ parts = uri.split(":")
+ server_address = parts[0]
+ try:
+ server_port = int(parts[1])
+ except (ValueError, IndexError):
+ pass
+ else:
+ server_address = uri
+
+ return server_address, server_port
+
+def common_milvus_logic(scope, environment, application_name,
+ metrics, capture_message_content, disable_metrics, version,
+ instance=None, endpoint=None):
+ """
+ Process Milvus database request and generate telemetry.
+
+ Args:
+ scope: Scope object containing span, response, and operation details
+ environment: Deployment environment
+ application_name: Name of the application
+ metrics: Metrics dictionary for recording telemetry
+ capture_message_content: Flag to capture message content
+ disable_metrics: Flag to disable metrics collection
+ version: Version of the Milvus client
+ instance: Milvus client instance
+ endpoint: Operation endpoint for differentiation
+ """
+ scope._end_time = time.time()
+
+ # Set common database span attributes using helper
+ common_db_span_attributes(scope, SemanticConvention.DB_SYSTEM_MILVUS,
+ scope._server_address, scope._server_port, environment, application_name, version)
+
+ # Set DB operation specific attributes
+ scope._span.set_attribute(SemanticConvention.DB_OPERATION_NAME, scope._db_operation)
+ scope._span.set_attribute(SemanticConvention.DB_CLIENT_OPERATION_DURATION,
+ scope._end_time - scope._start_time)
+
+ if scope._db_operation == SemanticConvention.DB_OPERATION_CREATE_COLLECTION:
+ collection_name = scope._kwargs.get("collection_name", "unknown")
+
+ # Standard database attributes
+ scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+ scope._span.set_attribute(SemanticConvention.DB_COLLECTION_DIMENSION,
+ scope._kwargs.get("dimension", -1))
+
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name} "
+ f"dimension={scope._kwargs.get('dimension', 'None')}")
+
+ elif scope._db_operation == SemanticConvention.DB_OPERATION_DELETE_COLLECTION:
+ collection_name = scope._kwargs.get("collection_name", "unknown")
+
+ # Standard database attributes
+ scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name}")
+
+ elif scope._db_operation == SemanticConvention.DB_OPERATION_INSERT:
+ collection_name = scope._kwargs.get("collection_name", "unknown")
+ data = scope._kwargs.get("data", [])
+
+ # Standard database attributes
+ scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+ scope._span.set_attribute(SemanticConvention.DB_VECTOR_COUNT, object_count(data))
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(data))
+
+ # Response metrics
+ if scope._response and scope._response.get("insert_count"):
+ scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
+ scope._response["insert_count"])
+
+ if scope._response and scope._response.get("cost"):
+ scope._span.set_attribute(SemanticConvention.DB_OPERATION_COST,
+ scope._response["cost"])
+
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name} "
+ f"data_count={object_count(data)}")
+
+ elif scope._db_operation == SemanticConvention.DB_OPERATION_UPSERT:
+ collection_name = scope._kwargs.get("collection_name", "unknown")
+ data = scope._kwargs.get("data", [])
+
+ # Standard database attributes
+ scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+ scope._span.set_attribute(SemanticConvention.DB_VECTOR_COUNT, object_count(data))
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(data))
+
+ # Response metrics
+ if scope._response and scope._response.get("upsert_count"):
+ scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
+ scope._response["upsert_count"])
+
+ if scope._response and scope._response.get("cost"):
+ scope._span.set_attribute(SemanticConvention.DB_OPERATION_COST,
+ scope._response["cost"])
+
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name} "
+ f"data_count={object_count(data)}")
+
+ elif scope._db_operation == SemanticConvention.DB_OPERATION_SEARCH:
+ collection_name = scope._kwargs.get("collection_name", "unknown")
+ data = scope._kwargs.get("data", [])
+
+ # Standard database attributes
+ scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(data))
+ scope._span.set_attribute(SemanticConvention.DB_VECTOR_QUERY_TOP_K,
+ scope._kwargs.get("limit", -1))
+
+ # Search specific attributes
+ scope._span.set_attribute(SemanticConvention.DB_FILTER,
+ str(scope._kwargs.get("filter", "")))
+
+ # Response metrics
+ if scope._response and isinstance(scope._response, list):
+ scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
+ len(scope._response))
+
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name} "
+ f"data={str(data)[:100]}... "
+ f"limit={scope._kwargs.get('limit', 'None')}")
+
+ elif scope._db_operation in [SemanticConvention.DB_OPERATION_QUERY, SemanticConvention.DB_OPERATION_GET]:
+ collection_name = scope._kwargs.get("collection_name", "unknown")
+ output_fields = scope._kwargs.get("output_fields", [])
+
+ # Standard database attributes
+ scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, str(output_fields))
+
+ # Query specific attributes
+ scope._span.set_attribute(SemanticConvention.DB_FILTER,
+ str(scope._kwargs.get("filter", "")))
+
+ # Response metrics
+ if scope._response and isinstance(scope._response, list):
+ scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
+ len(scope._response))
+
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name} "
+ f"output_fields={output_fields} "
+ f"filter={scope._kwargs.get('filter', 'None')}")
+
+ elif scope._db_operation == SemanticConvention.DB_OPERATION_DELETE:
+ collection_name = scope._kwargs.get("collection_name", "unknown")
+
+ # Standard database attributes
+ scope._span.set_attribute(SemanticConvention.DB_COLLECTION_NAME, collection_name)
+ scope._span.set_attribute(SemanticConvention.DB_FILTER,
+ str(scope._kwargs.get("filter", "")))
+
+ # Response metrics
+ if scope._response and scope._response.get("delete_count"):
+ scope._span.set_attribute(SemanticConvention.DB_RESPONSE_RETURNED_ROWS,
+ scope._response["delete_count"])
+
+ scope._span.set_attribute(SemanticConvention.DB_QUERY_SUMMARY,
+ f"{scope._db_operation} {collection_name} "
+ f"filter={scope._kwargs.get('filter', 'None')}")
+
+ scope._span.set_status(Status(StatusCode.OK))
+
+ # Record metrics using helper
+ if not disable_metrics:
+ record_db_metrics(metrics, SemanticConvention.DB_SYSTEM_MILVUS,
+ scope._server_address, scope._server_port, environment, application_name,
+ scope._start_time, scope._end_time)
+
+def process_milvus_response(response, db_operation, server_address, server_port,
+ environment, application_name, metrics, start_time, span,
+ capture_message_content, disable_metrics, version, instance, args, **kwargs):
+ """
+ Process Milvus response and generate telemetry.
+
+ Args:
+ response: Response from Milvus operation
+ db_operation: Database operation type
+ server_address: Server address
+ server_port: Server port
+ environment: Deployment environment
+ application_name: Application name
+ metrics: Metrics dictionary
+ start_time: Start time of the operation
+ span: OpenTelemetry span
+ capture_message_content: Flag to capture message content
+ disable_metrics: Flag to disable metrics
+ version: Milvus client version
+ instance: Milvus client instance
+ args: Positional arguments
+ **kwargs: Keyword arguments
+
+ Returns:
+ Original response
+ """
+
+ # Create a scope object to hold all the context
+ scope = type("GenericScope", (), {})()
+ scope._response = response
+ scope._db_operation = db_operation
+ scope._server_address = server_address
+ scope._server_port = server_port
+ scope._start_time = start_time
+ scope._span = span
+ scope._kwargs = kwargs
+
+ # Process the response using common logic
+ common_milvus_logic(scope, environment, application_name,
+ metrics, capture_message_content, disable_metrics, version, instance)
+
+ return response