monocle-apptrace 0.5.0b1__py3-none-any.whl → 0.5.1b1__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/file_exporter.py +2 -1
- monocle_apptrace/instrumentation/common/__init__.py +7 -5
- monocle_apptrace/instrumentation/common/constants.py +103 -12
- monocle_apptrace/instrumentation/common/instrumentor.py +1 -6
- monocle_apptrace/instrumentation/common/method_wrappers.py +10 -125
- monocle_apptrace/instrumentation/common/scope_wrapper.py +126 -0
- monocle_apptrace/instrumentation/common/span_handler.py +32 -8
- monocle_apptrace/instrumentation/common/utils.py +34 -3
- monocle_apptrace/instrumentation/common/wrapper.py +208 -41
- monocle_apptrace/instrumentation/common/wrapper_method.py +9 -1
- monocle_apptrace/instrumentation/metamodel/a2a/entities/inference.py +3 -1
- monocle_apptrace/instrumentation/metamodel/adk/__init__.py +0 -0
- monocle_apptrace/instrumentation/metamodel/adk/_helper.py +206 -0
- monocle_apptrace/instrumentation/metamodel/adk/entities/agent.py +111 -0
- monocle_apptrace/instrumentation/metamodel/adk/entities/tool.py +59 -0
- monocle_apptrace/instrumentation/metamodel/adk/methods.py +31 -0
- monocle_apptrace/instrumentation/metamodel/agents/__init__.py +0 -0
- monocle_apptrace/instrumentation/metamodel/agents/_helper.py +225 -0
- monocle_apptrace/instrumentation/metamodel/agents/agents_processor.py +174 -0
- monocle_apptrace/instrumentation/metamodel/agents/entities/__init__.py +0 -0
- monocle_apptrace/instrumentation/metamodel/agents/entities/inference.py +196 -0
- monocle_apptrace/instrumentation/metamodel/agents/methods.py +55 -0
- monocle_apptrace/instrumentation/metamodel/aiohttp/entities/http.py +2 -1
- monocle_apptrace/instrumentation/metamodel/anthropic/_helper.py +82 -5
- monocle_apptrace/instrumentation/metamodel/anthropic/entities/inference.py +6 -1
- monocle_apptrace/instrumentation/metamodel/azfunc/entities/http.py +2 -1
- monocle_apptrace/instrumentation/metamodel/azureaiinference/entities/inference.py +2 -1
- monocle_apptrace/instrumentation/metamodel/botocore/entities/inference.py +2 -1
- monocle_apptrace/instrumentation/metamodel/fastapi/entities/http.py +2 -1
- monocle_apptrace/instrumentation/metamodel/fastapi/methods.py +18 -18
- monocle_apptrace/instrumentation/metamodel/finish_types.py +79 -1
- monocle_apptrace/instrumentation/metamodel/flask/entities/http.py +2 -1
- monocle_apptrace/instrumentation/metamodel/gemini/entities/inference.py +7 -3
- monocle_apptrace/instrumentation/metamodel/gemini/entities/retrieval.py +2 -1
- monocle_apptrace/instrumentation/metamodel/gemini/methods.py +8 -1
- monocle_apptrace/instrumentation/metamodel/haystack/_helper.py +64 -0
- monocle_apptrace/instrumentation/metamodel/haystack/entities/inference.py +12 -1
- monocle_apptrace/instrumentation/metamodel/haystack/entities/retrieval.py +2 -1
- monocle_apptrace/instrumentation/metamodel/lambdafunc/entities/http.py +2 -1
- monocle_apptrace/instrumentation/metamodel/langchain/_helper.py +18 -0
- monocle_apptrace/instrumentation/metamodel/langchain/entities/inference.py +6 -1
- monocle_apptrace/instrumentation/metamodel/langchain/entities/retrieval.py +2 -1
- monocle_apptrace/instrumentation/metamodel/langgraph/_helper.py +6 -0
- monocle_apptrace/instrumentation/metamodel/langgraph/entities/inference.py +10 -5
- monocle_apptrace/instrumentation/metamodel/langgraph/langgraph_processor.py +11 -4
- monocle_apptrace/instrumentation/metamodel/langgraph/methods.py +27 -23
- monocle_apptrace/instrumentation/metamodel/litellm/__init__.py +0 -0
- monocle_apptrace/instrumentation/metamodel/litellm/_helper.py +89 -0
- monocle_apptrace/instrumentation/metamodel/litellm/entities/__init__.py +0 -0
- monocle_apptrace/instrumentation/metamodel/litellm/entities/inference.py +109 -0
- monocle_apptrace/instrumentation/metamodel/litellm/methods.py +19 -0
- monocle_apptrace/instrumentation/metamodel/llamaindex/entities/agent.py +9 -4
- monocle_apptrace/instrumentation/metamodel/llamaindex/entities/inference.py +2 -1
- monocle_apptrace/instrumentation/metamodel/llamaindex/entities/retrieval.py +2 -1
- monocle_apptrace/instrumentation/metamodel/llamaindex/llamaindex_processor.py +14 -3
- monocle_apptrace/instrumentation/metamodel/llamaindex/methods.py +1 -1
- monocle_apptrace/instrumentation/metamodel/mcp/_helper.py +2 -1
- monocle_apptrace/instrumentation/metamodel/mcp/entities/inference.py +3 -1
- monocle_apptrace/instrumentation/metamodel/mcp/mcp_processor.py +0 -5
- monocle_apptrace/instrumentation/metamodel/mcp/methods.py +1 -1
- monocle_apptrace/instrumentation/metamodel/openai/_helper.py +110 -5
- monocle_apptrace/instrumentation/metamodel/openai/entities/inference.py +59 -13
- monocle_apptrace/instrumentation/metamodel/requests/entities/http.py +2 -1
- monocle_apptrace/instrumentation/metamodel/teamsai/_helper.py +12 -1
- monocle_apptrace/instrumentation/metamodel/teamsai/entities/inference/teamsai_output_processor.py +12 -1
- monocle_apptrace/mcp_server.py +94 -0
- {monocle_apptrace-0.5.0b1.dist-info → monocle_apptrace-0.5.1b1.dist-info}/METADATA +41 -11
- {monocle_apptrace-0.5.0b1.dist-info → monocle_apptrace-0.5.1b1.dist-info}/RECORD +72 -53
- monocle_apptrace-0.5.1b1.dist-info/entry_points.txt +2 -0
- {monocle_apptrace-0.5.0b1.dist-info → monocle_apptrace-0.5.1b1.dist-info}/WHEEL +0 -0
- {monocle_apptrace-0.5.0b1.dist-info → monocle_apptrace-0.5.1b1.dist-info}/licenses/LICENSE +0 -0
- {monocle_apptrace-0.5.0b1.dist-info → monocle_apptrace-0.5.1b1.dist-info}/licenses/NOTICE +0 -0
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
from os import linesep, path
|
|
4
4
|
from io import TextIOWrapper
|
|
5
5
|
from datetime import datetime
|
|
6
|
+
import os
|
|
6
7
|
from typing import Optional, Callable, Sequence, Dict, Tuple
|
|
7
8
|
from opentelemetry.sdk.trace import ReadableSpan
|
|
8
9
|
from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult
|
|
@@ -32,7 +33,7 @@ class FileSpanExporter(SpanExporterBase):
|
|
|
32
33
|
self.file_handles: Dict[int, Tuple[TextIOWrapper, str, datetime, bool]] = {}
|
|
33
34
|
self.formatter = formatter
|
|
34
35
|
self.service_name = service_name
|
|
35
|
-
self.output_path = out_path
|
|
36
|
+
self.output_path = os.getenv("MONOCLE_TRACE_OUTPUT_PATH", out_path)
|
|
36
37
|
self.file_prefix = file_prefix
|
|
37
38
|
self.time_format = time_format
|
|
38
39
|
self.task_processor = task_processor
|
|
@@ -2,16 +2,18 @@ from .instrumentor import (
|
|
|
2
2
|
setup_monocle_telemetry,
|
|
3
3
|
start_trace,
|
|
4
4
|
stop_trace,
|
|
5
|
-
start_scope,
|
|
6
|
-
stop_scope,
|
|
7
5
|
http_route_handler,
|
|
8
|
-
monocle_trace_scope,
|
|
9
|
-
amonocle_trace_scope,
|
|
10
|
-
monocle_trace_scope_method,
|
|
11
6
|
monocle_trace,
|
|
12
7
|
amonocle_trace,
|
|
13
8
|
monocle_trace_method,
|
|
14
9
|
monocle_trace_http_route,
|
|
15
10
|
is_valid_trace_id_uuid
|
|
16
11
|
)
|
|
12
|
+
from .scope_wrapper import (
|
|
13
|
+
start_scope,
|
|
14
|
+
stop_scope,
|
|
15
|
+
monocle_trace_scope,
|
|
16
|
+
amonocle_trace_scope,
|
|
17
|
+
monocle_trace_scope_method
|
|
18
|
+
)
|
|
17
19
|
from .utils import MonocleSpanException
|
|
@@ -26,7 +26,7 @@ service_type_map = {
|
|
|
26
26
|
AZURE_APP_SERVICE_ENV_NAME: AZURE_APP_SERVICE_NAME,
|
|
27
27
|
AZURE_FUNCTION_WORKER_ENV_NAME: AZURE_FUNCTION_NAME,
|
|
28
28
|
AWS_LAMBDA_ENV_NAME: AWS_LAMBDA_SERVICE_NAME,
|
|
29
|
-
GITHUB_CODESPACE_ENV_NAME: GITHUB_CODESPACE_SERVICE_NAME
|
|
29
|
+
GITHUB_CODESPACE_ENV_NAME: GITHUB_CODESPACE_SERVICE_NAME,
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
# Env variables to identify infra service name
|
|
@@ -35,7 +35,7 @@ service_name_map = {
|
|
|
35
35
|
AZURE_FUNCTION_NAME: AZURE_FUNCTION_IDENTIFIER_ENV_NAME,
|
|
36
36
|
AZURE_ML_SERVICE_NAME: AZURE_ML_ENDPOINT_ENV_NAME,
|
|
37
37
|
AWS_LAMBDA_SERVICE_NAME: AWS_LAMBDA_FUNCTION_IDENTIFIER_ENV_NAME,
|
|
38
|
-
GITHUB_CODESPACE_SERVICE_NAME: GITHUB_CODESPACE_IDENTIFIER_ENV_NAME
|
|
38
|
+
GITHUB_CODESPACE_SERVICE_NAME: GITHUB_CODESPACE_IDENTIFIER_ENV_NAME,
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
|
|
@@ -49,11 +49,11 @@ llm_type_map = {
|
|
|
49
49
|
"sagemakerllm": "aws_sagemaker",
|
|
50
50
|
"chatbedrock": "aws_bedrock",
|
|
51
51
|
"openaigenerator": "openai",
|
|
52
|
-
"bedrockruntime":"aws_bedrock",
|
|
53
|
-
"sagemakerruntime":"aws_sagemaker",
|
|
52
|
+
"bedrockruntime": "aws_bedrock",
|
|
53
|
+
"sagemakerruntime": "aws_sagemaker",
|
|
54
54
|
"anthropic": "anthropic",
|
|
55
|
-
"chatanthropic":"anthropic",
|
|
56
|
-
"anthropicchatgenerator":"anthropic",
|
|
55
|
+
"chatanthropic": "anthropic",
|
|
56
|
+
"anthropicchatgenerator": "anthropic",
|
|
57
57
|
"chatcompletionsclient": "azure_ai_inference",
|
|
58
58
|
"embeddingsclient": "azure_ai_inference",
|
|
59
59
|
"imageembeddingsclient": "azure_ai_inference",
|
|
@@ -61,6 +61,8 @@ llm_type_map = {
|
|
|
61
61
|
"googleaigeminichatgenerator": "gemini",
|
|
62
62
|
"gemini": "gemini",
|
|
63
63
|
"chatgooglegenerativeai": "gemini",
|
|
64
|
+
"azurechatcompletion": "azure_openai",
|
|
65
|
+
"openaichatcompletion": "openai",
|
|
64
66
|
}
|
|
65
67
|
|
|
66
68
|
MONOCLE_INSTRUMENTOR = "monocle_apptrace"
|
|
@@ -73,11 +75,11 @@ QUERY = "input"
|
|
|
73
75
|
RESPONSE = "response"
|
|
74
76
|
SESSION_PROPERTIES_KEY = "session"
|
|
75
77
|
INFRA_SERVICE_KEY = "infra_service_name"
|
|
76
|
-
META_DATA =
|
|
78
|
+
META_DATA = "metadata"
|
|
77
79
|
MONOCLE_SCOPE_NAME_PREFIX = "monocle.scope."
|
|
78
|
-
SCOPE_METHOD_LIST =
|
|
79
|
-
SCOPE_METHOD_FILE =
|
|
80
|
-
SCOPE_CONFIG_PATH =
|
|
80
|
+
SCOPE_METHOD_LIST = "MONOCLE_SCOPE_METHODS"
|
|
81
|
+
SCOPE_METHOD_FILE = "monocle_scopes.json"
|
|
82
|
+
SCOPE_CONFIG_PATH = "MONOCLE_SCOPE_CONFIG_PATH"
|
|
81
83
|
TRACE_PROPOGATION_URLS = "MONOCLE_TRACE_PROPAGATATION_URLS"
|
|
82
84
|
WORKFLOW_TYPE_KEY = "monocle.workflow_type"
|
|
83
85
|
ADD_NEW_WORKFLOW = "monocle.add_new_workflow"
|
|
@@ -85,5 +87,94 @@ WORKFLOW_TYPE_GENERIC = "workflow.generic"
|
|
|
85
87
|
MONOCLE_SDK_VERSION = "monocle_apptrace.version"
|
|
86
88
|
MONOCLE_SDK_LANGUAGE = "monocle_apptrace.language"
|
|
87
89
|
MONOCLE_DETECTED_SPAN_ERROR = "monocle_apptrace.detected_span_error"
|
|
88
|
-
HTTP_SUCCESS_CODES = (
|
|
89
|
-
CHILD_ERROR_CODE = "child.error.code"
|
|
90
|
+
HTTP_SUCCESS_CODES = ("200", "201", "202", "204", "205", "206")
|
|
91
|
+
CHILD_ERROR_CODE = "child.error.code"
|
|
92
|
+
|
|
93
|
+
AGENT_PREFIX_KEY = "monocle.agent.prefix"
|
|
94
|
+
|
|
95
|
+
# agentic sub types
|
|
96
|
+
INFERENCE_AGENT_DELEGATION = "delegation"
|
|
97
|
+
INFERENCE_TOOL_CALL = "tool_call"
|
|
98
|
+
INFERENCE_TURN_END = "turn_end"
|
|
99
|
+
|
|
100
|
+
SCOPE_NAME = "scope_name"
|
|
101
|
+
AGENT_INVOCATION_SPAN_NAME = "agentic.invocation"
|
|
102
|
+
AGENT_REQUEST_SPAN_NAME = "agentic.request"
|
|
103
|
+
|
|
104
|
+
AGENTIC_SPANS = [AGENT_INVOCATION_SPAN_NAME, AGENT_REQUEST_SPAN_NAME]
|
|
105
|
+
|
|
106
|
+
# Span sub types
|
|
107
|
+
|
|
108
|
+
## OPTIONAL right next to span.type, span.subtype:
|
|
109
|
+
## subtype is one perspective , are non overlapping, limitations: only one classification scheme for subtypes
|
|
110
|
+
# 1 planning
|
|
111
|
+
SPAN_SUBTYPE_PLANNING = "planning"
|
|
112
|
+
|
|
113
|
+
# 2 routing and selection INFERENCE_TOOL_CALL, INFERENCE_AGENT_DELEGATION
|
|
114
|
+
SPAN_SUBTYPE_ROUTING = "routing"
|
|
115
|
+
|
|
116
|
+
# 3 content processing
|
|
117
|
+
SPAN_SUBTYPE_CONTENT_PROCESSING = "content_processing"
|
|
118
|
+
|
|
119
|
+
# 4 content generation
|
|
120
|
+
SPAN_SUBTYPE_CONTENT_GENERATION = "content_generation"
|
|
121
|
+
|
|
122
|
+
# 5 communication INFERENCE_TURN_END
|
|
123
|
+
SPAN_SUBTYPE_COMMUNICATION = "communication"
|
|
124
|
+
|
|
125
|
+
# 6 transformations , if structured output
|
|
126
|
+
SPAN_SUBTYPE_TRANSFORMATIONS = "transformations"
|
|
127
|
+
|
|
128
|
+
# 7 domain specific,
|
|
129
|
+
SPAN_SUBTYPE_DOMAIN_SPECIFIC = "domain_specific"
|
|
130
|
+
|
|
131
|
+
# 8 generic (we may skip this property)
|
|
132
|
+
SPAN_SUBTYPE_GENERIC = "generic"
|
|
133
|
+
|
|
134
|
+
class SPAN_TYPES:
|
|
135
|
+
GENERIC = "generic"
|
|
136
|
+
AGENTIC_DELEGATION = "agentic.delegation"
|
|
137
|
+
AGENTIC_TOOL_INVOCATION = "agentic.tool.invocation"
|
|
138
|
+
AGENTIC_INVOCATION = "agentic.invocation"
|
|
139
|
+
AGENTIC_MCP_INVOCATION = "agentic.mcp.invocation"
|
|
140
|
+
AGENTIC_REQUEST = "agentic.request"
|
|
141
|
+
|
|
142
|
+
# http.process
|
|
143
|
+
HTTP_PROCESS = "http.process"
|
|
144
|
+
HTTP_SEND = "http.send"
|
|
145
|
+
|
|
146
|
+
RETRIEVAL = "retrieval"
|
|
147
|
+
INFERENCE = "inference"
|
|
148
|
+
INFERENCE_FRAMEWORK = "inference.framework"
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
class SPAN_SUBTYPES:
|
|
152
|
+
PLANNING = SPAN_SUBTYPE_PLANNING
|
|
153
|
+
ROUTING = SPAN_SUBTYPE_ROUTING
|
|
154
|
+
CONTENT_PROCESSING = SPAN_SUBTYPE_CONTENT_PROCESSING
|
|
155
|
+
CONTENT_GENERATION = SPAN_SUBTYPE_CONTENT_GENERATION
|
|
156
|
+
COMMUNICATION = SPAN_SUBTYPE_COMMUNICATION
|
|
157
|
+
TRANSFORMATIONS = SPAN_SUBTYPE_TRANSFORMATIONS
|
|
158
|
+
DOMAIN_SPECIFIC = SPAN_SUBTYPE_DOMAIN_SPECIFIC
|
|
159
|
+
GENERIC = SPAN_SUBTYPE_GENERIC
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
MAP_ATTRIBUTES_TO_SPAN_SUBTYPE = {
|
|
163
|
+
# inference attributes
|
|
164
|
+
INFERENCE_AGENT_DELEGATION: SPAN_SUBTYPES.ROUTING,
|
|
165
|
+
INFERENCE_TOOL_CALL: SPAN_SUBTYPES.ROUTING,
|
|
166
|
+
INFERENCE_TURN_END: SPAN_SUBTYPES.COMMUNICATION,
|
|
167
|
+
|
|
168
|
+
# agentic span.types
|
|
169
|
+
SPAN_TYPES.AGENTIC_DELEGATION: SPAN_SUBTYPES.ROUTING,
|
|
170
|
+
SPAN_TYPES.AGENTIC_TOOL_INVOCATION: SPAN_SUBTYPES.ROUTING,
|
|
171
|
+
SPAN_TYPES.AGENTIC_INVOCATION: SPAN_SUBTYPES.ROUTING,
|
|
172
|
+
SPAN_TYPES.AGENTIC_MCP_INVOCATION: SPAN_SUBTYPES.ROUTING,
|
|
173
|
+
|
|
174
|
+
# MAYBE?
|
|
175
|
+
# agentic span.types
|
|
176
|
+
SPAN_TYPES.AGENTIC_REQUEST: SPAN_SUBTYPES.PLANNING,
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
}
|
|
@@ -235,17 +235,12 @@ def is_valid_trace_id_uuid(traceId: str) -> bool:
|
|
|
235
235
|
return False
|
|
236
236
|
|
|
237
237
|
from monocle_apptrace.instrumentation.common.method_wrappers import (
|
|
238
|
-
monocle_trace_scope_method,
|
|
239
|
-
amonocle_trace_scope,
|
|
240
238
|
monocle_trace,
|
|
241
239
|
amonocle_trace,
|
|
242
240
|
monocle_trace_method,
|
|
243
241
|
monocle_trace_http_route,
|
|
244
|
-
start_scope,
|
|
245
|
-
stop_scope,
|
|
246
242
|
start_trace,
|
|
247
243
|
stop_trace,
|
|
248
|
-
http_route_handler
|
|
249
|
-
monocle_trace_scope,
|
|
244
|
+
http_route_handler
|
|
250
245
|
)
|
|
251
246
|
|
|
@@ -5,14 +5,13 @@ from functools import wraps
|
|
|
5
5
|
import inspect
|
|
6
6
|
from opentelemetry.context import attach, get_current, detach
|
|
7
7
|
from opentelemetry.sdk.trace import Span
|
|
8
|
-
from opentelemetry.
|
|
8
|
+
from opentelemetry.trace.span import INVALID_SPAN
|
|
9
9
|
from opentelemetry.trace import get_tracer
|
|
10
|
-
from opentelemetry.trace.propagation import set_span_in_context, _SPAN_KEY
|
|
11
10
|
from contextlib import contextmanager, asynccontextmanager
|
|
12
11
|
from monocle_apptrace.instrumentation.common.span_handler import SpanHandler
|
|
13
|
-
from monocle_apptrace.instrumentation.common.wrapper import atask_wrapper, task_wrapper
|
|
12
|
+
from monocle_apptrace.instrumentation.common.wrapper import atask_wrapper, get_current_monocle_span, set_monocle_span_in_context, task_wrapper
|
|
14
13
|
from monocle_apptrace.instrumentation.common.utils import (
|
|
15
|
-
|
|
14
|
+
http_route_handler, http_async_route_handler
|
|
16
15
|
)
|
|
17
16
|
from monocle_apptrace.instrumentation.common.constants import MONOCLE_INSTRUMENTOR
|
|
18
17
|
from monocle_apptrace.instrumentation.common.instrumentor import get_tracer_provider
|
|
@@ -48,7 +47,7 @@ def start_trace(
|
|
|
48
47
|
tracer = get_tracer(instrumenting_module_name= MONOCLE_INSTRUMENTOR, tracer_provider= get_tracer_provider())
|
|
49
48
|
span_name = span_name or "custom_span"
|
|
50
49
|
span = tracer.start_span(name=span_name)
|
|
51
|
-
updated_span_context =
|
|
50
|
+
updated_span_context = set_monocle_span_in_context(span=span)
|
|
52
51
|
|
|
53
52
|
# Set default monocle attributes
|
|
54
53
|
SpanHandler.set_default_monocle_attributes(span)
|
|
@@ -57,7 +56,7 @@ def start_trace(
|
|
|
57
56
|
|
|
58
57
|
# Set custom attributes and events using common method
|
|
59
58
|
_setup_span_attributes_and_events(span, attributes, events)
|
|
60
|
-
|
|
59
|
+
|
|
61
60
|
token = attach(updated_span_context)
|
|
62
61
|
return token
|
|
63
62
|
except Exception as e:
|
|
@@ -81,62 +80,16 @@ def stop_trace(
|
|
|
81
80
|
None
|
|
82
81
|
"""
|
|
83
82
|
try:
|
|
84
|
-
|
|
85
|
-
if
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
_setup_span_attributes_and_events(parent_span, final_attributes, final_events)
|
|
90
|
-
|
|
91
|
-
parent_span.end()
|
|
83
|
+
parent_span = get_current_monocle_span()
|
|
84
|
+
if parent_span is not None and parent_span != INVALID_SPAN:
|
|
85
|
+
# Set final attributes and events using common method
|
|
86
|
+
_setup_span_attributes_and_events(parent_span, final_attributes, final_events)
|
|
87
|
+
parent_span.end()
|
|
92
88
|
if token is not None:
|
|
93
89
|
detach(token)
|
|
94
90
|
except Exception as e:
|
|
95
91
|
logger.warning(f"Failed to stop trace: {e}")
|
|
96
92
|
|
|
97
|
-
def start_scope(
|
|
98
|
-
scope_name: str,
|
|
99
|
-
scope_value: Optional[str] = None
|
|
100
|
-
) -> object:
|
|
101
|
-
"""
|
|
102
|
-
Start a new scope with the given name and optional value. If no value is provided, a random UUID will be generated.
|
|
103
|
-
All the spans, across traces created after this call will have the scope attached until the scope is stopped.
|
|
104
|
-
|
|
105
|
-
Args:
|
|
106
|
-
scope_name: The name of the scope.
|
|
107
|
-
scope_value: Optional value of the scope. If None, a random UUID will be generated.
|
|
108
|
-
|
|
109
|
-
Returns:
|
|
110
|
-
Token: A token representing the attached context for the scope. This token is to be used later to stop the current scope.
|
|
111
|
-
"""
|
|
112
|
-
try:
|
|
113
|
-
# Set the scope using existing utility
|
|
114
|
-
token = set_scope(scope_name, scope_value)
|
|
115
|
-
return token
|
|
116
|
-
except Exception as e:
|
|
117
|
-
logger.warning(f"Failed to start scope: {e}")
|
|
118
|
-
return None
|
|
119
|
-
|
|
120
|
-
def stop_scope(
|
|
121
|
-
token: object
|
|
122
|
-
) -> None:
|
|
123
|
-
"""
|
|
124
|
-
Stop the active scope. All the spans created after this will not have the scope attached.
|
|
125
|
-
|
|
126
|
-
Args:
|
|
127
|
-
token: The token that was returned when the scope was started.
|
|
128
|
-
|
|
129
|
-
Returns:
|
|
130
|
-
None
|
|
131
|
-
"""
|
|
132
|
-
try:
|
|
133
|
-
# Remove the scope
|
|
134
|
-
remove_scope(token)
|
|
135
|
-
except Exception as e:
|
|
136
|
-
logger.warning(f"Failed to stop scope: {e}")
|
|
137
|
-
return
|
|
138
|
-
|
|
139
|
-
|
|
140
93
|
@contextmanager
|
|
141
94
|
def monocle_trace(
|
|
142
95
|
span_name: Optional[str] = None,
|
|
@@ -211,74 +164,6 @@ async def amonocle_trace(
|
|
|
211
164
|
logger.warning(f"Failed in amonocle_trace: {e}")
|
|
212
165
|
yield # Still yield to not break the context manager
|
|
213
166
|
|
|
214
|
-
@contextmanager
|
|
215
|
-
def monocle_trace_scope(
|
|
216
|
-
scope_name: str,
|
|
217
|
-
scope_value: Optional[str] = None
|
|
218
|
-
):
|
|
219
|
-
"""
|
|
220
|
-
Context manager to start and stop a scope. All the spans, across traces created within the encapsulated code will have the scope attached.
|
|
221
|
-
|
|
222
|
-
Args:
|
|
223
|
-
scope_name: The name of the scope.
|
|
224
|
-
scope_value: Optional value of the scope. If None, a random UUID will be generated.
|
|
225
|
-
"""
|
|
226
|
-
token = start_scope(scope_name, scope_value)
|
|
227
|
-
try:
|
|
228
|
-
yield
|
|
229
|
-
finally:
|
|
230
|
-
stop_scope(token)
|
|
231
|
-
|
|
232
|
-
@asynccontextmanager
|
|
233
|
-
async def amonocle_trace_scope(
|
|
234
|
-
scope_name: str,
|
|
235
|
-
scope_value: Optional[str] = None
|
|
236
|
-
):
|
|
237
|
-
"""
|
|
238
|
-
Async context manager to start and stop a scope. All the spans, across traces created within the encapsulated code will have the scope attached.
|
|
239
|
-
|
|
240
|
-
Args:
|
|
241
|
-
scope_name: The name of the scope.
|
|
242
|
-
scope_value: Optional value of the scope. If None, a random UUID will be generated.
|
|
243
|
-
"""
|
|
244
|
-
token = start_scope(scope_name, scope_value)
|
|
245
|
-
try:
|
|
246
|
-
yield
|
|
247
|
-
finally:
|
|
248
|
-
stop_scope(token)
|
|
249
|
-
|
|
250
|
-
def monocle_trace_scope_method(
|
|
251
|
-
scope_name: str,
|
|
252
|
-
scope_value: Optional[str] = None
|
|
253
|
-
):
|
|
254
|
-
"""
|
|
255
|
-
Decorator to start and stop a scope for a method. All the spans, across traces created in the method will have the scope attached.
|
|
256
|
-
|
|
257
|
-
Args:
|
|
258
|
-
scope_name: The name of the scope.
|
|
259
|
-
scope_value: Optional value of the scope. If None, a random UUID will be generated.
|
|
260
|
-
"""
|
|
261
|
-
def decorator(func):
|
|
262
|
-
if inspect.iscoroutinefunction(func):
|
|
263
|
-
@wraps(func)
|
|
264
|
-
async def wrapper(*args, **kwargs):
|
|
265
|
-
async with amonocle_trace_scope(
|
|
266
|
-
scope_name, scope_value
|
|
267
|
-
):
|
|
268
|
-
result = await func(*args, **kwargs)
|
|
269
|
-
return result
|
|
270
|
-
return wrapper
|
|
271
|
-
else:
|
|
272
|
-
@wraps(func)
|
|
273
|
-
def wrapper(*args, **kwargs):
|
|
274
|
-
with monocle_trace_scope(
|
|
275
|
-
scope_name, scope_value
|
|
276
|
-
):
|
|
277
|
-
result = func(*args, **kwargs)
|
|
278
|
-
return result
|
|
279
|
-
return wrapper
|
|
280
|
-
return decorator
|
|
281
|
-
|
|
282
167
|
def monocle_trace_method(
|
|
283
168
|
span_name: Optional[str] = None
|
|
284
169
|
):
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import inspect
|
|
3
|
+
from typing import Dict, List, Optional, Any
|
|
4
|
+
from functools import wraps
|
|
5
|
+
from contextlib import contextmanager, asynccontextmanager
|
|
6
|
+
from opentelemetry.context import Context
|
|
7
|
+
from monocle_apptrace.instrumentation.common.utils import (
|
|
8
|
+
set_scope, remove_scope, http_route_handler, http_async_route_handler
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
logger = logging.getLogger(__name__)
|
|
12
|
+
|
|
13
|
+
def start_scope(
|
|
14
|
+
scope_name: str,
|
|
15
|
+
scope_value: Optional[str] = None,
|
|
16
|
+
context: Optional[Context] = None
|
|
17
|
+
) -> object:
|
|
18
|
+
"""
|
|
19
|
+
Start a new scope with the given name and optional value. If no value is provided, a random UUID will be generated.
|
|
20
|
+
All the spans, across traces created after this call will have the scope attached until the scope is stopped.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
scope_name: The name of the scope.
|
|
24
|
+
scope_value: Optional value of the scope. If None, a random UUID will be generated.
|
|
25
|
+
|
|
26
|
+
Returns:
|
|
27
|
+
Token: A token representing the attached context for the scope. This token is to be used later to stop the current scope.
|
|
28
|
+
"""
|
|
29
|
+
try:
|
|
30
|
+
# Set the scope using existing utility
|
|
31
|
+
token = set_scope(scope_name, scope_value, context)
|
|
32
|
+
return token
|
|
33
|
+
except Exception as e:
|
|
34
|
+
logger.warning(f"Failed to start scope: {e}")
|
|
35
|
+
return None
|
|
36
|
+
|
|
37
|
+
def stop_scope(
|
|
38
|
+
token: object
|
|
39
|
+
) -> None:
|
|
40
|
+
"""
|
|
41
|
+
Stop the active scope. All the spans created after this will not have the scope attached.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
token: The token that was returned when the scope was started.
|
|
45
|
+
|
|
46
|
+
Returns:
|
|
47
|
+
None
|
|
48
|
+
"""
|
|
49
|
+
try:
|
|
50
|
+
# Remove the scope
|
|
51
|
+
remove_scope(token)
|
|
52
|
+
except Exception as e:
|
|
53
|
+
logger.warning(f"Failed to stop scope: {e}")
|
|
54
|
+
return
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@contextmanager
|
|
59
|
+
def monocle_trace_scope(
|
|
60
|
+
scope_name: str,
|
|
61
|
+
scope_value: Optional[str] = None
|
|
62
|
+
):
|
|
63
|
+
"""
|
|
64
|
+
Context manager to start and stop a scope. All the spans, across traces created within the encapsulated code will have the scope attached.
|
|
65
|
+
|
|
66
|
+
Args:
|
|
67
|
+
scope_name: The name of the scope.
|
|
68
|
+
scope_value: Optional value of the scope. If None, a random UUID will be generated.
|
|
69
|
+
"""
|
|
70
|
+
token = None
|
|
71
|
+
if scope_name:
|
|
72
|
+
token = start_scope(scope_name, scope_value)
|
|
73
|
+
try:
|
|
74
|
+
yield
|
|
75
|
+
finally:
|
|
76
|
+
stop_scope(token)
|
|
77
|
+
|
|
78
|
+
@asynccontextmanager
|
|
79
|
+
async def amonocle_trace_scope(
|
|
80
|
+
scope_name: str,
|
|
81
|
+
scope_value: Optional[str] = None
|
|
82
|
+
):
|
|
83
|
+
"""
|
|
84
|
+
Async context manager to start and stop a scope. All the spans, across traces created within the encapsulated code will have the scope attached.
|
|
85
|
+
|
|
86
|
+
Args:
|
|
87
|
+
scope_name: The name of the scope.
|
|
88
|
+
scope_value: Optional value of the scope. If None, a random UUID will be generated.
|
|
89
|
+
"""
|
|
90
|
+
token = start_scope(scope_name, scope_value)
|
|
91
|
+
try:
|
|
92
|
+
yield
|
|
93
|
+
finally:
|
|
94
|
+
stop_scope(token)
|
|
95
|
+
|
|
96
|
+
def monocle_trace_scope_method(
|
|
97
|
+
scope_name: str,
|
|
98
|
+
scope_value: Optional[str] = None
|
|
99
|
+
):
|
|
100
|
+
"""
|
|
101
|
+
Decorator to start and stop a scope for a method. All the spans, across traces created in the method will have the scope attached.
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
scope_name: The name of the scope.
|
|
105
|
+
scope_value: Optional value of the scope. If None, a random UUID will be generated.
|
|
106
|
+
"""
|
|
107
|
+
def decorator(func):
|
|
108
|
+
if inspect.iscoroutinefunction(func):
|
|
109
|
+
@wraps(func)
|
|
110
|
+
async def wrapper(*args, **kwargs):
|
|
111
|
+
async with amonocle_trace_scope(
|
|
112
|
+
scope_name, scope_value
|
|
113
|
+
):
|
|
114
|
+
result = await func(*args, **kwargs)
|
|
115
|
+
return result
|
|
116
|
+
return wrapper
|
|
117
|
+
else:
|
|
118
|
+
@wraps(func)
|
|
119
|
+
def wrapper(*args, **kwargs):
|
|
120
|
+
with monocle_trace_scope(
|
|
121
|
+
scope_name, scope_value
|
|
122
|
+
):
|
|
123
|
+
result = func(*args, **kwargs)
|
|
124
|
+
return result
|
|
125
|
+
return wrapper
|
|
126
|
+
return decorator
|
|
@@ -25,6 +25,7 @@ WORKFLOW_TYPE_MAP = {
|
|
|
25
25
|
"openai": "workflow.openai",
|
|
26
26
|
"anthropic": "workflow.anthropic",
|
|
27
27
|
"gemini": "workflow.gemini",
|
|
28
|
+
"litellm": "workflow.litellm",
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
FRAMEWORK_WORKFLOW_LIST = [
|
|
@@ -32,6 +33,7 @@ FRAMEWORK_WORKFLOW_LIST = [
|
|
|
32
33
|
"workflow.langchain",
|
|
33
34
|
"workflow.haystack",
|
|
34
35
|
"workflow.teams_ai",
|
|
36
|
+
"workflow.litellm",
|
|
35
37
|
]
|
|
36
38
|
class SpanHandler:
|
|
37
39
|
|
|
@@ -63,11 +65,16 @@ class SpanHandler:
|
|
|
63
65
|
span.set_attribute("span.type", span_type)
|
|
64
66
|
else:
|
|
65
67
|
logger.warning("type of span not found or incorrect written in entity json")
|
|
68
|
+
if "subtype" in output_processor:
|
|
69
|
+
span.set_attribute("span.subtype", output_processor["subtype"])
|
|
66
70
|
return span_type
|
|
67
71
|
|
|
68
72
|
def pre_task_processing(self, to_wrap, wrapped, instance, args,kwargs, span):
|
|
69
|
-
|
|
70
|
-
|
|
73
|
+
try:
|
|
74
|
+
if "pipeline" in to_wrap['package']:
|
|
75
|
+
set_attribute(QUERY, args[0]['prompt_builder']['question'])
|
|
76
|
+
except Exception as e:
|
|
77
|
+
logger.warning("Warning: Error occurred in pre_task_processing: %s", str(e))
|
|
71
78
|
|
|
72
79
|
@staticmethod
|
|
73
80
|
def set_default_monocle_attributes(span: Span, source_path = "" ):
|
|
@@ -94,6 +101,17 @@ class SpanHandler:
|
|
|
94
101
|
def post_task_processing(self, to_wrap, wrapped, instance, args, kwargs, result, ex, span:Span, parent_span:Span):
|
|
95
102
|
pass
|
|
96
103
|
|
|
104
|
+
def should_skip(self, processor, instance, args, kwargs) -> bool:
|
|
105
|
+
should_skip = False
|
|
106
|
+
accessor = processor.get('should_skip')
|
|
107
|
+
if accessor:
|
|
108
|
+
arguments = {"instance":instance, "args":args, "kwargs":kwargs}
|
|
109
|
+
should_skip = accessor(arguments)
|
|
110
|
+
if not isinstance(should_skip, bool):
|
|
111
|
+
logger.warning("Warning: 'should_skip' accessor did not return a boolean value")
|
|
112
|
+
return True
|
|
113
|
+
return should_skip
|
|
114
|
+
|
|
97
115
|
def hydrate_span(self, to_wrap, wrapped, instance, args, kwargs, result, span, parent_span = None, ex:Exception = None) -> bool:
|
|
98
116
|
try:
|
|
99
117
|
detected_error_in_attribute = self.hydrate_attributes(to_wrap, wrapped, instance, args, kwargs, result, span, parent_span)
|
|
@@ -145,7 +163,7 @@ class SpanHandler:
|
|
|
145
163
|
span.set_attribute("entity.count", span_index)
|
|
146
164
|
return detected_error
|
|
147
165
|
|
|
148
|
-
def hydrate_events(self, to_wrap, wrapped, instance, args, kwargs, ret_result, span, parent_span=None, ex:Exception=None) -> bool:
|
|
166
|
+
def hydrate_events(self, to_wrap, wrapped, instance, args, kwargs, ret_result, span: Span, parent_span=None, ex:Exception=None) -> bool:
|
|
149
167
|
detected_error:bool = False
|
|
150
168
|
if 'output_processor' in to_wrap and to_wrap["output_processor"] is not None:
|
|
151
169
|
output_processor=to_wrap['output_processor']
|
|
@@ -181,10 +199,16 @@ class SpanHandler:
|
|
|
181
199
|
except Exception as e:
|
|
182
200
|
logger.debug(f"Error evaluating accessor for attribute '{attribute_key}': {e}")
|
|
183
201
|
matching_timestamp = getattr(ret_result, "timestamps", {}).get(event_name, None)
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
202
|
+
alreadyExist = False
|
|
203
|
+
for existing_event in span.events:
|
|
204
|
+
if event_name == existing_event.name:
|
|
205
|
+
existing_event.attributes._dict.update(event_attributes)
|
|
206
|
+
alreadyExist = True
|
|
207
|
+
if not alreadyExist:
|
|
208
|
+
if isinstance(matching_timestamp, int):
|
|
209
|
+
span.add_event(name=event_name, attributes=event_attributes, timestamp=matching_timestamp)
|
|
210
|
+
else:
|
|
211
|
+
span.add_event(name=event_name, attributes=event_attributes)
|
|
188
212
|
return detected_error
|
|
189
213
|
|
|
190
214
|
@staticmethod
|
|
@@ -215,7 +239,7 @@ class SpanHandler:
|
|
|
215
239
|
if to_wrap is not None:
|
|
216
240
|
package_name = to_wrap.get('package')
|
|
217
241
|
for (package, framework_workflow_type) in WORKFLOW_TYPE_MAP.items():
|
|
218
|
-
if (package_name is not None and package
|
|
242
|
+
if (package_name is not None and package_name.startswith(package)):
|
|
219
243
|
workflow_type = framework_workflow_type
|
|
220
244
|
break
|
|
221
245
|
return workflow_type
|
|
@@ -11,6 +11,8 @@ from opentelemetry.propagate import extract
|
|
|
11
11
|
from opentelemetry import baggage
|
|
12
12
|
from monocle_apptrace.instrumentation.common.constants import MONOCLE_SCOPE_NAME_PREFIX, SCOPE_METHOD_FILE, SCOPE_CONFIG_PATH, llm_type_map, MONOCLE_SDK_VERSION, ADD_NEW_WORKFLOW
|
|
13
13
|
from importlib.metadata import version
|
|
14
|
+
from opentelemetry.trace.span import INVALID_SPAN
|
|
15
|
+
_MONOCLE_SPAN_KEY = "monocle" + _SPAN_KEY
|
|
14
16
|
|
|
15
17
|
T = TypeVar('T')
|
|
16
18
|
U = TypeVar('U')
|
|
@@ -188,8 +190,8 @@ def __generate_scope_id() -> str:
|
|
|
188
190
|
global scope_id_generator
|
|
189
191
|
return f"{hex(scope_id_generator.generate_trace_id())}"
|
|
190
192
|
|
|
191
|
-
def set_scope(scope_name: str, scope_value:str = None) -> object:
|
|
192
|
-
return set_scopes({scope_name: scope_value})
|
|
193
|
+
def set_scope(scope_name: str, scope_value:str = None, context:Context = None) -> object:
|
|
194
|
+
return set_scopes({scope_name: scope_value}, context)
|
|
193
195
|
|
|
194
196
|
def set_scopes(scopes:dict[str, object], baggage_context:Context = None) -> object:
|
|
195
197
|
if baggage_context is None:
|
|
@@ -428,4 +430,33 @@ def patch_instance_method(obj, method_name, func):
|
|
|
428
430
|
new_cls = type(f"Patched{cls.__name__}", (cls,), {
|
|
429
431
|
method_name: func
|
|
430
432
|
})
|
|
431
|
-
obj.__class__ = new_cls
|
|
433
|
+
obj.__class__ = new_cls
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
def set_monocle_span_in_context(
|
|
437
|
+
span: Span, context: Optional[Context] = None
|
|
438
|
+
) -> Context:
|
|
439
|
+
"""Set the span in the given context.
|
|
440
|
+
|
|
441
|
+
Args:
|
|
442
|
+
span: The Span to set.
|
|
443
|
+
context: a Context object. if one is not passed, the
|
|
444
|
+
default current context is used instead.
|
|
445
|
+
"""
|
|
446
|
+
ctx = set_value(_MONOCLE_SPAN_KEY, span, context=context)
|
|
447
|
+
return ctx
|
|
448
|
+
|
|
449
|
+
def get_current_monocle_span(context: Optional[Context] = None) -> Span:
|
|
450
|
+
"""Retrieve the current span.
|
|
451
|
+
|
|
452
|
+
Args:
|
|
453
|
+
context: A Context object. If one is not passed, the
|
|
454
|
+
default current context is used instead.
|
|
455
|
+
|
|
456
|
+
Returns:
|
|
457
|
+
The Span set in the context if it exists. INVALID_SPAN otherwise.
|
|
458
|
+
"""
|
|
459
|
+
span = get_value(_MONOCLE_SPAN_KEY, context=context)
|
|
460
|
+
if span is None or not isinstance(span, Span):
|
|
461
|
+
return INVALID_SPAN
|
|
462
|
+
return span
|