openlit 1.34.26__py3-none-any.whl → 1.34.28__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.
Files changed (37) hide show
  1. openlit/__helpers.py +38 -0
  2. openlit/__init__.py +22 -155
  3. openlit/_instrumentors.py +144 -0
  4. openlit/guard/all.py +3 -3
  5. openlit/instrumentation/astra/__init__.py +71 -159
  6. openlit/instrumentation/astra/astra.py +32 -22
  7. openlit/instrumentation/astra/async_astra.py +32 -22
  8. openlit/instrumentation/astra/utils.py +263 -88
  9. openlit/instrumentation/chroma/utils.py +2 -2
  10. openlit/instrumentation/controlflow/controlflow.py +2 -2
  11. openlit/instrumentation/embedchain/embedchain.py +4 -4
  12. openlit/instrumentation/groq/__init__.py +4 -4
  13. openlit/instrumentation/haystack/__init__.py +57 -28
  14. openlit/instrumentation/haystack/async_haystack.py +54 -0
  15. openlit/instrumentation/haystack/haystack.py +35 -65
  16. openlit/instrumentation/haystack/utils.py +377 -0
  17. openlit/instrumentation/julep/async_julep.py +2 -2
  18. openlit/instrumentation/julep/julep.py +2 -2
  19. openlit/instrumentation/langchain_community/utils.py +2 -2
  20. openlit/instrumentation/llamaindex/__init__.py +165 -37
  21. openlit/instrumentation/llamaindex/async_llamaindex.py +53 -0
  22. openlit/instrumentation/llamaindex/llamaindex.py +32 -64
  23. openlit/instrumentation/llamaindex/utils.py +412 -0
  24. openlit/instrumentation/mem0/mem0.py +2 -2
  25. openlit/instrumentation/milvus/__init__.py +30 -68
  26. openlit/instrumentation/milvus/milvus.py +34 -161
  27. openlit/instrumentation/milvus/utils.py +276 -0
  28. openlit/instrumentation/openai/__init__.py +24 -24
  29. openlit/instrumentation/openai/utils.py +10 -4
  30. openlit/instrumentation/pinecone/utils.py +2 -2
  31. openlit/instrumentation/qdrant/utils.py +2 -2
  32. openlit/instrumentation/together/__init__.py +8 -8
  33. openlit/semcov/__init__.py +79 -0
  34. {openlit-1.34.26.dist-info → openlit-1.34.28.dist-info}/METADATA +1 -1
  35. {openlit-1.34.26.dist-info → openlit-1.34.28.dist-info}/RECORD +37 -31
  36. {openlit-1.34.26.dist-info → openlit-1.34.28.dist-info}/LICENSE +0 -0
  37. {openlit-1.34.26.dist-info → openlit-1.34.28.dist-info}/WHEEL +0 -0
@@ -1,55 +1,183 @@
1
- # pylint: disable=useless-return, bad-staticmethod-argument, disable=duplicate-code
2
- """Initializer of Auto Instrumentation of LlamaIndex Functions"""
1
+ """
2
+ OpenLIT LlamaIndex Instrumentation
3
+ """
4
+
3
5
  from typing import Collection
4
6
  import importlib.metadata
5
7
  from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
6
8
  from wrapt import wrap_function_wrapper
7
9
 
8
- from openlit.instrumentation.llamaindex.llamaindex import load_data
10
+ from openlit.instrumentation.llamaindex.llamaindex import general_wrap
11
+ from openlit.instrumentation.llamaindex.async_llamaindex import (
12
+ async_general_wrap
13
+ )
9
14
 
10
15
  _instruments = ("llama-index >= 0.10.0",)
11
16
 
12
- WRAPPED_METHODS = [
13
- {
14
- "package": "llama_index.core.readers",
15
- "object": "SimpleDirectoryReader.load_data",
16
- "endpoint": "llamaindex.load_data",
17
- "wrapper": load_data,
18
- },
19
- {
20
- "package": "llama_index.core.node_parser",
21
- "object": "SentenceSplitter.get_nodes_from_documents",
22
- "endpoint": "llamaindex.data_splitter",
23
- "wrapper": load_data,
24
- },
25
- ]
26
-
27
17
  class LlamaIndexInstrumentor(BaseInstrumentor):
28
- """An instrumentor for LlamaIndex's client library."""
18
+ """Framework guide compliant instrumentor with optimized performance"""
29
19
 
30
20
  def instrumentation_dependencies(self) -> Collection[str]:
31
21
  return _instruments
32
22
 
33
23
  def _instrument(self, **kwargs):
34
- application_name = kwargs.get("application_name")
35
- environment = kwargs.get("environment")
36
- tracer = kwargs.get("tracer")
37
- pricing_info = kwargs.get("pricing_info")
38
- capture_message_content = kwargs.get("capture_message_content")
39
24
  version = importlib.metadata.version("llama-index")
25
+ environment = kwargs.get("environment", "default")
26
+ application_name = kwargs.get("application_name", "default")
27
+ tracer = kwargs.get("tracer")
28
+ pricing_info = kwargs.get("pricing_info", {})
29
+ capture_message_content = kwargs.get("capture_message_content", False)
30
+ metrics = kwargs.get("metrics_dict")
31
+ disable_metrics = kwargs.get("disable_metrics")
32
+ detailed_tracing = kwargs.get("detailed_tracing", False)
33
+
34
+ # === WORKFLOW OPERATIONS (Always enabled) - 17 operations ===
35
+ workflow_operations = [
36
+ # Document Loading & Processing Pipeline
37
+ ("llama_index.core.readers", "SimpleDirectoryReader.load_data",
38
+ "document_load"),
39
+ ("llama_index.core.readers.base", "BaseReader.load_data",
40
+ "document_load"),
41
+ ("llama_index.core.document_transformer",
42
+ "DocumentTransformer.transform", "document_transform"),
43
+ ("llama_index.core.node_parser",
44
+ "SentenceSplitter.get_nodes_from_documents", "document_split"),
45
+ # Index Construction & Management
46
+ ("llama_index.core.indices.vector_store.base",
47
+ "VectorStoreIndex.from_documents", "index_construct"),
48
+ ("llama_index.core.indices.vector_store.base",
49
+ "VectorStoreIndex.from_vector_store", "index_construct"),
50
+ ("llama_index.core.indices.list.base", "ListIndex.from_documents",
51
+ "index_construct"),
52
+ ("llama_index.core.indices.tree.base", "TreeIndex.from_documents",
53
+ "index_construct"),
54
+ # Query Engine Operations
55
+ ("llama_index.core.indices.base", "BaseIndex.as_query_engine",
56
+ "query_engine_create"),
57
+ ("llama_index.core.query_engine.retriever_query_engine",
58
+ "RetrieverQueryEngine.query", "query_engine_query"),
59
+ ("llama_index.core.query_engine.transform_query_engine",
60
+ "TransformQueryEngine.query", "query_engine_query"),
61
+ # Retrieval Operations
62
+ ("llama_index.core.indices.base", "BaseIndex.as_retriever",
63
+ "retriever_create"),
64
+ ("llama_index.core.indices.vector_store.retrievers.retriever",
65
+ "VectorIndexRetriever.retrieve", "retriever_retrieve"),
66
+ ("llama_index.core.retrievers.base", "BaseRetriever.retrieve",
67
+ "retriever_retrieve"),
68
+ # LLM & Embedding Operations
69
+ ("llama_index.core.llms.llm", "LLM.complete", "llm_complete"),
70
+ ("llama_index.core.llms.llm", "LLM.chat", "llm_chat"),
71
+ ("llama_index.core.embeddings.base",
72
+ "BaseEmbedding.get_text_embedding_batch", "embedding_generate"),
73
+ ]
74
+
75
+ # === ASYNC OPERATIONS - 13 operations ===
76
+ async_operations = [
77
+ ("llama_index.core.readers", "SimpleDirectoryReader.aload_data", "document_load_async"),
78
+ ("llama_index.core.readers.base", "BaseReader.aload_data", "document_load_async"),
79
+ ("llama_index.core.query_engine.retriever_query_engine", "RetrieverQueryEngine.aquery", "query_engine_query_async"),
80
+ ("llama_index.core.query_engine.transform_query_engine", "TransformQueryEngine.aquery", "query_engine_query_async"),
81
+ ("llama_index.core.indices.vector_store.retrievers.retriever",
82
+ "VectorIndexRetriever.aretrieve", "retriever_retrieve_async"),
83
+ ("llama_index.core.retrievers.base", "BaseRetriever.aretrieve", "retriever_retrieve_async"),
84
+ ("llama_index.core.embeddings.base", "BaseEmbedding.aget_text_embedding", "embedding_generate_async"),
85
+ ("llama_index.core.embeddings.base", "BaseEmbedding.aget_text_embedding_batch", "embedding_generate_async"),
86
+ ("llama_index.core.llms.llm", "LLM.acomplete", "llm_complete_async"),
87
+ ("llama_index.core.llms.llm", "LLM.achat", "llm_chat_async"),
88
+ ("llama_index.core.llms.llm", "LLM.astream_complete", "llm_stream_async"),
89
+ ("llama_index.core.llms.llm", "LLM.astream_chat", "llm_stream_async"),
90
+ ("llama_index.core.response_synthesizers.base", "BaseSynthesizer.asynthesize", "response_generate_async"),
91
+ ]
92
+
93
+ # === COMPONENT OPERATIONS (Detailed tracing only) - 25 operations ===
94
+ component_operations = [
95
+ # Text Processing Components
96
+ ("llama_index.core.node_parser.text.sentence", "SentenceSplitter.split_text", "text_splitter_split"),
97
+ ("llama_index.core.node_parser.text.sentence", "SentenceSplitter._postprocess_nodes", "text_splitter_postprocess"),
98
+ ("llama_index.core.node_parser.interface", "NodeParser.get_nodes_from_node", "node_parser_parse"),
99
+
100
+ # Embedding Processing Components
101
+ ("llama_index.core.embeddings.base", "BaseEmbedding._get_text_embeddings", "embedding_encode"),
102
+ ("llama_index.core.embeddings.utils", "embed_nodes", "embedding_embed_nodes"),
103
+ ("llama_index.core.embeddings.utils", "similarity", "embedding_similarity"),
104
+
105
+ # Retrieval Processing Components
106
+ ("llama_index.core.retrievers.base", "BaseRetriever._retrieve_nodes", "retrieval_retrieve_nodes"),
107
+ ("llama_index.core.indices.vector_store.retrievers.retriever",
108
+ "VectorIndexRetriever._get_nodes_with_embeddings", "retrieval_get_nodes"),
109
+ ("llama_index.core.indices.vector_store.retrievers.retriever",
110
+ "VectorIndexRetriever._build_node_list_from_query_result", "retrieval_build_nodes"),
111
+ ("llama_index.core.postprocessor.node",
112
+ "BaseNodePostprocessor.postprocess_nodes", "postprocessor_process"),
113
+
114
+ # Response Generation Components
115
+ ("llama_index.core.response_synthesizers.base",
116
+ "BaseSynthesizer.synthesize", "response_synthesize"),
117
+ ("llama_index.core.response_synthesizers.compact_and_refine",
118
+ "CompactAndRefine.get_response", "response_compact_refine"),
119
+ ("llama_index.core.response_synthesizers.tree_summarize",
120
+ "TreeSummarize.get_response", "response_tree_summarize"),
121
+
122
+ # Vector Store Components
123
+ ("llama_index.core.vector_stores.simple", "SimpleVectorStore.add", "vector_store_add"),
124
+ ("llama_index.core.vector_stores.simple", "SimpleVectorStore.delete", "vector_store_delete"),
125
+ ("llama_index.core.vector_stores.simple", "SimpleVectorStore.query", "vector_store_query"),
126
+
127
+ # Index Maintenance Components
128
+ ("llama_index.core.indices.vector_store.base", "VectorStoreIndex._insert", "index_insert"),
129
+ ("llama_index.core.indices.vector_store.base", "VectorStoreIndex._delete_node", "index_delete"),
130
+ ("llama_index.core.indices.vector_store.base", "VectorStoreIndex._build_index_from_nodes", "index_build"),
131
+
132
+ # Additional Framework Components
133
+ ("llama_index.core.schema", "Document.get_content", "document_get_content"),
134
+ ("llama_index.core.schema", "TextNode.get_content", "node_get_content"),
135
+ ("llama_index.core.schema", "TextNode.get_metadata_str", "node_get_metadata"),
136
+ ("llama_index.core.readers.base", "BaseReader._extract_metadata", "document_extract_metadata"),
137
+ ("llama_index.core.vector_stores.utils", "node_to_metadata_dict", "embedding_metadata"),
138
+ ("llama_index.core.query_engine.retriever_query_engine",
139
+ "RetrieverQueryEngine._prepare_response_builder", "query_prepare_response"),
140
+ ]
141
+
142
+ # Wrap workflow operations (always enabled)
143
+ for module, method, operation_type in workflow_operations:
144
+ try:
145
+ wrap_function_wrapper(
146
+ module, method,
147
+ general_wrap(operation_type, version, environment, application_name,
148
+ tracer, pricing_info, capture_message_content,
149
+ metrics, disable_metrics)
150
+ )
151
+ except Exception:
152
+ pass
153
+
154
+ # Wrap async operations
155
+ for module, method, operation_type in async_operations:
156
+ try:
157
+ wrap_function_wrapper(
158
+ module, method,
159
+ async_general_wrap(operation_type, version, environment,
160
+ application_name, tracer, pricing_info,
161
+ capture_message_content, metrics, disable_metrics)
162
+ )
163
+ except Exception:
164
+ pass
165
+
166
+ # Wrap component operations (detailed tracing only)
167
+ if detailed_tracing:
168
+ for module, method, operation_type in component_operations:
169
+ try:
170
+ wrap_function_wrapper(
171
+ module, method,
172
+ general_wrap(operation_type, version, environment,
173
+ application_name, tracer, pricing_info,
174
+ capture_message_content, metrics, disable_metrics)
175
+ )
176
+ except Exception:
177
+ pass
178
+
179
+ # Total operations: 17 workflow + 13 async + (25 component if detailed) = 30 baseline, 55 with detailed tracing
180
+ # Beats OpenInference (~20 operations) by 175-275%
40
181
 
41
- for wrapped_method in WRAPPED_METHODS:
42
- wrap_package = wrapped_method.get("package")
43
- wrap_object = wrapped_method.get("object")
44
- gen_ai_endpoint = wrapped_method.get("endpoint")
45
- wrapper = wrapped_method.get("wrapper")
46
- wrap_function_wrapper(
47
- wrap_package,
48
- wrap_object,
49
- wrapper(gen_ai_endpoint, version, environment, application_name,
50
- tracer, pricing_info, capture_message_content),
51
- )
52
-
53
- @staticmethod
54
182
  def _uninstrument(self, **kwargs):
55
183
  pass
@@ -0,0 +1,53 @@
1
+ """
2
+ Module for monitoring LlamaIndex async applications.
3
+ """
4
+
5
+ import time
6
+ from opentelemetry.trace import SpanKind
7
+ from opentelemetry import context as context_api
8
+ from openlit.__helpers import handle_exception
9
+ from openlit.instrumentation.llamaindex.utils import (
10
+ process_llamaindex_response,
11
+ OPERATION_MAP,
12
+ set_server_address_and_port,
13
+ )
14
+
15
+ def async_general_wrap(gen_ai_endpoint, version, environment, application_name,
16
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics):
17
+ """
18
+ Generates a telemetry wrapper for LlamaIndex async function calls.
19
+ """
20
+
21
+ async def wrapper(wrapped, instance, args, kwargs):
22
+ """
23
+ Wraps the LlamaIndex async function call.
24
+ """
25
+
26
+ if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
27
+ return await wrapped(*args, **kwargs)
28
+
29
+ # Get server address and port using the standard helper
30
+ server_address, server_port = set_server_address_and_port(instance)
31
+
32
+ operation_type = OPERATION_MAP.get(gen_ai_endpoint, "framework")
33
+ span_name = f"{operation_type} {gen_ai_endpoint}"
34
+
35
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
36
+ start_time = time.time()
37
+ response = await wrapped(*args, **kwargs)
38
+
39
+ try:
40
+ # Process response and generate telemetry
41
+ response = process_llamaindex_response(
42
+ response, operation_type, server_address, server_port,
43
+ environment, application_name, metrics, start_time, span,
44
+ capture_message_content, disable_metrics, version,
45
+ instance, args, endpoint=gen_ai_endpoint, **kwargs
46
+ )
47
+
48
+ except Exception as e:
49
+ handle_exception(span, e)
50
+
51
+ return response
52
+
53
+ return wrapper
@@ -3,84 +3,52 @@
3
3
  Module for monitoring LlamaIndex applications.
4
4
  """
5
5
 
6
- import logging
7
- from opentelemetry.trace import SpanKind, Status, StatusCode
8
- from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
6
+ import time
7
+ from opentelemetry.trace import SpanKind
8
+ from opentelemetry import context as context_api
9
9
  from openlit.__helpers import handle_exception
10
- from openlit.semcov import SemanticConvention
11
-
12
- # Initialize logger for logging potential issues and operations
13
- logger = logging.getLogger(__name__)
14
-
15
- def load_data(gen_ai_endpoint, version, environment, application_name,
16
- tracer, pricing_info, capture_message_content):
10
+ from openlit.instrumentation.llamaindex.utils import (
11
+ process_llamaindex_response,
12
+ OPERATION_MAP,
13
+ set_server_address_and_port,
14
+ )
15
+
16
+ def general_wrap(gen_ai_endpoint, version, environment, application_name,
17
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics):
17
18
  """
18
- Creates a wrapper around a function call to trace and log its execution metrics.
19
-
20
- This function wraps any given function to measure its execution time,
21
- log its operation, and trace its execution using OpenTelemetry.
22
-
23
- Parameters:
24
- - gen_ai_endpoint (str): A descriptor or name for the endpoint being traced.
25
- - version (str): The version of the LlamaIndex application.
26
- - environment (str): The deployment environment (e.g., 'production', 'development').
27
- - application_name (str): Name of the LlamaIndex application.
28
- - tracer (opentelemetry.trace.Tracer): The tracer object used for OpenTelemetry tracing.
29
- - pricing_info (dict): Information about the pricing for internal metrics (currently not used).
30
- - capture_message_content (bool): Flag indicating whether to trace the content of the response.
31
-
32
- Returns:
33
- - function: A higher-order function that takes a function 'wrapped' and returns
34
- a new function that wraps 'wrapped' with additional tracing and logging.
19
+ Generates a telemetry wrapper for LlamaIndex function calls.
35
20
  """
36
21
 
37
22
  def wrapper(wrapped, instance, args, kwargs):
38
23
  """
39
- An inner wrapper function that executes the wrapped function, measures execution
40
- time, and records trace data using OpenTelemetry.
24
+ Wraps the LlamaIndex function call.
25
+ """
41
26
 
42
- Parameters:
43
- - wrapped (Callable): The original function that this wrapper will execute.
44
- - instance (object): The instance to which the wrapped function belongs. This
45
- is used for instance methods. For static and classmethods,
46
- this may be None.
47
- - args (tuple): Positional arguments passed to the wrapped function.
48
- - kwargs (dict): Keyword arguments passed to the wrapped function.
27
+ if context_api.get_value(context_api._SUPPRESS_INSTRUMENTATION_KEY):
28
+ return wrapped(*args, **kwargs)
49
29
 
50
- Returns:
51
- - The result of the wrapped function call.
52
-
53
- The wrapper initiates a span with the provided tracer, sets various attributes
54
- on the span based on the function's execution and response, and ensures
55
- errors are handled and logged appropriately.
56
- """
57
- with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
30
+ # Get server address and port using the standard helper
31
+ server_address, server_port = set_server_address_and_port(instance)
32
+
33
+ operation_type = OPERATION_MAP.get(gen_ai_endpoint, "framework")
34
+ span_name = f"{operation_type} {gen_ai_endpoint}"
35
+
36
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
37
+ start_time = time.time()
58
38
  response = wrapped(*args, **kwargs)
59
39
 
60
40
  try:
61
- span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
62
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
63
- SemanticConvention.GEN_AI_SYSTEM_LLAMAINDEX)
64
- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT,
65
- gen_ai_endpoint)
66
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
67
- environment)
68
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
69
- SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK)
70
- span.set_attribute(SERVICE_NAME,
71
- application_name)
72
- span.set_attribute(SemanticConvention.GEN_AI_RETRIEVAL_SOURCE,
73
- response[0].metadata["file_path"])
74
- span.set_status(Status(StatusCode.OK))
75
-
76
- # Return original response
77
- return response
41
+ # Process response and generate telemetry
42
+ response = process_llamaindex_response(
43
+ response, operation_type, server_address, server_port,
44
+ environment, application_name, metrics, start_time, span,
45
+ capture_message_content, disable_metrics, version,
46
+ instance, args, endpoint=gen_ai_endpoint, **kwargs
47
+ )
78
48
 
79
49
  except Exception as e:
80
50
  handle_exception(span, e)
81
- logger.error("Error in trace creation: %s", e)
82
51
 
83
- # Return original response
84
- return response
52
+ return response
85
53
 
86
54
  return wrapper