diff --git a/MoesifWorker.js b/MoesifWorker.js index 65ba9e9..f7e3cf7 100644 --- a/MoesifWorker.js +++ b/MoesifWorker.js @@ -30,7 +30,7 @@ if (typeof INSTALL_OPTIONS === 'undefined') { * MOESIF_INSTALL * Set Your Moesif Application Id *********************************/ - "appId": "", + "applicationId": "", // Only used by CloudFlare App Worker Framework. Modify identifyUser() function instead. "userIdHeader": "", @@ -60,7 +60,7 @@ if (typeof INSTALL_OPTIONS === 'undefined') { } let { - appId, + applicationId, hideCreditCards, disableTransactionId, sessionTokenHeader, @@ -116,19 +116,19 @@ if (typeof INSTALL_PRODUCT === 'undefined') { INSTALL_PRODUCT = undefined; } -urlPatterns = urlPatterns.map(({ appId, regex }) => { +urlPatterns = urlPatterns.map(({ applicationId, regex }) => { try { return { regex: new RegExp(regex), - appId + applicationId }; } catch (e) { console.error(e); } }).filter(x => x && x.regex); // filter invalid regular expressions / blank entries -if (!appId && urlPatterns.length === 0) { - console.error('Cannot track events. No App ID or valid URL Pattern specified.'); +if (!applicationId && urlPatterns.length === 0) { + console.error('Cannot log API calls. No Moesif Application Id or valid URL Patterns specified.'); } const overrideApplicationId = moesifEvent => { @@ -136,8 +136,8 @@ const overrideApplicationId = moesifEvent => { const pattern = urlPatterns.find(({ regex }) => regex.test(moesifEvent.request.uri)); return pattern - ? pattern.appId // may be an empty string, which means don't track this - : appId; + ? pattern.applicationId // may be an empty string, which means don't track this + : applicationId; }; const BATCH_URL = 'https://api.moesif.net/v1/events/batch'; @@ -145,7 +145,7 @@ let batchRunning = false; let jobs = []; function isMoesif(request) { - return request.url.indexOf('https://api.moesif.net') !== -1; + return request.url.indexOf('moesif.net') !== -1; } function sleep(ms) { @@ -318,7 +318,7 @@ async function makeMoesifEvent(request, response, before, after, txId) { headers: headersToObject(request.headers), ip_address: request.headers.get('cf-connecting-ip') }, - response: { + response: response.isEmpty ? undefined : { time: after, body: responseBody ? doHideCreditCards(responseBody) : undefined, status: response.status, @@ -354,36 +354,40 @@ function batch() { moesifLog(`batch start`) if (jobs.length > 0) { - const appIdMap = {}; + const applicationIdMap = {}; moesifLog(`batch has jobs`) - jobs.forEach(({ applicationId, moesifEvent }) => { - if (!(applicationId in appIdMap)) { - appIdMap[applicationId] = []; + jobs.forEach(({ appId, moesifEvent }) => { + if (!(appId in applicationIdMap)) { + applicationIdMap[appId] = []; } if ((moesifEvent.direction === 'Outgoing' && logOutgoingRequests) || (moesifEvent.direction === 'Incoming' && logIncomingRequests)) { - appIdMap[applicationId].push(moesifEvent); + applicationIdMap[appId].push(moesifEvent); } }); let promises = []; - Object.keys(appIdMap).forEach(applicationId => { - const body = JSON.stringify(appIdMap[applicationId]); + Object.keys(applicationIdMap).forEach(appId => { + + const moesifHeaders = { + 'Accept': 'application/json; charset=utf-8', + 'X-Moesif-Application-Id': appId, + 'User-Agent': 'moesif-cloudflare', + 'X-Moesif-Cf-Install-Id': INSTALL_ID, + 'X-Moesif-Cf-Install-Product': (INSTALL_PRODUCT && INSTALL_PRODUCT.id), + 'X-Moesif-Cf-Install-Type': INSTALL_TYPE, + } + moesifLog(JSON.stringify(moesifHeaders)); + + const body = JSON.stringify(applicationIdMap[appId]); moesifLog(body); const options = { method: 'POST', - headers: { - 'Accept': 'application/json; charset=utf-8', - 'X-Moesif-Application-Id': applicationId, - 'User-Agent': 'moesif-cloudflare', - 'X-Moesif-Cf-Install-Id': INSTALL_ID, - 'X-Moesif-Cf-Install-Product': (INSTALL_PRODUCT && INSTALL_PRODUCT.id), - 'X-Moesif-Cf-Install-Type': INSTALL_TYPE, - }, + headers: moesifHeaders, body: body }; @@ -401,15 +405,15 @@ async function tryTrackRequest(event, request, response, before, after, txId) { moesifLog(`tryTrackRequest start url=${request.url}`) const moesifEvent = await makeMoesifEvent(request, response, before, after, txId); - const applicationId = runHook(() => overrideApplicationId(moesifEvent), 'overrideApplicationId', appId); + const appId = runHook(() => overrideApplicationId(moesifEvent), 'overrideApplicationId', applicationId); event.waitUntil(moesifEvent); - if (applicationId) { + if (appId) { // only track this if there's an associated applicationId // services may want to not report certain requests jobs.push({ - applicationId, + appId, moesifEvent }); diff --git a/README.md b/README.md index 88ed9e7..4042edf 100644 --- a/README.md +++ b/README.md @@ -49,14 +49,14 @@ We recommend matching all requests for your domain. For example if your domain i ### 3. Set Moesif options -Set the `INSTALL_OPTIONS.appId` field to your Moesif Application Id. +Set the `INSTALL_OPTIONS.applicationId` field to your Moesif Application Id. Your Moesif Application Id will be displayed during the onboarding steps when signing up for [Moesif](https://www.moesif.com/). Additional options are below: ```javascript INSTALL_OPTIONS = { // Your Moesif Application Id - "appId": "Your Moesif Application Id", + "applicationId": "Your Moesif Application Id", // Only used by CloudFlare App Worker Framework. Modify identifyUser() function instead. "userIdHeader": "", diff --git a/install.json b/install.json index f288f6d..71a3a88 100644 --- a/install.json +++ b/install.json @@ -7,51 +7,51 @@ ], "options": { "properties": { - "appId": { + "applicationId": { "order": 10, - "title": "Default Moesif App Id", - "description": "Requred, unless \"App Id Overrides\" are used", + "title": "Your Moesif Application Id", + "description": "Required, unless you set the urlPatterns option. sign up for free on Moesif's website", "type": "string", "default": "" }, "userIdHeader": { "order": 20, - "title": "Request / Response Header to Identify User", - "description": "Optional
For more info, see User Profiles and Company Profiles.", + "title": "Request or Response header name to identify users", + "description": "Optional
For more info, see Company Profiles.", "type": "string", "default": "" }, "companyIdHeader": { "order": 40, - "title": "Request / Response Header to Identify Company", - "description": "Optional", + "title": "Request or Response header name to identify companies", + "description": "Optional
For more info, see User Profiles.", "type": "string", "default": "" }, "sessionTokenHeader": { "order": 50, - "title": "Request / Response Header Containing Session Token", + "title": "Request or Response header name containing a user's API key or session token", "description": "Optional", "type": "string", "default": "" }, "hideCreditCards": { "order": 60, - "title": "Remove Credit Card Numbers from API Requests. Leave this enabled if your API processes credit card information.", + "title": "Remove Credit Card Numbers from API Requests based on a Luhn check.", "type": "boolean", "default": true }, "disableTransactionId": { "order": 65, "title": "Disable Transaction Id generation", - "description": "Setting to true will prevent insertion of the X-Moesif-Transaction-Id header.", + "description": "Setting to true will prevent insertion of the X-Moesif-Transaction-Id headers.", "type": "boolean", "default": false }, "urlPatterns": { "order": 70, - "title": "App Id Overrides", - "description": "Advanced usage.
For assistance with advanced configuration, please contact support@moesif.com. Regular expressions are evaluated in order (top to bottom), and the appId for the first matching regex will be used for a given request.
To manually test your regular expressions, you can use the following JavaScript code: new RegExp(urlRegex).test(moesifAppId)", + "title": "Mapping of URL RegEx to Moesif Application Ids", + "description": "Use multiple applications ids based on URL patterns. For example, if your worker has multiple environments like a staging at staging.api.acmeinc.com and production at api.acmeinc.com, you can log to different application in Moesif. Patterns are prioritized in order from top.
You can manually test your expression via this site.", "type": "array", "items": { "title": "Item", @@ -59,19 +59,40 @@ "properties": { "regex": { "order": 1, - "title": "URL Regex", - "placeholder": "^https://mydomain.com/api", + "title": "URL RegEx", + "placeholder": "^https://staging.acmeinc.com/api", "type": "string" }, - "appId": { + "applicationId": { "order": 2, - "title": "Moesif App Id for Requests with URLs Matching Above Regular Expression", - "placeholder": "leave blank to ignore requests matching this pattern", + "title": "Your Moesif Application Id. If blank, requests are not logged to Moesif.", + "placeholder": "Your Moesif Application Id", "type": "string" } } } + }, + "logIncomingRequests": { + "order": 75, + "title": "Log Incoming API Requests", + "description": "Log incoming API calls hitting your Cloudflare Worker.", + "type": "boolean", + "default": true + }, + "logOutgoingRequests": { + "order": 80, + "title": "Log Outgoing API Requests", + "description": "Log outgoing calls to your origin server from your Cloudflare Worker.", + "type": "boolean", + "default": true + }, + "debug": { + "order": 100, + "title": "Enable debug mode", + "description": "Print debug messages to console which may be helpful when contacting Moesif support.", + "type": "boolean", + "default": false } } } -} \ No newline at end of file +}