Skip to content

Commit

Permalink
added support for metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
xinghengwang committed Oct 11, 2017
1 parent 38167b4 commit 447bd83
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ 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.

#### __`MASK_EVENT_MODEL`__
(optional) _(EventModel) => EventModel_, a function that takes an EventModel and returns an EventModel with desired data removed. Use this if you prefer to write your own mask function than use the string based filter options: REQUEST_BODY_MASKS, REQUEST_HEADER_MASKS, RESPONSE_BODY_MASKS, & RESPONSE_HEADER_MASKS. The return value must be a valid EventModel required by Moesif data ingestion API. For details regarding EventModel please see the [Moesif Python API Documentation](https://www.moesif.com/docs/api?python).

Expand Down Expand Up @@ -114,13 +119,21 @@ def mask_event(eventmodel):
# be sure not to remove any required fields.
return eventmodel

def get_metadata(req, res):
return {
'foo': '12345',
'bar': '23456',
}


MOESIF_MIDDLEWARE = {
'APPLICATION_ID': 'Your application id',
'LOCAL_DEBUG': False,
'IDENTIFY_USER': identifyUser,
'GET_SESSION_TOKEN': get_token,
'SKIP': should_skip,
'MASK_EVENT_MODEL': mask_event,
'GET_METADATA': get_metadata,
}

```
Expand Down
13 changes: 12 additions & 1 deletion moesifdjango/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ def mapper(key):
if DEBUG:
print("can not execute identify_user function, please check moesif settings.")

metadata = None
try:
get_metadata = middleware_settings.get('GET_METADATA', None)
if get_metadata is not None:
metadata = get_metadata(request, response)
except:
if DEBUG:
print("can not execute get_metadata function, please check moesif settings.")



session_token = None
try:
Expand All @@ -195,7 +205,8 @@ def mapper(key):
event_model = EventModel(request=event_req,
response=event_rsp,
user_id=username,
session_token=session_token)
session_token=session_token,
metadata=metadata)

try:
mask_event_model = middleware_settings.get('MASK_EVENT_MODEL', None)
Expand Down
12 changes: 11 additions & 1 deletion moesifdjango/middleware_pre19.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ def mapper(key):
if self.DEBUG:
print("can not execute identify_user function, please check moesif settings.")

metadata = None
try:
get_metadata = middleware_settings.get('GET_METADATA', None)
if get_metadata is not None:
metadata = get_metadata(request, response)
except:
if DEBUG:
print("can not execute get_metadata function, please check moesif settings.")


session_token = None
try:
Expand All @@ -193,7 +202,8 @@ def mapper(key):
event_model = EventModel(request=event_req,
response=event_rsp,
user_id=username,
session_token=session_token)
session_token=session_token
metadata=metadata)

try:
mask_event_model = self.middleware_settings.get('MASK_EVENT_MODEL', None)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ jsonpickle==0.7.1
python-dateutil==2.5.3
nose==1.3.7
isodatetimehandler==1.0.2
moesifapi==1.1.0
moesifapi==1.2.1

0 comments on commit 447bd83

Please sign in to comment.