From f81643d00f60966ceeee34b0745e0572ebda339b Mon Sep 17 00:00:00 2001 From: Keyur Date: Tue, 25 Jun 2019 10:16:27 -0700 Subject: [PATCH] Add: LogBody configuration option to remove request and response body Add: LogBody configuration option to remove request and response body Update: moesifpythonrequest dependency to 0.1.10 Update: README.md Bump version to 1.5.7 --- README.md | 7 ++++++- moesifdjango/middleware.py | 32 +++++++++++++++++--------------- moesifdjango/middleware_pre19.py | 30 ++++++++++++++++-------------- requirements.txt | 2 +- setup.py | 2 +- 5 files changed, 41 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index e88c614..5152288 100755 --- a/README.md +++ b/README.md @@ -96,7 +96,6 @@ You can find your Application Id from [_Moesif Dashboard_](https://www.moesif.co #### __`GET_SESSION_TOKEN`__ (optional) _(request, response) => string_, a function that takes a request and a response, and returns a string that is the session token for this event. Again, Moesif tries to get the session token automatically, but if you setup is very different from standard, this function will be very help for tying events together, and help you replay the events. - #### __`GET_METADATA`__ (optional) _(request, response) => dictionary_, getMetadata is a function that returns an object that allows you to add custom metadata that will be associated with the event. The metadata must be a dictionary that can be converted to JSON. For example, you may want to save a VM instance_id, a trace_id, or a tenant_id with the request. @@ -122,6 +121,9 @@ _boolean_, Default False. Set to True to use Celery for queuing sending data to #### __`RESPONSE_BODY_MASKS`__ (deprecated), _string[]_, performs the same task for response body. Will be removed in future version. Replaced by the function based 'MASK_EVENT_MODEL' for additional flexibility. +#### __`LOG_BODY`__ +(optional) _boolean_, a flag to remove logging request and response body. + #### __`CAPTURE_OUTGOING_REQUESTS`__ _boolean_, Default False. Set to True to capture all outgoing API calls from your app to third parties like Stripe or to your own dependencies while using [Requests](http://docs.python-requests.org/en/master/) library. The options below is applied to outgoing API calls. When the request is outgoing, for options functions that take request and response as input arguments, the request and response objects passed in are [Requests](http://docs.python-requests.org/en/master/api/) request or response objects. @@ -145,6 +147,9 @@ to associate this event with custom metadata. For example, you may want to save ##### __`GET_SESSION_TOKEN_OUTGOING`__ (optional) _(req, res) => string_, a function that takes [Requests](http://docs.python-requests.org/en/master/api/) request and response, and returns a string that is the session token for this event. Again, Moesif tries to get the session token automatically, but if you setup is very different from standard, this function will be very help for tying events together, and help you replay the events. +##### __`LOG_BODY_OUTGOING`__ +(optional) _boolean_, a flag to remove logging request and response body. + ### Example: ```python diff --git a/moesifdjango/middleware.py b/moesifdjango/middleware.py index 2a5b8a8..9ee9259 100644 --- a/moesifdjango/middleware.py +++ b/moesifdjango/middleware.py @@ -63,6 +63,7 @@ def __init__(self, get_response): # One-time configuration and initialization. self.middleware_settings = settings.MOESIF_MIDDLEWARE self.DEBUG = self.middleware_settings.get('LOCAL_DEBUG', False) + self.LOG_BODY = self.middleware_settings.get('LOG_BODY', True) self.client = MoesifAPIClient(self.middleware_settings.get('APPLICATION_ID')) # below comment for setting moesif base_uri to a test server. if self.middleware_settings.get('LOCAL_DEBUG', False): @@ -167,17 +168,18 @@ def flatten_to_string(value): req_body = None req_body_transfer_encoding = None - try: - # print("about to serialize request body" + request.body) - if self.DEBUG: - print("about to process request body") - if request._mo_body: - req_body = json.loads(request._mo_body) - req_body = mask_body(req_body, self.middleware_settings.get('REQUEST_BODY_MASKS')) - except: - if request._mo_body: - req_body = base64.standard_b64encode(request._mo_body) - req_body_transfer_encoding = 'base64' + if self.LOG_BODY: + try: + # print("about to serialize request body" + request.body) + if self.DEBUG: + print("about to process request body") + if request._mo_body: + req_body = json.loads(request._mo_body) + req_body = mask_body(req_body, self.middleware_settings.get('REQUEST_BODY_MASKS')) + except: + if request._mo_body: + req_body = base64.standard_b64encode(request._mo_body) + req_body_transfer_encoding = 'base64' ip_address = get_client_ip(request) uri = request.scheme + "://" + request.get_host() + request.get_full_path() @@ -196,10 +198,10 @@ def mapper(key): rsp_body = None rsp_body_transfer_encoding = None - if self.DEBUG: - print("about to process response") - print(response.content) - if response.content: + if self.LOG_BODY and response.content: + if self.DEBUG: + print("about to process response") + print(response.content) try: rsp_body = json.loads(response.content) if self.DEBUG: diff --git a/moesifdjango/middleware_pre19.py b/moesifdjango/middleware_pre19.py index a1f3977..d6cacb0 100644 --- a/moesifdjango/middleware_pre19.py +++ b/moesifdjango/middleware_pre19.py @@ -31,6 +31,7 @@ class MoesifMiddlewarePre19(object): def __init__(self): self.middleware_settings = settings.MOESIF_MIDDLEWARE self.DEBUG = self.middleware_settings.get('LOCAL_DEBUG', False) + self.LOG_BODY = self.middleware_settings.get('LOG_BODY', True) self.client = MoesifAPIClient(self.middleware_settings.get('APPLICATION_ID')) # below comment for setting moesif base_uri to a test server. if self.middleware_settings.get('LOCAL_DEBUG', False): @@ -149,16 +150,17 @@ def flatten_to_string(value): req_body = None req_body_transfer_encoding = None - try: - # print("about to serialize request body" + request.body) - if self.DEBUG: - print("about to process request body") - if request._mo_body: - req_body = json.loads(request._mo_body) - except: - if request._mo_body: - req_body = base64.standard_b64encode(request._mo_body) - req_body_transfer_encoding = 'base64' + if self.LOG_BODY: + try: + # print("about to serialize request body" + request.body) + if self.DEBUG: + print("about to process request body") + if request._mo_body: + req_body = json.loads(request._mo_body) + except: + if request._mo_body: + req_body = base64.standard_b64encode(request._mo_body) + req_body_transfer_encoding = 'base64' ip_address = get_client_ip(request) @@ -176,10 +178,10 @@ def mapper(key): rsp_body = None rsp_body_transfer_encoding = None - if self.DEBUG: - print("about to process response") - print(response.content) - if response.content: + if self.LOG_BODY and response.content: + if self.DEBUG: + print("about to process response") + print(response.content) try: rsp_body = json.loads(response.content) if self.DEBUG: diff --git a/requirements.txt b/requirements.txt index 987a1da..bfbe3a9 100755 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,4 @@ nose==1.3.7 isodatetimehandler==1.0.2 moesifapi==1.2.5 celery==4.1.0 -moesifpythonrequest==0.1.9 +moesifpythonrequest==0.1.10 diff --git a/setup.py b/setup.py index 2ec44b6..5edf78f 100644 --- a/setup.py +++ b/setup.py @@ -28,7 +28,7 @@ # Versions should comply with PEP440. For a discussion on single-sourcing # the version across setup.py and the project code, see # https://packaging.python.org/en/latest/single_source_version.html - version='1.5.6', + version='1.5.7', description='Moesif Middleware for Python Django', long_description=long_description,