Skip to content

Commit 71b77e1

Browse files
committed
Tweak backoff calculation
1 parent d506d54 commit 71b77e1

File tree

5 files changed

+11
-20
lines changed

5 files changed

+11
-20
lines changed

exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/exporter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def _export(
334334
backoff_seconds,
335335
)
336336
sleep(backoff_seconds)
337-
backoff_seconds *= 2 * random.uniform(0.8, 1.2)
337+
backoff_seconds = 2 ** retry_num * random.uniform(0.8, 1.2)
338338
# Not possible to reach here but the linter is complaining.
339339
return self._result.FAILURE
340340

exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_exporter_mixin.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,12 @@ class TraceServiceServicerWithExportParams(TraceServiceServicer):
9090
def __init__(
9191
self,
9292
export_result: StatusCode,
93-
optional_first_time_retry_millis: Optional[int] = None,
93+
optional_retry_millis: Optional[int] = None,
9494
optional_export_sleep: Optional[float] = None,
9595
):
9696
self.export_result = export_result
9797
self.optional_export_sleep = optional_export_sleep
98-
self.optional_first_time_retry_millis = (
99-
optional_first_time_retry_millis
100-
)
98+
self.optional_retry_millis = optional_retry_millis
10199
self.num_requests = 0
102100

103101
# pylint: disable=invalid-name,unused-argument
@@ -106,17 +104,14 @@ def Export(self, request, context):
106104
if self.optional_export_sleep:
107105
time.sleep(self.optional_export_sleep)
108106
if self.export_result != StatusCode.OK:
109-
if (
110-
self.optional_first_time_retry_millis
111-
and self.num_requests == 1
112-
):
107+
if self.optional_retry_millis and self.num_requests == 1:
113108
context.set_trailing_metadata(
114109
(
115110
(
116111
"google.rpc.retryinfo-bin",
117112
RetryInfo(
118113
retry_delay=Duration(
119-
nanos=self.optional_first_time_retry_millis
114+
nanos=self.optional_retry_millis
120115
)
121116
).SerializeToString(),
122117
),
@@ -377,7 +372,7 @@ def test_export_over_closed_grpc_channel(self):
377372

378373
def test_retry_info_is_respected(self):
379374
mock_trace_service = TraceServiceServicerWithExportParams(
380-
StatusCode.UNAVAILABLE, optional_first_time_retry_millis=200
375+
StatusCode.UNAVAILABLE, optional_retry_millis=200
381376
)
382377
add_TraceServiceServicer_to_server(
383378
mock_trace_service,
@@ -390,13 +385,9 @@ def test_retry_info_is_respected(self):
390385
SpanExportResult.FAILURE,
391386
)
392387
after = time.time()
393-
# We set the `grpc-retry-pushback-ms` header to 200 millis on the first server response only,
394-
# and then we do exponential backoff using that first value.
395-
# So we expect the first request at time 0, second at time 0.2,
396-
# third at .6, fourth at 1.4, fifth at 3, and final one at 6.2.
397388
self.assertEqual(mock_trace_service.num_requests, 6)
398-
# The backoffs have a jitter +- 20%, so we have to put a higher bound than 6.2.
399-
self.assertTrue(after - before < 7.5)
389+
# 200 * 5 = 1 second, plus some wiggle room so the test passes consistently.
390+
self.assertTrue(after - before < 1.1)
400391

401392
def test_retry_not_made_if_would_exceed_timeout(self):
402393
mock_trace_service = TraceServiceServicerWithExportParams(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def export(self, batch: Sequence[LogData]) -> LogExportResult:
185185
backoff_seconds,
186186
)
187187
sleep(backoff_seconds)
188-
backoff_seconds *= 2 * random.uniform(0.8, 1.2)
188+
backoff_seconds = 2**retry_num * random.uniform(0.8, 1.2)
189189
# Not possible to reach here but the linter is complaining.
190190
return LogExportResult.FAILURE
191191

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def export(
235235
backoff_seconds,
236236
)
237237
sleep(backoff_seconds)
238-
backoff_seconds *= 2 * random.uniform(0.8, 1.2)
238+
backoff_seconds = 2**retry_num * random.uniform(0.8, 1.2)
239239
# Not possible to reach here but the linter is complaining.
240240
return MetricExportResult.FAILURE
241241

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def export(self, spans: Sequence[ReadableSpan]) -> SpanExportResult:
183183
backoff_seconds,
184184
)
185185
sleep(backoff_seconds)
186-
backoff_seconds *= 2 * random.uniform(0.8, 1.2)
186+
backoff_seconds = 2**retry_num * random.uniform(0.8, 1.2)
187187
# Not possible to reach here but the linter is complaining.
188188
return SpanExportResult.FAILURE
189189

0 commit comments

Comments
 (0)