3838_REACHED_INGESTION_STATUS_CODES = (200 , 206 , 402 , 408 , 429 , 439 , 500 )
3939
4040
41+ class TransportStatusCode :
42+ SUCCESS = 0
43+ RETRY = 1
44+ DROP = 2
45+ STATSBEAT_SHUTDOWN = 3
46+
47+
4148class TransportMixin (object ):
4249
4350 # check to see if collecting requests information related to statsbeats
@@ -59,7 +66,7 @@ def _transmit_from_storage(self):
5966 if blob .lease (self .options .timeout + 5 ):
6067 envelopes = blob .get ()
6168 result = self ._transmit (envelopes )
62- if result > 0 :
69+ if result is TransportStatusCode . RETRY :
6370 blob .lease (result )
6471 else :
6572 blob .delete ()
@@ -74,7 +81,7 @@ def _transmit(self, envelopes):
7481 """
7582 if not envelopes :
7683 return 0
77- exception = None
84+ status = None
7885 try :
7986 start_time = time .time ()
8087 headers = {
@@ -101,37 +108,37 @@ def _transmit(self, envelopes):
101108 if not self ._is_stats_exporter ():
102109 logger .warning (
103110 'Request time out. Ingestion may be backed up. Retrying.' )
104- exception = self . options . minimum_retry_interval
111+ status = TransportStatusCode . RETRY
105112 except requests .RequestException as ex :
106113 if not self ._is_stats_exporter ():
107114 logger .warning (
108115 'Retrying due to transient client side error %s.' , ex )
109116 # client side error (retryable)
110- exception = self . options . minimum_retry_interval
117+ status = TransportStatusCode . RETRY
111118 except CredentialUnavailableError as ex :
112119 if not self ._is_stats_exporter ():
113120 logger .warning ('Credential error. %s. Dropping telemetry.' , ex )
114- exception = - 1
121+ status = TransportStatusCode . DROP
115122 except ClientAuthenticationError as ex :
116123 if not self ._is_stats_exporter ():
117124 logger .warning ('Authentication error %s' , ex )
118- exception = self . options . minimum_retry_interval
125+ status = TransportStatusCode . RETRY
119126 except Exception as ex :
120127 if not self ._is_stats_exporter ():
121128 logger .warning (
122129 'Error when sending request %s. Dropping telemetry.' , ex )
123130 # Extraneous error (non-retryable)
124- exception = - 1
131+ status = TransportStatusCode . DROP
125132 finally :
126133 end_time = time .time ()
127134 if self ._check_stats_collection ():
128135 with _requests_lock :
129136 duration = _requests_map .get ('duration' , 0 )
130137 _requests_map ['duration' ] = duration + (end_time - start_time ) # noqa: E501
131- if exception is not None :
138+ if status is not None :
132139 if self ._check_stats_collection ():
133140 with _requests_lock :
134- if exception >= 0 :
141+ if status is TransportStatusCode . RETRY :
135142 _requests_map ['retry' ] = _requests_map .get ('retry' , 0 ) + 1 # noqa: E501
136143 else :
137144 _requests_map ['exception' ] = _requests_map .get ('exception' , 0 ) + 1 # noqa: E501
@@ -141,8 +148,8 @@ def _transmit(self, envelopes):
141148 # If ingestion threshold during statsbeat initialization is
142149 # reached, return back code to shut it down
143150 if _statsbeat_failure_reached_threshold ():
144- return - 2
145- return exception
151+ return TransportStatusCode . STATSBEAT_SHUTDOWN
152+ return status
146153
147154 text = 'N/A'
148155 data = None
@@ -167,14 +174,14 @@ def _transmit(self, envelopes):
167174 elif _statsbeat_failure_reached_threshold ():
168175 # If ingestion threshold during statsbeat initialization is
169176 # reached, return back code to shut it down
170- return - 2
177+ return TransportStatusCode . STATSBEAT_SHUTDOWN
171178
172179 if response .status_code == 200 :
173180 self ._consecutive_redirects = 0
174181 if self ._check_stats_collection ():
175182 with _requests_lock :
176183 _requests_map ['success' ] = _requests_map .get ('success' , 0 ) + 1 # noqa: E501
177- return 0
184+ return TransportStatusCode . SUCCESS
178185 # Status code not 200, 439 or 402 counts as failures
179186 if self ._check_stats_collection ():
180187 if response .status_code != 439 and response .status_code != 402 :
@@ -211,7 +218,7 @@ def _transmit(self, envelopes):
211218 text ,
212219 ex ,
213220 )
214- return - response . status_code
221+ return TransportStatusCode . DROP
215222 # cannot parse response body, fallback to retry
216223 if response .status_code in (
217224 206 , # Partial Content
@@ -229,7 +236,7 @@ def _transmit(self, envelopes):
229236 if self ._check_stats_collection ():
230237 with _requests_lock :
231238 _requests_map ['retry' ] = _requests_map .get ('retry' , 0 ) + 1 # noqa: E501
232- return self . options . minimum_retry_interval
239+ return TransportStatusCode . RETRY
233240 # Authentication error
234241 if response .status_code == 401 :
235242 if not self ._is_stats_exporter ():
@@ -241,7 +248,7 @@ def _transmit(self, envelopes):
241248 if self ._check_stats_collection ():
242249 with _requests_lock :
243250 _requests_map ['retry' ] = _requests_map .get ('retry' , 0 ) + 1 # noqa: E501
244- return self . options . minimum_retry_interval
251+ return TransportStatusCode . RETRY
245252 # Forbidden error
246253 # Can occur when v2 endpoint is used while AI resource is configured
247254 # with disableLocalAuth
@@ -255,7 +262,7 @@ def _transmit(self, envelopes):
255262 if self ._check_stats_collection ():
256263 with _requests_lock :
257264 _requests_map ['retry' ] = _requests_map .get ('retry' , 0 ) + 1 # noqa: E501
258- return self . options . minimum_retry_interval
265+ return TransportStatusCode . RETRY
259266 # Redirect
260267 if response .status_code in (307 , 308 ):
261268 self ._consecutive_redirects += 1
@@ -296,7 +303,7 @@ def _transmit(self, envelopes):
296303 # 439: Monthly Quota Exceeded (old SDK) <- Currently OC SDK
297304 with _requests_lock :
298305 _requests_map ['throttle' ] = _requests_map .get ('throttle' , 0 ) + 1 # noqa: E501
299- return - response . status_code
306+ return TransportStatusCode . DROP
300307
301308
302309def _reached_ingestion_status_code (status_code ):
0 commit comments