Skip to content

Commit f4e8a34

Browse files
committed
feat(http_exporter): allow to run retry loop on connection errors
1 parent 9452ad1 commit f4e8a34

File tree

3 files changed

+48
-36
lines changed
  • exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http

3 files changed

+48
-36
lines changed

exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/_log_exporter/__init__.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,30 +186,34 @@ def export(
186186
serialized_data = encode_logs(batch).SerializeToString()
187187
deadline_sec = time() + self._timeout
188188
for retry_num in range(_MAX_RETRYS):
189+
# multiplying by a random number between .8 and 1.2 introduces a +/20% jitter to each backoff.
190+
backoff_seconds = 2**retry_num * random.uniform(0.8, 1.2)
189191
try:
190192
resp = self._export(serialized_data, deadline_sec - time())
193+
if resp.ok:
194+
return LogExportResult.SUCCESS
195+
if not _is_retryable(resp):
196+
_logger.error(
197+
"Failed to export logs batch code: %s, reason: %s",
198+
resp.status_code,
199+
resp.text,
200+
)
201+
return LogExportResult.FAILURE
191202
except Exception as error:
192203
_logger.error("Failed to export logs batch reason: %s", error)
193-
return LogExportResult.FAILURE
194-
if resp.ok:
195-
return LogRecordExportResult.SUCCESS
196-
# multiplying by a random number between .8 and 1.2 introduces a +/20% jitter to each backoff.
197-
backoff_seconds = 2**retry_num * random.uniform(0.8, 1.2)
204+
198205
if (
199-
not _is_retryable(resp)
200-
or retry_num + 1 == _MAX_RETRYS
206+
retry_num + 1 == _MAX_RETRYS
201207
or backoff_seconds > (deadline_sec - time())
202208
or self._shutdown
203209
):
204210
_logger.error(
205-
"Failed to export logs batch code: %s, reason: %s",
206-
resp.status_code,
207-
resp.text,
211+
"Failed to export logs batch due to timeout,"
212+
"max retries or shutdown."
208213
)
209214
return LogRecordExportResult.FAILURE
210215
_logger.warning(
211-
"Transient error %s encountered while exporting logs batch, retrying in %.2fs.",
212-
resp.reason,
216+
"Transient error encountered while exporting logs batch, retrying in %.2fs.",
213217
backoff_seconds,
214218
)
215219
shutdown = self._shutdown_is_occuring.wait(backoff_seconds)

exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/metric_exporter/__init__.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,32 +231,36 @@ def export(
231231
serialized_data = encode_metrics(metrics_data).SerializeToString()
232232
deadline_sec = time() + self._timeout
233233
for retry_num in range(_MAX_RETRYS):
234+
# multiplying by a random number between .8 and 1.2 introduces a +/20% jitter to each backoff.
235+
backoff_seconds = 2**retry_num * random.uniform(0.8, 1.2)
234236
try:
235237
resp = self._export(serialized_data, deadline_sec - time())
238+
if resp.ok:
239+
return MetricExportResult.SUCCESS
240+
if not _is_retryable(resp):
241+
_logger.error(
242+
"Failed to export metrics batch code: %s, reason: %s",
243+
resp.status_code,
244+
resp.text,
245+
)
246+
return MetricExportResult.FAILURE
236247
except Exception as error:
237248
_logger.error(
238249
"Failed to export metrics batch reason: %s", error
239250
)
240-
return MetricExportResult.FAILURE
241-
if resp.ok:
242-
return MetricExportResult.SUCCESS
243-
# multiplying by a random number between .8 and 1.2 introduces a +/20% jitter to each backoff.
244-
backoff_seconds = 2**retry_num * random.uniform(0.8, 1.2)
251+
245252
if (
246-
not _is_retryable(resp)
247-
or retry_num + 1 == _MAX_RETRYS
253+
retry_num + 1 == _MAX_RETRYS
248254
or backoff_seconds > (deadline_sec - time())
249255
or self._shutdown
250256
):
251257
_logger.error(
252-
"Failed to export metrics batch code: %s, reason: %s",
253-
resp.status_code,
254-
resp.text,
258+
"Failed to export metrics batch due to timeout,"
259+
"max retries or shutdown."
255260
)
256261
return MetricExportResult.FAILURE
257262
_logger.warning(
258-
"Transient error %s encountered while exporting metrics batch, retrying in %.2fs.",
259-
resp.reason,
263+
"Transient error encountered while exporting metrics batch, retrying in %.2fs.",
260264
backoff_seconds,
261265
)
262266
shutdown = self._shutdown_in_progress.wait(backoff_seconds)

exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/trace_exporter/__init__.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,30 +179,34 @@ def export(self, spans: Sequence[ReadableSpan]) -> SpanExportResult:
179179
serialized_data = encode_spans(spans).SerializePartialToString()
180180
deadline_sec = time() + self._timeout
181181
for retry_num in range(_MAX_RETRYS):
182+
# multiplying by a random number between .8 and 1.2 introduces a +/20% jitter to each backoff.
183+
backoff_seconds = 2**retry_num * random.uniform(0.8, 1.2)
182184
try:
183185
resp = self._export(serialized_data, deadline_sec - time())
186+
if resp.ok:
187+
return SpanExportResult.SUCCESS
188+
if not _is_retryable(resp):
189+
_logger.error(
190+
"Failed to export span batch code: %s, reason: %s",
191+
resp.status_code,
192+
resp.text,
193+
)
194+
return SpanExportResult.FAILURE
184195
except Exception as error:
185196
_logger.error("Failed to export span batch reason: %s", error)
186-
return SpanExportResult.FAILURE
187-
if resp.ok:
188-
return SpanExportResult.SUCCESS
189-
# multiplying by a random number between .8 and 1.2 introduces a +/20% jitter to each backoff.
190-
backoff_seconds = 2**retry_num * random.uniform(0.8, 1.2)
197+
191198
if (
192-
not _is_retryable(resp)
193-
or retry_num + 1 == _MAX_RETRYS
199+
retry_num + 1 == _MAX_RETRYS
194200
or backoff_seconds > (deadline_sec - time())
195201
or self._shutdown
196202
):
197203
_logger.error(
198-
"Failed to export span batch code: %s, reason: %s",
199-
resp.status_code,
200-
resp.text,
204+
"Failed to export span batch due to timeout,"
205+
"max retries or shutdown."
201206
)
202207
return SpanExportResult.FAILURE
203208
_logger.warning(
204-
"Transient error %s encountered while exporting span batch, retrying in %.2fs.",
205-
resp.reason,
209+
"Transient error encountered while exporting span batch, retrying in %.2fs.",
206210
backoff_seconds,
207211
)
208212
shutdown = self._shutdown_in_progress.wait(backoff_seconds)

0 commit comments

Comments
 (0)