Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
## Bug Fixes

<!-- Here goes notable bug fixes that are worth a special mention or explanation -->

- `GrpcStreamBroadcaster` now logs stream errors on a single line, leaving out the debug string when it only repeats the code and details.
24 changes: 22 additions & 2 deletions src/frequenz/client/base/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@
_logger = logging.getLogger(__name__)


def _format_error(error: grpc.aio.AioRpcError) -> str:
"""Format a gRPC error as a single line.

The debug string is left out when it only repeats the code and details.

Args:
error: The error to format.

Returns:
The formatted error.
"""
code = error.code().name
details = error.details()
debug = error.debug_error_string()
error_str = f"Error: code={code} details={details!r}"
if debug and debug != f"{code}:{details}":
error_str += f" debug_error_string={debug!r}"
Comment thread
llucax marked this conversation as resolved.
Resolved
return error_str


InputT = TypeVar("InputT")
"""The input type of the stream."""

Expand Down Expand Up @@ -281,7 +301,7 @@ async def _run(self) -> None:
data_sender = self._data_channel.new_sender()

while True:
error: Exception | None = None
error: grpc.aio.AioRpcError | None = None
_logger.info("%s: starting to stream", self._stream_name)
try:
call = self._stream_method()
Expand Down Expand Up @@ -315,7 +335,7 @@ async def _run(self) -> None:
break

interval = self._retry_strategy.next_interval()
error_str = f"Error: {error}" if error else "Stream exhausted"
error_str = _format_error(error) if error else "Stream exhausted"
if interval is None:
_logger.error(
"%s: connection ended, retry limit exceeded (%s), giving up. %s.",
Expand Down
69 changes: 62 additions & 7 deletions tests/streaming/test_grpc_stream_broadcaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
StreamFatalError,
StreamRetrying,
StreamStarted,
_format_error,
)


Expand All @@ -45,14 +46,18 @@ def no_retry() -> mock.MagicMock:
return mock_retry


def make_error() -> grpc.aio.AioRpcError:
def make_error(
code: grpc.StatusCode = grpc.StatusCode.UNAVAILABLE,
details: str | None = "mock\ndetails",
debug: str | None = "mock debug_error_string",
) -> grpc.aio.AioRpcError:
"""Mock error for testing."""
return grpc.aio.AioRpcError(
code=grpc.StatusCode.UNAVAILABLE,
code=code,
initial_metadata=grpc.aio.Metadata(),
trailing_metadata=grpc.aio.Metadata(),
details="mock details",
debug_error_string="mock debug_error_string",
details=details,
debug_error_string=debug,
)


Expand Down Expand Up @@ -240,7 +245,8 @@ async def test_streaming_error( # pylint: disable=too-many-arguments
"frequenz.client.base.streaming",
logging.ERROR,
"test_helper: connection ended, retry limit exceeded (mock progress), "
f"giving up. Error: {error}.",
"giving up. Error: code=UNAVAILABLE details='mock\\ndetails' "
"debug_error_string='mock debug_error_string'.",
),
(
"frequenz.client.base.streaming",
Expand All @@ -250,6 +256,53 @@ async def test_streaming_error( # pylint: disable=too-many-arguments
]


@pytest.mark.parametrize(
"error, expected",
[
pytest.param(
make_error(
grpc.StatusCode.DEADLINE_EXCEEDED,
"Stream removed (Deadline Exceeded)",
"DEADLINE_EXCEEDED:Stream removed (Deadline Exceeded)",
),
"Error: code=DEADLINE_EXCEEDED details='Stream removed (Deadline Exceeded)'",
id="redundant-debug",
),
pytest.param(
make_error(
grpc.StatusCode.UNAVAILABLE,
"failed to connect",
"UNKNOWN:Error received from peer ipv4:10.0.0.1:443",
),
"Error: code=UNAVAILABLE details='failed to connect' "
"debug_error_string='UNKNOWN:Error received from peer ipv4:10.0.0.1:443'",
id="informative-debug",
),
pytest.param(
make_error(grpc.StatusCode.INTERNAL, "first\nsecond\r\nthird", "a\rb\nc"),
r"Error: code=INTERNAL details='first\nsecond\r\nthird' "
r"debug_error_string='a\rb\nc'",
id="multiline",
),
pytest.param(
make_error(grpc.StatusCode.UNAUTHENTICATED, None, None),
"Error: code=UNAUTHENTICATED details=None",
id="missing",
),
pytest.param(
make_error(grpc.StatusCode.UNAUTHENTICATED, "", ""),
"Error: code=UNAUTHENTICATED details=''",
id="empty",
),
],
)
def test_format_error(error: grpc.aio.AioRpcError, expected: str) -> None:
"""Test that errors are formatted on a single line."""
formatted = _format_error(error)
assert formatted == expected
assert len(formatted.splitlines()) == 1


async def test_streaming_transform_error( # pylint: disable=too-many-arguments
no_retry: mock.MagicMock, # pylint: disable=redefined-outer-name
receiver_ready_event: asyncio.Event, # pylint: disable=redefined-outer-name
Expand Down Expand Up @@ -366,13 +419,15 @@ async def test_retry_next_interval_zero( # pylint: disable=too-many-arguments
"frequenz.client.base.streaming",
logging.WARNING,
"test_helper: connection ended, retrying mock progress in 0.000 "
f"seconds. Error: {error}.",
"seconds. Error: code=UNAVAILABLE details='mock\\ndetails' "
"debug_error_string='mock debug_error_string'.",
),
(
"frequenz.client.base.streaming",
logging.ERROR,
"test_helper: connection ended, retry limit exceeded (mock progress), "
f"giving up. Error: {error}.",
"giving up. Error: code=UNAVAILABLE details='mock\\ndetails' "
"debug_error_string='mock debug_error_string'.",
),
]

Expand Down