Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimon committed Nov 16, 2024
1 parent 5f502a5 commit 43d89fc
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions apitally/client/request_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,33 +165,41 @@ def log_request(self, request: RequestDict, response: ResponseDict) -> None:

if not self.config.log_request_body or not self._has_supported_content_type(request["headers"]):
request["body"] = None
if not self.config.log_response_body or not self._has_supported_content_type(response["headers"]):
response["body"] = None

if request["body"] is not None and self.config.mask_request_body_callback is not None:
elif (
self.config.mask_request_body_callback is not None
and request["body"] is not None
and request["body"] != BODY_TOO_LARGE
):
try:
request["body"] = self.config.mask_request_body_callback(
request["method"], request["path"] or parsed_url.path, request["body"]
)
except Exception:
logger.exception("User-provided mask_request_body_callback raised an exception")
except Exception: # pragma: no cover
logger.exception("User-provided mask_request_body_callback function raised an exception")
request["body"] = None
if request["body"] is None:
request["body"] = BODY_MASKED
elif len(request["body"]) > MAX_BODY_SIZE:
request["body"] = BODY_TOO_LARGE
if response["body"] is not None and self.config.mask_response_body_callback is not None:
if request["body"] is not None and len(request["body"]) > MAX_BODY_SIZE:
request["body"] = BODY_TOO_LARGE

Check warning on line 183 in apitally/client/request_logging.py

View check run for this annotation

Codecov / codecov/patch

apitally/client/request_logging.py#L183

Added line #L183 was not covered by tests

if not self.config.log_response_body or not self._has_supported_content_type(response["headers"]):
response["body"] = None
elif (
self.config.mask_response_body_callback is not None
and response["body"] is not None
and response["body"] != BODY_TOO_LARGE
):
try:
response["body"] = self.config.mask_response_body_callback(
request["method"], request["path"] or parsed_url.path, response["body"]
)
except Exception:
logger.exception("User-provided mask_response_body_callback raised an exception")
except Exception: # pragma: no cover
logger.exception("User-provided mask_response_body_callback function raised an exception")
response["body"] = None
if response["body"] is None:
response["body"] = BODY_MASKED
elif len(response["body"]) > MAX_BODY_SIZE:
response["body"] = BODY_TOO_LARGE
if response["body"] is not None and len(response["body"]) > MAX_BODY_SIZE:
response["body"] = BODY_TOO_LARGE

Check warning on line 202 in apitally/client/request_logging.py

View check run for this annotation

Codecov / codecov/patch

apitally/client/request_logging.py#L202

Added line #L202 was not covered by tests

item = {
"time_ns": time.time_ns() - response["response_time"] * 1_000_000_000,
Expand Down Expand Up @@ -284,7 +292,7 @@ def _check_writable_fs():
try:
with tempfile.TemporaryFile():
return True
except (IOError, OSError):
except (IOError, OSError): # pragma: no cover
logger.error("Unable to create temporary file for request logging")
return False

Expand All @@ -293,7 +301,7 @@ def _get_json_serializer() -> Callable[[Any], bytes]:
def default(obj: Any) -> Any:
if isinstance(obj, bytes):
return base64.b64encode(obj).decode()
raise TypeError
raise TypeError # pragma: no cover

try:
import orjson # type: ignore
Expand Down

0 comments on commit 43d89fc

Please sign in to comment.