gatsby-plugin-newrelic
Version:
Gatsby plugin for implementing the New Relic Browser agent
145 lines (137 loc) • 8.91 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireDefault(require("react"));
var _latest = require("../browser-agents/latest");
var _default = exports["default"] = function _default(_ref, pluginOptions) {
var setHeadComponents = _ref.setHeadComponents;
var userConfigs = pluginOptions.configs,
userConfig = pluginOptions.config;
var requiredConfig = {
accountId: "",
trustKey: "",
agentID: "",
licenseKey: "",
applicationID: "",
beacon: "bam.nr-data.net",
errorBeacon: "bam.nr-data.net",
instrumentationType: "lite" // Options are 'lite', 'pro', 'proAndSPA'
};
var env = process.env.GATSBY_NEWRELIC_ENV;
var userEnvConfig = env ? userConfigs[env] : userConfig;
if (!userEnvConfig) {
console.warn("gatsby-plugin-newrelic is missing the configuration" + (env ? " for the " + env + " environment" : ""));
return;
}
if (env) {
console.warn("gatsby-plugin-newrelic has deprecated using GATSBY_NEWRELIC_ENV for multiple environments");
}
var allowedInstrumentationTypes = ["lite", "pro", "proAndSPA"];
var itExists = allowedInstrumentationTypes.find(function (i) {
return i === userEnvConfig.instrumentationType;
});
if (!itExists) {
// TO DO - Error/Warn about wrong instrumentation type
}
var options = (0, _extends2["default"])({}, requiredConfig, userEnvConfig);
var instrumentationType = options.instrumentationType;
var emptyOptions = Object.entries(options).filter(function (_ref2) {
var v = _ref2[1];
return v === "";
});
if (emptyOptions.length > 0) {
// TO DO - Warn about missing options
}
// --- Possible Browser > Application Settings in the NR1 UI and their options ---
// Do not supply hard coded defaults for missing options. If the key is not provided, let the browser agent handle those values.
var init;
if (options !== null && options !== void 0 && options.settings) {
var _options$settings = options === null || options === void 0 ? void 0 : options.settings,
dt = _options$settings.distributed_tracing,
sr = _options$settings.session_replay,
privacy = _options$settings.privacy,
ajax = _options$settings.ajax;
// 'Distributed tracing'
var distributedTracingInitString = "";
if (dt !== null && dt !== void 0 && dt.enabled) {
var distributedTracingConfigObject = {
enabled: "enabled:" + dt.enabled,
exclude_newrelic_header: dt.exclude_newrelic_header ? "exclude_newrelic_header:" + dt.exclude_newrelic_header : "",
cors_use_newrelic_header: dt.cors_use_newrelic_header ? "cors_use_newrelic_header:" + dt.cors_use_newrelic_header : "",
cors_use_tracecontext_headers: dt.cors_use_tracecontext_headers ? "cors_use_tracecontext_headers:" + dt.cors_use_tracecontext_headers : "",
allowed_origins: dt.allowed_origins ? "dt.allowed_origins:" + JSON.stringify(dt.allowed_origins) : ""
};
distributedTracingInitString = "distributed_tracing:{" + Object.values(distributedTracingConfigObject).filter(function (v) {
return v !== "";
}).join(",") + "},";
}
// 'Session Replay'
var sessionReplayInitString = "";
if (sr !== null && sr !== void 0 && sr.enabled) {
var maskOptionsString = "";
var mOption = sr.mask_input_options;
// if user selects custom privacy settings:
var maskOptionsObject = {
color: mOption !== null && mOption !== void 0 && mOption.color ? "color:" + (mOption === null || mOption === void 0 ? void 0 : mOption.color) : "",
date: mOption !== null && mOption !== void 0 && mOption.date ? "date:" + (mOption === null || mOption === void 0 ? void 0 : mOption.date) : "",
datetime_local: mOption !== null && mOption !== void 0 && mOption.datetime_local ? "datetime_local:" + (mOption === null || mOption === void 0 ? void 0 : mOption.datetime_local) : "",
email: mOption !== null && mOption !== void 0 && mOption.email ? "email:" + (mOption === null || mOption === void 0 ? void 0 : mOption.email) : "",
month: mOption !== null && mOption !== void 0 && mOption.month ? "month:" + (mOption === null || mOption === void 0 ? void 0 : mOption.month) : "",
number: mOption !== null && mOption !== void 0 && mOption.number ? "number:" + (mOption === null || mOption === void 0 ? void 0 : mOption.number) : "",
range: mOption !== null && mOption !== void 0 && mOption.range ? "range:" + (mOption === null || mOption === void 0 ? void 0 : mOption.range) : "",
search: mOption !== null && mOption !== void 0 && mOption.search ? "search:" + (mOption === null || mOption === void 0 ? void 0 : mOption.search) : "",
tel: mOption !== null && mOption !== void 0 && mOption.tel ? "tel:" + (mOption === null || mOption === void 0 ? void 0 : mOption.tel) : "",
text: mOption !== null && mOption !== void 0 && mOption.text ? "text:" + (mOption === null || mOption === void 0 ? void 0 : mOption.text) : "",
time: mOption !== null && mOption !== void 0 && mOption.time ? "time:" + (mOption === null || mOption === void 0 ? void 0 : mOption.time) : "",
url: mOption !== null && mOption !== void 0 && mOption.url ? "url:" + (mOption === null || mOption === void 0 ? void 0 : mOption.url) : "",
week: mOption !== null && mOption !== void 0 && mOption.week ? "week:" + (mOption === null || mOption === void 0 ? void 0 : mOption.week) : "",
text_area: mOption !== null && mOption !== void 0 && mOption.text_area ? "text_area:" + (mOption === null || mOption === void 0 ? void 0 : mOption.text_area) : "",
select: mOption !== null && mOption !== void 0 && mOption.select ? "select:" + (mOption === null || mOption === void 0 ? void 0 : mOption.select) : ""
};
maskOptionsString = "" + Object.values(maskOptionsObject).filter(function (v) {
return v !== "";
}).join(",");
var sessionReplayConfigObject = {
enabled: sr.enabled ? "enabled:" + sr.enabled : "",
block_selector: sr.block_selector ? "block_selector:'" + sr.block_selector + "'" : "",
mask_text_selector: sr.mask_text_selector ? "mask_text_selector:'" + sr.mask_text_selector + "'" : "",
sampling_rate: sr.sampling_rate ? "sampling_rate:" + sr.sampling_rate : "",
error_sampling_rate: sr.error_sampling_rate ? "error_sampling_rate:" + sr.error_sampling_rate : "",
mask_all_inputs: sr.mask_all_inputs ? "mask_all_inputs:" + sr.mask_all_inputs : "",
collect_fonts: sr.collect_fonts ? "collect_fonts:" + sr.collect_fonts : "",
inline_images: sr.inline_images ? "inline_images:" + sr.inline_images : "",
inline_stylesheet: sr.inline_stylesheet ? "inline_stylesheet:" + sr.inline_stylesheet : "",
mask_input_options: maskOptionsString.length > 0 ? "mask_input_options:{" + maskOptionsString + "}" : ""
};
sessionReplayInitString = "session_replay:{" + Object.values(sessionReplayConfigObject).filter(function (v) {
return v !== "";
}).join(",") + "},";
}
// 'Browser Settings'
var privacyInitString = privacy !== null && privacy !== void 0 && privacy.cookies_enabled ? "privacy:{cookies_enabled:" + (privacy === null || privacy === void 0 ? void 0 : privacy.cookies_enabled) + "}," : "";
// 'AJAX request deny list'
var ajaxInitString = ajax !== null && ajax !== void 0 && ajax.deny_list ? "ajax:{deny_list:" + JSON.stringify(ajax === null || ajax === void 0 ? void 0 : ajax.deny_list) + "}," : "";
init = sessionReplayInitString || distributedTracingInitString || privacyInitString || ajaxInitString ? ";window.NREUM||(NREUM={});NREUM.init={" + sessionReplayInitString + distributedTracingInitString + privacyInitString + ajaxInitString + "};" : "";
}
var agent;
if (instrumentationType === "lite") {
agent = _latest.liteAgent;
}
if (instrumentationType === "pro") {
agent = _latest.proAgent;
}
if (instrumentationType === "proAndSPA") {
agent = _latest.proAndSpaAgent;
}
var configs = "\n ;NREUM.loader_config={accountID:\"" + options.accountId + "\",trustKey:\"" + options.trustKey + "\",agentID:\"" + options.agentID + "\",licenseKey:\"" + options.licenseKey + "\",applicationID:\"" + options.applicationID + "\"}\n ;NREUM.info={beacon:\"" + options.beacon + "\",errorBeacon:\"" + options.errorBeacon + "\",licenseKey:\"" + options.licenseKey + "\",applicationID:\"" + options.applicationID + "\",sa:1}\n ";
if (agent && configs) {
setHeadComponents([/*#__PURE__*/_react["default"].createElement("script", {
key: "gatsby-plugin-newrelic",
dangerouslySetInnerHTML: {
__html: init + agent + configs
}
})]);
}
};