openlit 1.5.0__py3-none-any.whl → 1.7.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
openlit/__init__.py CHANGED
@@ -20,12 +20,14 @@ from openlit.instrumentation.mistral import MistralInstrumentor
20
20
  from openlit.instrumentation.bedrock import BedrockInstrumentor
21
21
  from openlit.instrumentation.vertexai import VertexAIInstrumentor
22
22
  from openlit.instrumentation.groq import GroqInstrumentor
23
+ from openlit.instrumentation.ollama import OllamaInstrumentor
23
24
  from openlit.instrumentation.langchain import LangChainInstrumentor
24
25
  from openlit.instrumentation.llamaindex import LlamaIndexInstrumentor
25
26
  from openlit.instrumentation.haystack import HaystackInstrumentor
26
27
  from openlit.instrumentation.chroma import ChromaInstrumentor
27
28
  from openlit.instrumentation.pinecone import PineconeInstrumentor
28
29
  from openlit.instrumentation.qdrant import QdrantInstrumentor
30
+ from openlit.instrumentation.milvus import MilvusInstrumentor
29
31
  from openlit.instrumentation.transformers import TransformersInstrumentor
30
32
 
31
33
  # Set up logging for error and information messages.
@@ -154,12 +156,14 @@ def init(environment="default", application_name="default", tracer=None, otlp_en
154
156
  "bedrock": "boto3",
155
157
  "vertexai": "vertexai",
156
158
  "groq": "groq",
159
+ "ollama": "ollama",
157
160
  "langchain": "langchain",
158
161
  "llama_index": "llama_index",
159
162
  "haystack": "haystack",
160
163
  "chroma": "chromadb",
161
164
  "pinecone": "pinecone",
162
165
  "qdrant": "qdrant_client",
166
+ "milvus": "pymilvus",
163
167
  "transformers": "transformers"
164
168
  }
165
169
 
@@ -206,12 +210,14 @@ def init(environment="default", application_name="default", tracer=None, otlp_en
206
210
  "bedrock": BedrockInstrumentor(),
207
211
  "vertexai": VertexAIInstrumentor(),
208
212
  "groq": GroqInstrumentor(),
213
+ "ollama": OllamaInstrumentor(),
209
214
  "langchain": LangChainInstrumentor(),
210
215
  "llama_index": LlamaIndexInstrumentor(),
211
216
  "haystack": HaystackInstrumentor(),
212
217
  "chroma": ChromaInstrumentor(),
213
218
  "pinecone": PineconeInstrumentor(),
214
219
  "qdrant": QdrantInstrumentor(),
220
+ "milvus": MilvusInstrumentor(),
215
221
  "transformers": TransformersInstrumentor()
216
222
  }
217
223
 
@@ -0,0 +1,94 @@
1
+ # pylint: disable=useless-return, bad-staticmethod-argument, disable=duplicate-code
2
+ """Initializer of Auto Instrumentation of Milvus Functions"""
3
+ from typing import Collection
4
+ import importlib.metadata
5
+ from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
6
+ from wrapt import wrap_function_wrapper
7
+
8
+ from openlit.instrumentation.milvus.milvus import general_wrap
9
+
10
+ _instruments = ("pymilvus >= 2.4.3",)
11
+
12
+ WRAPPED_METHODS = [
13
+ {
14
+ "package": "pymilvus",
15
+ "object": "MilvusClient.create_collection",
16
+ "endpoint": "milvus.create_collection",
17
+ "wrapper": general_wrap,
18
+ },
19
+ {
20
+ "package": "pymilvus",
21
+ "object": "MilvusClient.drop_collection",
22
+ "endpoint": "milvus.drop_collection",
23
+ "wrapper": general_wrap,
24
+ },
25
+ {
26
+ "package": "pymilvus",
27
+ "object": "MilvusClient.insert",
28
+ "endpoint": "milvus.insert",
29
+ "wrapper": general_wrap,
30
+ },
31
+ {
32
+ "package": "pymilvus",
33
+ "object": "MilvusClient.upsert",
34
+ "endpoint": "milvus.upsert",
35
+ "wrapper": general_wrap,
36
+ },
37
+ {
38
+ "package": "pymilvus",
39
+ "object": "MilvusClient.search",
40
+ "endpoint": "milvus.search",
41
+ "wrapper": general_wrap,
42
+ },
43
+ {
44
+ "package": "pymilvus",
45
+ "object": "MilvusClient.query",
46
+ "endpoint": "milvus.query",
47
+ "wrapper": general_wrap,
48
+ },
49
+ {
50
+ "package": "pymilvus",
51
+ "object": "MilvusClient.get",
52
+ "endpoint": "milvus.get",
53
+ "wrapper": general_wrap,
54
+ },
55
+ {
56
+ "package": "pymilvus",
57
+ "object": "MilvusClient.delete",
58
+ "endpoint": "milvus.delete",
59
+ "wrapper": general_wrap,
60
+ },
61
+ ]
62
+
63
+ class MilvusInstrumentor(BaseInstrumentor):
64
+ """An instrumentor for Milvus's client library."""
65
+
66
+ def instrumentation_dependencies(self) -> Collection[str]:
67
+ return _instruments
68
+
69
+ def _instrument(self, **kwargs):
70
+ application_name = kwargs.get("application_name")
71
+ environment = kwargs.get("environment")
72
+ tracer = kwargs.get("tracer")
73
+ metrics = kwargs.get("metrics_dict")
74
+ pricing_info = kwargs.get("pricing_info")
75
+ trace_content = kwargs.get("trace_content")
76
+ disable_metrics = kwargs.get("disable_metrics")
77
+ version = importlib.metadata.version("pymilvus")
78
+
79
+ for wrapped_method in WRAPPED_METHODS:
80
+ wrap_package = wrapped_method.get("package")
81
+ wrap_object = wrapped_method.get("object")
82
+ gen_ai_endpoint = wrapped_method.get("endpoint")
83
+ wrapper = wrapped_method.get("wrapper")
84
+ wrap_function_wrapper(
85
+ wrap_package,
86
+ wrap_object,
87
+ wrapper(gen_ai_endpoint, version, environment, application_name,
88
+ tracer, pricing_info, trace_content, metrics, disable_metrics),
89
+ )
90
+
91
+
92
+ @staticmethod
93
+ def _uninstrument(self, **kwargs):
94
+ pass
@@ -0,0 +1,179 @@
1
+ # pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument, possibly-used-before-assignment, too-many-branches
2
+ """
3
+ Module for monitoring Milvus.
4
+ """
5
+
6
+ import logging
7
+ from opentelemetry.trace import SpanKind, Status, StatusCode
8
+ from opentelemetry.sdk.resources import TELEMETRY_SDK_NAME
9
+ from openlit.__helpers import handle_exception
10
+ from openlit.semcov import SemanticConvetion
11
+
12
+ # Initialize logger for logging potential issues and operations
13
+ logger = logging.getLogger(__name__)
14
+
15
+ def object_count(obj):
16
+ """
17
+ Counts Length of object if it exists, Else returns None
18
+ """
19
+ try:
20
+ cnt = len(obj)
21
+ # pylint: disable=bare-except
22
+ except:
23
+ cnt = 0
24
+
25
+ return cnt
26
+
27
+ def general_wrap(gen_ai_endpoint, version, environment, application_name,
28
+ tracer, pricing_info, trace_content, metrics, disable_metrics):
29
+ """
30
+ Creates a wrapper around a function call to trace and log its execution metrics.
31
+
32
+ This function wraps any given function to measure its execution time,
33
+ log its operation, and trace its execution using OpenTelemetry.
34
+
35
+ Parameters:
36
+ - gen_ai_endpoint (str): A descriptor or name for the endpoint being traced.
37
+ - version (str): The version of the Langchain application.
38
+ - environment (str): The deployment environment (e.g., 'production', 'development').
39
+ - application_name (str): Name of the Langchain application.
40
+ - tracer (opentelemetry.trace.Tracer): The tracer object used for OpenTelemetry tracing.
41
+ - pricing_info (dict): Information about the pricing for internal metrics (currently not used).
42
+ - trace_content (bool): Flag indicating whether to trace the content of the response.
43
+
44
+ Returns:
45
+ - function: A higher-order function that takes a function 'wrapped' and returns
46
+ a new function that wraps 'wrapped' with additional tracing and logging.
47
+ """
48
+
49
+ def wrapper(wrapped, instance, args, kwargs):
50
+ """
51
+ An inner wrapper function that executes the wrapped function, measures execution
52
+ time, and records trace data using OpenTelemetry.
53
+
54
+ Parameters:
55
+ - wrapped (Callable): The original function that this wrapper will execute.
56
+ - instance (object): The instance to which the wrapped function belongs. This
57
+ is used for instance methods. For static and classmethods,
58
+ this may be None.
59
+ - args (tuple): Positional arguments passed to the wrapped function.
60
+ - kwargs (dict): Keyword arguments passed to the wrapped function.
61
+
62
+ Returns:
63
+ - The result of the wrapped function call.
64
+
65
+ The wrapper initiates a span with the provided tracer, sets various attributes
66
+ on the span based on the function's execution and response, and ensures
67
+ errors are handled and logged appropriately.
68
+ """
69
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
70
+ response = wrapped(*args, **kwargs)
71
+
72
+ try:
73
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
74
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
75
+ gen_ai_endpoint)
76
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
77
+ environment)
78
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
79
+ application_name)
80
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
81
+ SemanticConvetion.GEN_AI_TYPE_VECTORDB)
82
+ span.set_attribute(SemanticConvetion.DB_SYSTEM,
83
+ SemanticConvetion.DB_SYSTEM_MILVUS)
84
+
85
+ if gen_ai_endpoint == "milvus.create_collection":
86
+ db_operation = SemanticConvetion.DB_OPERATION_CREATE_COLLECTION
87
+ span.set_attribute(SemanticConvetion.DB_OPERATION,
88
+ SemanticConvetion.DB_OPERATION_CREATE_COLLECTION)
89
+ span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
90
+ kwargs.get("collection_name", ""))
91
+ span.set_attribute(SemanticConvetion.DB_COLLECTION_DIMENSION,
92
+ kwargs.get("dimension", ""))
93
+
94
+ elif gen_ai_endpoint == "milvus.drop_collection":
95
+ db_operation = SemanticConvetion.DB_OPERATION_DELETE_COLLECTION
96
+ span.set_attribute(SemanticConvetion.DB_OPERATION,
97
+ SemanticConvetion.DB_OPERATION_DELETE_COLLECTION)
98
+ span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
99
+ kwargs.get("collection_name", ""))
100
+
101
+ elif gen_ai_endpoint == "milvus.insert":
102
+ db_operation = SemanticConvetion.DB_OPERATION_ADD
103
+ span.set_attribute(SemanticConvetion.DB_OPERATION,
104
+ SemanticConvetion.DB_OPERATION_ADD)
105
+ span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
106
+ kwargs.get("collection_name", ""))
107
+ span.set_attribute(SemanticConvetion.DB_VECTOR_COUNT,
108
+ object_count(kwargs.get("data")))
109
+ span.set_attribute(SemanticConvetion.DB_OPERATION_COST,
110
+ response["cost"])
111
+
112
+ elif gen_ai_endpoint == "milvus.search":
113
+ db_operation = SemanticConvetion.DB_OPERATION_QUERY
114
+ span.set_attribute(SemanticConvetion.DB_OPERATION,
115
+ SemanticConvetion.DB_OPERATION_QUERY)
116
+ span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
117
+ kwargs.get("collection_name", ""))
118
+ span.set_attribute(SemanticConvetion.DB_STATEMENT,
119
+ str(kwargs.get("data")))
120
+
121
+ elif gen_ai_endpoint in ["milvus.query", "milvus.get"]:
122
+ db_operation = SemanticConvetion.DB_OPERATION_QUERY
123
+ span.set_attribute(SemanticConvetion.DB_OPERATION,
124
+ SemanticConvetion.DB_OPERATION_QUERY)
125
+ span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
126
+ kwargs.get("collection_name", ""))
127
+ span.set_attribute(SemanticConvetion.DB_STATEMENT,
128
+ str(kwargs.get("output_fields")))
129
+
130
+ elif gen_ai_endpoint == "milvus.upsert":
131
+ db_operation = SemanticConvetion.DB_OPERATION_ADD
132
+ span.set_attribute(SemanticConvetion.DB_OPERATION,
133
+ SemanticConvetion.DB_OPERATION_UPSERT)
134
+ span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
135
+ kwargs.get("collection_name", ""))
136
+ span.set_attribute(SemanticConvetion.DB_VECTOR_COUNT,
137
+ object_count(kwargs.get("data")))
138
+ span.set_attribute(SemanticConvetion.DB_OPERATION_COST,
139
+ response["cost"])
140
+
141
+ elif gen_ai_endpoint == "milvus.delete":
142
+ db_operation = SemanticConvetion.DB_OPERATION_DELETE
143
+ span.set_attribute(SemanticConvetion.DB_OPERATION,
144
+ SemanticConvetion.DB_OPERATION_DELETE)
145
+ span.set_attribute(SemanticConvetion.DB_COLLECTION_NAME,
146
+ kwargs.get("collection_name", ""))
147
+ span.set_attribute(SemanticConvetion.DB_FILTER,
148
+ str(kwargs.get("filter", "")))
149
+
150
+ span.set_status(Status(StatusCode.OK))
151
+
152
+ if disable_metrics is False:
153
+ attributes = {
154
+ TELEMETRY_SDK_NAME:
155
+ "openlit",
156
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
157
+ application_name,
158
+ SemanticConvetion.DB_SYSTEM:
159
+ SemanticConvetion.DB_SYSTEM_MILVUS,
160
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
161
+ environment,
162
+ SemanticConvetion.GEN_AI_TYPE:
163
+ SemanticConvetion.GEN_AI_TYPE_VECTORDB,
164
+ SemanticConvetion.DB_OPERATION:
165
+ db_operation
166
+ }
167
+
168
+ metrics["db_requests"].add(1, attributes)
169
+
170
+ return response
171
+
172
+ except Exception as e:
173
+ handle_exception(span, e)
174
+ logger.error("Error in trace creation: %s", e)
175
+
176
+ # Return original response
177
+ return response
178
+
179
+ return wrapper
@@ -0,0 +1,104 @@
1
+ # pylint: disable=useless-return, bad-staticmethod-argument, disable=duplicate-code
2
+ """Initializer of Auto Instrumentation of Ollama Functions"""
3
+
4
+ from typing import Collection
5
+ import importlib.metadata
6
+ from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
7
+ from wrapt import wrap_function_wrapper
8
+
9
+ from openlit.instrumentation.ollama.ollama import (
10
+ chat, embeddings, generate
11
+ )
12
+ from openlit.instrumentation.ollama.async_ollama import (
13
+ async_chat, async_embeddings, async_generate
14
+ )
15
+
16
+ _instruments = ("ollama >= 0.2.0",)
17
+
18
+ class OllamaInstrumentor(BaseInstrumentor):
19
+ """
20
+ An instrumentor for Ollama's client library.
21
+ """
22
+
23
+ def instrumentation_dependencies(self) -> Collection[str]:
24
+ return _instruments
25
+
26
+ def _instrument(self, **kwargs):
27
+ application_name = kwargs.get("application_name", "default_application")
28
+ environment = kwargs.get("environment", "default_environment")
29
+ tracer = kwargs.get("tracer")
30
+ metrics = kwargs.get("metrics_dict")
31
+ pricing_info = kwargs.get("pricing_info", {})
32
+ trace_content = kwargs.get("trace_content", False)
33
+ disable_metrics = kwargs.get("disable_metrics")
34
+ version = importlib.metadata.version("ollama")
35
+
36
+ # sync chat
37
+ wrap_function_wrapper(
38
+ "ollama",
39
+ "chat",
40
+ chat("ollama.chat", version, environment, application_name,
41
+ tracer, pricing_info, trace_content, metrics, disable_metrics),
42
+ )
43
+ wrap_function_wrapper(
44
+ "ollama",
45
+ "Client.chat",
46
+ chat("ollama.chat", version, environment, application_name,
47
+ tracer, pricing_info, trace_content, metrics, disable_metrics),
48
+ )
49
+
50
+ # sync embeddings
51
+ wrap_function_wrapper(
52
+ "ollama",
53
+ "embeddings",
54
+ embeddings("ollama.embeddings", version, environment, application_name,
55
+ tracer, pricing_info, trace_content, metrics, disable_metrics),
56
+ )
57
+ wrap_function_wrapper(
58
+ "ollama",
59
+ "Client.embeddings",
60
+ embeddings("ollama.embeddings", version, environment, application_name,
61
+ tracer, pricing_info, trace_content, metrics, disable_metrics),
62
+ )
63
+
64
+ # sync generate
65
+ wrap_function_wrapper(
66
+ "ollama",
67
+ "generate",
68
+ generate("ollama.generate", version, environment, application_name,
69
+ tracer, pricing_info, trace_content, metrics, disable_metrics),
70
+ )
71
+ wrap_function_wrapper(
72
+ "ollama",
73
+ "Client.generate",
74
+ generate("ollama.generate", version, environment, application_name,
75
+ tracer, pricing_info, trace_content, metrics, disable_metrics),
76
+ )
77
+
78
+ # async chat
79
+ wrap_function_wrapper(
80
+ "ollama",
81
+ "AsyncClient.chat",
82
+ async_chat("ollama.chat", version, environment, application_name,
83
+ tracer, pricing_info, trace_content, metrics, disable_metrics),
84
+ )
85
+
86
+ # async embeddings
87
+ wrap_function_wrapper(
88
+ "ollama",
89
+ "AsyncClient.embeddings",
90
+ async_embeddings("ollama.embeddings", version, environment, application_name,
91
+ tracer, pricing_info, trace_content, metrics, disable_metrics),
92
+ )
93
+
94
+ # aync generate
95
+ wrap_function_wrapper(
96
+ "ollama",
97
+ "AsyncClient.generate",
98
+ async_generate("ollama.generate", version, environment, application_name,
99
+ tracer, pricing_info, trace_content, metrics, disable_metrics),
100
+ )
101
+
102
+ def _uninstrument(self, **kwargs):
103
+ # Proper uninstrumentation logic to revert patched methods
104
+ pass