monocle-apptrace 0.3.0b1__py3-none-any.whl → 0.3.0b3__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.
Potentially problematic release.
This version of monocle-apptrace might be problematic. Click here for more details.
- monocle_apptrace/exporters/aws/s3_exporter.py +1 -1
- monocle_apptrace/exporters/aws/s3_exporter_opendal.py +126 -0
- monocle_apptrace/exporters/azure/blob_exporter_opendal.py +147 -0
- monocle_apptrace/exporters/monocle_exporters.py +38 -20
- monocle_apptrace/instrumentation/__init__.py +0 -0
- monocle_apptrace/instrumentation/common/__init__.py +0 -0
- monocle_apptrace/{constants.py → instrumentation/common/constants.py} +13 -0
- monocle_apptrace/instrumentation/common/instrumentor.py +208 -0
- monocle_apptrace/instrumentation/common/span_handler.py +154 -0
- monocle_apptrace/instrumentation/common/utils.py +171 -0
- monocle_apptrace/instrumentation/common/wrapper.py +69 -0
- monocle_apptrace/instrumentation/common/wrapper_method.py +45 -0
- monocle_apptrace/instrumentation/metamodel/__init__.py +0 -0
- monocle_apptrace/instrumentation/metamodel/botocore/__init__.py +0 -0
- monocle_apptrace/instrumentation/metamodel/botocore/_helper.py +126 -0
- monocle_apptrace/instrumentation/metamodel/botocore/entities/__init__.py +0 -0
- monocle_apptrace/instrumentation/metamodel/botocore/entities/inference.py +65 -0
- monocle_apptrace/instrumentation/metamodel/botocore/methods.py +16 -0
- monocle_apptrace/instrumentation/metamodel/haystack/__init__.py +0 -0
- monocle_apptrace/instrumentation/metamodel/haystack/_helper.py +127 -0
- monocle_apptrace/instrumentation/metamodel/haystack/entities/__init__.py +0 -0
- monocle_apptrace/instrumentation/metamodel/haystack/entities/inference.py +76 -0
- monocle_apptrace/instrumentation/metamodel/haystack/entities/retrieval.py +61 -0
- monocle_apptrace/instrumentation/metamodel/haystack/methods.py +42 -0
- monocle_apptrace/instrumentation/metamodel/langchain/__init__.py +0 -0
- monocle_apptrace/instrumentation/metamodel/langchain/_helper.py +121 -0
- monocle_apptrace/instrumentation/metamodel/langchain/entities/__init__.py +0 -0
- monocle_apptrace/instrumentation/metamodel/langchain/entities/inference.py +71 -0
- monocle_apptrace/instrumentation/metamodel/langchain/entities/retrieval.py +58 -0
- monocle_apptrace/instrumentation/metamodel/langchain/methods.py +105 -0
- monocle_apptrace/instrumentation/metamodel/llamaindex/__init__.py +0 -0
- monocle_apptrace/instrumentation/metamodel/llamaindex/_helper.py +154 -0
- monocle_apptrace/instrumentation/metamodel/llamaindex/entities/__init__.py +0 -0
- monocle_apptrace/instrumentation/metamodel/llamaindex/entities/inference.py +71 -0
- monocle_apptrace/instrumentation/metamodel/llamaindex/entities/retrieval.py +57 -0
- monocle_apptrace/{metamodel/maps/llamaindex_methods.json → instrumentation/metamodel/llamaindex/methods.py} +28 -31
- {monocle_apptrace-0.3.0b1.dist-info → monocle_apptrace-0.3.0b3.dist-info}/METADATA +17 -2
- monocle_apptrace-0.3.0b3.dist-info/RECORD +48 -0
- {monocle_apptrace-0.3.0b1.dist-info → monocle_apptrace-0.3.0b3.dist-info}/WHEEL +1 -1
- monocle_apptrace/botocore/__init__.py +0 -9
- monocle_apptrace/haystack/__init__.py +0 -9
- monocle_apptrace/haystack/wrap_pipeline.py +0 -63
- monocle_apptrace/instrumentor.py +0 -121
- monocle_apptrace/langchain/__init__.py +0 -9
- monocle_apptrace/llamaindex/__init__.py +0 -16
- monocle_apptrace/message_processing.py +0 -80
- monocle_apptrace/metamodel/README.md +0 -47
- monocle_apptrace/metamodel/entities/README.md +0 -77
- monocle_apptrace/metamodel/entities/app_hosting_types.json +0 -29
- monocle_apptrace/metamodel/entities/entities.json +0 -49
- monocle_apptrace/metamodel/entities/inference_types.json +0 -33
- monocle_apptrace/metamodel/entities/model_types.json +0 -41
- monocle_apptrace/metamodel/entities/vector_store_types.json +0 -25
- monocle_apptrace/metamodel/entities/workflow_types.json +0 -22
- monocle_apptrace/metamodel/maps/attributes/inference/botocore_entities.json +0 -27
- monocle_apptrace/metamodel/maps/attributes/inference/haystack_entities.json +0 -57
- monocle_apptrace/metamodel/maps/attributes/inference/langchain_entities.json +0 -57
- monocle_apptrace/metamodel/maps/attributes/inference/llamaindex_entities.json +0 -57
- monocle_apptrace/metamodel/maps/attributes/retrieval/haystack_entities.json +0 -31
- monocle_apptrace/metamodel/maps/attributes/retrieval/langchain_entities.json +0 -31
- monocle_apptrace/metamodel/maps/attributes/retrieval/llamaindex_entities.json +0 -31
- monocle_apptrace/metamodel/maps/botocore_methods.json +0 -13
- monocle_apptrace/metamodel/maps/haystack_methods.json +0 -45
- monocle_apptrace/metamodel/maps/langchain_methods.json +0 -129
- monocle_apptrace/metamodel/spans/README.md +0 -121
- monocle_apptrace/metamodel/spans/span_example.json +0 -140
- monocle_apptrace/metamodel/spans/span_format.json +0 -55
- monocle_apptrace/metamodel/spans/span_types.json +0 -16
- monocle_apptrace/utils.py +0 -252
- monocle_apptrace/wrap_common.py +0 -511
- monocle_apptrace/wrapper.py +0 -27
- monocle_apptrace-0.3.0b1.dist-info/RECORD +0 -48
- {monocle_apptrace-0.3.0b1.dist-info → monocle_apptrace-0.3.0b3.dist-info}/licenses/LICENSE +0 -0
- {monocle_apptrace-0.3.0b1.dist-info → monocle_apptrace-0.3.0b3.dist-info}/licenses/NOTICE +0 -0
monocle_apptrace/wrap_common.py
DELETED
|
@@ -1,511 +0,0 @@
|
|
|
1
|
-
# pylint: disable=protected-access
|
|
2
|
-
import logging
|
|
3
|
-
import os
|
|
4
|
-
import inspect
|
|
5
|
-
from importlib.metadata import version
|
|
6
|
-
from urllib.parse import urlparse
|
|
7
|
-
from opentelemetry.trace import Tracer
|
|
8
|
-
from opentelemetry.sdk.trace import Span
|
|
9
|
-
from monocle_apptrace.utils import resolve_from_alias, with_tracer_wrapper, get_embedding_model, get_attribute, get_workflow_name, set_embedding_model, set_app_hosting_identifier_attribute
|
|
10
|
-
from monocle_apptrace.utils import set_attribute, get_vectorstore_deployment
|
|
11
|
-
from monocle_apptrace.utils import get_fully_qualified_class_name, get_nested_value
|
|
12
|
-
from monocle_apptrace.message_processing import extract_messages, extract_assistant_message
|
|
13
|
-
from functools import wraps
|
|
14
|
-
|
|
15
|
-
logger = logging.getLogger(__name__)
|
|
16
|
-
WORKFLOW_TYPE_KEY = "workflow_type"
|
|
17
|
-
DATA_INPUT_KEY = "data.input"
|
|
18
|
-
DATA_OUTPUT_KEY = "data.output"
|
|
19
|
-
PROMPT_INPUT_KEY = "data.input"
|
|
20
|
-
PROMPT_OUTPUT_KEY = "data.output"
|
|
21
|
-
QUERY = "input"
|
|
22
|
-
RESPONSE = "response"
|
|
23
|
-
SESSION_PROPERTIES_KEY = "session"
|
|
24
|
-
INFRA_SERVICE_KEY = "infra_service_name"
|
|
25
|
-
|
|
26
|
-
TYPE = "type"
|
|
27
|
-
PROVIDER = "provider_name"
|
|
28
|
-
EMBEDDING_MODEL = "embedding_model"
|
|
29
|
-
VECTOR_STORE = 'vector_store'
|
|
30
|
-
META_DATA = 'metadata'
|
|
31
|
-
|
|
32
|
-
WORKFLOW_TYPE_MAP = {
|
|
33
|
-
"llama_index": "workflow.llamaindex",
|
|
34
|
-
"langchain": "workflow.langchain",
|
|
35
|
-
"haystack": "workflow.haystack"
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
def get_embedding_model_haystack(instance):
|
|
40
|
-
try:
|
|
41
|
-
if hasattr(instance, 'get_component'):
|
|
42
|
-
text_embedder = instance.get_component('text_embedder')
|
|
43
|
-
if text_embedder and hasattr(text_embedder, 'model'):
|
|
44
|
-
# Set the embedding model attribute
|
|
45
|
-
return text_embedder.model
|
|
46
|
-
except:
|
|
47
|
-
pass
|
|
48
|
-
|
|
49
|
-
return None
|
|
50
|
-
|
|
51
|
-
@with_tracer_wrapper
|
|
52
|
-
def task_wrapper(tracer: Tracer, to_wrap, wrapped, instance, args, kwargs):
|
|
53
|
-
"""Instruments and calls every function defined in TO_WRAP."""
|
|
54
|
-
|
|
55
|
-
# Some Langchain objects are wrapped elsewhere, so we ignore them here
|
|
56
|
-
if instance.__class__.__name__ in ("AgentExecutor"):
|
|
57
|
-
return wrapped(*args, **kwargs)
|
|
58
|
-
|
|
59
|
-
if hasattr(instance, "name") and instance.name:
|
|
60
|
-
name = f"{to_wrap.get('span_name')}.{instance.name.lower()}"
|
|
61
|
-
elif to_wrap.get("span_name"):
|
|
62
|
-
name = to_wrap.get("span_name")
|
|
63
|
-
else:
|
|
64
|
-
name = get_fully_qualified_class_name(instance)
|
|
65
|
-
|
|
66
|
-
if 'haystack.core.pipeline.pipeline' in to_wrap['package']:
|
|
67
|
-
embedding_model = get_embedding_model_haystack(instance)
|
|
68
|
-
set_embedding_model(embedding_model)
|
|
69
|
-
inputs = set()
|
|
70
|
-
workflow_input = get_workflow_input(args, inputs)
|
|
71
|
-
set_attribute(DATA_INPUT_KEY, workflow_input)
|
|
72
|
-
|
|
73
|
-
if to_wrap.get('skip_span'):
|
|
74
|
-
return_value = wrapped(*args, **kwargs)
|
|
75
|
-
botocore_processor(tracer, to_wrap, wrapped, instance, args, kwargs, return_value)
|
|
76
|
-
return return_value
|
|
77
|
-
|
|
78
|
-
with tracer.start_as_current_span(name) as span:
|
|
79
|
-
pre_task_processing(to_wrap, instance, args, span)
|
|
80
|
-
return_value = wrapped(*args, **kwargs)
|
|
81
|
-
process_span(to_wrap, span, instance, args, kwargs, return_value)
|
|
82
|
-
post_task_processing(to_wrap, span, return_value)
|
|
83
|
-
|
|
84
|
-
return return_value
|
|
85
|
-
|
|
86
|
-
def botocore_processor(tracer, to_wrap, wrapped, instance, args, kwargs,return_value):
|
|
87
|
-
if kwargs.get("service_name") == "sagemaker-runtime":
|
|
88
|
-
return_value.invoke_endpoint = _instrumented_endpoint_invoke(to_wrap,return_value,return_value.invoke_endpoint,tracer)
|
|
89
|
-
|
|
90
|
-
def _instrumented_endpoint_invoke(to_wrap, instance, fn, tracer):
|
|
91
|
-
@wraps(fn)
|
|
92
|
-
def with_instrumentation(*args, **kwargs):
|
|
93
|
-
|
|
94
|
-
with tracer.start_as_current_span("botocore-sagemaker-invoke-endpoint") as span:
|
|
95
|
-
response = fn(*args, **kwargs)
|
|
96
|
-
process_span(to_wrap, span, instance=instance,args=args, kwargs=kwargs, return_value=response)
|
|
97
|
-
return response
|
|
98
|
-
|
|
99
|
-
return with_instrumentation
|
|
100
|
-
|
|
101
|
-
def get_workflow_input(args, inputs):
|
|
102
|
-
if args is not None and len(args) > 0:
|
|
103
|
-
for value in args[0].values():
|
|
104
|
-
for text in value.values():
|
|
105
|
-
inputs.add(text)
|
|
106
|
-
|
|
107
|
-
workflow_input: str = ""
|
|
108
|
-
|
|
109
|
-
if inputs is not None and len(inputs) > 0:
|
|
110
|
-
for input_str in inputs:
|
|
111
|
-
workflow_input = workflow_input + input_str
|
|
112
|
-
return workflow_input
|
|
113
|
-
|
|
114
|
-
def process_span(to_wrap, span, instance, args, kwargs, return_value):
|
|
115
|
-
# Check if the output_processor is a valid JSON (in Python, that means it's a dictionary)
|
|
116
|
-
instance_args = {}
|
|
117
|
-
set_provider_name(instance, instance_args)
|
|
118
|
-
span_index = 0
|
|
119
|
-
if is_root_span(span):
|
|
120
|
-
span_index += set_workflow_attributes(to_wrap, span, span_index+1)
|
|
121
|
-
span_index += set_app_hosting_identifier_attribute(span, span_index+1)
|
|
122
|
-
if 'output_processor' in to_wrap:
|
|
123
|
-
output_processor=to_wrap['output_processor']
|
|
124
|
-
if isinstance(output_processor, dict) and len(output_processor) > 0:
|
|
125
|
-
if 'type' in output_processor:
|
|
126
|
-
span.set_attribute("span.type", output_processor['type'])
|
|
127
|
-
else:
|
|
128
|
-
logger.warning("type of span not found or incorrect written in entity json")
|
|
129
|
-
if 'attributes' in output_processor:
|
|
130
|
-
for processors in output_processor["attributes"]:
|
|
131
|
-
for processor in processors:
|
|
132
|
-
attribute = processor.get('attribute')
|
|
133
|
-
accessor = processor.get('accessor')
|
|
134
|
-
|
|
135
|
-
if attribute and accessor:
|
|
136
|
-
attribute_name = f"entity.{span_index+1}.{attribute}"
|
|
137
|
-
try:
|
|
138
|
-
arguments = {"instance":instance, "args":args, "kwargs":kwargs, "output":return_value}
|
|
139
|
-
result = eval(accessor)(arguments)
|
|
140
|
-
if result and isinstance(result, str):
|
|
141
|
-
span.set_attribute(attribute_name, result)
|
|
142
|
-
except Exception as e:
|
|
143
|
-
logger.error(f"Error processing accessor: {e}")
|
|
144
|
-
else:
|
|
145
|
-
logger.warning(f"{' and '.join([key for key in ['attribute', 'accessor'] if not processor.get(key)])} not found or incorrect in entity JSON")
|
|
146
|
-
span_index += 1
|
|
147
|
-
else:
|
|
148
|
-
logger.warning("attributes not found or incorrect written in entity json")
|
|
149
|
-
if 'events' in output_processor:
|
|
150
|
-
events = output_processor['events']
|
|
151
|
-
arguments = {"instance": instance, "args": args, "kwargs": kwargs, "output": return_value}
|
|
152
|
-
accessor_mapping = {
|
|
153
|
-
"arguments": arguments,
|
|
154
|
-
"response": return_value
|
|
155
|
-
}
|
|
156
|
-
for event in events:
|
|
157
|
-
event_name = event.get("name")
|
|
158
|
-
event_attributes = {}
|
|
159
|
-
attributes = event.get("attributes", [])
|
|
160
|
-
for attribute in attributes:
|
|
161
|
-
attribute_key = attribute.get("attribute")
|
|
162
|
-
accessor = attribute.get("accessor")
|
|
163
|
-
if accessor:
|
|
164
|
-
try:
|
|
165
|
-
accessor_function = eval(accessor)
|
|
166
|
-
for keyword, value in accessor_mapping.items():
|
|
167
|
-
if keyword in accessor:
|
|
168
|
-
evaluated_val = accessor_function(value)
|
|
169
|
-
if isinstance(evaluated_val, list):
|
|
170
|
-
evaluated_val = [str(d) for d in evaluated_val]
|
|
171
|
-
event_attributes[attribute_key] = evaluated_val
|
|
172
|
-
except Exception as e:
|
|
173
|
-
logger.error(f"Error evaluating accessor for attribute '{attribute_key}': {e}")
|
|
174
|
-
span.add_event(name=event_name, attributes=event_attributes)
|
|
175
|
-
|
|
176
|
-
else:
|
|
177
|
-
logger.warning("empty or entities json is not in correct format")
|
|
178
|
-
if span_index > 0:
|
|
179
|
-
span.set_attribute("entity.count", span_index)
|
|
180
|
-
|
|
181
|
-
def set_workflow_attributes(to_wrap, span: Span, span_index):
|
|
182
|
-
return_value = 1
|
|
183
|
-
workflow_name = get_workflow_name(span=span)
|
|
184
|
-
if workflow_name:
|
|
185
|
-
span.set_attribute("span.type", "workflow")
|
|
186
|
-
span.set_attribute(f"entity.{span_index}.name", workflow_name)
|
|
187
|
-
# workflow type
|
|
188
|
-
package_name = to_wrap.get('package')
|
|
189
|
-
workflow_type_set = False
|
|
190
|
-
for (package, workflow_type) in WORKFLOW_TYPE_MAP.items():
|
|
191
|
-
if (package_name is not None and package in package_name):
|
|
192
|
-
span.set_attribute(f"entity.{span_index}.type", workflow_type)
|
|
193
|
-
workflow_type_set = True
|
|
194
|
-
if not workflow_type_set:
|
|
195
|
-
span.set_attribute(f"entity.{span_index}.type", "workflow.generic")
|
|
196
|
-
return return_value
|
|
197
|
-
|
|
198
|
-
def post_task_processing(to_wrap, span, return_value):
|
|
199
|
-
try:
|
|
200
|
-
update_span_with_context_output(to_wrap=to_wrap, return_value=return_value, span=span)
|
|
201
|
-
|
|
202
|
-
if is_root_span(span):
|
|
203
|
-
update_span_with_prompt_output(to_wrap=to_wrap, wrapped_args=return_value, span=span)
|
|
204
|
-
except:
|
|
205
|
-
logger.exception("exception in post_task_processing")
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
def pre_task_processing(to_wrap, instance, args, span):
|
|
209
|
-
try:
|
|
210
|
-
if is_root_span(span):
|
|
211
|
-
try:
|
|
212
|
-
sdk_version = version("monocle_apptrace")
|
|
213
|
-
span.set_attribute("monocle_apptrace.version", sdk_version)
|
|
214
|
-
except:
|
|
215
|
-
logger.warning(f"Exception finding monocle-apptrace version.")
|
|
216
|
-
update_span_with_prompt_input(to_wrap=to_wrap, wrapped_args=args, span=span)
|
|
217
|
-
update_span_with_context_input(to_wrap=to_wrap, wrapped_args=args, span=span)
|
|
218
|
-
except:
|
|
219
|
-
logger.exception("exception in pre_task_processing")
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
@with_tracer_wrapper
|
|
223
|
-
async def atask_wrapper(tracer, to_wrap, wrapped, instance, args, kwargs):
|
|
224
|
-
"""Instruments and calls every function defined in TO_WRAP."""
|
|
225
|
-
|
|
226
|
-
# Some Langchain objects are wrapped elsewhere, so we ignore them here
|
|
227
|
-
if instance.__class__.__name__ in ("AgentExecutor"):
|
|
228
|
-
return wrapped(*args, **kwargs)
|
|
229
|
-
|
|
230
|
-
if hasattr(instance, "name") and instance.name:
|
|
231
|
-
name = f"{to_wrap.get('span_name')}.{instance.name.lower()}"
|
|
232
|
-
elif to_wrap.get("span_name"):
|
|
233
|
-
name = to_wrap.get("span_name")
|
|
234
|
-
else:
|
|
235
|
-
name = get_fully_qualified_class_name(instance)
|
|
236
|
-
if 'haystack.core.pipeline.pipeline' in to_wrap['package']:
|
|
237
|
-
embedding_model = get_embedding_model_haystack(instance)
|
|
238
|
-
set_embedding_model(embedding_model)
|
|
239
|
-
inputs = set()
|
|
240
|
-
workflow_input = get_workflow_input(args, inputs)
|
|
241
|
-
set_attribute(DATA_INPUT_KEY, workflow_input)
|
|
242
|
-
|
|
243
|
-
with tracer.start_as_current_span(name) as span:
|
|
244
|
-
pre_task_processing(to_wrap, instance, args, span)
|
|
245
|
-
return_value = await wrapped(*args, **kwargs)
|
|
246
|
-
process_span(to_wrap, span, instance, args, kwargs, return_value)
|
|
247
|
-
post_task_processing(to_wrap, span, return_value)
|
|
248
|
-
|
|
249
|
-
return return_value
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
@with_tracer_wrapper
|
|
253
|
-
async def allm_wrapper(tracer, to_wrap, wrapped, instance, args, kwargs):
|
|
254
|
-
# Some Langchain objects are wrapped elsewhere, so we ignore them here
|
|
255
|
-
if instance.__class__.__name__ in ("AgentExecutor"):
|
|
256
|
-
return wrapped(*args, **kwargs)
|
|
257
|
-
|
|
258
|
-
if callable(to_wrap.get("span_name_getter")):
|
|
259
|
-
name = to_wrap.get("span_name_getter")(instance)
|
|
260
|
-
|
|
261
|
-
elif hasattr(instance, "name") and instance.name:
|
|
262
|
-
name = f"{to_wrap.get('span_name')}.{instance.name.lower()}"
|
|
263
|
-
elif to_wrap.get("span_name"):
|
|
264
|
-
name = to_wrap.get("span_name")
|
|
265
|
-
else:
|
|
266
|
-
name = get_fully_qualified_class_name(instance)
|
|
267
|
-
with tracer.start_as_current_span(name) as span:
|
|
268
|
-
provider_name, inference_endpoint = get_provider_name(instance)
|
|
269
|
-
return_value = await wrapped(*args, **kwargs)
|
|
270
|
-
kwargs.update({"provider_name": provider_name, "inference_endpoint": inference_endpoint or getattr(instance, 'endpoint', None)})
|
|
271
|
-
process_span(to_wrap, span, instance, args, kwargs, return_value)
|
|
272
|
-
update_span_from_llm_response(response=return_value, span=span, instance=instance)
|
|
273
|
-
|
|
274
|
-
return return_value
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
@with_tracer_wrapper
|
|
278
|
-
def llm_wrapper(tracer: Tracer, to_wrap, wrapped, instance, args, kwargs):
|
|
279
|
-
# Some Langchain objects are wrapped elsewhere, so we ignore them here
|
|
280
|
-
if instance.__class__.__name__ in ("AgentExecutor"):
|
|
281
|
-
return wrapped(*args, **kwargs)
|
|
282
|
-
|
|
283
|
-
if callable(to_wrap.get("span_name_getter")):
|
|
284
|
-
name = to_wrap.get("span_name_getter")(instance)
|
|
285
|
-
|
|
286
|
-
elif hasattr(instance, "name") and instance.name:
|
|
287
|
-
name = f"{to_wrap.get('span_name')}.{instance.name.lower()}"
|
|
288
|
-
elif to_wrap.get("span_name"):
|
|
289
|
-
name = to_wrap.get("span_name")
|
|
290
|
-
else:
|
|
291
|
-
name = get_fully_qualified_class_name(instance)
|
|
292
|
-
|
|
293
|
-
with tracer.start_as_current_span(name) as span:
|
|
294
|
-
provider_name, inference_endpoint = get_provider_name(instance)
|
|
295
|
-
return_value = wrapped(*args, **kwargs)
|
|
296
|
-
kwargs.update({"provider_name": provider_name, "inference_endpoint": inference_endpoint or getattr(instance, 'endpoint', None)})
|
|
297
|
-
process_span(to_wrap, span, instance, args, kwargs, return_value)
|
|
298
|
-
update_span_from_llm_response(response=return_value, span=span, instance=instance)
|
|
299
|
-
|
|
300
|
-
return return_value
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
def update_llm_endpoint(curr_span: Span, instance):
|
|
304
|
-
# Lambda to set attributes if values are not None
|
|
305
|
-
__set_span_attribute_if_not_none = lambda span, **kwargs: [
|
|
306
|
-
span.set_attribute(k, v) for k, v in kwargs.items() if v is not None
|
|
307
|
-
]
|
|
308
|
-
|
|
309
|
-
triton_llm_endpoint = os.environ.get("TRITON_LLM_ENDPOINT")
|
|
310
|
-
if triton_llm_endpoint is not None and len(triton_llm_endpoint) > 0:
|
|
311
|
-
curr_span.set_attribute("server_url", triton_llm_endpoint)
|
|
312
|
-
else:
|
|
313
|
-
# Get temperature if present
|
|
314
|
-
temp_val = instance.__dict__.get("temperature")
|
|
315
|
-
|
|
316
|
-
# Resolve values for model name, deployment, and inference endpoint
|
|
317
|
-
model_name = resolve_from_alias(instance.__dict__, ["model", "model_name"])
|
|
318
|
-
deployment_name = resolve_from_alias(instance.__dict__,
|
|
319
|
-
["engine", "azure_deployment", "deployment_name", "deployment_id",
|
|
320
|
-
"deployment"])
|
|
321
|
-
inference_ep = resolve_from_alias(instance.__dict__, ["azure_endpoint", "api_base"])
|
|
322
|
-
|
|
323
|
-
# Use the lambda to set attributes conditionally
|
|
324
|
-
__set_span_attribute_if_not_none(
|
|
325
|
-
curr_span,
|
|
326
|
-
temperature=temp_val,
|
|
327
|
-
model_name=model_name,
|
|
328
|
-
az_openai_deployment=deployment_name,
|
|
329
|
-
inference_endpoint=inference_ep
|
|
330
|
-
)
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
def get_provider_name(instance):
|
|
334
|
-
provider_url = ""
|
|
335
|
-
inference_endpoint = ""
|
|
336
|
-
parsed_provider_url = ""
|
|
337
|
-
try:
|
|
338
|
-
base_url = getattr(instance.client._client, "base_url", None)
|
|
339
|
-
if base_url:
|
|
340
|
-
if isinstance(getattr(base_url, "host", None), str):
|
|
341
|
-
provider_url = base_url.host
|
|
342
|
-
inference_endpoint = base_url if isinstance(base_url, str) else str(base_url)
|
|
343
|
-
except:
|
|
344
|
-
pass
|
|
345
|
-
|
|
346
|
-
try:
|
|
347
|
-
if isinstance(instance.client.meta.endpoint_url, str):
|
|
348
|
-
inference_endpoint = instance.client.meta.endpoint_url
|
|
349
|
-
except:
|
|
350
|
-
pass
|
|
351
|
-
|
|
352
|
-
api_base = getattr(instance, "api_base", None)
|
|
353
|
-
if isinstance(api_base, str):
|
|
354
|
-
provider_url = api_base
|
|
355
|
-
|
|
356
|
-
# Handle inference endpoint for Mistral AI (llamaindex)
|
|
357
|
-
sdk_config = getattr(instance, "_client", None)
|
|
358
|
-
if sdk_config and hasattr(sdk_config, "sdk_configuration"):
|
|
359
|
-
inference_endpoint = getattr(sdk_config.sdk_configuration, "server_url", inference_endpoint)
|
|
360
|
-
|
|
361
|
-
if provider_url:
|
|
362
|
-
try:
|
|
363
|
-
parsed_provider_url = urlparse(provider_url)
|
|
364
|
-
except:
|
|
365
|
-
pass
|
|
366
|
-
|
|
367
|
-
return parsed_provider_url.hostname if parsed_provider_url else provider_url, inference_endpoint
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
def set_provider_name(instance, instance_args: dict):
|
|
371
|
-
provider_url = ""
|
|
372
|
-
parsed_provider_url = ""
|
|
373
|
-
try:
|
|
374
|
-
if isinstance(instance.client._client.base_url.host, str):
|
|
375
|
-
provider_url = instance.client._client.base_url.host
|
|
376
|
-
except:
|
|
377
|
-
pass
|
|
378
|
-
|
|
379
|
-
try:
|
|
380
|
-
if isinstance(instance.api_base, str):
|
|
381
|
-
provider_url = instance.api_base
|
|
382
|
-
except:
|
|
383
|
-
pass
|
|
384
|
-
try:
|
|
385
|
-
if len(provider_url) > 0:
|
|
386
|
-
parsed_provider_url = urlparse(provider_url).hostname
|
|
387
|
-
except:
|
|
388
|
-
pass
|
|
389
|
-
if parsed_provider_url or provider_url:
|
|
390
|
-
instance_args[PROVIDER] = parsed_provider_url or provider_url
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
def is_root_span(curr_span: Span) -> bool:
|
|
394
|
-
return curr_span.parent is None
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
def get_input_from_args(chain_args):
|
|
398
|
-
if len(chain_args) > 0 and isinstance(chain_args[0], str):
|
|
399
|
-
return chain_args[0]
|
|
400
|
-
return ""
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
def update_span_from_llm_response(response, span: Span, instance):
|
|
404
|
-
if (response is not None and isinstance(response, dict) and "meta" in response) or (
|
|
405
|
-
response is not None and hasattr(response, "response_metadata")):
|
|
406
|
-
token_usage = None
|
|
407
|
-
if (response is not None and isinstance(response, dict) and "meta" in response): # haystack
|
|
408
|
-
token_usage = response["meta"][0]["usage"]
|
|
409
|
-
|
|
410
|
-
if (response is not None and hasattr(response, "response_metadata")):
|
|
411
|
-
if hasattr(response, "usage_metadata") and response.usage_metadata is not None:
|
|
412
|
-
token_usage = response.usage_metadata
|
|
413
|
-
else:
|
|
414
|
-
response_metadata = response.response_metadata
|
|
415
|
-
token_usage = response_metadata.get("token_usage")
|
|
416
|
-
|
|
417
|
-
meta_dict = {}
|
|
418
|
-
if token_usage is not None:
|
|
419
|
-
temperature = instance.__dict__.get("temperature", None)
|
|
420
|
-
meta_dict.update({"temperature": temperature})
|
|
421
|
-
meta_dict.update({"completion_tokens": token_usage.get("completion_tokens") or token_usage.get("output_tokens")})
|
|
422
|
-
meta_dict.update({"prompt_tokens": token_usage.get("prompt_tokens") or token_usage.get("input_tokens")})
|
|
423
|
-
meta_dict.update({"total_tokens": token_usage.get("total_tokens")})
|
|
424
|
-
span.add_event(META_DATA, meta_dict)
|
|
425
|
-
# extract token usage from llamaindex openai
|
|
426
|
-
if (response is not None and hasattr(response, "raw")):
|
|
427
|
-
try:
|
|
428
|
-
meta_dict = {}
|
|
429
|
-
if response.raw is not None:
|
|
430
|
-
token_usage = response.raw.get("usage") if isinstance(response.raw, dict) else getattr(response.raw,
|
|
431
|
-
"usage", None)
|
|
432
|
-
if token_usage is not None:
|
|
433
|
-
temperature = instance.__dict__.get("temperature", None)
|
|
434
|
-
meta_dict.update({"temperature": temperature})
|
|
435
|
-
if getattr(token_usage, "completion_tokens", None):
|
|
436
|
-
meta_dict.update({"completion_tokens": getattr(token_usage, "completion_tokens")})
|
|
437
|
-
if getattr(token_usage, "prompt_tokens", None):
|
|
438
|
-
meta_dict.update({"prompt_tokens": getattr(token_usage, "prompt_tokens")})
|
|
439
|
-
if getattr(token_usage, "total_tokens", None):
|
|
440
|
-
meta_dict.update({"total_tokens": getattr(token_usage, "total_tokens")})
|
|
441
|
-
span.add_event(META_DATA, meta_dict)
|
|
442
|
-
except AttributeError:
|
|
443
|
-
token_usage = None
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
def update_workflow_type(to_wrap, span: Span):
|
|
447
|
-
package_name = to_wrap.get('package')
|
|
448
|
-
|
|
449
|
-
for (package, workflow_type) in WORKFLOW_TYPE_MAP.items():
|
|
450
|
-
if (package_name is not None and package in package_name):
|
|
451
|
-
span.set_attribute(WORKFLOW_TYPE_KEY, workflow_type)
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
def update_span_with_context_input(to_wrap, wrapped_args, span: Span):
|
|
455
|
-
package_name: str = to_wrap.get('package')
|
|
456
|
-
input_arg_text = ""
|
|
457
|
-
if "langchain_core.retrievers" in package_name and len(wrapped_args) > 0:
|
|
458
|
-
input_arg_text += wrapped_args[0]
|
|
459
|
-
if "llama_index.core.indices.base_retriever" in package_name and len(wrapped_args) > 0:
|
|
460
|
-
input_arg_text += wrapped_args[0].query_str
|
|
461
|
-
if "haystack.components.retrievers.in_memory" in package_name:
|
|
462
|
-
input_arg_text += get_attribute(DATA_INPUT_KEY)
|
|
463
|
-
if input_arg_text:
|
|
464
|
-
span.add_event(DATA_INPUT_KEY, {QUERY: input_arg_text})
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
def update_span_with_context_output(to_wrap, return_value, span: Span):
|
|
468
|
-
package_name: str = to_wrap.get('package')
|
|
469
|
-
output_arg_text = ""
|
|
470
|
-
if "langchain_core.retrievers" in package_name:
|
|
471
|
-
output_arg_text += " ".join([doc.page_content for doc in return_value if hasattr(doc, 'page_content')])
|
|
472
|
-
if len(output_arg_text) > 100:
|
|
473
|
-
output_arg_text = output_arg_text[:100] + "..."
|
|
474
|
-
if "llama_index.core.indices.base_retriever" in package_name and len(return_value) > 0:
|
|
475
|
-
output_arg_text += return_value[0].text
|
|
476
|
-
if "haystack.components.retrievers.in_memory" in package_name:
|
|
477
|
-
output_arg_text += " ".join([doc.content for doc in return_value['documents']])
|
|
478
|
-
if len(output_arg_text) > 100:
|
|
479
|
-
output_arg_text = output_arg_text[:100] + "..."
|
|
480
|
-
if output_arg_text:
|
|
481
|
-
span.add_event(DATA_OUTPUT_KEY, {RESPONSE: output_arg_text})
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
def update_span_with_prompt_input(to_wrap, wrapped_args, span: Span):
|
|
485
|
-
input_arg_text = wrapped_args[0]
|
|
486
|
-
|
|
487
|
-
prompt_inputs = get_nested_value(input_arg_text, ['prompt_builder', 'question'])
|
|
488
|
-
if prompt_inputs is not None: # haystack
|
|
489
|
-
span.add_event(PROMPT_INPUT_KEY, {QUERY: prompt_inputs})
|
|
490
|
-
elif isinstance(input_arg_text, dict):
|
|
491
|
-
span.add_event(PROMPT_INPUT_KEY, {QUERY: input_arg_text['input']})
|
|
492
|
-
else:
|
|
493
|
-
span.add_event(PROMPT_INPUT_KEY, {QUERY: input_arg_text})
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
def update_span_with_prompt_output(to_wrap, wrapped_args, span: Span):
|
|
497
|
-
package_name: str = to_wrap.get('package')
|
|
498
|
-
|
|
499
|
-
if "llama_index.core.base.base_query_engine" in package_name:
|
|
500
|
-
span.add_event(PROMPT_OUTPUT_KEY, {RESPONSE: wrapped_args.response})
|
|
501
|
-
elif "haystack.core.pipeline.pipeline" in package_name:
|
|
502
|
-
resp = get_nested_value(wrapped_args, ['llm', 'replies'])
|
|
503
|
-
if resp is not None:
|
|
504
|
-
if isinstance(resp, list) and hasattr(resp[0], 'content'):
|
|
505
|
-
span.add_event(PROMPT_OUTPUT_KEY, {RESPONSE: resp[0].content})
|
|
506
|
-
else:
|
|
507
|
-
span.add_event(PROMPT_OUTPUT_KEY, {RESPONSE: resp[0]})
|
|
508
|
-
elif isinstance(wrapped_args, str):
|
|
509
|
-
span.add_event(PROMPT_OUTPUT_KEY, {RESPONSE: wrapped_args})
|
|
510
|
-
elif isinstance(wrapped_args, dict):
|
|
511
|
-
span.add_event(PROMPT_OUTPUT_KEY, wrapped_args)
|
monocle_apptrace/wrapper.py
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
from monocle_apptrace.haystack import HAYSTACK_METHODS
|
|
3
|
-
from monocle_apptrace.langchain import LANGCHAIN_METHODS
|
|
4
|
-
from monocle_apptrace.llamaindex import LLAMAINDEX_METHODS
|
|
5
|
-
from monocle_apptrace.botocore import BOTOCORE_METHODS
|
|
6
|
-
from monocle_apptrace.wrap_common import task_wrapper
|
|
7
|
-
|
|
8
|
-
# pylint: disable=too-few-public-methods
|
|
9
|
-
class WrapperMethod:
|
|
10
|
-
def __init__(
|
|
11
|
-
self,
|
|
12
|
-
package: str,
|
|
13
|
-
object_name: str,
|
|
14
|
-
method: str,
|
|
15
|
-
span_name: str = None,
|
|
16
|
-
output_processor : list[str] = None,
|
|
17
|
-
wrapper = task_wrapper
|
|
18
|
-
):
|
|
19
|
-
self.package = package
|
|
20
|
-
self.object = object_name
|
|
21
|
-
self.method = method
|
|
22
|
-
self.span_name = span_name
|
|
23
|
-
self.output_processor=output_processor
|
|
24
|
-
|
|
25
|
-
self.wrapper = wrapper
|
|
26
|
-
|
|
27
|
-
INBUILT_METHODS_LIST = LANGCHAIN_METHODS + LLAMAINDEX_METHODS + HAYSTACK_METHODS + BOTOCORE_METHODS
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
monocle_apptrace/README.md,sha256=T5NFC01bF8VR0oVnAX_n0bhsEtttwqfTxDNAe5Y_ivE,3765
|
|
2
|
-
monocle_apptrace/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
-
monocle_apptrace/constants.py,sha256=Q1rREmswflNn_ZeNhDGCOCpHXY6msaOqesS_LoXXBdg,1633
|
|
4
|
-
monocle_apptrace/instrumentor.py,sha256=bTgN3pWz5cGyOBBEbiNt8O4W5QJAprUCk7JhvLdd_C8,5346
|
|
5
|
-
monocle_apptrace/message_processing.py,sha256=NEg_F1epAAGNtwN_ht6TCKPnTMgx1da95-bamm-dZeE,3025
|
|
6
|
-
monocle_apptrace/utils.py,sha256=Png2ucLNww_0_UFQxzdPs5xaEMqyezhIbjpbb5gy-GI,9764
|
|
7
|
-
monocle_apptrace/wrap_common.py,sha256=DTb1SsqKNYWDodsbISJOy6WagRJxcN4h1F5KS3rXnW8,22465
|
|
8
|
-
monocle_apptrace/wrapper.py,sha256=GVgImJasbHfrNxT8FLoMeZmGB_JRp09RjSs6Nn7OouE,904
|
|
9
|
-
monocle_apptrace/botocore/__init__.py,sha256=SkRoVm6zFUHFG8JVFKhBXWsMHk2yOLpAxgq3mD-euY4,398
|
|
10
|
-
monocle_apptrace/exporters/base_exporter.py,sha256=Gov_QKp5fonVZ-YdNM2ynoPot7GCaSNmKbCHIP3bDlE,1680
|
|
11
|
-
monocle_apptrace/exporters/exporter_processor.py,sha256=BTcBgMuFLHCdCgVvc9TKIo9y8g1BvShI0L4vX6Q-cmk,393
|
|
12
|
-
monocle_apptrace/exporters/file_exporter.py,sha256=gN9pJ_X5pcstVVsyivgHsjWhr443eRa6Y6Hx1rGLQAM,2280
|
|
13
|
-
monocle_apptrace/exporters/monocle_exporters.py,sha256=mvVoGr4itvUBuakH17zNEuXYpHtqRhq2CBH5JXhleoM,1418
|
|
14
|
-
monocle_apptrace/exporters/aws/s3_exporter.py,sha256=1WhZ_xCk1_c1vD8u2d_6z2QIedM89A-wsHGHLQsnGtY,6618
|
|
15
|
-
monocle_apptrace/exporters/azure/blob_exporter.py,sha256=m7Hsw3OXlP2GOCQcdxf8LM6Fe12fZmih45x82Z12dbI,5597
|
|
16
|
-
monocle_apptrace/exporters/okahu/okahu_exporter.py,sha256=p2rjStwo0OMEdHWQt_QvREpUWXbDm5jGx3qXeYai4_M,4407
|
|
17
|
-
monocle_apptrace/haystack/__init__.py,sha256=zMvF6Dh1-vVIAQD__HC1ubT5bs-EupUghg7HNhDv7_c,448
|
|
18
|
-
monocle_apptrace/haystack/wrap_pipeline.py,sha256=yZAw7Hdv7FXe6rrM7gA2y5SjaZYQZCAi0q-R-uqUEvk,2254
|
|
19
|
-
monocle_apptrace/langchain/__init__.py,sha256=3yhbdw1h9I1nVEfnOOPKz9yD5NqwdLSZsxtXbMplRkw,400
|
|
20
|
-
monocle_apptrace/llamaindex/__init__.py,sha256=TgV1ZM8Cz113pZ2aAgpYwsCJyHA2aHOvNoW1QMBp0mM,709
|
|
21
|
-
monocle_apptrace/metamodel/README.md,sha256=KYuuYqgA9PNbOjG0zYj2nAdvNEpyNN_Bk9M2tNdnZ_s,4598
|
|
22
|
-
monocle_apptrace/metamodel/entities/README.md,sha256=dY7Q8QT_Ju-2ul65J-k0x6beDLvRirlbGufZN1Q0tpk,2068
|
|
23
|
-
monocle_apptrace/metamodel/entities/app_hosting_types.json,sha256=MWeHUe77n4HvO1hm2PX8iWjbRONBuNy2I84vd-lymYk,568
|
|
24
|
-
monocle_apptrace/metamodel/entities/entities.json,sha256=6eDoDm_6aPUgH_ROjI2H9Tk3J5Lj55VkX3Li-TJmaxg,1285
|
|
25
|
-
monocle_apptrace/metamodel/entities/inference_types.json,sha256=Gkq7qNw5Qn91tuq-rBxHJ4BSzT-etCshNjT5_G4Bg8I,651
|
|
26
|
-
monocle_apptrace/metamodel/entities/model_types.json,sha256=Oz9DzuX_ptpLEVTc1Epv4_Y80TrYxR7cyc9kM0Zk780,785
|
|
27
|
-
monocle_apptrace/metamodel/entities/vector_store_types.json,sha256=EA0KayHMOG7wMIkec54en03_3yT1qpdGh5TiDrAoh4g,462
|
|
28
|
-
monocle_apptrace/metamodel/entities/workflow_types.json,sha256=eD0W3_FocKrzrrW0bks0hIJb9Kj7w8c1dpXzxmGLOwk,386
|
|
29
|
-
monocle_apptrace/metamodel/maps/botocore_methods.json,sha256=FvobptTS-whE5fI14-3QGqGErLijmUshIvzJWF1-6fE,338
|
|
30
|
-
monocle_apptrace/metamodel/maps/haystack_methods.json,sha256=yWOs2LHgrDpwhGC2aVJqNOLt0kPwlzZdzMb2yzHP1QI,1611
|
|
31
|
-
monocle_apptrace/metamodel/maps/langchain_methods.json,sha256=lfU8nd6td5qzTI01glM063Vg0UoZM9HGPirwN4OZcKc,4285
|
|
32
|
-
monocle_apptrace/metamodel/maps/llamaindex_methods.json,sha256=dqfDQ4H8qB-GvctBzrgH0ldjdarbTRV0ID5a4LerN5I,3328
|
|
33
|
-
monocle_apptrace/metamodel/maps/attributes/inference/botocore_entities.json,sha256=GCFQPlTTlE64dFc9DPM1sf3Uk1x6g0UdsrABlyFwl6k,936
|
|
34
|
-
monocle_apptrace/metamodel/maps/attributes/inference/haystack_entities.json,sha256=e-0SOcP6b4A1hRYtDBQT3Mx94J73xqQNqdvqWyJSFmo,1756
|
|
35
|
-
monocle_apptrace/metamodel/maps/attributes/inference/langchain_entities.json,sha256=QZJkP4JZ0VmWWtegCxFXASO8bJSYtJTz2ObpWzSyF7o,1826
|
|
36
|
-
monocle_apptrace/metamodel/maps/attributes/inference/llamaindex_entities.json,sha256=-0ARREkmWRQLbFAufiBIAbPhunuUYVBTUoP5lNJrk90,1755
|
|
37
|
-
monocle_apptrace/metamodel/maps/attributes/retrieval/haystack_entities.json,sha256=_SNwWIoO4anww-UDrTFIvDeFaHJKmg3R8IzZLbRGBD8,1035
|
|
38
|
-
monocle_apptrace/metamodel/maps/attributes/retrieval/langchain_entities.json,sha256=-Dj7U0of2TmPjhYZ2SAMztQBHeeN81H-_4rTCfoAzyQ,916
|
|
39
|
-
monocle_apptrace/metamodel/maps/attributes/retrieval/llamaindex_entities.json,sha256=opfmuUWizZvfTD7FVxG21OrpHXc8HcL32En9ftC3eRA,903
|
|
40
|
-
monocle_apptrace/metamodel/spans/README.md,sha256=_uMkLLaWitQ_rPh7oQbW5Oe7uGSv2h_QA6YwxHRJi74,5433
|
|
41
|
-
monocle_apptrace/metamodel/spans/span_example.json,sha256=7ZAUssL1UYXlbKHre8PfeNlIhHnM28e2CH9EyHDTOt8,4402
|
|
42
|
-
monocle_apptrace/metamodel/spans/span_format.json,sha256=GhfioGgMhG7St0DeYA1fgNtMkbr9wiQ1L2hovekRQ24,1512
|
|
43
|
-
monocle_apptrace/metamodel/spans/span_types.json,sha256=jwVyPbYMhEf2Ea6Egmb3m1Za28ap9dgZIpJDhdE1BlY,361
|
|
44
|
-
monocle_apptrace-0.3.0b1.dist-info/METADATA,sha256=PSSgPDB9-1HHTIw9WpIzuN3s2_B_a_fNgrD5EtH5UQs,5503
|
|
45
|
-
monocle_apptrace-0.3.0b1.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
|
|
46
|
-
monocle_apptrace-0.3.0b1.dist-info/licenses/LICENSE,sha256=ay9trLiP5I7ZsFXo6AqtkLYdRqe5S9r-DrPOvsNlZrg,9136
|
|
47
|
-
monocle_apptrace-0.3.0b1.dist-info/licenses/NOTICE,sha256=9jn4xtwM_uUetJMx5WqGnhrR7MIhpoRlpokjSTlyt8c,112
|
|
48
|
-
monocle_apptrace-0.3.0b1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|