UNPKG

ca-apm-probe

Version:

CA APM Node.js Agent monitors real-time health and performance of Node.js applications

141 lines (124 loc) 5.25 kB
/** * Copyright (c) 2015 CA. All rights reserved. * * This software and all information contained therein is confidential and proprietary and * shall not be duplicated, used, disclosed or disseminated in any way except as authorized * by the applicable license agreement, without the express written permission of CA. All * authorized reproductions must be marked with this language. * * EXCEPT AS SET FORTH IN THE APPLICABLE LICENSE AGREEMENT, TO THE EXTENT * PERMITTED BY APPLICABLE LAW, CA PROVIDES THIS SOFTWARE WITHOUT WARRANTY * OF ANY KIND, INCLUDING WITHOUT LIMITATION, ANY IMPLIED WARRANTIES OF * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL CA BE * LIABLE TO THE END USER OR ANY THIRD PARTY FOR ANY LOSS OR DAMAGE, DIRECT OR * INDIRECT, FROM THE USE OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, LOST * PROFITS, BUSINESS INTERRUPTION, GOODWILL, OR LOST DATA, EVEN IF CA IS * EXPRESSLY ADVISED OF SUCH LOSS OR DAMAGE. */ var agent = require('./agent'); var logger = require("./logger.js"); var config = require('./configdata').getConfigData(); var path = require('path'); var fs = require('fs'); var baSnippet; module.exports = { initializeBrowserAgentParameters, prepareScript, getBaResponseBt, validateRespIfJson } function initializeBrowserAgentParameters() { isCorGuidEnabled = false; maxCookieExpInMs = 3000; allowedResContentType = 'text/html'; isAutoInjectionEnabled = (config.http.browseragent.autoInjectionEnabled == undefined) ? false : config.http.browseragent.autoInjectionEnabled; isResponseDecEnabled = (config.http.browseragent.response.decoration.enabled == undefined) ? false : config.http.browseragent.response.decoration.enabled; if (isResponseDecEnabled) { baSnippet = getSnippet(); isCorGuidEnabled = (config.http.browseragent.corGuidEnabled == undefined) ? true : config.http.browseragent.corGuidEnabled; } maxCookieExpInMs = (config.http.browseragent.response.decoration.cookie.expirationTime == undefined) ? 3000 : config.http.browseragent.response.decoration.cookie.expirationTime; allowedResContentType = (config.http.browseragent.response.decoration.cookie.contentType == undefined) ? 'text/html' : config.http.browseragent.response.decoration.cookie.contentType; return { baSnippet, isAutoInjectionEnabled, isResponseDecEnabled, isCorGuidEnabled, maxCookieExpInMs, allowedResContentType }; } function updateDefaultSnippet(baSnippet) { if (config.http.browseragent.autoInjection.snippetLocation != undefined) { var filepath = path.join(__dirname, config.http.browseragent.autoInjection.snippetLocation); try { fs.writeFileSync(filepath, baSnippet); logger.debug('Snippet updated in default.snippet'); } catch (error) { logger.debug(error); } } } function getDefaultSnippet() { if (config.http.browseragent.autoInjection.snippetLocation != undefined) { var filepath = path.join(__dirname, config.http.browseragent.autoInjection.snippetLocation); var buffer = fs.readFileSync(filepath); if (buffer != undefined) { return buffer.toString(); } } else { return null; } } function getSnippet() { if (config.http.browseragent.snippetString && config.http.browseragent.snippetString !== '') { //This snippet is already gets tranformed in the configdata.js postConfig method return config.http.browseragent.snippetString; } else { return getDefaultSnippet(); } } function validateGraphqlResp(bufString) { try { JSON.parse(bufString) return false; } catch (error) { return true; } } function prepareScript(ctx) { var baResponseBt = encodeURIComponent(getBaResponseBt(ctx, false)); var snippetArr = baSnippet.split('script '); var finalScript = snippetArr[0] + 'script x-apm-ba-response-bt="' + baResponseBt + '" ' + snippetArr[1]; return finalScript; } function getBaResponseBt(ctx, isGqlUrl) { var CorBrowsGUID; var serverTime = getDateTimeMilliseconds(); var startTime = ',startTime=' + serverTime; serverTime = 'servertime=' + serverTime; var apmAgentProcess = ',apmAgentProcess=nodejs-probes'; var apmAgentName = ',apmAgentName=' + agent.getProbeName(); var apmWebAppname = ',apmWebAppname=' + agent.getProbeName(); var apmServletName = ',apmServletName=' + agent.getProbeName(); var apmAgentHost = ',apmAgentHost=' + ctx.hostName; var endTime = ',endTime=' + getDateTimeMilliseconds(); if (isCorGuidEnabled) { if (ctx.corIdObj) { CorBrowsGUID = ',CorBrowsGUID=' + ctx.corIdObj.guid; } else { CorBrowsGUID = ',CorBrowsGUID=' + ctx.corId.substring(0, ctx.corId.indexOf(',')); } var headerVal = serverTime + CorBrowsGUID + startTime + endTime + apmServletName + apmAgentProcess + apmAgentName + apmWebAppname + apmAgentHost; if (isGqlUrl) { return headerVal + ',isGrpql'; } else { return headerVal; } } else { return serverTime + startTime + endTime + apmServletName + apmAgentProcess + apmAgentName + apmWebAppname + apmAgentHost; } } function getDateTimeMilliseconds() { return Date.now(); } function validateRespIfJson(bufString) { try { JSON.parse(bufString) return false; } catch (error) { return true; } }