Skip to content

Commit c1d2413

Browse files
committed
Update: README with latest options.
1 parent f59cc7d commit c1d2413

File tree

1 file changed

+90
-40
lines changed

1 file changed

+90
-40
lines changed

README.md

Lines changed: 90 additions & 40 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,6 +90,9 @@ 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

@@ -105,34 +109,34 @@ options.identifyUser = function (event, context) {
105109
}
106110
```
107111

108-
#### __`getSessionToken`__
112+
#### __`identifyCompany`__
109113

110114
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).
115+
identifyCompany is a function that takes AWS lambda `event` and `context` objects as arguments
116+
and returns a companyId. If your business is B2B, this enables Moesif to attribute
117+
API requests to specific companies or organizations so you can understand which accounts are
118+
calling your API. This can be used simultaneously with `identifyUser` to track both
119+
individual customers and the companies their a part of.
113120

114121

115122
```javascript
116-
options.getSessionToken = function (event, context) {
117-
// your code here, must return a string.
118-
return event.headers['Authorization'];
123+
options.identifyCompany = function (event, context) {
124+
// your code here, must return a string
125+
return '5678'
119126
}
120127
```
121128

122-
#### __`getTags`__
129+
#### __`getSessionToken`__
123130

124131
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.
132+
getSessionToken a function that takes AWS lambda `event` and `context` objects as arguments and returns a
133+
session token (i.e. such as an API key).
127134

128135

129136
```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'
137+
options.getSessionToken = function (event, context) {
138+
// your code here, must return a string.
139+
return event.headers['Authorization'];
136140
}
137141
```
138142

@@ -150,6 +154,23 @@ options.getApiVersion = function (event, context) {
150154
}
151155
```
152156

157+
#### __`getMetadata`__
158+
159+
Type: `(event, context) => String`
160+
getMetadata is a function that AWS lambda `event` and `context` objects as arguments and returns an object that allows you
161+
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.
162+
163+
164+
```javascript
165+
options.getMetadata = function (event, context) {
166+
// your code here:
167+
return {
168+
foo: 'custom data',
169+
bar: 'another custom data'
170+
};
171+
}
172+
```
173+
153174
#### __`skip`__
154175

155176
Type: `(event, context) => Boolean`
@@ -189,7 +210,7 @@ options.maskContent = function(moesifEvent) {
189210
```json
190211
{
191212
"request": {
192-
"time": "2016-09-09T04:45:42.914",
213+
"time": "2019-08-08T04:45:42.914",
193214
"uri": "https://api.acmeinc.com/items/83738/reviews/",
194215
"verb": "POST",
195216
"api_version": "1.1.0",
@@ -218,7 +239,7 @@ options.maskContent = function(moesifEvent) {
218239
}
219240
},
220241
"response": {
221-
"time": "2016-09-09T04:45:42.914",
242+
"time": "2019-08-08T04:45:42.924",
222243
"status": 500,
223244
"headers": {
224245
"Vary": "Accept-Encoding",
@@ -232,7 +253,8 @@ options.maskContent = function(moesifEvent) {
232253
"Message": "Missing field location"
233254
}
234255
},
235-
"user_id": "mndug437f43",
256+
"user_id": "my_user_id",
257+
"company_id": "my_company_id",
236258
"session_token":"end_user_session_token",
237259
"tags": "tag1, tag2"
238260
}
@@ -242,36 +264,41 @@ options.maskContent = function(moesifEvent) {
242264
For more documentation regarding what fields and meaning,
243265
see below or the [Moesif Node API Documentation](https://www.moesif.com/docs/api?javascript).
244266

245-
Fields | Required | Description
267+
Name | Required | Description
246268
--------- | -------- | -----------
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
269+
request | __true__ | The object that specifies the request message
270+
request.time| __true__ | Timestamp for the request in ISO 8601 format
271+
request.uri| __true__ | Full uri such as _https://api.com/?query=string_ including host, query string, etc
272+
request.verb| __true__ | HTTP method used, i.e. `GET`, `POST`
273+
request.api_version| false | API Version you want to tag this request with such as _1.0.0_
274+
request.ip_address| false | IP address of the requester, If not set, we use the IP address of your logging API calls.
275+
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)
276+
request.body| false | Body of the request in JSON format or Base64 encoded binary data (see _transfer_encoding_)
277+
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
254278
||
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
279+
response | false | The object that specifies the response message, not set implies no response received such as a timeout.
280+
response.time| __true__ | Timestamp for the response in ISO 8601 format
281+
response.status| __true__ | HTTP status code as number such as _200_ or _500_
282+
response.ip_address| false | IP address of the responding server
283+
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)
284+
response.body| false | Body of the response in JSON format or Base64 encoded binary data (see _transfer_encoding_)
285+
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
286+
||
287+
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.
288+
user_id | _Recommend_ | Identifies this API call to a permanent user_id
289+
metadata | false | A JSON Object consisting of any custom metadata to be stored with this event.
260290

261291

262292
### updateUser method
263293

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

266296

267297
```javascript
268-
'use strict'
269-
const moesif = require('moesif-aws-lambda');
270-
271298
const moesifOptions = {
272-
applicationId: 'Your Moesif application_id',
273-
299+
applicationId: 'Your Moesif Application Id',
274300
};
301+
275302
var moesifMiddleware = moesif(options);
276303
var user = {
277304
userId: 'your user id', // required.
@@ -285,6 +312,29 @@ moesifMiddleware.updateUser(user, callback);
285312

286313
```
287314

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

0 commit comments

Comments
 (0)