monocle-apptrace 0.5.0__py3-none-any.whl → 0.5.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.
- monocle_apptrace/instrumentation/common/__init__.py +7 -5
- monocle_apptrace/instrumentation/common/constants.py +95 -12
- monocle_apptrace/instrumentation/common/instrumentor.py +1 -6
- monocle_apptrace/instrumentation/common/method_wrappers.py +1 -112
- monocle_apptrace/instrumentation/common/scope_wrapper.py +126 -0
- monocle_apptrace/instrumentation/common/span_handler.py +13 -0
- monocle_apptrace/instrumentation/common/utils.py +2 -2
- monocle_apptrace/instrumentation/common/wrapper.py +113 -51
- monocle_apptrace/instrumentation/metamodel/a2a/entities/inference.py +3 -1
- monocle_apptrace/instrumentation/metamodel/adk/_helper.py +24 -0
- monocle_apptrace/instrumentation/metamodel/adk/entities/agent.py +62 -1
- monocle_apptrace/instrumentation/metamodel/adk/entities/tool.py +3 -1
- monocle_apptrace/instrumentation/metamodel/adk/methods.py +9 -2
- monocle_apptrace/instrumentation/metamodel/agents/_helper.py +5 -0
- monocle_apptrace/instrumentation/metamodel/agents/agents_processor.py +30 -8
- monocle_apptrace/instrumentation/metamodel/agents/entities/inference.py +10 -5
- monocle_apptrace/instrumentation/metamodel/agents/methods.py +3 -4
- monocle_apptrace/instrumentation/metamodel/aiohttp/entities/http.py +2 -1
- monocle_apptrace/instrumentation/metamodel/anthropic/_helper.py +3 -3
- monocle_apptrace/instrumentation/metamodel/anthropic/entities/inference.py +2 -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/finish_types.py +2 -0
- monocle_apptrace/instrumentation/metamodel/flask/entities/http.py +2 -1
- monocle_apptrace/instrumentation/metamodel/gemini/entities/inference.py +2 -1
- monocle_apptrace/instrumentation/metamodel/gemini/entities/retrieval.py +2 -1
- monocle_apptrace/instrumentation/metamodel/haystack/entities/inference.py +2 -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 +2 -2
- monocle_apptrace/instrumentation/metamodel/langchain/entities/inference.py +2 -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 +8 -4
- monocle_apptrace/instrumentation/metamodel/langgraph/methods.py +2 -0
- monocle_apptrace/instrumentation/metamodel/litellm/entities/inference.py +2 -1
- 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/mcp/_helper.py +2 -1
- monocle_apptrace/instrumentation/metamodel/mcp/entities/inference.py +3 -1
- monocle_apptrace/instrumentation/metamodel/mcp/methods.py +1 -1
- monocle_apptrace/instrumentation/metamodel/openai/_helper.py +2 -2
- monocle_apptrace/instrumentation/metamodel/openai/entities/inference.py +2 -1
- monocle_apptrace/instrumentation/metamodel/requests/entities/http.py +2 -1
- monocle_apptrace/instrumentation/metamodel/teamsai/entities/inference/teamsai_output_processor.py +2 -1
- {monocle_apptrace-0.5.0.dist-info → monocle_apptrace-0.5.1.dist-info}/METADATA +1 -1
- {monocle_apptrace-0.5.0.dist-info → monocle_apptrace-0.5.1.dist-info}/RECORD +54 -53
- {monocle_apptrace-0.5.0.dist-info → monocle_apptrace-0.5.1.dist-info}/WHEEL +0 -0
- {monocle_apptrace-0.5.0.dist-info → monocle_apptrace-0.5.1.dist-info}/licenses/LICENSE +0 -0
- {monocle_apptrace-0.5.0.dist-info → monocle_apptrace-0.5.1.dist-info}/licenses/NOTICE +0 -0
|
@@ -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",
|
|
@@ -75,11 +75,11 @@ QUERY = "input"
|
|
|
75
75
|
RESPONSE = "response"
|
|
76
76
|
SESSION_PROPERTIES_KEY = "session"
|
|
77
77
|
INFRA_SERVICE_KEY = "infra_service_name"
|
|
78
|
-
META_DATA =
|
|
78
|
+
META_DATA = "metadata"
|
|
79
79
|
MONOCLE_SCOPE_NAME_PREFIX = "monocle.scope."
|
|
80
|
-
SCOPE_METHOD_LIST =
|
|
81
|
-
SCOPE_METHOD_FILE =
|
|
82
|
-
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"
|
|
83
83
|
TRACE_PROPOGATION_URLS = "MONOCLE_TRACE_PROPAGATATION_URLS"
|
|
84
84
|
WORKFLOW_TYPE_KEY = "monocle.workflow_type"
|
|
85
85
|
ADD_NEW_WORKFLOW = "monocle.add_new_workflow"
|
|
@@ -87,11 +87,94 @@ WORKFLOW_TYPE_GENERIC = "workflow.generic"
|
|
|
87
87
|
MONOCLE_SDK_VERSION = "monocle_apptrace.version"
|
|
88
88
|
MONOCLE_SDK_LANGUAGE = "monocle_apptrace.language"
|
|
89
89
|
MONOCLE_DETECTED_SPAN_ERROR = "monocle_apptrace.detected_span_error"
|
|
90
|
-
HTTP_SUCCESS_CODES = (
|
|
90
|
+
HTTP_SUCCESS_CODES = ("200", "201", "202", "204", "205", "206")
|
|
91
91
|
CHILD_ERROR_CODE = "child.error.code"
|
|
92
92
|
|
|
93
93
|
AGENT_PREFIX_KEY = "monocle.agent.prefix"
|
|
94
94
|
|
|
95
|
+
# agentic sub types
|
|
95
96
|
INFERENCE_AGENT_DELEGATION = "delegation"
|
|
96
97
|
INFERENCE_TOOL_CALL = "tool_call"
|
|
97
|
-
|
|
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
|
|
|
@@ -11,7 +11,7 @@ from contextlib import contextmanager, asynccontextmanager
|
|
|
11
11
|
from monocle_apptrace.instrumentation.common.span_handler import SpanHandler
|
|
12
12
|
from monocle_apptrace.instrumentation.common.wrapper import atask_wrapper, get_current_monocle_span, set_monocle_span_in_context, task_wrapper
|
|
13
13
|
from monocle_apptrace.instrumentation.common.utils import (
|
|
14
|
-
|
|
14
|
+
http_route_handler, http_async_route_handler
|
|
15
15
|
)
|
|
16
16
|
from monocle_apptrace.instrumentation.common.constants import MONOCLE_INSTRUMENTOR
|
|
17
17
|
from monocle_apptrace.instrumentation.common.instrumentor import get_tracer_provider
|
|
@@ -90,49 +90,6 @@ def stop_trace(
|
|
|
90
90
|
except Exception as e:
|
|
91
91
|
logger.warning(f"Failed to stop trace: {e}")
|
|
92
92
|
|
|
93
|
-
def start_scope(
|
|
94
|
-
scope_name: str,
|
|
95
|
-
scope_value: Optional[str] = None
|
|
96
|
-
) -> object:
|
|
97
|
-
"""
|
|
98
|
-
Start a new scope with the given name and optional value. If no value is provided, a random UUID will be generated.
|
|
99
|
-
All the spans, across traces created after this call will have the scope attached until the scope is stopped.
|
|
100
|
-
|
|
101
|
-
Args:
|
|
102
|
-
scope_name: The name of the scope.
|
|
103
|
-
scope_value: Optional value of the scope. If None, a random UUID will be generated.
|
|
104
|
-
|
|
105
|
-
Returns:
|
|
106
|
-
Token: A token representing the attached context for the scope. This token is to be used later to stop the current scope.
|
|
107
|
-
"""
|
|
108
|
-
try:
|
|
109
|
-
# Set the scope using existing utility
|
|
110
|
-
token = set_scope(scope_name, scope_value)
|
|
111
|
-
return token
|
|
112
|
-
except Exception as e:
|
|
113
|
-
logger.warning(f"Failed to start scope: {e}")
|
|
114
|
-
return None
|
|
115
|
-
|
|
116
|
-
def stop_scope(
|
|
117
|
-
token: object
|
|
118
|
-
) -> None:
|
|
119
|
-
"""
|
|
120
|
-
Stop the active scope. All the spans created after this will not have the scope attached.
|
|
121
|
-
|
|
122
|
-
Args:
|
|
123
|
-
token: The token that was returned when the scope was started.
|
|
124
|
-
|
|
125
|
-
Returns:
|
|
126
|
-
None
|
|
127
|
-
"""
|
|
128
|
-
try:
|
|
129
|
-
# Remove the scope
|
|
130
|
-
remove_scope(token)
|
|
131
|
-
except Exception as e:
|
|
132
|
-
logger.warning(f"Failed to stop scope: {e}")
|
|
133
|
-
return
|
|
134
|
-
|
|
135
|
-
|
|
136
93
|
@contextmanager
|
|
137
94
|
def monocle_trace(
|
|
138
95
|
span_name: Optional[str] = None,
|
|
@@ -207,74 +164,6 @@ async def amonocle_trace(
|
|
|
207
164
|
logger.warning(f"Failed in amonocle_trace: {e}")
|
|
208
165
|
yield # Still yield to not break the context manager
|
|
209
166
|
|
|
210
|
-
@contextmanager
|
|
211
|
-
def monocle_trace_scope(
|
|
212
|
-
scope_name: str,
|
|
213
|
-
scope_value: Optional[str] = None
|
|
214
|
-
):
|
|
215
|
-
"""
|
|
216
|
-
Context manager to start and stop a scope. All the spans, across traces created within the encapsulated code will have the scope attached.
|
|
217
|
-
|
|
218
|
-
Args:
|
|
219
|
-
scope_name: The name of the scope.
|
|
220
|
-
scope_value: Optional value of the scope. If None, a random UUID will be generated.
|
|
221
|
-
"""
|
|
222
|
-
token = start_scope(scope_name, scope_value)
|
|
223
|
-
try:
|
|
224
|
-
yield
|
|
225
|
-
finally:
|
|
226
|
-
stop_scope(token)
|
|
227
|
-
|
|
228
|
-
@asynccontextmanager
|
|
229
|
-
async def amonocle_trace_scope(
|
|
230
|
-
scope_name: str,
|
|
231
|
-
scope_value: Optional[str] = None
|
|
232
|
-
):
|
|
233
|
-
"""
|
|
234
|
-
Async context manager to start and stop a scope. All the spans, across traces created within the encapsulated code will have the scope attached.
|
|
235
|
-
|
|
236
|
-
Args:
|
|
237
|
-
scope_name: The name of the scope.
|
|
238
|
-
scope_value: Optional value of the scope. If None, a random UUID will be generated.
|
|
239
|
-
"""
|
|
240
|
-
token = start_scope(scope_name, scope_value)
|
|
241
|
-
try:
|
|
242
|
-
yield
|
|
243
|
-
finally:
|
|
244
|
-
stop_scope(token)
|
|
245
|
-
|
|
246
|
-
def monocle_trace_scope_method(
|
|
247
|
-
scope_name: str,
|
|
248
|
-
scope_value: Optional[str] = None
|
|
249
|
-
):
|
|
250
|
-
"""
|
|
251
|
-
Decorator to start and stop a scope for a method. All the spans, across traces created in the method will have the scope attached.
|
|
252
|
-
|
|
253
|
-
Args:
|
|
254
|
-
scope_name: The name of the scope.
|
|
255
|
-
scope_value: Optional value of the scope. If None, a random UUID will be generated.
|
|
256
|
-
"""
|
|
257
|
-
def decorator(func):
|
|
258
|
-
if inspect.iscoroutinefunction(func):
|
|
259
|
-
@wraps(func)
|
|
260
|
-
async def wrapper(*args, **kwargs):
|
|
261
|
-
async with amonocle_trace_scope(
|
|
262
|
-
scope_name, scope_value
|
|
263
|
-
):
|
|
264
|
-
result = await func(*args, **kwargs)
|
|
265
|
-
return result
|
|
266
|
-
return wrapper
|
|
267
|
-
else:
|
|
268
|
-
@wraps(func)
|
|
269
|
-
def wrapper(*args, **kwargs):
|
|
270
|
-
with monocle_trace_scope(
|
|
271
|
-
scope_name, scope_value
|
|
272
|
-
):
|
|
273
|
-
result = func(*args, **kwargs)
|
|
274
|
-
return result
|
|
275
|
-
return wrapper
|
|
276
|
-
return decorator
|
|
277
|
-
|
|
278
167
|
def monocle_trace_method(
|
|
279
168
|
span_name: Optional[str] = None
|
|
280
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
|
|
@@ -65,6 +65,8 @@ class SpanHandler:
|
|
|
65
65
|
span.set_attribute("span.type", span_type)
|
|
66
66
|
else:
|
|
67
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"])
|
|
68
70
|
return span_type
|
|
69
71
|
|
|
70
72
|
def pre_task_processing(self, to_wrap, wrapped, instance, args,kwargs, span):
|
|
@@ -99,6 +101,17 @@ class SpanHandler:
|
|
|
99
101
|
def post_task_processing(self, to_wrap, wrapped, instance, args, kwargs, result, ex, span:Span, parent_span:Span):
|
|
100
102
|
pass
|
|
101
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
|
+
|
|
102
115
|
def hydrate_span(self, to_wrap, wrapped, instance, args, kwargs, result, span, parent_span = None, ex:Exception = None) -> bool:
|
|
103
116
|
try:
|
|
104
117
|
detected_error_in_attribute = self.hydrate_attributes(to_wrap, wrapped, instance, args, kwargs, result, span, parent_span)
|
|
@@ -190,8 +190,8 @@ def __generate_scope_id() -> str:
|
|
|
190
190
|
global scope_id_generator
|
|
191
191
|
return f"{hex(scope_id_generator.generate_trace_id())}"
|
|
192
192
|
|
|
193
|
-
def set_scope(scope_name: str, scope_value:str = None) -> object:
|
|
194
|
-
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)
|
|
195
195
|
|
|
196
196
|
def set_scopes(scopes:dict[str, object], baggage_context:Context = None) -> object:
|
|
197
197
|
if baggage_context is None:
|