forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract retry mechanic from OTLPExporterMixin
This is the first change in a chain of commits to rework the retry mechanic. It is based on the work of open-telemetry#3764 and basically trying to land the changes proposed by this monolithic commit step by step. The plan is roughly to proceed in these steps: * Extract retry mechanic from GRPC exporters * Consolidate HTTP with GRPC exporter retry implementation * Pipe timeout through RetryingExporter * Make exporter lock protect the whole export instead of just a single iteration * Make timeout float instead of int * Add back-off with jitter It's pretty likely that the plan will change along the way.
- Loading branch information
1 parent
19e0a09
commit 6395d42
Showing
9 changed files
with
204 additions
and
155 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
...metry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/exporter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import threading | ||
from logging import getLogger | ||
from time import sleep | ||
from typing import Callable, Generic, Type, TypeVar, Optional | ||
|
||
from ._internal import _create_exp_backoff_generator | ||
|
||
ExportResultT = TypeVar("ExportResultT", covariant=True) | ||
ExportPayloadT = TypeVar("ExportPayloadT", covariant=True) | ||
|
||
logger = getLogger(__name__) | ||
|
||
|
||
class RetryableExportError(Exception): | ||
def __init__(self, retry_delay_sec: Optional[int]): | ||
super().__init__() | ||
self.retry_delay_sec = retry_delay_sec | ||
|
||
|
||
class RetryingExporter(Generic[ExportResultT]): | ||
def __init__( | ||
self, | ||
export_function: Callable[[ExportPayloadT], ExportResultT], | ||
result: Type[ExportResultT], | ||
): | ||
self._export_function = export_function | ||
self._result = result | ||
|
||
self._shutdown = False | ||
self._export_lock = threading.Lock() | ||
|
||
def shutdown(self, timeout_millis: float = 30_000) -> None: | ||
# wait for the last export if any | ||
self._export_lock.acquire( # pylint: disable=consider-using-with | ||
timeout=timeout_millis / 1e3 | ||
) | ||
self._shutdown = True | ||
self._export_lock.release() | ||
|
||
def export_with_retry(self, payload: ExportPayloadT) -> ExportResultT: | ||
# After the call to shutdown, subsequent calls to Export are | ||
# not allowed and should return a Failure result. | ||
if self._shutdown: | ||
logger.warning("Exporter already shutdown, ignoring batch") | ||
return self._result.FAILURE | ||
|
||
max_value = 64 | ||
# expo returns a generator that yields delay values which grow | ||
# exponentially. Once delay is greater than max_value, the yielded | ||
# value will remain constant. | ||
for delay in _create_exp_backoff_generator(max_value=max_value): | ||
if delay == max_value or self._shutdown: | ||
return self._result.FAILURE | ||
|
||
with self._export_lock: | ||
try: | ||
return self._export_function(payload) | ||
except RetryableExportError as exc: | ||
delay_sec = ( | ||
exc.retry_delay_sec | ||
if exc.retry_delay_sec is not None | ||
else delay | ||
) | ||
logger.warning("Retrying in %ss", delay_sec) | ||
sleep(delay_sec) | ||
|
||
return self._result.FAILURE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.