17
17
import sys
18
18
from time import perf_counter
19
19
from typing import List
20
+ import functools
20
21
21
22
from sagemaker .utils import resolve_value_from_config
22
23
from sagemaker .config .config_schema import TELEMETRY_OPT_OUT_PATH
47
48
FEATURE_TO_CODE = {
48
49
str (Feature .SDK_DEFAULTS ): 1 ,
49
50
str (Feature .LOCAL_MODE ): 2 ,
51
+ str (Feature .REMOTE_FUNCTION ): 3 ,
50
52
}
51
53
52
54
STATUS_TO_CODE = {
@@ -59,77 +61,95 @@ def _telemetry_emitter(feature: str, func_name: str):
59
61
"""Decorator to emit telemetry logs for SageMaker Python SDK functions"""
60
62
61
63
def decorator (func ):
62
- def wrapper (self , * args , ** kwargs ):
63
- logger .info (TELEMETRY_OPT_OUT_MESSAGING )
64
- response = None
65
- caught_ex = None
66
- studio_app_type = process_studio_metadata_file ()
67
-
68
- # Check if telemetry is opted out
69
- telemetry_opt_out_flag = resolve_value_from_config (
70
- direct_input = None ,
71
- config_path = TELEMETRY_OPT_OUT_PATH ,
72
- default_value = False ,
73
- sagemaker_session = self .sagemaker_session ,
74
- )
75
- logger .debug ("TelemetryOptOut flag is set to: %s" , telemetry_opt_out_flag )
76
-
77
- # Construct the feature list to track feature combinations
78
- feature_list : List [int ] = [FEATURE_TO_CODE [str (feature )]]
79
- if self .sagemaker_session :
80
- if self .sagemaker_session .sagemaker_config and feature != Feature .SDK_DEFAULTS :
64
+ @functools .wraps (func )
65
+ def wrapper (* args , ** kwargs ):
66
+ sagemaker_session = None
67
+ # Check if sagemaker_session is passed as a function keyword argument
68
+ if "sagemaker_session" in kwargs :
69
+ sagemaker_session = kwargs ["sagemaker_session" ]
70
+ # Check if sagemaker_session is passed in the instance method
71
+ elif len (args ) > 0 and hasattr (args [0 ], "sagemaker_session" ):
72
+ sagemaker_session = args [0 ].sagemaker_session
73
+
74
+ if sagemaker_session :
75
+ logger .debug ("sagemaker_session found, preparing to emit telemetry..." )
76
+ logger .info (TELEMETRY_OPT_OUT_MESSAGING )
77
+ response = None
78
+ caught_ex = None
79
+ studio_app_type = process_studio_metadata_file ()
80
+
81
+ # Check if telemetry is opted out
82
+ telemetry_opt_out_flag = resolve_value_from_config (
83
+ direct_input = None ,
84
+ config_path = TELEMETRY_OPT_OUT_PATH ,
85
+ default_value = False ,
86
+ sagemaker_session = sagemaker_session ,
87
+ )
88
+ logger .debug ("TelemetryOptOut flag is set to: %s" , telemetry_opt_out_flag )
89
+
90
+ # Construct the feature list to track feature combinations
91
+ feature_list : List [int ] = [FEATURE_TO_CODE [str (feature )]]
92
+
93
+ if sagemaker_session .sagemaker_config and feature != Feature .SDK_DEFAULTS :
81
94
feature_list .append (FEATURE_TO_CODE [str (Feature .SDK_DEFAULTS )])
82
95
83
- if self . sagemaker_session .local_mode and feature != Feature .LOCAL_MODE :
96
+ if sagemaker_session .local_mode and feature != Feature .LOCAL_MODE :
84
97
feature_list .append (FEATURE_TO_CODE [str (Feature .LOCAL_MODE )])
85
98
86
- # Construct the extra info to track platform and environment usage metadata
87
- extra = (
88
- f"{ func_name } "
89
- f"&x-sdkVersion={ SDK_VERSION } "
90
- f"&x-env={ PYTHON_VERSION } "
91
- f"&x-sys={ OS_NAME_VERSION } "
92
- f"&x-platform={ studio_app_type } "
93
- )
94
-
95
- # Add endpoint ARN to the extra info if available
96
- if self .sagemaker_session and self .sagemaker_session .endpoint_arn :
97
- extra += f"&x-endpointArn={ self .sagemaker_session .endpoint_arn } "
98
-
99
- start_timer = perf_counter ()
100
- try :
101
- # Call the original function
102
- response = func (self , * args , ** kwargs )
103
- stop_timer = perf_counter ()
104
- elapsed = stop_timer - start_timer
105
- extra += f"&x-latency={ round (elapsed , 2 )} "
106
- if not telemetry_opt_out_flag :
107
- _send_telemetry_request (
108
- STATUS_TO_CODE [str (Status .SUCCESS )],
109
- feature_list ,
110
- self .sagemaker_session ,
111
- None ,
112
- None ,
113
- extra ,
114
- )
115
- except Exception as e : # pylint: disable=W0703
116
- stop_timer = perf_counter ()
117
- elapsed = stop_timer - start_timer
118
- extra += f"&x-latency={ round (elapsed , 2 )} "
119
- if not telemetry_opt_out_flag :
120
- _send_telemetry_request (
121
- STATUS_TO_CODE [str (Status .FAILURE )],
122
- feature_list ,
123
- self .sagemaker_session ,
124
- str (e ),
125
- e .__class__ .__name__ ,
126
- extra ,
127
- )
128
- caught_ex = e
129
- finally :
130
- if caught_ex :
131
- raise caught_ex
132
- return response # pylint: disable=W0150
99
+ # Construct the extra info to track platform and environment usage metadata
100
+ extra = (
101
+ f"{ func_name } "
102
+ f"&x-sdkVersion={ SDK_VERSION } "
103
+ f"&x-env={ PYTHON_VERSION } "
104
+ f"&x-sys={ OS_NAME_VERSION } "
105
+ f"&x-platform={ studio_app_type } "
106
+ )
107
+
108
+ # Add endpoint ARN to the extra info if available
109
+ if sagemaker_session .endpoint_arn :
110
+ extra += f"&x-endpointArn={ sagemaker_session .endpoint_arn } "
111
+
112
+ start_timer = perf_counter ()
113
+ try :
114
+ # Call the original function
115
+ response = func (* args , ** kwargs )
116
+ stop_timer = perf_counter ()
117
+ elapsed = stop_timer - start_timer
118
+ extra += f"&x-latency={ round (elapsed , 2 )} "
119
+ if not telemetry_opt_out_flag :
120
+ _send_telemetry_request (
121
+ STATUS_TO_CODE [str (Status .SUCCESS )],
122
+ feature_list ,
123
+ sagemaker_session ,
124
+ None ,
125
+ None ,
126
+ extra ,
127
+ )
128
+ except Exception as e : # pylint: disable=W0703
129
+ stop_timer = perf_counter ()
130
+ elapsed = stop_timer - start_timer
131
+ extra += f"&x-latency={ round (elapsed , 2 )} "
132
+ if not telemetry_opt_out_flag :
133
+ _send_telemetry_request (
134
+ STATUS_TO_CODE [str (Status .FAILURE )],
135
+ feature_list ,
136
+ sagemaker_session ,
137
+ str (e ),
138
+ e .__class__ .__name__ ,
139
+ extra ,
140
+ )
141
+ caught_ex = e
142
+ finally :
143
+ if caught_ex :
144
+ raise caught_ex
145
+ return response # pylint: disable=W0150
146
+ else :
147
+ logger .debug (
148
+ "Unable to send telemetry for function %s. "
149
+ "sagemaker_session is not provided or not valid." ,
150
+ func_name ,
151
+ )
152
+ return func (* args , ** kwargs )
133
153
134
154
return wrapper
135
155
@@ -165,9 +185,9 @@ def _send_telemetry_request(
165
185
# Send the telemetry request
166
186
logger .debug ("Sending telemetry request to [%s]" , url )
167
187
_requests_helper (url , 2 )
168
- logger .debug ("SageMaker Python SDK telemetry successfully emitted! " )
188
+ logger .debug ("SageMaker Python SDK telemetry successfully emitted. " )
169
189
except Exception : # pylint: disable=W0703
170
- logger .debug ("SageMaker Python SDK telemetry not emitted!! " )
190
+ logger .debug ("SageMaker Python SDK telemetry not emitted!" )
171
191
172
192
173
193
def _construct_url (
0 commit comments