From f1adc4039147159bde472583df21340e60769e79 Mon Sep 17 00:00:00 2001 From: Lukas Vojt Date: Tue, 20 Aug 2024 13:10:38 +0200 Subject: [PATCH] Lukas/log request params on error (#33) * fix(verbose_logging): exclude auth and headers on errors also json dump params * chore(version): release 0.16.2 --- request_session/request_session.py | 26 +++++++++++++++++--------- setup.py | 2 +- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/request_session/request_session.py b/request_session/request_session.py index 1be392f..a36138f 100644 --- a/request_session/request_session.py +++ b/request_session/request_session.py @@ -317,7 +317,7 @@ def _process( max_runs = 1 + (self.max_retries if max_retries is None else max_retries) run, retries_on_econnreset = 0, 0 # this will set maximum number of retries to max_runs where econnreset retries - # are not counting and maximum number of of retries on econnreset is also + # are not counting and maximum number of retries on econnreset is also # set to max_runs while run < max_runs + retries_on_econnreset: run += 1 @@ -480,11 +480,13 @@ def _log_with_params( """ extra_params = ( { - "request_params": { - k: v - for k, v in deepcopy(request_params).items() - if k not in self.request_param_logging_blacklist - }, + "request_params": json.dumps( + { + k: v + for k, v in deepcopy(request_params).items() + if k not in self.request_param_logging_blacklist + } + ), "response_text": self.get_response_text(response), } if self.verbose_logging @@ -557,11 +559,11 @@ def _exception_log_and_metrics( # type: (...) -> None """Assign appropriate metric and log for exception. - :param requests.RequestException error: exception that occured + :param requests.RequestException error: exception that occurred :param str request_category: String describing request category. :param Dict request_params: Parameters used to make the HTTP call. :param List[str] dd_tags: Tags to increment metric with. - :param Union[int, None] Status_code: HTTP status code of the response. + :param Union[int, None] status_code: HTTP status code of the response. """ tags = ["status:error"] @@ -586,7 +588,13 @@ def _exception_log_and_metrics( tags.append("error:request_exception") if self.verbose_logging is True: - extra_params["request_params"] = json.dumps(request_params) + extra_params["request_params"] = json.dumps( + { + k: v + for k, v in deepcopy(request_params).items() + if k not in self.request_param_logging_blacklist + } + ) self.log( "exception", diff --git a/setup.py b/setup.py index b65e4c0..0ef74d5 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ setup( name="request_session", - version="0.16.1", + version="0.16.2", url="https://github.com/kiwicom/request-session", description="Python HTTP requests on steroids", long_description=readme,