monocle-apptrace 0.3.0b7__py3-none-any.whl → 0.3.1__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 (41) hide show
  1. monocle_apptrace/exporters/monocle_exporters.py +5 -2
  2. monocle_apptrace/instrumentation/common/__init__.py +1 -1
  3. monocle_apptrace/instrumentation/common/constants.py +7 -1
  4. monocle_apptrace/instrumentation/common/instrumentor.py +20 -9
  5. monocle_apptrace/instrumentation/common/span_handler.py +36 -25
  6. monocle_apptrace/instrumentation/common/tracing.md +68 -0
  7. monocle_apptrace/instrumentation/common/utils.py +25 -10
  8. monocle_apptrace/instrumentation/common/wrapper.py +24 -22
  9. monocle_apptrace/instrumentation/common/wrapper_method.py +5 -2
  10. monocle_apptrace/instrumentation/metamodel/anthropic/__init__.py +0 -0
  11. monocle_apptrace/instrumentation/metamodel/anthropic/_helper.py +64 -0
  12. monocle_apptrace/instrumentation/metamodel/anthropic/entities/__init__.py +0 -0
  13. monocle_apptrace/instrumentation/metamodel/anthropic/entities/inference.py +72 -0
  14. monocle_apptrace/instrumentation/metamodel/anthropic/methods.py +24 -0
  15. monocle_apptrace/instrumentation/metamodel/botocore/entities/inference.py +2 -2
  16. monocle_apptrace/instrumentation/metamodel/botocore/handlers/botocore_span_handler.py +1 -1
  17. monocle_apptrace/instrumentation/metamodel/flask/_helper.py +45 -3
  18. monocle_apptrace/instrumentation/metamodel/flask/entities/http.py +49 -0
  19. monocle_apptrace/instrumentation/metamodel/flask/methods.py +10 -1
  20. monocle_apptrace/instrumentation/metamodel/haystack/entities/inference.py +4 -1
  21. monocle_apptrace/instrumentation/metamodel/haystack/methods.py +1 -4
  22. monocle_apptrace/instrumentation/metamodel/langchain/_helper.py +12 -4
  23. monocle_apptrace/instrumentation/metamodel/langchain/methods.py +6 -14
  24. monocle_apptrace/instrumentation/metamodel/llamaindex/methods.py +2 -15
  25. monocle_apptrace/instrumentation/metamodel/openai/_helper.py +9 -4
  26. monocle_apptrace/instrumentation/metamodel/openai/methods.py +16 -2
  27. monocle_apptrace/instrumentation/metamodel/requests/_helper.py +31 -0
  28. monocle_apptrace/instrumentation/metamodel/requests/entities/http.py +51 -0
  29. monocle_apptrace/instrumentation/metamodel/requests/methods.py +2 -1
  30. monocle_apptrace/instrumentation/metamodel/teamsai/__init__.py +0 -0
  31. monocle_apptrace/instrumentation/metamodel/teamsai/_helper.py +58 -0
  32. monocle_apptrace/instrumentation/metamodel/teamsai/entities/__init__.py +0 -0
  33. monocle_apptrace/instrumentation/metamodel/teamsai/entities/inference/__init__.py +0 -0
  34. monocle_apptrace/instrumentation/metamodel/teamsai/entities/inference/actionplanner_output_processor.py +80 -0
  35. monocle_apptrace/instrumentation/metamodel/teamsai/entities/inference/teamsai_output_processor.py +70 -0
  36. monocle_apptrace/instrumentation/metamodel/teamsai/methods.py +26 -0
  37. {monocle_apptrace-0.3.0b7.dist-info → monocle_apptrace-0.3.1.dist-info}/METADATA +2 -1
  38. {monocle_apptrace-0.3.0b7.dist-info → monocle_apptrace-0.3.1.dist-info}/RECORD +41 -26
  39. {monocle_apptrace-0.3.0b7.dist-info → monocle_apptrace-0.3.1.dist-info}/WHEEL +0 -0
  40. {monocle_apptrace-0.3.0b7.dist-info → monocle_apptrace-0.3.1.dist-info}/licenses/LICENSE +0 -0
  41. {monocle_apptrace-0.3.0b7.dist-info → monocle_apptrace-0.3.1.dist-info}/licenses/NOTICE +0 -0
@@ -0,0 +1,72 @@
1
+ from monocle_apptrace.instrumentation.metamodel.anthropic import (
2
+ _helper,
3
+ )
4
+ from monocle_apptrace.instrumentation.common.utils import resolve_from_alias, get_llm_type
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.' + (get_llm_type(arguments['instance']) or 'generic')
14
+
15
+ },
16
+ {
17
+ "attribute": "provider_name",
18
+ "accessor": lambda arguments: _helper.extract_provider_name(arguments['instance'])
19
+ },
20
+ {
21
+ "attribute": "deployment",
22
+ "accessor": lambda arguments: resolve_from_alias(arguments['instance'].__dict__, ['engine', 'azure_deployment', 'deployment_name', 'deployment_id', 'deployment'])
23
+ },
24
+ {
25
+ "attribute": "inference_endpoint",
26
+ "accessor": lambda arguments: resolve_from_alias(arguments['instance'].__dict__, ['azure_endpoint', 'api_base', 'endpoint']) or _helper.extract_inference_endpoint(arguments['instance'])
27
+ }
28
+ ],
29
+ [
30
+ {
31
+ "_comment": "LLM Model",
32
+ "attribute": "name",
33
+ "accessor": lambda arguments: resolve_from_alias(arguments['kwargs'], ['model', 'model_name', 'endpoint_name', 'deployment_name'])
34
+ },
35
+ {
36
+ "attribute": "type",
37
+ "accessor": lambda arguments: 'model.llm.' + resolve_from_alias(arguments['kwargs'], ['model', 'model_name', 'endpoint_name', 'deployment_name'])
38
+ }
39
+ ]
40
+ ],
41
+ "events": [
42
+ {"name": "data.input",
43
+ "attributes": [
44
+
45
+ {
46
+ "_comment": "this is instruction and user query to LLM",
47
+ "attribute": "input",
48
+ "accessor": lambda arguments: _helper.extract_messages(arguments['kwargs'])
49
+ }
50
+ ]
51
+ },
52
+ {
53
+ "name": "data.output",
54
+ "attributes": [
55
+ {
56
+ "_comment": "this is result from LLM",
57
+ "attribute": "response",
58
+ "accessor": lambda arguments: _helper.extract_assistant_message(arguments['result'])
59
+ }
60
+ ]
61
+ },
62
+ {
63
+ "name": "metadata",
64
+ "attributes": [
65
+ {
66
+ "_comment": "this is metadata usage from LLM",
67
+ "accessor": lambda arguments: _helper.update_span_from_llm_response(arguments['result'])
68
+ }
69
+ ]
70
+ }
71
+ ]
72
+ }
@@ -0,0 +1,24 @@
1
+ from monocle_apptrace.instrumentation.common.wrapper import atask_wrapper, task_wrapper
2
+ from monocle_apptrace.instrumentation.metamodel.anthropic.entities.inference import (
3
+ INFERENCE,
4
+ )
5
+
6
+ ANTHROPIC_METHODS = [
7
+ {
8
+ "package": "anthropic.resources",
9
+ "object": "Messages",
10
+ "method": "create",
11
+ "wrapper_method": task_wrapper,
12
+ "span_handler": "non_framework_handler",
13
+ "output_processor": INFERENCE
14
+ },
15
+ {
16
+ "package": "anthropic.resources",
17
+ "object": "AsyncMessages",
18
+ "method": "create",
19
+ "wrapper_method": atask_wrapper,
20
+ "span_handler": "non_framework_handler",
21
+ "output_processor": INFERENCE
22
+ },
23
+
24
+ ]
@@ -1,7 +1,7 @@
1
1
  from monocle_apptrace.instrumentation.metamodel.botocore import (
2
2
  _helper,
3
3
  )
4
-
4
+ from monocle_apptrace.instrumentation.common.utils import get_llm_type
5
5
  INFERENCE = {
6
6
  "type": "inference",
7
7
  "attributes": [
@@ -9,7 +9,7 @@ INFERENCE = {
9
9
  {
10
10
  "_comment": "provider type , inference_endpoint",
11
11
  "attribute": "type",
12
- "accessor": lambda arguments: 'inference.aws_sagemaker'
12
+ "accessor": lambda arguments: 'inference.'+(get_llm_type(arguments['instance']) or 'generic')
13
13
  },
14
14
  {
15
15
  "attribute": "inference_endpoint",
@@ -23,4 +23,4 @@ class BotoCoreSpanHandler(SpanHandler):
23
23
  def post_tracing(self, to_wrap, wrapped, instance, args, kwargs, return_value):
24
24
  self._botocore_processor(to_wrap=to_wrap, wrapped=wrapped, instance=instance, return_value=return_value, args=args,
25
25
  kwargs=kwargs)
26
- return super().pre_tracing(to_wrap, wrapped, instance, args, kwargs)
26
+ return super().post_tracing(to_wrap, wrapped, instance, args, kwargs,return_value)
@@ -1,11 +1,41 @@
1
+ import logging
1
2
  from threading import local
2
3
  from monocle_apptrace.instrumentation.common.utils import extract_http_headers, clear_http_scopes
3
- from opentelemetry.propagate import extract
4
- from opentelemetry.context import Context, attach, detach
5
4
  from monocle_apptrace.instrumentation.common.span_handler import SpanHandler
5
+ from urllib.parse import unquote
6
+ from opentelemetry.context import get_current
7
+ from opentelemetry.trace import Span, get_current_span
8
+ from opentelemetry.trace.propagation import _SPAN_KEY
9
+
10
+ logger = logging.getLogger(__name__)
11
+ MAX_DATA_LENGTH = 1000
6
12
  token_data = local()
7
13
  token_data.current_token = None
8
14
 
15
+ def get_route(args) -> str:
16
+ return args[0]['PATH_INFO'] if 'PATH_INFO' in args[0] else ""
17
+
18
+ def get_method(args) -> str:
19
+ return args[0]['REQUEST_METHOD'] if 'REQUEST_METHOD' in args[0] else ""
20
+
21
+ def get_params(args) -> dict:
22
+ params = args[0]['QUERY_STRING'] if 'QUERY_STRING' in args[0] else ""
23
+ return unquote(params)
24
+
25
+ def get_body(args) -> dict:
26
+ return ""
27
+
28
+ def extract_response(instance) -> str:
29
+ if hasattr(instance, 'data') and hasattr(instance, 'content_length'):
30
+ response = instance.data[0:max(instance.content_length, MAX_DATA_LENGTH)]
31
+ else:
32
+ response = ""
33
+ return response
34
+
35
+ def extract_status(instance) -> str:
36
+ status = instance.status if hasattr(instance, 'status') else ""
37
+ return status
38
+
9
39
  def flask_pre_tracing(args):
10
40
  headers = dict()
11
41
  for key, value in args[0].items():
@@ -26,4 +56,16 @@ class FlaskSpanHandler(SpanHandler):
26
56
 
27
57
  def post_tracing(self, to_wrap, wrapped, instance, args, kwargs, return_value):
28
58
  flask_post_tracing()
29
- return super().post_tracing(to_wrap, wrapped, instance, args, kwargs, return_value)
59
+ return super().post_tracing(to_wrap, wrapped, instance, args, kwargs, return_value)
60
+
61
+ class FlaskResponseSpanHandler(SpanHandler):
62
+ def post_tracing(self, to_wrap, wrapped, instance, args, kwargs, return_value):
63
+ try:
64
+ _parent_span_context = get_current()
65
+ if _parent_span_context is not None:
66
+ parent_span: Span = _parent_span_context.get(_SPAN_KEY, None)
67
+ if parent_span is not None:
68
+ self.hydrate_events(to_wrap, wrapped, instance, args, kwargs, return_value, parent_span)
69
+ except Exception as e:
70
+ logger.info(f"Failed to propogate flask response: {e}")
71
+ super().post_tracing(to_wrap, wrapped, instance, args, kwargs, return_value)
@@ -0,0 +1,49 @@
1
+ from monocle_apptrace.instrumentation.metamodel.flask import _helper
2
+ FLASK_HTTP_PROCESSOR = {
3
+ "type": "http.process",
4
+ "attributes": [
5
+ [
6
+ {
7
+ "_comment": "request method, request URI",
8
+ "attribute": "method",
9
+ "accessor": lambda arguments: _helper.get_method(arguments['args'])
10
+ },
11
+ {
12
+ "_comment": "request method, request URI",
13
+ "attribute": "route",
14
+ "accessor": lambda arguments: _helper.get_route(arguments['args'])
15
+ },
16
+ ]
17
+ ]
18
+ }
19
+
20
+ FLASK_RESPONSE_PROCESSOR = {
21
+ "type": "http.process",
22
+ "events": [
23
+ {
24
+ "name": "data.input",
25
+ "attributes": [
26
+ {
27
+ "_comment": "route params",
28
+ "attribute": "params",
29
+ "accessor": lambda arguments: _helper.get_params(arguments['args'])
30
+ }
31
+ ]
32
+ },
33
+ {
34
+ "name": "data.output",
35
+ "attributes": [
36
+ {
37
+ "_comment": "status from HTTP response",
38
+ "attribute": "status",
39
+ "accessor": lambda arguments: _helper.extract_status(arguments['instance'])
40
+ },
41
+ {
42
+ "_comment": "this is result from LLM",
43
+ "attribute": "response",
44
+ "accessor": lambda arguments: _helper.extract_response(arguments['instance'])
45
+ }
46
+ ]
47
+ }
48
+ ]
49
+ }
@@ -1,13 +1,22 @@
1
1
  from monocle_apptrace.instrumentation.common.wrapper import task_wrapper
2
+ from monocle_apptrace.instrumentation.metamodel.flask.entities.http import FLASK_HTTP_PROCESSOR, FLASK_RESPONSE_PROCESSOR
2
3
 
3
4
  FLASK_METHODS = [
4
5
  {
5
6
  "package": "flask.app",
6
7
  "object": "Flask",
7
8
  "method": "wsgi_app",
8
- "span_name": "Flask.wsgi_app",
9
9
  "wrapper_method": task_wrapper,
10
10
  "span_handler": "flask_handler",
11
+ "output_processor": FLASK_HTTP_PROCESSOR,
12
+ },
13
+ {
14
+ "package": "werkzeug.wrappers.response",
15
+ "object": "Response",
16
+ "method": "__call__",
17
+ "wrapper_method": task_wrapper,
18
+ "span_handler": "flask_response_handler",
19
+ "output_processor": FLASK_RESPONSE_PROCESSOR,
11
20
  "skip_span": True
12
21
  }
13
22
  ]
@@ -1,6 +1,7 @@
1
1
  from monocle_apptrace.instrumentation.metamodel.haystack import (
2
2
  _helper,
3
3
  )
4
+ from monocle_apptrace.instrumentation.common.utils import get_llm_type
4
5
 
5
6
  INFERENCE = {
6
7
  "type": "inference",
@@ -9,7 +10,9 @@ INFERENCE = {
9
10
  {
10
11
  "_comment": "provider type ,name , deployment , inference_endpoint",
11
12
  "attribute": "type",
12
- "accessor": lambda arguments: 'inference.azure_openai'
13
+ # "accessor": lambda arguments: 'inference.azure_openai'
14
+ "accessor": lambda arguments: 'inference.' + (get_llm_type(arguments['instance']) or 'generic')
15
+
13
16
  },
14
17
  {
15
18
  "attribute": "provider_name",
@@ -7,7 +7,6 @@ HAYSTACK_METHODS = [
7
7
  "package": "haystack.components.retrievers.in_memory",
8
8
  "object": "InMemoryEmbeddingRetriever",
9
9
  "method": "run",
10
- "span_name": "haystack.retriever",
11
10
  "wrapper_method": task_wrapper,
12
11
  "output_processor": RETRIEVAL
13
12
  },
@@ -15,7 +14,6 @@ HAYSTACK_METHODS = [
15
14
  "package": "haystack_integrations.components.retrievers.opensearch",
16
15
  "object": "OpenSearchEmbeddingRetriever",
17
16
  "method": "run",
18
- "span_name": "haystack.retriever",
19
17
  "wrapper_method": task_wrapper,
20
18
  "output_processor": RETRIEVAL
21
19
  },
@@ -37,7 +35,6 @@ HAYSTACK_METHODS = [
37
35
  "package": "haystack.core.pipeline.pipeline",
38
36
  "object": "Pipeline",
39
37
  "method": "run",
40
- "wrapper_method": task_wrapper,
41
- "span_type": "workflow"
38
+ "wrapper_method": task_wrapper
42
39
  }
43
40
  ]
@@ -50,14 +50,22 @@ def extract_assistant_message(response):
50
50
 
51
51
 
52
52
  def extract_provider_name(instance):
53
- provider_url: Option[str] = try_option(getattr, instance.client._client.base_url, 'host')
53
+ provider_url: Option[str] = None
54
+ if hasattr(instance,'client'):
55
+ provider_url: Option[str] = try_option(getattr, instance.client._client.base_url, 'host')
56
+ if hasattr(instance, '_client'):
57
+ provider_url = try_option(getattr, instance._client.base_url, 'host')
54
58
  return provider_url.unwrap_or(None)
55
59
 
56
60
 
57
61
  def extract_inference_endpoint(instance):
58
- inference_endpoint: Option[str] = try_option(getattr, instance.client._client, 'base_url').map(str)
59
- if inference_endpoint.is_none() and "meta" in instance.client.__dict__:
60
- inference_endpoint = try_option(getattr, instance.client.meta, 'endpoint_url').map(str)
62
+ inference_endpoint: Option[str] = None
63
+ if hasattr(instance,'client'):
64
+ inference_endpoint: Option[str] = try_option(getattr, instance.client._client, 'base_url').map(str)
65
+ if inference_endpoint.is_none() and "meta" in instance.client.__dict__:
66
+ inference_endpoint = try_option(getattr, instance.client.meta, 'endpoint_url').map(str)
67
+ if hasattr(instance,'_client'):
68
+ inference_endpoint = try_option(getattr, instance._client, 'base_url').map(str)
61
69
 
62
70
  return inference_endpoint.unwrap_or(extract_provider_name(instance))
63
71
 
@@ -11,15 +11,13 @@ LANGCHAIN_METHODS = [
11
11
  "package": "langchain.prompts.base",
12
12
  "object": "BasePromptTemplate",
13
13
  "method": "invoke",
14
- "wrapper_method": task_wrapper,
15
- "span_type": "workflow"
14
+ "wrapper_method": task_wrapper
16
15
  },
17
16
  {
18
17
  "package": "langchain.prompts.base",
19
18
  "object": "BasePromptTemplate",
20
19
  "method": "ainvoke",
21
- "wrapper_method": atask_wrapper,
22
- "span_type": "workflow"
20
+ "wrapper_method": atask_wrapper
23
21
  },
24
22
  {
25
23
  "package": "langchain.chat_models.base",
@@ -82,30 +80,24 @@ LANGCHAIN_METHODS = [
82
80
  "package": "langchain.schema",
83
81
  "object": "BaseOutputParser",
84
82
  "method": "invoke",
85
- "wrapper_method": task_wrapper,
86
- "span_type": "workflow"
83
+ "wrapper_method": task_wrapper
87
84
  },
88
85
  {
89
86
  "package": "langchain.schema",
90
87
  "object": "BaseOutputParser",
91
88
  "method": "ainvoke",
92
- "wrapper_method": atask_wrapper,
93
- "span_type": "workflow"
89
+ "wrapper_method": atask_wrapper
94
90
  },
95
91
  {
96
92
  "package": "langchain.schema.runnable",
97
93
  "object": "RunnableSequence",
98
94
  "method": "invoke",
99
- "span_name": "langchain.workflow",
100
- "wrapper_method": task_wrapper,
101
- "span_type": "workflow"
95
+ "wrapper_method": task_wrapper
102
96
  },
103
97
  {
104
98
  "package": "langchain.schema.runnable",
105
99
  "object": "RunnableSequence",
106
100
  "method": "ainvoke",
107
- "span_name": "langchain.workflow",
108
- "wrapper_method": atask_wrapper,
109
- "span_type": "workflow"
101
+ "wrapper_method": atask_wrapper
110
102
  }
111
103
  ]
@@ -13,7 +13,6 @@ LLAMAINDEX_METHODS = [
13
13
  "package": "llama_index.core.indices.base_retriever",
14
14
  "object": "BaseRetriever",
15
15
  "method": "retrieve",
16
- "span_name": "llamaindex.retrieve",
17
16
  "wrapper_method": task_wrapper,
18
17
  "output_processor": RETRIEVAL
19
18
  },
@@ -21,7 +20,6 @@ LLAMAINDEX_METHODS = [
21
20
  "package": "llama_index.core.indices.base_retriever",
22
21
  "object": "BaseRetriever",
23
22
  "method": "aretrieve",
24
- "span_name": "llamaindex.retrieve",
25
23
  "wrapper_method": atask_wrapper,
26
24
  "output_processor": RETRIEVAL
27
25
  },
@@ -29,23 +27,18 @@ LLAMAINDEX_METHODS = [
29
27
  "package": "llama_index.core.base.base_query_engine",
30
28
  "object": "BaseQueryEngine",
31
29
  "method": "query",
32
- "span_name": "llamaindex.query",
33
- "wrapper_method": task_wrapper,
34
- "span_type": "workflow"
30
+ "wrapper_method": task_wrapper
35
31
  },
36
32
  {
37
33
  "package": "llama_index.core.base.base_query_engine",
38
34
  "object": "BaseQueryEngine",
39
35
  "method": "aquery",
40
- "span_name": "llamaindex.query",
41
- "wrapper_method": atask_wrapper,
42
- "span_type": "workflow"
36
+ "wrapper_method": atask_wrapper
43
37
  },
44
38
  {
45
39
  "package": "llama_index.core.llms.custom",
46
40
  "object": "CustomLLM",
47
41
  "method": "chat",
48
- "span_name": "llamaindex.llmchat",
49
42
  "wrapper_method": task_wrapper,
50
43
  "output_processor": INFERENCE
51
44
  },
@@ -53,7 +46,6 @@ LLAMAINDEX_METHODS = [
53
46
  "package": "llama_index.core.llms.custom",
54
47
  "object": "CustomLLM",
55
48
  "method": "achat",
56
- "span_name": "llamaindex.llmchat",
57
49
  "wrapper_method": atask_wrapper,
58
50
  "output_processor": INFERENCE,
59
51
 
@@ -62,7 +54,6 @@ LLAMAINDEX_METHODS = [
62
54
  "package": "llama_index.llms.openai.base",
63
55
  "object": "OpenAI",
64
56
  "method": "chat",
65
- "span_name": "llamaindex.openai",
66
57
  "wrapper_method": task_wrapper,
67
58
  "output_processor": INFERENCE
68
59
  },
@@ -70,7 +61,6 @@ LLAMAINDEX_METHODS = [
70
61
  "package": "llama_index.llms.openai.base",
71
62
  "object": "OpenAI",
72
63
  "method": "achat",
73
- "span_name": "llamaindex.openai",
74
64
  "wrapper_method": atask_wrapper,
75
65
  "output_processor": INFERENCE
76
66
  },
@@ -78,7 +68,6 @@ LLAMAINDEX_METHODS = [
78
68
  "package": "llama_index.llms.mistralai.base",
79
69
  "object": "MistralAI",
80
70
  "method": "chat",
81
- "span_name": "llamaindex.mistralai",
82
71
  "wrapper_method": task_wrapper,
83
72
  "output_processor": INFERENCE
84
73
  },
@@ -86,7 +75,6 @@ LLAMAINDEX_METHODS = [
86
75
  "package": "llama_index.llms.mistralai.base",
87
76
  "object": "MistralAI",
88
77
  "method": "achat",
89
- "span_name": "llamaindex.mistralai",
90
78
  "wrapper_method": atask_wrapper,
91
79
  "output_processor": INFERENCE
92
80
  },
@@ -94,7 +82,6 @@ LLAMAINDEX_METHODS = [
94
82
  "package": "llama_index.core.agent",
95
83
  "object": "ReActAgent",
96
84
  "method": "chat",
97
- "span_name": "react.agent",
98
85
  "wrapper_method": task_wrapper,
99
86
  "output_processor": AGENT
100
87
  }
@@ -19,6 +19,10 @@ def extract_messages(kwargs):
19
19
  """Extract system and user messages"""
20
20
  try:
21
21
  messages = []
22
+ if 'instructions' in kwargs:
23
+ messages.append({'instructions': kwargs.get('instructions', {})})
24
+ if 'input' in kwargs:
25
+ messages.append({'input': kwargs.get('input', {})})
22
26
  if 'messages' in kwargs and len(kwargs['messages']) >0:
23
27
  for msg in kwargs['messages']:
24
28
  if msg.get('content') and msg.get('role'):
@@ -32,6 +36,8 @@ def extract_messages(kwargs):
32
36
 
33
37
  def extract_assistant_message(response):
34
38
  try:
39
+ if hasattr(response,"output_text") and len(response.output_text):
40
+ return response.output_text
35
41
  if response is not None and hasattr(response,"choices") and len(response.choices) >0:
36
42
  if hasattr(response.choices[0],"message"):
37
43
  return response.choices[0].message.content
@@ -85,10 +91,9 @@ def update_span_from_llm_response(response):
85
91
  response_metadata = response.response_metadata
86
92
  token_usage = response_metadata.get("token_usage")
87
93
  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)})
94
+ meta_dict.update({"completion_tokens": getattr(token_usage,"completion_tokens",None) or getattr(token_usage,"output_tokens",None)})
95
+ meta_dict.update({"prompt_tokens": getattr(token_usage, "prompt_tokens", None) or getattr(token_usage, "input_tokens", None)})
96
+ meta_dict.update({"total_tokens": getattr(token_usage,"total_tokens")})
92
97
  return meta_dict
93
98
 
94
99
  def extract_vector_input(vector_input: dict):
@@ -28,7 +28,6 @@ OPENAI_METHODS = [
28
28
  "object": "Embeddings",
29
29
  "method": "create",
30
30
  "wrapper_method": task_wrapper,
31
- "span_name": "openai_embeddings",
32
31
  "span_handler": "non_framework_handler",
33
32
  "output_processor": RETRIEVAL
34
33
  },
@@ -37,9 +36,24 @@ OPENAI_METHODS = [
37
36
  "object": "AsyncEmbeddings",
38
37
  "method": "create",
39
38
  "wrapper_method": atask_wrapper,
40
- "span_name": "openai_embeddings",
41
39
  "span_handler": "non_framework_handler",
42
40
  "output_processor": RETRIEVAL
41
+ },
42
+ {
43
+ "package": "openai.resources.responses",
44
+ "object": "Responses",
45
+ "method": "create",
46
+ "wrapper_method": task_wrapper,
47
+ "span_handler": "non_framework_handler",
48
+ "output_processor": INFERENCE
49
+ },
50
+ {
51
+ "package": "openai.resources.responses",
52
+ "object": "AsyncResponses",
53
+ "method": "create",
54
+ "wrapper_method": atask_wrapper,
55
+ "span_handler": "non_framework_handler",
56
+ "output_processor": INFERENCE
43
57
  }
44
58
 
45
59
  ]
@@ -2,6 +2,36 @@ import os
2
2
  from monocle_apptrace.instrumentation.metamodel.requests import allowed_urls
3
3
  from opentelemetry.propagate import inject
4
4
  from monocle_apptrace.instrumentation.common.span_handler import SpanHandler
5
+ from monocle_apptrace.instrumentation.common.utils import add_monocle_trace_state
6
+ from urllib.parse import urlparse, ParseResult
7
+
8
+
9
+ def get_route(kwargs):
10
+ url:str = kwargs['url']
11
+ parsed_url:ParseResult = urlparse(url)
12
+ return f"{parsed_url.netloc}{parsed_url.path}"
13
+
14
+ def get_method(kwargs) -> str:
15
+ return kwargs['method'] if 'method' in kwargs else 'GET'
16
+
17
+ def get_params(kwargs) -> dict:
18
+ url:str = kwargs['url']
19
+ parsed_url:ParseResult = urlparse(url)
20
+ return parsed_url.query
21
+
22
+ def get_headers(kwargs) -> dict:
23
+ return kwargs['headers'] if 'headers' in kwargs else {}
24
+
25
+ def get_body(kwargs) -> dict:
26
+ body = {}
27
+ return body
28
+
29
+ def extract_response(result) -> str:
30
+ return result.text if hasattr(result, 'text') else str(result)
31
+
32
+ def extract_status(result) -> str:
33
+ return f"{result.status_code} {result.reason}"
34
+
5
35
 
6
36
  def request_pre_task_processor(kwargs):
7
37
  # add traceparent to the request headers in kwargs
@@ -9,6 +39,7 @@ def request_pre_task_processor(kwargs):
9
39
  headers = {}
10
40
  else:
11
41
  headers = kwargs['headers'].copy()
42
+ add_monocle_trace_state(headers)
12
43
  inject(headers)
13
44
  kwargs['headers'] = headers
14
45
 
@@ -0,0 +1,51 @@
1
+ from monocle_apptrace.instrumentation.metamodel.requests import _helper
2
+ REQUEST_HTTP_PROCESSOR = {
3
+ "type": "http.send",
4
+ "attributes": [
5
+ [
6
+ {
7
+ "_comment": "request method, request URI",
8
+ "attribute": "method",
9
+ "accessor": lambda arguments: _helper.get_method(arguments['kwargs'])
10
+ },
11
+ {
12
+ "_comment": "request method, request URI",
13
+ "attribute": "URL",
14
+ "accessor": lambda arguments: _helper.get_route(arguments['kwargs'])
15
+ }
16
+
17
+ ]
18
+ ],
19
+ "events": [
20
+ {"name": "data.input",
21
+ "attributes": [
22
+ {
23
+ "_comment": "route params",
24
+ "attribute": "http.params",
25
+ "accessor": lambda arguments: _helper.get_params(arguments['kwargs'])
26
+ },
27
+ {
28
+ "_comment": "route body",
29
+ "attribute": "body",
30
+ "accessor": lambda arguments: _helper.get_body(arguments['kwargs'])
31
+ },
32
+
33
+ ]
34
+ },
35
+ {
36
+ "name": "data.output",
37
+ "attributes": [
38
+ {
39
+ "_comment": "status from HTTP response",
40
+ "attribute": "status",
41
+ "accessor": lambda arguments: _helper.extract_status(arguments['result'])
42
+ },
43
+ {
44
+ "_comment": "this is result from LLM",
45
+ "attribute": "response",
46
+ "accessor": lambda arguments: _helper.extract_response(arguments['result'])
47
+ }
48
+ ]
49
+ }
50
+ ]
51
+ }
@@ -1,12 +1,13 @@
1
1
  from monocle_apptrace.instrumentation.common.wrapper import task_wrapper
2
+ from monocle_apptrace.instrumentation.metamodel.requests.entities.http import REQUEST_HTTP_PROCESSOR
2
3
 
3
4
  REQUESTS_METHODS = [
4
5
  {
5
6
  "package": "requests.sessions",
6
7
  "object": "Session",
7
8
  "method": "request",
8
- "span_name": "http_requests",
9
9
  "wrapper_method": task_wrapper,
10
10
  "span_handler":"request_handler",
11
+ "output_processor": REQUEST_HTTP_PROCESSOR
11
12
  }
12
13
  ]