@@ -203,69 +203,69 @@ def export(
203203 _logger .warning ("Exporter already shutdown, ignoring batch" )
204204 return LogRecordExportResult .FAILURE
205205
206- finish_export = self ._metrics .start_export (len (batch ))
206+ with self ._metrics .export_operation (len (batch )) as result :
207+ serialized_data = encode_logs (batch ).SerializeToString ()
208+ deadline_sec = time () + self ._timeout
209+ for retry_num in range (_MAX_RETRYS ):
210+ # multiplying by a random number between .8 and 1.2 introduces a +/20% jitter to each backoff.
211+ backoff_seconds = 2 ** retry_num * random .uniform (0.8 , 1.2 )
212+ export_error : Optional [Exception ] = None
213+ try :
214+ resp = self ._export (serialized_data , deadline_sec - time ())
215+ if resp .ok :
216+ return LogRecordExportResult .SUCCESS
217+ except requests .exceptions .RequestException as error :
218+ reason = error
219+ export_error = error
220+ retryable = isinstance (error , ConnectionError )
221+ status_code = None
222+ else :
223+ reason = resp .reason
224+ retryable = _is_retryable (resp )
225+ status_code = resp .status_code
207226
208- serialized_data = encode_logs (batch ).SerializeToString ()
209- deadline_sec = time () + self ._timeout
210- for retry_num in range (_MAX_RETRYS ):
211- # multiplying by a random number between .8 and 1.2 introduces a +/20% jitter to each backoff.
212- backoff_seconds = 2 ** retry_num * random .uniform (0.8 , 1.2 )
213- export_error : Optional [Exception ] = None
214- try :
215- resp = self ._export (serialized_data , deadline_sec - time ())
216- if resp .ok :
217- finish_export (None , None )
218- return LogRecordExportResult .SUCCESS
219- except requests .exceptions .RequestException as error :
220- reason = error
221- export_error = error
222- retryable = isinstance (error , ConnectionError )
223- status_code = None
224- else :
225- reason = resp .reason
226- retryable = _is_retryable (resp )
227- status_code = resp .status_code
227+ if not retryable :
228+ _logger .error (
229+ "Failed to export logs batch code: %s, reason: %s" ,
230+ status_code ,
231+ reason ,
232+ )
233+ error_attrs = (
234+ {HTTP_RESPONSE_STATUS_CODE : status_code }
235+ if status_code is not None
236+ else None
237+ )
238+ result .error = export_error
239+ result .error_attrs = error_attrs
240+ return LogRecordExportResult .FAILURE
228241
229- if not retryable :
230- _logger .error (
231- "Failed to export logs batch code: %s, reason: %s" ,
232- status_code ,
242+ if (
243+ retry_num + 1 == _MAX_RETRYS
244+ or backoff_seconds > (deadline_sec - time ())
245+ or self ._shutdown
246+ ):
247+ _logger .error (
248+ "Failed to export logs batch due to timeout, "
249+ "max retries or shutdown."
250+ )
251+ error_attrs = (
252+ {HTTP_RESPONSE_STATUS_CODE : status_code }
253+ if status_code is not None
254+ else None
255+ )
256+ result .error = export_error
257+ result .error_attrs = error_attrs
258+ return LogRecordExportResult .FAILURE
259+ _logger .warning (
260+ "Transient error %s encountered while exporting logs batch, retrying in %.2fs." ,
233261 reason ,
262+ backoff_seconds ,
234263 )
235- error_attrs = (
236- {HTTP_RESPONSE_STATUS_CODE : status_code }
237- if status_code is not None
238- else None
239- )
240- finish_export (export_error , error_attrs )
241- return LogRecordExportResult .FAILURE
242-
243- if (
244- retry_num + 1 == _MAX_RETRYS
245- or backoff_seconds > (deadline_sec - time ())
246- or self ._shutdown
247- ):
248- _logger .error (
249- "Failed to export logs batch due to timeout, "
250- "max retries or shutdown."
251- )
252- error_attrs = (
253- {HTTP_RESPONSE_STATUS_CODE : status_code }
254- if status_code is not None
255- else None
256- )
257- finish_export (export_error , error_attrs )
258- return LogRecordExportResult .FAILURE
259- _logger .warning (
260- "Transient error %s encountered while exporting logs batch, retrying in %.2fs." ,
261- reason ,
262- backoff_seconds ,
263- )
264- shutdown = self ._shutdown_is_occuring .wait (backoff_seconds )
265- if shutdown :
266- _logger .warning ("Shutdown in progress, aborting retry." )
267- break
268- return LogRecordExportResult .FAILURE
264+ shutdown = self ._shutdown_is_occuring .wait (backoff_seconds )
265+ if shutdown :
266+ _logger .warning ("Shutdown in progress, aborting retry." )
267+ break
268+ return LogRecordExportResult .FAILURE
269269
270270 def force_flush (self , timeout_millis : float = 10_000 ) -> bool :
271271 """Nothing is buffered in this exporter, so this method does nothing."""
0 commit comments