Skip to content

Commit ece32c4

Browse files
authoredFeb 24, 2020
Merge pull request #9 from Moesif/add-capture-outgoing-support
Add: Support for capturing outgoing requests
2 parents 7f9c2ba + 62ca119 commit ece32c4

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed
 

‎README.md

+31
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,37 @@ Type: `Boolean`
179179

180180
`LOG_BODY` is default to true, set to false to remove logging request and response body to Moesif.
181181

182+
#### __`CAPTURE_OUTGOING_REQUESTS`__
183+
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.
184+
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.
185+
186+
```python
187+
from moesif_aws_lambda.middleware import *
188+
start_capture_outgoing(moesif_options) # moesif_options are the configuration options.
189+
```
190+
191+
##### __`SKIP_OUTGOING`__
192+
(optional) _(req, res) => boolean_, a function that takes a [Requests](http://docs.python-requests.org/en/master/api/) request and response,
193+
and returns true if you want to skip this particular event.
194+
195+
##### __`IDENTIFY_USER_OUTGOING`__
196+
(optional, but highly recommended) _(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 user id used by your system. While Moesif tries to identify users automatically,
197+
but different frameworks and your implementation might be very different, it would be helpful and much more accurate to provide this function.
198+
199+
##### __`IDENTIFY_COMPANY_OUTGOING`__
200+
(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 company id for this event.
201+
202+
##### __`GET_METADATA_OUTGOING`__
203+
(optional) _(req, res) => dictionary_, a function that takes [Requests](http://docs.python-requests.org/en/master/api/) request and response, and
204+
returns a dictionary (must be able to be encoded into JSON). This allows
205+
to associate this event with custom metadata. For example, you may want to save a VM instance_id, a trace_id, or a tenant_id with the request.
206+
207+
##### __`GET_SESSION_TOKEN_OUTGOING`__
208+
(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.
209+
210+
##### __`LOG_BODY_OUTGOING`__
211+
(optional) _boolean_, default True, Set to False to remove logging request and response body.
212+
182213
## Update User
183214

184215
### Update A Single User

‎lambda_function.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
from moesif_aws_lambda.middleware import MoesifLogger
1+
from moesif_aws_lambda.middleware import *
22
import os
3+
import requests
4+
import json
35

46
moesif_options = {
57
'LOG_BODY': True,
@@ -8,6 +10,13 @@
810

911
@MoesifLogger(moesif_options)
1012
def lambda_handler(event, context):
13+
14+
# Outgoing API call to third parties like Github / Stripe or to your own dependencies
15+
start_capture_outgoing(moesif_options)
16+
third_party = requests.get('https://httpbin.org/ip', json=json.dumps({'test': 2}),
17+
headers={"content-type": "text", "Authorization": "Bearer sdf4854wer"},
18+
auth=('Basic', "testauth"))
19+
1120
return {
1221
'statusCode': 200,
1322
'isBase64Encoded': False,

‎moesif_aws_lambda/middleware.py

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
from urllib import urlencode
1818
except ImportError:
1919
from urllib.parse import urlencode
20+
from moesifpythonrequest.start_capture.start_capture import StartCapture
21+
22+
def start_capture_outgoing(moesif_options):
23+
try:
24+
if moesif_options.get('DEBUG', False):
25+
print('Start capturing outgoing requests')
26+
# Start capturing outgoing requests
27+
moesif_options['APPLICATION_ID'] = os.environ["MOESIF_APPLICATION_ID"]
28+
StartCapture().start_capture_outgoing(moesif_options)
29+
except Exception as e:
30+
print('Error while starting to capture the outgoing events')
31+
print(e)
2032

2133
# Initialized the client
2234
if os.environ["MOESIF_APPLICATION_ID"]:

‎requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
moesifapi==1.3.2
22
lambda_decorators==0.3.0
3+
moesifpythonrequest==0.2.0

‎setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# Versions should comply with PEP440. For a discussion on single-sourcing
2929
# the version across setup.py and the project code, see
3030
# https://packaging.python.org/en/latest/single_source_version.html
31-
version='1.0.6',
31+
version='1.0.7',
3232

3333
description='Moesif Middleware to automatically log API calls from AWS Lambda functions',
3434
long_description=long_description,
@@ -84,7 +84,7 @@
8484
# your project is installed. For an analysis of "install_requires" vs pip's
8585
# requirements files see:
8686
# https://packaging.python.org/en/latest/requirements.html
87-
install_requires=['moesifapi', 'lambda_decorators'],
87+
install_requires=['moesifapi', 'lambda_decorators', 'moesifpythonrequest'],
8888

8989
# List additional groups of dependencies here (e.g. development
9090
# dependencies). You can install these using the following syntax,

0 commit comments

Comments
 (0)