Skip to content

Commit b4df54a

Browse files
committed
Calculate backoff in 1 place instead of 2
1 parent 28b9399 commit b4df54a

File tree

4 files changed

+6
-8
lines changed
  • exporter
    • opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc
    • opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http

4 files changed

+6
-8
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,10 @@ def _export(
296296
retry_info = RetryInfo()
297297
with self._export_lock:
298298
deadline_sec = time() + self._timeout
299-
backoff_seconds = 1 * random.uniform(0.8, 1.2)
300299
for retry_num in range(1, _MAX_RETRYS + 1):
300+
backoff_seconds = 2 ** (retry_num - 1) * random.uniform(
301+
0.8, 1.2
302+
)
301303
try:
302304
self._client.Export(
303305
request=self._translate_data(data),
@@ -334,7 +336,6 @@ def _export(
334336
backoff_seconds,
335337
)
336338
sleep(backoff_seconds)
337-
backoff_seconds = 2**retry_num * random.uniform(0.8, 1.2)
338339
# Not possible to reach here but the linter is complaining.
339340
return self._result.FAILURE
340341

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ def export(self, batch: Sequence[LogData]) -> LogExportResult:
163163

164164
serialized_data = encode_logs(batch).SerializeToString()
165165
deadline_sec = time() + self._timeout
166-
backoff_seconds = 1 * random.uniform(0.8, 1.2)
167166
for retry_num in range(1, _MAX_RETRYS + 1):
167+
backoff_seconds = 2 ** (retry_num - 1) * random.uniform(0.8, 1.2)
168168
resp = self._export(serialized_data, deadline_sec - time())
169169
if resp.ok:
170170
return LogExportResult.SUCCESS
@@ -185,7 +185,6 @@ def export(self, batch: Sequence[LogData]) -> LogExportResult:
185185
backoff_seconds,
186186
)
187187
sleep(backoff_seconds)
188-
backoff_seconds = 2**retry_num * random.uniform(0.8, 1.2)
189188
# Not possible to reach here but the linter is complaining.
190189
return LogExportResult.FAILURE
191190

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ def export(
213213
deadline_sec = time() + (
214214
timeout_millis / 1e3 if timeout_millis else self._timeout
215215
)
216-
backoff_seconds = 1 * random.uniform(0.8, 1.2)
217216
for retry_num in range(1, _MAX_RETRYS + 1):
217+
backoff_seconds = 2 ** (retry_num - 1) * random.uniform(0.8, 1.2)
218218
resp = self._export(serialized_data, deadline_sec - time())
219219
if resp.ok:
220220
return MetricExportResult.SUCCESS
@@ -235,7 +235,6 @@ def export(
235235
backoff_seconds,
236236
)
237237
sleep(backoff_seconds)
238-
backoff_seconds = 2**retry_num * random.uniform(0.8, 1.2)
239238
# Not possible to reach here but the linter is complaining.
240239
return MetricExportResult.FAILURE
241240

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ def export(self, spans: Sequence[ReadableSpan]) -> SpanExportResult:
161161

162162
serialized_data = encode_spans(spans).SerializePartialToString()
163163
deadline_sec = time() + self._timeout
164-
backoff_seconds = 1 * random.uniform(0.8, 1.2)
165164
for retry_num in range(1, _MAX_RETRYS + 1):
165+
backoff_seconds = 2 ** (retry_num - 1) * random.uniform(0.8, 1.2)
166166
resp = self._export(serialized_data, deadline_sec - time())
167167
if resp.ok:
168168
return SpanExportResult.SUCCESS
@@ -183,7 +183,6 @@ def export(self, spans: Sequence[ReadableSpan]) -> SpanExportResult:
183183
backoff_seconds,
184184
)
185185
sleep(backoff_seconds)
186-
backoff_seconds = 2**retry_num * random.uniform(0.8, 1.2)
187186
# Not possible to reach here but the linter is complaining.
188187
return SpanExportResult.FAILURE
189188

0 commit comments

Comments
 (0)