Skip to content

Commit

Permalink
feat(verbose_logging): exclude auth and headers from logging
Browse files Browse the repository at this point in the history
they can contain secrets or objects that are not serializable
  • Loading branch information
lucas03 committed Aug 15, 2024
1 parent 92ff429 commit 19da875
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
42 changes: 42 additions & 0 deletions monitor.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
PING 1.1.1.1 (1.1.1.1): 56 data bytes
13:45:20.858347 64 bytes from 1.1.1.1: icmp_seq=0 ttl=57 time=25.972 ms
13:45:30.862935 64 bytes from 1.1.1.1: icmp_seq=1 ttl=57 time=25.363 ms
13:45:40.864336 64 bytes from 1.1.1.1: icmp_seq=2 ttl=57 time=25.508 ms
13:45:50.867034 64 bytes from 1.1.1.1: icmp_seq=3 ttl=57 time=25.704 ms
13:46:00.864104 64 bytes from 1.1.1.1: icmp_seq=4 ttl=57 time=17.554 ms
13:46:10.866148 64 bytes from 1.1.1.1: icmp_seq=5 ttl=57 time=16.935 ms
13:46:20.879561 64 bytes from 1.1.1.1: icmp_seq=6 ttl=57 time=25.127 ms
13:46:30.873691 64 bytes from 1.1.1.1: icmp_seq=7 ttl=57 time=18.626 ms
13:46:40.877977 64 bytes from 1.1.1.1: icmp_seq=8 ttl=57 time=17.579 ms
13:46:50.882024 64 bytes from 1.1.1.1: icmp_seq=9 ttl=57 time=17.424 ms
13:47:00.887970 64 bytes from 1.1.1.1: icmp_seq=10 ttl=57 time=18.074 ms
13:47:10.890842 64 bytes from 1.1.1.1: icmp_seq=11 ttl=57 time=17.206 ms
13:47:20.902612 64 bytes from 1.1.1.1: icmp_seq=12 ttl=57 time=25.747 ms
13:47:30.895695 64 bytes from 1.1.1.1: icmp_seq=13 ttl=57 time=15.749 ms
13:47:40.898916 64 bytes from 1.1.1.1: icmp_seq=14 ttl=57 time=17.057 ms
13:47:50.909489 64 bytes from 1.1.1.1: icmp_seq=15 ttl=57 time=22.829 ms
13:48:00.908748 64 bytes from 1.1.1.1: icmp_seq=16 ttl=57 time=18.078 ms
13:48:10.916153 64 bytes from 1.1.1.1: icmp_seq=17 ttl=57 time=24.551 ms
13:48:20.918031 64 bytes from 1.1.1.1: icmp_seq=18 ttl=57 time=23.363 ms
13:48:30.916995 64 bytes from 1.1.1.1: icmp_seq=19 ttl=57 time=17.145 ms
13:48:40.921970 64 bytes from 1.1.1.1: icmp_seq=20 ttl=57 time=16.958 ms
13:48:50.934594 64 bytes from 1.1.1.1: icmp_seq=21 ttl=57 time=24.277 ms
13:49:00.937448 64 bytes from 1.1.1.1: icmp_seq=22 ttl=57 time=21.834 ms
13:49:10.937662 64 bytes from 1.1.1.1: icmp_seq=23 ttl=57 time=16.796 ms
13:49:20.949101 64 bytes from 1.1.1.1: icmp_seq=24 ttl=57 time=25.369 ms
13:49:30.945304 64 bytes from 1.1.1.1: icmp_seq=25 ttl=57 time=16.644 ms
13:49:40.947308 64 bytes from 1.1.1.1: icmp_seq=26 ttl=57 time=17.288 ms
13:49:50.951138 64 bytes from 1.1.1.1: icmp_seq=27 ttl=57 time=16.693 ms
13:50:00.962674 64 bytes from 1.1.1.1: icmp_seq=28 ttl=57 time=25.372 ms
13:50:10.959827 64 bytes from 1.1.1.1: icmp_seq=29 ttl=57 time=17.284 ms
13:50:20.967440 64 bytes from 1.1.1.1: icmp_seq=30 ttl=57 time=19.403 ms
13:50:30.971763 64 bytes from 1.1.1.1: icmp_seq=31 ttl=57 time=23.103 ms
13:50:40.966882 64 bytes from 1.1.1.1: icmp_seq=32 ttl=57 time=16.351 ms
13:50:50.973269 64 bytes from 1.1.1.1: icmp_seq=33 ttl=57 time=17.486 ms
13:51:00.979918 64 bytes from 1.1.1.1: icmp_seq=34 ttl=57 time=23.021 ms
13:51:10.974573 64 bytes from 1.1.1.1: icmp_seq=35 ttl=57 time=17.043 ms
13:51:20.978183 64 bytes from 1.1.1.1: icmp_seq=36 ttl=57 time=20.303 ms
13:51:30.983370 64 bytes from 1.1.1.1: icmp_seq=37 ttl=57 time=24.248 ms
13:51:40.979944 64 bytes from 1.1.1.1: icmp_seq=38 ttl=57 time=17.548 ms
13:51:50.988132 64 bytes from 1.1.1.1: icmp_seq=39 ttl=57 time=20.933 ms
13:52:00.994559 64 bytes from 1.1.1.1: icmp_seq=40 ttl=57 time=26.143 ms
13 changes: 12 additions & 1 deletion request_session/request_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import time
from collections import namedtuple
from copy import deepcopy
from typing import List # pylint: disable=unused-import
from typing import Any, Callable, Dict, Optional, Tuple, Union

Expand Down Expand Up @@ -40,6 +41,9 @@ class RequestSession(object):
server error. Defaults to 0.
:param bool verbose_logging: (optional) If true, add request's parameters to event
being logged. Defaults to ``False``.
:param tuple request_param_logging_blacklist: (optional)
Request params keys that won't be logged with verbose_logging.
e.g. auth or header keys might contain secrets. Defaults to ``("auth", "headers")``.
:param str request_category: (optional) Name of the event. ``request_category`` has
to passed to the object or as an argument when calling some HTTP method.
:param bool raise_for_status: (optional) Raise an exception in case of an error.
Expand Down Expand Up @@ -72,6 +76,7 @@ def __init__(
verify=True, # type: Union[bool, str]
max_retries=0, # type: int
verbose_logging=False, # type: bool
request_param_logging_blacklist=None, # type: Optional[Tuple[str]]
headers=None, # type: Optional[Dict]
request_category=None, # type: Optional[str]
raise_for_status=True, # type: bool
Expand Down Expand Up @@ -101,6 +106,7 @@ def __init__(
self.verify = verify
self.max_retries = max_retries
self.verbose_logging = verbose_logging
self.request_param_logging_blacklist = request_param_logging_blacklist or ("auth", "headers")
self.headers = headers
self.request_category = request_category
self.raise_for_status = raise_for_status
Expand Down Expand Up @@ -476,9 +482,14 @@ def _log_with_params(
:param List[str] tags: Tags denoting success of the request.
:param str request_category: Category of the request.
"""

extra_params = (
{
"request_params": json.dumps(request_params),
"request_params": {
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
Expand Down

0 comments on commit 19da875

Please sign in to comment.