diff --git a/apitally/client/client_asyncio.py b/apitally/client/client_asyncio.py index daa86d4..995a6f4 100644 --- a/apitally/client/client_asyncio.py +++ b/apitally/client/client_asyncio.py @@ -5,7 +5,7 @@ import random import time from functools import partial -from typing import Any, Dict, Iterable, Optional, Tuple +from typing import Any, AsyncIterator, Dict, Optional, Tuple from uuid import UUID import backoff @@ -62,6 +62,11 @@ async def _run_sync_loop(self) -> None: except Exception: # pragma: no cover logger.exception("An error occurred during sync with Apitally hub") + try: + self.request_logger.maybe_rotate_file() + except Exception: # pragma: no cover + logger.exception("An error occurred while rotating request log files") + await asyncio.sleep(1) def stop_sync_loop(self) -> None: @@ -139,7 +144,7 @@ async def _send_sync_data(self, client: httpx.AsyncClient, data: Dict[str, Any]) response = await client.post(url="/sync", json=data) self._handle_hub_response(response) - async def _send_log_data(self, client: httpx.AsyncClient, uuid: UUID, stream: Iterable[bytes]) -> None: + async def _send_log_data(self, client: httpx.AsyncClient, uuid: UUID, stream: AsyncIterator[bytes]) -> None: logger.debug("Streaming request log data to Apitally hub") response = await client.post(url=f"{self.hub_url}/log?uuid={uuid}", content=stream) self._handle_hub_response(response) diff --git a/apitally/client/request_logging.py b/apitally/client/request_logging.py index 9bfad0e..b4e0bbd 100644 --- a/apitally/client/request_logging.py +++ b/apitally/client/request_logging.py @@ -9,7 +9,7 @@ from functools import lru_cache from io import BufferedReader from pathlib import Path -from typing import Any, Callable, Dict, Iterator, List, Mapping, Optional, Tuple, TypedDict +from typing import Any, AsyncIterator, Callable, Dict, List, Mapping, Optional, Tuple, TypedDict from urllib.parse import parse_qsl, urlencode, urlparse, urlunparse from uuid import uuid4 @@ -126,7 +126,7 @@ def write_line(self, data: bytes) -> None: def open_compressed(self) -> BufferedReader: return open(self.path, "rb") - def stream_lines_compressed(self) -> Iterator[bytes]: + async def stream_lines_compressed(self) -> AsyncIterator[bytes]: with open(self.path, "rb") as fp: for line in fp: yield line diff --git a/tests/test_client_request_logging.py b/tests/test_client_request_logging.py index 6ab2796..9be83f2 100644 --- a/tests/test_client_request_logging.py +++ b/tests/test_client_request_logging.py @@ -56,7 +56,7 @@ def response_dict() -> ResponseDict: } -def test_request_logger_end_to_end( +async def test_request_logger_end_to_end( request_logger: RequestLogger, request_dict: RequestDict, response_dict: ResponseDict ): for _ in range(3): @@ -70,7 +70,7 @@ def test_request_logger_end_to_end( assert file is not None compressed_data1 = b"" - for chunk in file.stream_lines_compressed(): + async for chunk in file.stream_lines_compressed(): compressed_data1 += chunk assert len(compressed_data1) > 0