Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 0919b61

Browse files
authored
Refactor azure exporter transport response codes (#1131)
1 parent 7cbf82f commit 0919b61

File tree

8 files changed

+184
-93
lines changed

8 files changed

+184
-93
lines changed

contrib/opencensus-ext-azure/opencensus/ext/azure/common/transport.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@
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+
4148
class 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

302309
def _reached_ingestion_status_code(status_code):

contrib/opencensus-ext-azure/opencensus/ext/azure/log_exporter/__init__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
Message,
3232
)
3333
from opencensus.ext.azure.common.storage import LocalFileStorage
34-
from opencensus.ext.azure.common.transport import TransportMixin
34+
from opencensus.ext.azure.common.transport import (
35+
TransportMixin,
36+
TransportStatusCode,
37+
)
3538
from opencensus.ext.azure.statsbeat import statsbeat
3639
from opencensus.trace import execution_context
3740

@@ -78,8 +81,11 @@ def _export(self, batch, event=None): # pragma: NO COVER
7881
envelopes = self.apply_telemetry_processors(envelopes)
7982
result = self._transmit(envelopes)
8083
# Only store files if local storage enabled
81-
if self.storage and result > 0:
82-
self.storage.put(envelopes, result)
84+
if self.storage and result is TransportStatusCode.RETRY:
85+
self.storage.put(
86+
envelopes,
87+
self.options.minimum_retry_interval,
88+
)
8389
if event:
8490
if isinstance(event, QueueExitEvent):
8591
self._transmit_from_storage() # send files before exit

contrib/opencensus-ext-azure/opencensus/ext/azure/metrics_exporter/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
MetricData,
2626
)
2727
from opencensus.ext.azure.common.storage import LocalFileStorage
28-
from opencensus.ext.azure.common.transport import TransportMixin
28+
from opencensus.ext.azure.common.transport import (
29+
TransportMixin,
30+
TransportStatusCode,
31+
)
2932
from opencensus.ext.azure.metrics_exporter import standard_metrics
3033
from opencensus.metrics import transport
3134
from opencensus.metrics.export.metric_descriptor import MetricDescriptorType
@@ -72,13 +75,14 @@ def export_metrics(self, metrics):
7275
batch = self.apply_telemetry_processors(batch)
7376
result = self._transmit(batch)
7477
# If statsbeat exporter and received signal to shutdown
75-
if self._is_stats_exporter() and result == -2:
78+
if self._is_stats_exporter() and result is \
79+
TransportStatusCode.STATSBEAT_SHUTDOWN:
7680
from opencensus.ext.azure.statsbeat import statsbeat
7781
statsbeat.shutdown_statsbeat_metrics()
7882
return
7983
# Only store files if local storage enabled
80-
if self.storage and result > 0:
81-
self.storage.put(batch, result)
84+
if self.storage and result is TransportStatusCode.RETRY:
85+
self.storage.put(batch, self.options.minimum_retry_interval)
8286

8387
# If there is still room to transmit envelopes, transmit from storage
8488
# if available

contrib/opencensus-ext-azure/opencensus/ext/azure/trace_exporter/__init__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
Request,
3030
)
3131
from opencensus.ext.azure.common.storage import LocalFileStorage
32-
from opencensus.ext.azure.common.transport import TransportMixin
32+
from opencensus.ext.azure.common.transport import (
33+
TransportMixin,
34+
TransportStatusCode,
35+
)
3336
from opencensus.ext.azure.statsbeat import statsbeat
3437
from opencensus.trace import attributes_helper
3538
from opencensus.trace.span import SpanKind
@@ -204,8 +207,11 @@ def emit(self, batch, event=None):
204207
envelopes = self.apply_telemetry_processors(envelopes)
205208
result = self._transmit(envelopes)
206209
# Only store files if local storage enabled
207-
if self.storage and result > 0:
208-
self.storage.put(envelopes, result)
210+
if self.storage and result is TransportStatusCode.RETRY:
211+
self.storage.put(
212+
envelopes,
213+
self.options.minimum_retry_interval
214+
)
209215
if event:
210216
if isinstance(event, QueueExitEvent):
211217
self._transmit_from_storage() # send files before exit

0 commit comments

Comments
 (0)