Skip to content

Commit 6596b31

Browse files
committed
2 parents 1f73cce + fd412c3 commit 6596b31

File tree

1 file changed

+93
-42
lines changed

1 file changed

+93
-42
lines changed

README.md

Lines changed: 93 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
Middleware (NodeJS) to automatically log API calls from AWS Lambda functions
1010
and sends to [Moesif](https://www.moesif.com) for API analytics and log analysis.
1111

12-
Designed for APIs that are hosted on AWS Lambda and using
13-
Amazon API Gateway as a trigger.
14-
12+
Designed for APIs that are hosted on AWS Lambda using Amazon API Gateway as a trigger.
1513

1614
This middleware expects the
1715
[Lambda proxy integration type.](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html#api-gateway-set-up-lambda-proxy-integration-on-proxy-resource)
@@ -55,6 +53,9 @@ const moesifOptions = {
5553

5654
identifyUser: function (event, context) {
5755
return event.requestContext.identity.cognitoIdentityId
56+
},
57+
identifyCompany: function (event, context) {
58+
return '5678'
5859
}
5960
};
6061

@@ -89,13 +90,17 @@ into the [_Moesif Portal_](https://www.moesif.com/), click on the top right menu
8990

9091
## Configuration options
9192

93+
#### __`logBody`__
94+
Type: `Boolean`
95+
logBody is default to true, set to false to remove logging request and response body to Moesif.
9296

9397
#### __`identifyUser`__
9498

9599
Type: `(event, context) => String`
96100
identifyUser is a function that takes AWS lambda `event` and `context` objects as arguments
97-
and returns a userId. This helps us attribute requests to unique users. Even though Moesif can
98-
automatically retrieve the userId without this, this is highly recommended to ensure accurate attribution.
101+
and returns a userId. This enables Moesif to attribute API requests to individual unique users
102+
so you can understand who calling your API. This can be used simultaneously with `identifyCompany`
103+
to track both individual customers and the companies their a part of.
99104

100105

101106
```javascript
@@ -105,34 +110,34 @@ options.identifyUser = function (event, context) {
105110
}
106111
```
107112

108-
#### __`getSessionToken`__
113+
#### __`identifyCompany`__
109114

110115
Type: `(event, context) => String`
111-
getSessionToken a function that takes AWS lambda `event` and `context` objects as arguments and returns a
112-
session token (i.e. such as an API key).
116+
identifyCompany is a function that takes AWS lambda `event` and `context` objects as arguments
117+
and returns a companyId. If your business is B2B, this enables Moesif to attribute
118+
API requests to specific companies or organizations so you can understand which accounts are
119+
calling your API. This can be used simultaneously with `identifyUser` to track both
120+
individual customers and the companies their a part of.
113121

114122

115123
```javascript
116-
options.getSessionToken = function (event, context) {
117-
// your code here, must return a string.
118-
return event.headers['Authorization'];
124+
options.identifyCompany = function (event, context) {
125+
// your code here, must return a string
126+
return '5678'
119127
}
120128
```
121129

122-
#### __`getTags`__
130+
#### __`getSessionToken`__
123131

124132
Type: `(event, context) => String`
125-
getTags is a function that takes AWS lambda `event` and `context` objects as arguments and returns a comma-separated string containing a list of tags.
126-
See Moesif documentation for full list of tags.
133+
getSessionToken a function that takes AWS lambda `event` and `context` objects as arguments and returns a
134+
session token (i.e. such as an API key).
127135

128136

129137
```javascript
130-
options.getTags = function (event, context) {
131-
// your code here. must return a comma-separated string.
132-
if (event.path.startsWith('/users') && event.httpMethod == 'GET'){
133-
return 'user'
134-
}
135-
return 'random_tag_1, random_tag2'
138+
options.getSessionToken = function (event, context) {
139+
// your code here, must return a string.
140+
return event.headers['Authorization'];
136141
}
137142
```
138143

@@ -150,6 +155,23 @@ options.getApiVersion = function (event, context) {
150155
}
151156
```
152157

158+
#### __`getMetadata`__
159+
160+
Type: `(event, context) => String`
161+
getMetadata is a function that AWS lambda `event` and `context` objects as arguments and returns an object that allows you
162+
to add custom metadata that will be associated with the req. The metadata must be a simple javascript object 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.
163+
164+
165+
```javascript
166+
options.getMetadata = function (event, context) {
167+
// your code here:
168+
return {
169+
foo: 'custom data',
170+
bar: 'another custom data'
171+
};
172+
}
173+
```
174+
153175
#### __`skip`__
154176

155177
Type: `(event, context) => Boolean`
@@ -189,7 +211,7 @@ options.maskContent = function(moesifEvent) {
189211
```json
190212
{
191213
"request": {
192-
"time": "2016-09-09T04:45:42.914",
214+
"time": "2019-08-08T04:45:42.914",
193215
"uri": "https://api.acmeinc.com/items/83738/reviews/",
194216
"verb": "POST",
195217
"api_version": "1.1.0",
@@ -218,7 +240,7 @@ options.maskContent = function(moesifEvent) {
218240
}
219241
},
220242
"response": {
221-
"time": "2016-09-09T04:45:42.914",
243+
"time": "2019-08-08T04:45:42.924",
222244
"status": 500,
223245
"headers": {
224246
"Vary": "Accept-Encoding",
@@ -232,7 +254,8 @@ options.maskContent = function(moesifEvent) {
232254
"Message": "Missing field location"
233255
}
234256
},
235-
"user_id": "mndug437f43",
257+
"user_id": "my_user_id",
258+
"company_id": "my_company_id",
236259
"session_token":"end_user_session_token",
237260
"tags": "tag1, tag2"
238261
}
@@ -242,36 +265,41 @@ options.maskContent = function(moesifEvent) {
242265
For more documentation regarding what fields and meaning,
243266
see below or the [Moesif Node API Documentation](https://www.moesif.com/docs/api?javascript).
244267

245-
Fields | Required | Description
268+
Name | Required | Description
246269
--------- | -------- | -----------
247-
request.time | Required | Timestamp for the request in ISO 8601 format
248-
request.uri | Required | Full uri such as https://api.com/?query=string including host, query string, etc
249-
request.verb | Required | HTTP method used, i.e. `GET`, `POST`
250-
request.api_version | Optional | API Version you want to tag this request with
251-
request.ip_address | Optional | IP address of the end user
252-
request.headers | Required | Headers of the request
253-
request.body | Optional | Body of the request in JSON format
270+
request | __true__ | The object that specifies the request message
271+
request.time| __true__ | Timestamp for the request in ISO 8601 format
272+
request.uri| __true__ | Full uri such as _https://api.com/?query=string_ including host, query string, etc
273+
request.verb| __true__ | HTTP method used, i.e. `GET`, `POST`
274+
request.api_version| false | API Version you want to tag this request with such as _1.0.0_
275+
request.ip_address| false | IP address of the requester, If not set, we use the IP address of your logging API calls.
276+
request.headers| __true__ | Headers of the request as a `Map<string, string>`. Multiple headers with the same key name should be combined together such that the values are joined by a comma. [HTTP Header Protocol on w3.org](https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2)
277+
request.body| false | Body of the request in JSON format or Base64 encoded binary data (see _transfer_encoding_)
278+
request.transfer_encoding| false | A string that specifies the transfer encoding of Body being sent to Moesif. If field nonexistent, body assumed to be JSON or text. Only possible value is _base64_ for sending binary data like protobuf
254279
||
255-
response.time | Required | Timestamp for the response in ISO 8601 format
256-
response.status | Required | HTTP status code such as 200 or 500
257-
request.ip_address | Optional | IP address of the responding server
258-
response.headers | Required | Headers of the response
259-
response.body | Required | Body of the response in JSON format
280+
response | false | The object that specifies the response message, not set implies no response received such as a timeout.
281+
response.time| __true__ | Timestamp for the response in ISO 8601 format
282+
response.status| __true__ | HTTP status code as number such as _200_ or _500_
283+
response.ip_address| false | IP address of the responding server
284+
response.headers| __true__ | Headers of the response as a `Map<string, string>`. Multiple headers with the same key name should be combined together such that the values are joined by a comma. [HTTP Header Protocol on w3.org](https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2)
285+
response.body| false | Body of the response in JSON format or Base64 encoded binary data (see _transfer_encoding_)
286+
response.transfer_encoding| false | A string that specifies the transfer encoding of Body being sent to Moesif. If field nonexistent, body assumed to be JSON or text. Only possible value is _base64_ for sending binary data like protobuf
287+
||
288+
session_token | _Recommend_ | The end user session token such as a JWT or API key, which may or may not be temporary. Moesif will auto-detect the session token automatically if not set.
289+
user_id | _Recommend_ | Identifies this API call to a permanent user_id
290+
metadata | false | A JSON Object consisting of any custom metadata to be stored with this event.
260291

261292

262293
### updateUser method
263294

264-
A method is attached to the moesif middleware object to update the users profile or metadata.
295+
A method is attached to the Moesif middleware object to update the user's profile or metadata.
265296

266297

267298
```javascript
268-
'use strict'
269-
const moesif = require('moesif-aws-lambda');
270-
271299
const moesifOptions = {
272-
applicationId: 'Your Moesif application_id',
273-
300+
applicationId: 'Your Moesif Application Id',
274301
};
302+
275303
var moesifMiddleware = moesif(options);
276304
var user = {
277305
userId: 'your user id', // required.
@@ -285,6 +313,29 @@ moesifMiddleware.updateUser(user, callback);
285313

286314
```
287315

316+
### updateCompany method
317+
318+
A method is attached to the Moesif middleware object to update the company's profile or metadata.
319+
320+
321+
```javascript
322+
const moesifOptions = {
323+
applicationId: 'Your Moesif Application Id',
324+
};
325+
326+
var moesifMiddleware = moesif(options);
327+
var company = {
328+
companyId: 'your company id', // required.
329+
companyDomain: 'acmeinc.com',
330+
metadata: {
331+
numEmployees: 9001
332+
}
333+
}
334+
335+
moesifMiddleware.updateCompany(user, callback);
336+
337+
```
338+
288339
The metadata field can be any custom data you want to set on the user.
289340
The userId field is required.
290341

0 commit comments

Comments
 (0)