monocle-apptrace 0.3.0b4__py3-none-any.whl → 0.3.0b6__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 (45) hide show
  1. monocle_apptrace/__main__.py +19 -0
  2. monocle_apptrace/exporters/azure/blob_exporter.py +7 -0
  3. monocle_apptrace/exporters/monocle_exporters.py +5 -4
  4. monocle_apptrace/exporters/okahu/okahu_exporter.py +1 -1
  5. monocle_apptrace/instrumentation/common/constants.py +22 -0
  6. monocle_apptrace/instrumentation/common/instrumentor.py +119 -39
  7. monocle_apptrace/instrumentation/common/span_handler.py +103 -44
  8. monocle_apptrace/instrumentation/common/utils.py +161 -5
  9. monocle_apptrace/instrumentation/common/wrapper.py +58 -37
  10. monocle_apptrace/instrumentation/common/wrapper_method.py +34 -7
  11. monocle_apptrace/instrumentation/metamodel/botocore/_helper.py +0 -31
  12. monocle_apptrace/instrumentation/metamodel/botocore/handlers/botocore_span_handler.py +25 -0
  13. monocle_apptrace/instrumentation/metamodel/botocore/methods.py +6 -6
  14. monocle_apptrace/instrumentation/metamodel/flask/__init__.py +0 -0
  15. monocle_apptrace/instrumentation/metamodel/flask/_helper.py +29 -0
  16. monocle_apptrace/instrumentation/metamodel/flask/methods.py +13 -0
  17. monocle_apptrace/instrumentation/metamodel/haystack/entities/inference.py +1 -1
  18. monocle_apptrace/instrumentation/metamodel/haystack/methods.py +2 -1
  19. monocle_apptrace/instrumentation/metamodel/langchain/_helper.py +4 -0
  20. monocle_apptrace/instrumentation/metamodel/langchain/entities/inference.py +3 -2
  21. monocle_apptrace/instrumentation/metamodel/langchain/methods.py +12 -6
  22. monocle_apptrace/instrumentation/metamodel/langgraph/__init__.py +0 -0
  23. monocle_apptrace/instrumentation/metamodel/langgraph/_helper.py +48 -0
  24. monocle_apptrace/instrumentation/metamodel/langgraph/entities/__init__.py +0 -0
  25. monocle_apptrace/instrumentation/metamodel/langgraph/entities/inference.py +56 -0
  26. monocle_apptrace/instrumentation/metamodel/langgraph/methods.py +14 -0
  27. monocle_apptrace/instrumentation/metamodel/llamaindex/_helper.py +37 -19
  28. monocle_apptrace/instrumentation/metamodel/llamaindex/entities/agent.py +47 -0
  29. monocle_apptrace/instrumentation/metamodel/llamaindex/entities/inference.py +5 -3
  30. monocle_apptrace/instrumentation/metamodel/llamaindex/methods.py +15 -3
  31. monocle_apptrace/instrumentation/metamodel/openai/__init__.py +0 -0
  32. monocle_apptrace/instrumentation/metamodel/openai/_helper.py +112 -0
  33. monocle_apptrace/instrumentation/metamodel/openai/entities/__init__.py +0 -0
  34. monocle_apptrace/instrumentation/metamodel/openai/entities/inference.py +71 -0
  35. monocle_apptrace/instrumentation/metamodel/openai/entities/retrieval.py +43 -0
  36. monocle_apptrace/instrumentation/metamodel/openai/methods.py +45 -0
  37. monocle_apptrace/instrumentation/metamodel/requests/__init__.py +4 -0
  38. monocle_apptrace/instrumentation/metamodel/requests/_helper.py +31 -0
  39. monocle_apptrace/instrumentation/metamodel/requests/methods.py +12 -0
  40. {monocle_apptrace-0.3.0b4.dist-info → monocle_apptrace-0.3.0b6.dist-info}/METADATA +2 -1
  41. monocle_apptrace-0.3.0b6.dist-info/RECORD +68 -0
  42. monocle_apptrace-0.3.0b4.dist-info/RECORD +0 -48
  43. {monocle_apptrace-0.3.0b4.dist-info → monocle_apptrace-0.3.0b6.dist-info}/WHEEL +0 -0
  44. {monocle_apptrace-0.3.0b4.dist-info → monocle_apptrace-0.3.0b6.dist-info}/licenses/LICENSE +0 -0
  45. {monocle_apptrace-0.3.0b4.dist-info → monocle_apptrace-0.3.0b6.dist-info}/licenses/NOTICE +0 -0
@@ -0,0 +1,48 @@
1
+ from monocle_apptrace.instrumentation.common.utils import resolve_from_alias
2
+ import logging
3
+ logger = logging.getLogger(__name__)
4
+
5
+ def handle_openai_response(response):
6
+ try:
7
+ if 'messages' in response:
8
+ output = response["messages"][-1]
9
+ return str(output.content)
10
+ except Exception as e:
11
+ logger.warning("Warning: Error occurred in handle_openai_response: %s", str(e))
12
+ return ""
13
+
14
+ def agent_instructions(arguments):
15
+ if callable(arguments['kwargs']['agent'].instructions):
16
+ return arguments['kwargs']['agent'].instructions(arguments['kwargs']['context_variables'])
17
+ else:
18
+ return arguments['kwargs']['agent'].instructions
19
+
20
+ def extract_input(arguments):
21
+ history = arguments['result']['messages']
22
+ for message in history:
23
+ if hasattr(message, 'content') and hasattr(message, 'type') and message.type == "human": # Check if the message is a HumanMessage
24
+ return message.content
25
+
26
+ def get_inference_endpoint(arguments):
27
+ inference_endpoint = resolve_from_alias(arguments['instance'].client.__dict__, ['azure_endpoint', 'api_base', '_base_url'])
28
+ return str(inference_endpoint)
29
+
30
+ def tools(instance):
31
+ if hasattr(instance,'nodes') and ('tools' in instance.nodes):
32
+ tools= instance.nodes['tools']
33
+ if hasattr(tools,'bound') and hasattr(tools.bound,'tools_by_name'):
34
+ return list(tools.bound.tools_by_name.keys())
35
+
36
+
37
+ def update_span_from_llm_response(response):
38
+ meta_dict = {}
39
+ token_usage = None
40
+ if response is not None and "messages" in response:
41
+ token = response["messages"][-1]
42
+ if token.response_metadata is not None:
43
+ token_usage = token.response_metadata["token_usage"]
44
+ if token_usage is not None:
45
+ meta_dict.update({"completion_tokens": token_usage.get('completion_tokens')})
46
+ meta_dict.update({"prompt_tokens": token_usage.get('prompt_tokens')})
47
+ meta_dict.update({"total_tokens": token_usage.get('total_tokens')})
48
+ return meta_dict
@@ -0,0 +1,56 @@
1
+ from monocle_apptrace.instrumentation.metamodel.langgraph import (
2
+ _helper
3
+ )
4
+ INFERENCE = {
5
+ "type": "agent",
6
+ "attributes": [
7
+ [
8
+ {
9
+ "_comment": "agent type",
10
+ "attribute": "type",
11
+ "accessor": lambda arguments:'agent.oai'
12
+ },
13
+ {
14
+ "_comment": "name of the agent",
15
+ "attribute": "name",
16
+ "accessor": lambda arguments:arguments['instance'].name
17
+ },
18
+ {
19
+ "_comment": "agent tools",
20
+ "attribute": "tools",
21
+ "accessor": lambda arguments: _helper.tools(arguments['instance'])
22
+ }
23
+ ]
24
+ ],
25
+ "events": [
26
+ {
27
+ "name":"data.input",
28
+ "attributes": [
29
+ {
30
+ "_comment": "this is LLM input",
31
+ "attribute": "query",
32
+ "accessor": lambda arguments: _helper.extract_input(arguments)
33
+ }
34
+ ]
35
+ },
36
+ {
37
+ "name":"data.output",
38
+ "attributes": [
39
+ {
40
+ "_comment": "this is response from LLM",
41
+ "attribute": "response",
42
+ "accessor": lambda arguments: _helper.handle_openai_response(arguments['result'])
43
+ }
44
+ ]
45
+ },
46
+ {
47
+ "name": "metadata",
48
+ "attributes": [
49
+ {
50
+ "_comment": "this is metadata usage from LLM",
51
+ "accessor": lambda arguments: _helper.update_span_from_llm_response(arguments['result'])
52
+ }
53
+ ]
54
+ }
55
+ ]
56
+ }
@@ -0,0 +1,14 @@
1
+ from monocle_apptrace.instrumentation.common.wrapper import task_wrapper
2
+ from monocle_apptrace.instrumentation.metamodel.langgraph.entities.inference import (
3
+ INFERENCE,
4
+ )
5
+ LANGGRAPH_METHODS = [
6
+ {
7
+ "package": "langgraph.graph.state",
8
+ "object": "CompiledStateGraph",
9
+ "method": "invoke",
10
+ "span_name": "langgraph.graph.invoke",
11
+ "wrapper_method": task_wrapper,
12
+ "output_processor": INFERENCE
13
+ }
14
+ ]
@@ -16,34 +16,50 @@ from monocle_apptrace.instrumentation.common.utils import (
16
16
  logger = logging.getLogger(__name__)
17
17
 
18
18
 
19
+ def extract_tools(instance):
20
+ tools = []
21
+ if not hasattr(instance, 'state') or not hasattr(instance.state, 'task_dict'):
22
+ return []
23
+ try:
24
+ data = next(iter(instance.state.task_dict.values())).task
25
+ except (AttributeError, StopIteration):
26
+ return []
27
+
28
+ if hasattr(data,'extra_state') and 'sources' in data.extra_state:
29
+ for tool_output in data.extra_state['sources']:
30
+ tool_name = tool_output.tool_name
31
+ if tool_name:
32
+ tools.append(tool_name)
33
+ return tools
34
+
35
+
19
36
  def extract_messages(args):
20
37
  """Extract system and user messages"""
21
38
  try:
22
39
  messages = []
23
- if args and isinstance(args, (list, tuple)) and args[0]:
40
+
41
+ def process_message(msg):
42
+ """Processes a single message and extracts relevant information."""
43
+ if hasattr(msg, 'content') and hasattr(msg, 'role'):
44
+ role = getattr(msg.role, 'value', msg.role)
45
+ content = msg.content if role == "system" else extract_query_from_content(msg.content)
46
+ messages.append({role: content})
47
+
48
+ if isinstance(args, (list, tuple)) and args:
24
49
  for msg in args[0]:
25
- if hasattr(msg, 'content') and hasattr(msg, 'role'):
26
- role = getattr(msg.role, 'value', msg.role)
27
- if role == "system":
28
- messages.append({role: msg.content})
29
- elif role in ["user", "human"]:
30
- user_message = extract_query_from_content(msg.content)
31
- messages.append({role: user_message})
32
- if args and isinstance(args, dict):
50
+ process_message(msg)
51
+ if isinstance(args, dict):
33
52
  for msg in args.get("messages", []):
34
- if hasattr(msg, 'content') and hasattr(msg, 'role'):
35
- role = getattr(msg.role, 'value', msg.role)
36
- if role == "system":
37
- messages.append({role: msg.content})
38
- elif role in ["user", "human"]:
39
- user_message = msg.content
40
- messages.append({role: user_message})
53
+ process_message(msg)
54
+ if args and isinstance(args, tuple):
55
+ messages.append(args[0])
56
+
41
57
  return [str(message) for message in messages]
58
+
42
59
  except Exception as e:
43
- logger.warning("Warning: Error occurred in extract_messages: %s", str(e))
60
+ logger.warning("Error in extract_messages: %s", str(e))
44
61
  return []
45
62
 
46
-
47
63
  def extract_assistant_message(response):
48
64
  try:
49
65
  if isinstance(response, str):
@@ -52,6 +68,8 @@ def extract_assistant_message(response):
52
68
  return [response.content]
53
69
  if hasattr(response, "message") and hasattr(response.message, "content"):
54
70
  return [response.message.content]
71
+ if hasattr(response,"response") and isinstance(response.response, str):
72
+ return [response.response]
55
73
  except Exception as e:
56
74
  logger.warning("Warning: Error occurred in extract_assistant_message: %s", str(e))
57
75
  return []
@@ -63,7 +81,7 @@ def extract_query_from_content(content):
63
81
  answer_prefix = "Answer:"
64
82
  query_start = content.find(query_prefix)
65
83
  if query_start == -1:
66
- return None
84
+ return content
67
85
 
68
86
  query_start += len(query_prefix)
69
87
  answer_start = content.find(answer_prefix, query_start)
@@ -0,0 +1,47 @@
1
+ from monocle_apptrace.instrumentation.metamodel.llamaindex import (
2
+ _helper,
3
+ )
4
+
5
+ AGENT = {
6
+ "type": "agent",
7
+ "attributes": [
8
+ [
9
+ {
10
+ "_comment": "Agent name, type and Tools.",
11
+ "attribute": "name",
12
+ "accessor": lambda arguments: arguments['instance'].__class__.__name__
13
+ },
14
+ {
15
+ "attribute": "type",
16
+ "accessor": lambda arguments: 'Agent.oai'
17
+ },
18
+ {
19
+ "attribute": "tools",
20
+ "accessor": lambda arguments: _helper.extract_tools(arguments['instance'])
21
+ }
22
+ ]
23
+
24
+ ],
25
+ "events": [
26
+ {"name": "data.input",
27
+ "attributes": [
28
+
29
+ {
30
+ "_comment": "this is instruction and user query to LLM",
31
+ "attribute": "input",
32
+ "accessor": lambda arguments: _helper.extract_messages(arguments['args'])
33
+ }
34
+ ]
35
+ },
36
+ {
37
+ "name": "data.output",
38
+ "attributes": [
39
+ {
40
+ "_comment": "this is response from LLM",
41
+ "attribute": "response",
42
+ "accessor": lambda arguments: _helper.extract_assistant_message(arguments['result'])
43
+ }
44
+ ]
45
+ }
46
+ ]
47
+ }
@@ -1,7 +1,7 @@
1
1
  from monocle_apptrace.instrumentation.metamodel.llamaindex import (
2
2
  _helper,
3
3
  )
4
- from monocle_apptrace.instrumentation.common.utils import resolve_from_alias
4
+ from monocle_apptrace.instrumentation.common.utils import resolve_from_alias, get_llm_type
5
5
 
6
6
  INFERENCE = {
7
7
  "type": "inference",
@@ -10,11 +10,13 @@ INFERENCE = {
10
10
  {
11
11
  "_comment": "provider type ,name , deployment , inference_endpoint",
12
12
  "attribute": "type",
13
- "accessor": lambda arguments: 'inference.azure_oai'
13
+ "accessor": lambda arguments: 'inference.' + (get_llm_type(arguments['instance']) or 'generic')
14
+
14
15
  },
15
16
  {
16
17
  "attribute": "provider_name",
17
- "accessor": lambda arguments: _helper.extract_provider_name(arguments['instance'])
18
+ "accessor": lambda arguments: arguments['kwargs'].get('provider_name') or _helper.extract_provider_name(arguments['instance'])
19
+
18
20
  },
19
21
  {
20
22
  "attribute": "deployment",
@@ -2,6 +2,7 @@ from monocle_apptrace.instrumentation.common.wrapper import atask_wrapper, task_
2
2
  from monocle_apptrace.instrumentation.metamodel.llamaindex.entities.inference import (
3
3
  INFERENCE,
4
4
  )
5
+ from monocle_apptrace.instrumentation.metamodel.llamaindex.entities.agent import AGENT
5
6
  from monocle_apptrace.instrumentation.metamodel.llamaindex.entities.retrieval import (
6
7
  RETRIEVAL,
7
8
  )
@@ -29,14 +30,16 @@ LLAMAINDEX_METHODS = [
29
30
  "object": "BaseQueryEngine",
30
31
  "method": "query",
31
32
  "span_name": "llamaindex.query",
32
- "wrapper_method": task_wrapper
33
+ "wrapper_method": task_wrapper,
34
+ "span_type": "workflow"
33
35
  },
34
36
  {
35
37
  "package": "llama_index.core.base.base_query_engine",
36
38
  "object": "BaseQueryEngine",
37
39
  "method": "aquery",
38
40
  "span_name": "llamaindex.query",
39
- "wrapper_method": atask_wrapper
41
+ "wrapper_method": atask_wrapper,
42
+ "span_type": "workflow"
40
43
  },
41
44
  {
42
45
  "package": "llama_index.core.llms.custom",
@@ -52,7 +55,8 @@ LLAMAINDEX_METHODS = [
52
55
  "method": "achat",
53
56
  "span_name": "llamaindex.llmchat",
54
57
  "wrapper_method": atask_wrapper,
55
- "output_processor": INFERENCE
58
+ "output_processor": INFERENCE,
59
+
56
60
  },
57
61
  {
58
62
  "package": "llama_index.llms.openai.base",
@@ -85,5 +89,13 @@ LLAMAINDEX_METHODS = [
85
89
  "span_name": "llamaindex.mistralai",
86
90
  "wrapper_method": atask_wrapper,
87
91
  "output_processor": INFERENCE
92
+ },
93
+ {
94
+ "package": "llama_index.core.agent",
95
+ "object": "ReActAgent",
96
+ "method": "chat",
97
+ "span_name": "react.agent",
98
+ "wrapper_method": task_wrapper,
99
+ "output_processor": AGENT
88
100
  }
89
101
  ]
@@ -0,0 +1,112 @@
1
+ """
2
+ This module provides utility functions for extracting system, user,
3
+ and assistant messages from various input formats.
4
+ """
5
+
6
+ import logging
7
+ from monocle_apptrace.instrumentation.common.utils import (
8
+ Option,
9
+ get_keys_as_tuple,
10
+ get_nested_value,
11
+ try_option,
12
+ )
13
+
14
+
15
+ logger = logging.getLogger(__name__)
16
+
17
+
18
+ def extract_messages(kwargs):
19
+ """Extract system and user messages"""
20
+ try:
21
+ messages = []
22
+ if 'messages' in kwargs and len(kwargs['messages']) >0:
23
+ for msg in kwargs['messages']:
24
+ if msg.get('content') and msg.get('role'):
25
+ messages.append({msg['role']: msg['content']})
26
+
27
+ return [str(message) for message in messages]
28
+ except Exception as e:
29
+ logger.warning("Warning: Error occurred in extract_messages: %s", str(e))
30
+ return []
31
+
32
+
33
+ def extract_assistant_message(response):
34
+ try:
35
+ if response is not None and hasattr(response,"choices") and len(response.choices) >0:
36
+ if hasattr(response.choices[0],"message"):
37
+ return response.choices[0].message.content
38
+ except (IndexError, AttributeError) as e:
39
+ logger.warning("Warning: Error occurred in extract_assistant_message: %s", str(e))
40
+ return None
41
+
42
+ def extract_provider_name(instance):
43
+ provider_url: Option[str] = try_option(getattr, instance._client.base_url, 'host')
44
+ return provider_url.unwrap_or(None)
45
+
46
+
47
+ def extract_inference_endpoint(instance):
48
+ inference_endpoint: Option[str] = try_option(getattr, instance._client, 'base_url').map(str)
49
+ if inference_endpoint.is_none() and "meta" in instance.client.__dict__:
50
+ inference_endpoint = try_option(getattr, instance.client.meta, 'endpoint_url').map(str)
51
+
52
+ return inference_endpoint.unwrap_or(extract_provider_name(instance))
53
+
54
+ def resolve_from_alias(my_map, alias):
55
+ """Find a alias that is not none from list of aliases"""
56
+
57
+ for i in alias:
58
+ if i in my_map.keys():
59
+ return my_map[i]
60
+ return None
61
+
62
+
63
+ def update_input_span_events(kwargs):
64
+ if 'input' in kwargs and isinstance(kwargs['input'], list):
65
+ query = ' '.join(kwargs['input'])
66
+ return query
67
+
68
+
69
+ def update_output_span_events(results):
70
+ if hasattr(results,'data') and isinstance(results.data, list):
71
+ embeddings = results.data
72
+ embedding_strings = [f"index={e.index}, embedding={e.embedding}" for e in embeddings]
73
+ output = '\n'.join(embedding_strings)
74
+ if len(output) > 100:
75
+ output = output[:100] + "..."
76
+ return output
77
+
78
+
79
+ def update_span_from_llm_response(response):
80
+ meta_dict = {}
81
+ if response is not None and hasattr(response, "usage"):
82
+ if hasattr(response, "usage") and response.usage is not None:
83
+ token_usage = response.usage
84
+ else:
85
+ response_metadata = response.response_metadata
86
+ token_usage = response_metadata.get("token_usage")
87
+ if token_usage is not None:
88
+ meta_dict.update(
89
+ {"completion_tokens": getattr(response.usage, "completion_tokens", None)})
90
+ meta_dict.update({"prompt_tokens": getattr(response.usage, "prompt_tokens", None)})
91
+ meta_dict.update({"total_tokens": getattr(response.usage, "total_tokens", None)})
92
+ return meta_dict
93
+
94
+ def extract_vector_input(vector_input: dict):
95
+ if 'input' in vector_input:
96
+ return vector_input['input']
97
+ return ""
98
+
99
+ def extract_vector_output(vector_output):
100
+ try:
101
+ if hasattr(vector_output, 'data') and len(vector_output.data) > 0:
102
+ return vector_output.data[0].embedding
103
+ except Exception as e:
104
+ pass
105
+ return ""
106
+
107
+ def get_inference_type(instance):
108
+ inference_type: Option[str] = try_option(getattr, instance._client, '_api_version')
109
+ if inference_type.unwrap_or(None):
110
+ return 'azure_openai'
111
+ else:
112
+ return 'openai'
@@ -0,0 +1,71 @@
1
+ from monocle_apptrace.instrumentation.metamodel.openai import (
2
+ _helper,
3
+ )
4
+ from monocle_apptrace.instrumentation.common.utils import resolve_from_alias
5
+
6
+ INFERENCE = {
7
+ "type": "inference",
8
+ "attributes": [
9
+ [
10
+ {
11
+ "_comment": "provider type ,name , deployment , inference_endpoint",
12
+ "attribute": "type",
13
+ "accessor": lambda arguments: 'inference.' + (_helper.get_inference_type(arguments['instance'])) or 'openai'
14
+ },
15
+ {
16
+ "attribute": "provider_name",
17
+ "accessor": lambda arguments: _helper.extract_provider_name(arguments['instance'])
18
+ },
19
+ {
20
+ "attribute": "deployment",
21
+ "accessor": lambda arguments: resolve_from_alias(arguments['instance'].__dict__, ['engine', 'azure_deployment', 'deployment_name', 'deployment_id', 'deployment'])
22
+ },
23
+ {
24
+ "attribute": "inference_endpoint",
25
+ "accessor": lambda arguments: resolve_from_alias(arguments['instance'].__dict__, ['azure_endpoint', 'api_base', 'endpoint']) or _helper.extract_inference_endpoint(arguments['instance'])
26
+ }
27
+ ],
28
+ [
29
+ {
30
+ "_comment": "LLM Model",
31
+ "attribute": "name",
32
+ "accessor": lambda arguments: resolve_from_alias(arguments['kwargs'], ['model', 'model_name', 'endpoint_name', 'deployment_name'])
33
+ },
34
+ {
35
+ "attribute": "type",
36
+ "accessor": lambda arguments: 'model.llm.' + resolve_from_alias(arguments['kwargs'], ['model', 'model_name', 'endpoint_name', 'deployment_name'])
37
+ }
38
+ ]
39
+ ],
40
+ "events": [
41
+ {"name": "data.input",
42
+ "attributes": [
43
+
44
+ {
45
+ "_comment": "this is instruction and user query to LLM",
46
+ "attribute": "input",
47
+ "accessor": lambda arguments: _helper.extract_messages(arguments['kwargs'])
48
+ }
49
+ ]
50
+ },
51
+ {
52
+ "name": "data.output",
53
+ "attributes": [
54
+ {
55
+ "_comment": "this is result from LLM",
56
+ "attribute": "response",
57
+ "accessor": lambda arguments: _helper.extract_assistant_message(arguments['result'])
58
+ }
59
+ ]
60
+ },
61
+ {
62
+ "name": "metadata",
63
+ "attributes": [
64
+ {
65
+ "_comment": "this is metadata usage from LLM",
66
+ "accessor": lambda arguments: _helper.update_span_from_llm_response(arguments['result'])
67
+ }
68
+ ]
69
+ }
70
+ ]
71
+ }
@@ -0,0 +1,43 @@
1
+ from monocle_apptrace.instrumentation.metamodel.openai import (
2
+ _helper,
3
+ )
4
+ from monocle_apptrace.instrumentation.common.utils import resolve_from_alias
5
+
6
+ RETRIEVAL = {
7
+ "type": "retrieval",
8
+ "attributes": [
9
+ [
10
+ {
11
+ "_comment": "LLM Model",
12
+ "attribute": "name",
13
+ "accessor": lambda arguments: resolve_from_alias(arguments['kwargs'], ['model', 'model_name', 'endpoint_name', 'deployment_name'])
14
+ },
15
+ {
16
+ "attribute": "type",
17
+ "accessor": lambda arguments: 'model.embedding.' + resolve_from_alias(arguments['kwargs'], ['model', 'model_name', 'endpoint_name', 'deployment_name'])
18
+ }
19
+ ]
20
+ ],
21
+ "events": [
22
+ {
23
+ "name": "data.input",
24
+ "attributes": [
25
+ {
26
+ "_comment": "this is instruction and user query to LLM",
27
+ "attribute": "input",
28
+ "accessor": lambda arguments: _helper.update_input_span_events(arguments['kwargs'])
29
+ }
30
+ ]
31
+ },
32
+ {
33
+ "name": "data.output",
34
+ "attributes": [
35
+ {
36
+ "_comment": "this is result from LLM",
37
+ "attribute": "response",
38
+ "accessor": lambda arguments: _helper.update_output_span_events(arguments['result'])
39
+ }
40
+ ]
41
+ }
42
+ ]
43
+ }
@@ -0,0 +1,45 @@
1
+ from monocle_apptrace.instrumentation.common.wrapper import atask_wrapper, task_wrapper
2
+ from monocle_apptrace.instrumentation.metamodel.openai.entities.inference import (
3
+ INFERENCE,
4
+ )
5
+ from monocle_apptrace.instrumentation.metamodel.openai.entities.retrieval import (
6
+ RETRIEVAL,
7
+ )
8
+
9
+ OPENAI_METHODS = [
10
+ {
11
+ "package": "openai.resources.chat.completions",
12
+ "object": "Completions",
13
+ "method": "create",
14
+ "wrapper_method": task_wrapper,
15
+ "span_handler": "non_framework_handler",
16
+ "output_processor": INFERENCE
17
+ },
18
+ {
19
+ "package": "openai.resources.chat.completions",
20
+ "object": "AsyncCompletions",
21
+ "method": "create",
22
+ "wrapper_method": atask_wrapper,
23
+ "span_handler": "non_framework_handler",
24
+ "output_processor": INFERENCE
25
+ },
26
+ {
27
+ "package": "openai.resources.embeddings",
28
+ "object": "Embeddings",
29
+ "method": "create",
30
+ "wrapper_method": task_wrapper,
31
+ "span_name": "openai_embeddings",
32
+ "span_handler": "non_framework_handler",
33
+ "output_processor": RETRIEVAL
34
+ },
35
+ {
36
+ "package": "openai.resources.embeddings",
37
+ "object": "AsyncEmbeddings",
38
+ "method": "create",
39
+ "wrapper_method": atask_wrapper,
40
+ "span_name": "openai_embeddings",
41
+ "span_handler": "non_framework_handler",
42
+ "output_processor": RETRIEVAL
43
+ }
44
+
45
+ ]
@@ -0,0 +1,4 @@
1
+ from os import environ
2
+ from monocle_apptrace.instrumentation.common.constants import TRACE_PROPOGATION_URLS
3
+ allowed_url_str = environ.get(TRACE_PROPOGATION_URLS, "")
4
+ allowed_urls:list[str] = [] if allowed_url_str == "" else allowed_url_str.split(',')
@@ -0,0 +1,31 @@
1
+ import os
2
+ from monocle_apptrace.instrumentation.metamodel.requests import allowed_urls
3
+ from opentelemetry.propagate import inject
4
+ from monocle_apptrace.instrumentation.common.span_handler import SpanHandler
5
+
6
+ def request_pre_task_processor(kwargs):
7
+ # add traceparent to the request headers in kwargs
8
+ if 'headers' not in kwargs:
9
+ headers = {}
10
+ else:
11
+ headers = kwargs['headers'].copy()
12
+ inject(headers)
13
+ kwargs['headers'] = headers
14
+
15
+ def request_skip_span(kwargs) -> bool:
16
+ # add traceparent to the request headers in kwargs
17
+ if 'url' in kwargs:
18
+ url:str = kwargs['url']
19
+ for allowed_url in allowed_urls:
20
+ if url.startswith(allowed_url.strip()):
21
+ return False
22
+ return True
23
+
24
+ class RequestSpanHandler(SpanHandler):
25
+
26
+ def pre_task_processing(self, to_wrap, wrapped, instance, args,kwargs, span):
27
+ request_pre_task_processor(kwargs)
28
+ super().pre_task_processing(to_wrap, wrapped, instance, args,kwargs,span)
29
+
30
+ def skip_span(self, to_wrap, wrapped, instance, args, kwargs) -> bool:
31
+ return request_skip_span(kwargs)
@@ -0,0 +1,12 @@
1
+ from monocle_apptrace.instrumentation.common.wrapper import task_wrapper
2
+
3
+ REQUESTS_METHODS = [
4
+ {
5
+ "package": "requests.sessions",
6
+ "object": "Session",
7
+ "method": "request",
8
+ "span_name": "http_requests",
9
+ "wrapper_method": task_wrapper,
10
+ "span_handler":"request_handler",
11
+ }
12
+ ]