Skip to content

Commit

Permalink
Merge pull request #29 from Moesif/feature-log-body-option
Browse files Browse the repository at this point in the history
Add: LogBody configuration option to remove request and response body
  • Loading branch information
matthewoates committed Jun 25, 2019
2 parents 0b92104 + f81643d commit d315b0e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 32 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down
32 changes: 17 additions & 15 deletions moesifdjango/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand Down
30 changes: 16 additions & 14 deletions moesifdjango/middleware_pre19.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d315b0e

Please sign in to comment.