monocle-apptrace 0.5.3__py3-none-any.whl → 0.6.6__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.

Files changed (56) hide show
  1. monocle_apptrace/exporters/file_exporter.py +7 -1
  2. monocle_apptrace/instrumentation/common/constants.py +8 -0
  3. monocle_apptrace/instrumentation/common/instrumentor.py +1 -1
  4. monocle_apptrace/instrumentation/common/span_handler.py +75 -24
  5. monocle_apptrace/instrumentation/common/utils.py +63 -6
  6. monocle_apptrace/instrumentation/common/wrapper.py +111 -42
  7. monocle_apptrace/instrumentation/common/wrapper_method.py +6 -2
  8. monocle_apptrace/instrumentation/metamodel/a2a/methods.py +1 -1
  9. monocle_apptrace/instrumentation/metamodel/adk/_helper.py +7 -4
  10. monocle_apptrace/instrumentation/metamodel/adk/entities/agent.py +6 -1
  11. monocle_apptrace/instrumentation/metamodel/agents/_helper.py +8 -8
  12. monocle_apptrace/instrumentation/metamodel/agents/entities/inference.py +9 -2
  13. monocle_apptrace/instrumentation/metamodel/aiohttp/_helper.py +1 -1
  14. monocle_apptrace/instrumentation/metamodel/anthropic/entities/inference.py +1 -4
  15. monocle_apptrace/instrumentation/metamodel/azfunc/_helper.py +1 -1
  16. monocle_apptrace/instrumentation/metamodel/botocore/_helper.py +5 -0
  17. monocle_apptrace/instrumentation/metamodel/botocore/entities/inference.py +4 -0
  18. monocle_apptrace/instrumentation/metamodel/fastapi/_helper.py +4 -4
  19. monocle_apptrace/instrumentation/metamodel/fastapi/methods.py +4 -4
  20. monocle_apptrace/instrumentation/metamodel/finish_types.py +32 -1
  21. monocle_apptrace/instrumentation/metamodel/flask/_helper.py +3 -3
  22. monocle_apptrace/instrumentation/metamodel/hugging_face/__init__.py +0 -0
  23. monocle_apptrace/instrumentation/metamodel/hugging_face/_helper.py +138 -0
  24. monocle_apptrace/instrumentation/metamodel/hugging_face/entities/__init__.py +0 -0
  25. monocle_apptrace/instrumentation/metamodel/hugging_face/entities/inference.py +94 -0
  26. monocle_apptrace/instrumentation/metamodel/hugging_face/methods.py +23 -0
  27. monocle_apptrace/instrumentation/metamodel/lambdafunc/_helper.py +1 -1
  28. monocle_apptrace/instrumentation/metamodel/langchain/entities/inference.py +1 -4
  29. monocle_apptrace/instrumentation/metamodel/langgraph/_helper.py +34 -8
  30. monocle_apptrace/instrumentation/metamodel/langgraph/entities/inference.py +8 -3
  31. monocle_apptrace/instrumentation/metamodel/langgraph/langgraph_processor.py +88 -19
  32. monocle_apptrace/instrumentation/metamodel/langgraph/methods.py +22 -6
  33. monocle_apptrace/instrumentation/metamodel/llamaindex/_helper.py +30 -10
  34. monocle_apptrace/instrumentation/metamodel/llamaindex/entities/agent.py +4 -3
  35. monocle_apptrace/instrumentation/metamodel/llamaindex/llamaindex_processor.py +15 -7
  36. monocle_apptrace/instrumentation/metamodel/llamaindex/methods.py +1 -8
  37. monocle_apptrace/instrumentation/metamodel/mcp/_helper.py +7 -6
  38. monocle_apptrace/instrumentation/metamodel/mistral/_helper.py +98 -49
  39. monocle_apptrace/instrumentation/metamodel/mistral/entities/inference.py +15 -9
  40. monocle_apptrace/instrumentation/metamodel/mistral/entities/retrieval.py +41 -0
  41. monocle_apptrace/instrumentation/metamodel/mistral/methods.py +10 -1
  42. monocle_apptrace/instrumentation/metamodel/openai/_helper.py +47 -7
  43. monocle_apptrace/instrumentation/metamodel/openai/entities/inference.py +20 -4
  44. monocle_apptrace/instrumentation/metamodel/openai/methods.py +1 -1
  45. monocle_apptrace/instrumentation/metamodel/strands/_helper.py +44 -0
  46. monocle_apptrace/instrumentation/metamodel/strands/entities/agent.py +179 -0
  47. monocle_apptrace/instrumentation/metamodel/strands/entities/tool.py +62 -0
  48. monocle_apptrace/instrumentation/metamodel/strands/methods.py +20 -0
  49. {monocle_apptrace-0.5.3.dist-info → monocle_apptrace-0.6.6.dist-info}/METADATA +23 -79
  50. {monocle_apptrace-0.5.3.dist-info → monocle_apptrace-0.6.6.dist-info}/RECORD +53 -46
  51. monocle_apptrace/README.md +0 -101
  52. monocle_apptrace/mcp_server.py +0 -94
  53. monocle_apptrace-0.5.3.dist-info/licenses/NOTICE +0 -4
  54. {monocle_apptrace-0.5.3.dist-info → monocle_apptrace-0.6.6.dist-info}/WHEEL +0 -0
  55. {monocle_apptrace-0.5.3.dist-info → monocle_apptrace-0.6.6.dist-info}/entry_points.txt +0 -0
  56. {monocle_apptrace-0.5.3.dist-info → monocle_apptrace-0.6.6.dist-info}/licenses/LICENSE +0 -0
@@ -55,6 +55,9 @@ class FileSpanExporter(SpanExporterBase):
55
55
  else:
56
56
  return self._process_spans(spans, is_root_span=is_root_span)
57
57
 
58
+ def set_service_name(self, service_name: str) -> None:
59
+ self.service_name = service_name
60
+
58
61
  def _cleanup_expired_handles(self) -> None:
59
62
  """Close and remove file handles that have exceeded the timeout."""
60
63
  current_time = datetime.now()
@@ -130,7 +133,10 @@ class FileSpanExporter(SpanExporterBase):
130
133
 
131
134
  # Process spans for each trace
132
135
  for trace_id, trace_spans in spans_by_trace.items():
133
- service_name = trace_spans[0].resource.attributes.get(SERVICE_NAME, "unknown")
136
+ if self.service_name is not None:
137
+ service_name = self.service_name
138
+ else:
139
+ service_name = trace_spans[0].resource.attributes.get(SERVICE_NAME, "unknown")
134
140
  handle, file_path, is_first_span = self._get_or_create_handle(trace_id, service_name)
135
141
 
136
142
  if handle is None:
@@ -92,6 +92,9 @@ CHILD_ERROR_CODE = "child.error.code"
92
92
 
93
93
  AGENT_PREFIX_KEY = "monocle.agent.prefix"
94
94
 
95
+ MONOCLE_SKIP_EXECUTIONS = "monocle.skip_executions"
96
+ SKIPPED_EXECUTION = "monocle.skipped_execution"
97
+
95
98
  # agentic sub types
96
99
  INFERENCE_AGENT_DELEGATION = "delegation"
97
100
  INFERENCE_TOOL_CALL = "tool_call"
@@ -178,3 +181,8 @@ MAP_ATTRIBUTES_TO_SPAN_SUBTYPE = {
178
181
 
179
182
 
180
183
  }
184
+
185
+ PROVIDER_BASE_URLS = {
186
+ "deepseek": "https://api.deepseek.com",
187
+ # Add more providers here later
188
+ }
@@ -165,7 +165,7 @@ def setup_monocle_telemetry(
165
165
  span_handlers: Dict[str,SpanHandler] = None,
166
166
  wrapper_methods: List[Union[dict,WrapperMethod]] = None,
167
167
  union_with_default_methods: bool = True,
168
- monocle_exporters_list:str = None) -> None:
168
+ monocle_exporters_list:str = None) -> MonocleInstrumentor:
169
169
  """
170
170
  Set up Monocle telemetry for the application.
171
171
 
@@ -1,6 +1,7 @@
1
1
  import logging
2
2
  import os
3
3
  from contextlib import contextmanager
4
+ from typing import Union
4
5
  from opentelemetry.context import get_value, set_value, attach, detach
5
6
  from opentelemetry.sdk.trace import Span
6
7
  from opentelemetry.trace.status import Status, StatusCode
@@ -10,8 +11,8 @@ from monocle_apptrace.instrumentation.common.constants import (
10
11
  service_type_map,
11
12
  MONOCLE_SDK_VERSION, MONOCLE_SDK_LANGUAGE, MONOCLE_DETECTED_SPAN_ERROR
12
13
  )
13
- from monocle_apptrace.instrumentation.common.utils import set_attribute, get_scopes, MonocleSpanException, get_monocle_version
14
- from monocle_apptrace.instrumentation.common.constants import WORKFLOW_TYPE_KEY, WORKFLOW_TYPE_GENERIC, CHILD_ERROR_CODE
14
+ from monocle_apptrace.instrumentation.common.utils import set_attribute, get_scopes, MonocleSpanException, get_monocle_version, replace_placeholders
15
+ from monocle_apptrace.instrumentation.common.constants import WORKFLOW_TYPE_KEY, WORKFLOW_TYPE_GENERIC, CHILD_ERROR_CODE, MONOCLE_SKIP_EXECUTIONS, SKIPPED_EXECUTION
15
16
 
16
17
  logger = logging.getLogger(__name__)
17
18
 
@@ -26,7 +27,8 @@ WORKFLOW_TYPE_MAP = {
26
27
  "anthropic": "workflow.anthropic",
27
28
  "gemini": "workflow.gemini",
28
29
  "litellm": "workflow.litellm",
29
- "mistralai": "workflow.mistral"
30
+ "mistralai": "workflow.mistral",
31
+ "huggingface_hub": "workflow.huggingface"
30
32
  }
31
33
 
32
34
  FRAMEWORK_WORKFLOW_LIST = [
@@ -48,7 +50,7 @@ class SpanHandler:
48
50
  pass
49
51
 
50
52
  def pre_tracing(self, to_wrap, wrapped, instance, args, kwargs):
51
- pass
53
+ return None, None
52
54
 
53
55
  def post_tracing(self, to_wrap, wrapped, instance, args, kwargs, return_value, token=None):
54
56
  pass
@@ -66,8 +68,6 @@ class SpanHandler:
66
68
  span.set_attribute("span.type", span_type)
67
69
  else:
68
70
  logger.warning("type of span not found or incorrect written in entity json")
69
- if "subtype" in output_processor:
70
- span.set_attribute("span.subtype", output_processor["subtype"])
71
71
  return span_type
72
72
 
73
73
  def pre_task_processing(self, to_wrap, wrapped, instance, args,kwargs, span):
@@ -102,28 +102,32 @@ class SpanHandler:
102
102
  def post_task_processing(self, to_wrap, wrapped, instance, args, kwargs, result, ex, span:Span, parent_span:Span):
103
103
  pass
104
104
 
105
- def should_skip(self, processor, instance, args, kwargs) -> bool:
105
+ def should_skip(self, processor, instance, span, parent_span, args, kwargs) -> bool:
106
106
  should_skip = False
107
107
  accessor = processor.get('should_skip')
108
108
  if accessor:
109
- arguments = {"instance":instance, "args":args, "kwargs":kwargs}
110
- should_skip = accessor(arguments)
111
- if not isinstance(should_skip, bool):
112
- logger.warning("Warning: 'should_skip' accessor did not return a boolean value")
113
- return True
109
+ arguments = {"instance":instance, "span":span, "parent_span":parent_span, "args":args, "kwargs":kwargs}
110
+ try:
111
+ should_skip = accessor(arguments)
112
+ if not isinstance(should_skip, bool):
113
+ logger.warning("Warning: 'should_skip' accessor did not return a boolean value")
114
+ return False
115
+ except Exception as e:
116
+ logger.warning("Warning: Error occurred in 'should_skip' accessor: %s", str(e))
114
117
  return should_skip
115
118
 
116
- def hydrate_span(self, to_wrap, wrapped, instance, args, kwargs, result, span, parent_span = None, ex:Exception = None) -> bool:
119
+ def hydrate_span(self, to_wrap, wrapped, instance, args, kwargs, result, span, parent_span = None,
120
+ ex:Exception = None, is_post_exec:bool= False) -> bool:
117
121
  try:
118
- detected_error_in_attribute = self.hydrate_attributes(to_wrap, wrapped, instance, args, kwargs, result, span, parent_span)
119
- detected_error_in_event = self.hydrate_events(to_wrap, wrapped, instance, args, kwargs, result, span, parent_span, ex)
122
+ detected_error_in_attribute = self.hydrate_attributes(to_wrap, wrapped, instance, args, kwargs, result, span, parent_span, is_post_exec)
123
+ detected_error_in_event = self.hydrate_events(to_wrap, wrapped, instance, args, kwargs, result, span, parent_span, ex, is_post_exec)
120
124
  if detected_error_in_attribute or detected_error_in_event:
121
125
  span.set_attribute(MONOCLE_DETECTED_SPAN_ERROR, True)
122
126
  finally:
123
- if span.status.status_code == StatusCode.UNSET and ex is None:
127
+ if is_post_exec and span.status.status_code == StatusCode.UNSET and ex is None:
124
128
  span.set_status(StatusCode.OK)
125
129
 
126
- def hydrate_attributes(self, to_wrap, wrapped, instance, args, kwargs, result, span:Span, parent_span:Span) -> bool:
130
+ def hydrate_attributes(self, to_wrap, wrapped, instance, args, kwargs, result, span:Span, parent_span:Span, is_post_exec:bool) -> bool:
127
131
  detected_error:bool = False
128
132
  span_index = 0
129
133
  if SpanHandler.is_root_span(span):
@@ -132,7 +136,6 @@ class SpanHandler:
132
136
  output_processor=to_wrap['output_processor']
133
137
  self.set_span_type(to_wrap, wrapped, instance, output_processor, span, args, kwargs)
134
138
  skip_processors:list[str] = self.skip_processor(to_wrap, wrapped, instance, span, args, kwargs) or []
135
-
136
139
  if 'attributes' in output_processor and 'attributes' not in skip_processors:
137
140
  arguments = {"instance":instance, "args":args, "kwargs":kwargs, "result":result, "parent_span":parent_span, "span":span}
138
141
  for processors in output_processor["attributes"]:
@@ -143,9 +146,10 @@ class SpanHandler:
143
146
  if attribute and accessor:
144
147
  attribute_name = f"entity.{span_index+1}.{attribute}"
145
148
  try:
146
- processor_result = accessor(arguments)
147
- if processor_result and isinstance(processor_result, (str, list)):
148
- span.set_attribute(attribute_name, processor_result)
149
+ if (not is_post_exec and processor.get('phase', '') != 'post_execution') or (is_post_exec and processor.get('phase', '') == 'post_execution'):
150
+ processor_result = accessor(arguments)
151
+ if processor_result and isinstance(processor_result, (str, list)):
152
+ span.set_attribute(attribute_name, processor_result)
149
153
  except MonocleSpanException as e:
150
154
  span.set_status(StatusCode.ERROR, e.message)
151
155
  detected_error = True
@@ -164,13 +168,27 @@ class SpanHandler:
164
168
  span.set_attribute("entity.count", span_index)
165
169
  return detected_error
166
170
 
167
- def hydrate_events(self, to_wrap, wrapped, instance, args, kwargs, ret_result, span: Span, parent_span=None, ex:Exception=None) -> bool:
171
+ def hydrate_events(self, to_wrap, wrapped, instance, args, kwargs, ret_result, span: Span, parent_span=None, ex:Exception=None,
172
+ is_post_exec: bool = False) -> bool:
168
173
  detected_error:bool = False
169
174
  if 'output_processor' in to_wrap and to_wrap["output_processor"] is not None:
170
175
  output_processor=to_wrap['output_processor']
171
- skip_processors:list[str] = self.skip_processor(to_wrap, wrapped, instance, span, args, kwargs) or []
172
-
176
+ if is_post_exec:
177
+ skip_events:list[str] = ['events.data.input']
178
+ else:
179
+ skip_events:list[str] = ['events.data.output', 'events.metadata']
180
+ skip_processors:list[str] = list(set(self.skip_processor(to_wrap, wrapped, instance, span, args, kwargs) or []).union(set(skip_events)))
173
181
  arguments = {"instance": instance, "args": args, "kwargs": kwargs, "result": ret_result, "exception":ex, "parent_span":parent_span, "span": span}
182
+ subtype = output_processor.get('subtype')
183
+ if subtype:
184
+ if callable(subtype):
185
+ try:
186
+ subtype_result = subtype(arguments)
187
+ span.set_attribute("span.subtype", subtype_result)
188
+ except Exception as e:
189
+ logger.debug(f"Error processing subtype: {e}")
190
+ else:
191
+ span.set_attribute("span.subtype", subtype)
174
192
  # Process events if they are defined in the output_processor.
175
193
  # In case of inference.modelapi skip the event processing unless the span has an exception
176
194
  if 'events' in output_processor and ('events' not in skip_processors or ex is not None):
@@ -199,6 +217,13 @@ class SpanHandler:
199
217
  event_attributes[attribute_key] = result
200
218
  else:
201
219
  event_attributes.update(result)
220
+ if not is_post_exec and event_name == "data.input" and attribute_key == "input" and result:
221
+ # append memory to input if available
222
+ # update the result with new input
223
+ pass
224
+ elif not detected_error and is_post_exec and event_name == "data.output" and attribute_key == "response" and result:
225
+ # capture memory
226
+ pass
202
227
  except Exception as e:
203
228
  logger.debug(f"Error evaluating accessor for attribute '{attribute_key}': {e}")
204
229
  matching_timestamp = getattr(ret_result, "timestamps", {}).get(event_name, None)
@@ -298,6 +323,32 @@ class SpanHandler:
298
323
  finally:
299
324
  SpanHandler.detach_workflow_type(token)
300
325
 
326
+ @staticmethod
327
+ def get_iput_entity_type(span: Span) -> str:
328
+ for event in span.events:
329
+ if event.name == "data.input":
330
+ return event.attributes.get("entity.type", "")
331
+
332
+ @staticmethod
333
+ def skip_execution(span:Span) -> tuple[bool, Union[dict, list, str, None]]:
334
+ skip_execs = get_value(MONOCLE_SKIP_EXECUTIONS)
335
+ if skip_execs is not None:
336
+ skip_exec_entity = skip_execs.get(span.attributes.get("entity.1.name", ""),{})
337
+ if ((span.attributes.get("span.type") is not None and skip_exec_entity.get("span.type", "") == span.attributes.get("span.type")) and
338
+ (span.attributes.get("entity.1.type") is not None and skip_exec_entity.get("entity.type", "") == span.attributes.get("entity.1.type"))):
339
+ span.set_attribute(SKIPPED_EXECUTION, True)
340
+ if skip_exec_entity.get("raise_error", False):
341
+ raise MonocleSpanException(skip_exec_entity.get("error_message", ""))
342
+ response = skip_exec_entity.get("response", None)
343
+ response = replace_placeholders(response, span)
344
+ return True, response
345
+ return False, None
346
+
347
+ @staticmethod
348
+ def replace_placeholders_in_response(response: Union[dict, list, str], span:Span) -> Union[dict, list, str]:
349
+ if span.attributes.get(SKIPPED_EXECUTION, False):
350
+ return replace_placeholders(response, span)
351
+ return response
301
352
 
302
353
  class NonFrameworkSpanHandler(SpanHandler):
303
354
  # If the language framework is being executed, then skip generating direct openAI attributes and events
@@ -1,7 +1,8 @@
1
+ import ast
1
2
  import logging, json
2
3
  import os
3
4
  import traceback
4
- from typing import Callable, Generic, Optional, TypeVar, Mapping
5
+ from typing import Callable, Generic, Optional, TypeVar, Mapping, Union
5
6
 
6
7
  from opentelemetry.context import attach, detach, get_current, get_value, set_value, Context
7
8
  from opentelemetry.trace import NonRecordingSpan, Span
@@ -217,13 +218,16 @@ def remove_scopes(token:object) -> None:
217
218
  if token is not None:
218
219
  detach(token)
219
220
 
220
- def get_scopes() -> dict[str, object]:
221
+ def get_scopes(scope_name: Optional[str] = None) -> dict[str, object]:
221
222
  monocle_scopes:dict[str, object] = {}
222
223
  for key, val in baggage.get_all().items():
223
- if key.startswith(MONOCLE_SCOPE_NAME_PREFIX):
224
+ if key.startswith(MONOCLE_SCOPE_NAME_PREFIX) and (scope_name is None or key == f"{MONOCLE_SCOPE_NAME_PREFIX}{scope_name}"):
224
225
  monocle_scopes[key[len(MONOCLE_SCOPE_NAME_PREFIX):]] = val
225
226
  return monocle_scopes
226
227
 
228
+ def is_scope_set(scepe_name: str) -> bool:
229
+ return len(get_scopes(scepe_name)) > 0
230
+
227
231
  def get_baggage_for_scopes():
228
232
  baggage_context:Context = None
229
233
  for scope_key, scope_value in get_scopes():
@@ -357,7 +361,7 @@ class Option(Generic[T]):
357
361
 
358
362
  def and_then(self, func: Callable[[T], 'Option[U]']) -> 'Option[U]':
359
363
  if self.is_some():
360
- return func(self.value)
364
+ return Option(func(self.value))
361
365
  return Option(None)
362
366
 
363
367
  # Example usage
@@ -409,7 +413,7 @@ def get_error_message(arguments):
409
413
 
410
414
 
411
415
  def get_status_code(arguments):
412
- if arguments["exception"] is not None:
416
+ if "exception" in arguments and arguments["exception"] is not None:
413
417
  return get_exception_status_code(arguments)
414
418
  elif hasattr(arguments["result"], "status"):
415
419
  return arguments["result"].status
@@ -467,4 +471,57 @@ def get_current_monocle_span(context: Optional[Context] = None) -> Span:
467
471
  span = get_value(_MONOCLE_SPAN_KEY, context=context)
468
472
  if span is None or not isinstance(span, Span):
469
473
  return INVALID_SPAN
470
- return span
474
+ return span
475
+
476
+ def get_input_event_from_span(events: list[dict], search_key:str) -> Optional[Mapping]:
477
+ """Extract the 'data.input' event from the span if it exists.
478
+
479
+ Args:
480
+ span: The Span to extract the event from.
481
+
482
+ Returns:
483
+ The 'data.input' event if it exists, None otherwise.
484
+ """
485
+ input_request = None
486
+ for event in events:
487
+ if event.name == "data.input":
488
+ try:
489
+ #load the input attribute as dictionary from string
490
+ try:
491
+ input_dict = json.loads(event.attributes.get("input", "{}"))
492
+ except Exception as e:
493
+ if isinstance(e, json.JSONDecodeError):
494
+ input_dict = ast.literal_eval(event.attributes.get("input", {}))
495
+ else:
496
+ raise
497
+ if search_key in input_dict:
498
+ input_request = input_dict[search_key]
499
+ except Exception as e:
500
+ logger.debug(f"Error parsing input event attribute: {e}")
501
+ break
502
+ return input_request
503
+
504
+ def replace_placeholders(obj: Union[dict, list, str], span: Span) -> Union[dict, list, str]:
505
+ """Replace placeholders in strings with span context values."""
506
+ if isinstance(obj, dict):
507
+ return {k: replace_placeholders(v, span) for k, v in obj.items()}
508
+ elif isinstance(obj, list):
509
+ return [replace_placeholders(item, span) for item in obj]
510
+ elif isinstance(obj, str):
511
+ startIndex = 0
512
+ while True:
513
+ start = obj.find("{{", startIndex)
514
+ end = obj.find("}}", start + 2)
515
+ if start == -1 or end == -1:
516
+ break
517
+ key = obj[start + 2:end].strip()
518
+ value = get_input_event_from_span(span.events, key)
519
+ if value is not None:
520
+ obj = obj[:start] + str(value) + obj[end + 2:]
521
+ startIndex = start + len(str(value))
522
+ if startIndex >= len(obj):
523
+ break
524
+ return obj
525
+ else:
526
+ return obj
527
+