161 lines
4.4 KiB
TypeScript
161 lines
4.4 KiB
TypeScript
import { BrowserAgent } from '@newrelic/browser-agent/loaders/browser-agent';
|
|
import type { AgentOptions } from '@newrelic/browser-agent/loaders/agent';
|
|
|
|
type NewRelicBrowserAgent = InstanceType<typeof BrowserAgent>;
|
|
type NewRelicAttributeValue = string | number | boolean | null;
|
|
type NewRelicAttributes = Record<string, NewRelicAttributeValue>;
|
|
|
|
const DEFAULT_BEACON = 'bam.nr-data.net';
|
|
const DEFAULT_RELEASE_NAME = 'lolympus-web';
|
|
|
|
let browserAgent: NewRelicBrowserAgent | null = null;
|
|
|
|
function readEnvValue(name: string): string | undefined {
|
|
const value = import.meta.env[name];
|
|
return typeof value === 'string' && value.trim() ? value.trim() : undefined;
|
|
}
|
|
|
|
function readEnvNumber(name: string): number | undefined {
|
|
const value = Number(readEnvValue(name));
|
|
return Number.isFinite(value) ? value : undefined;
|
|
}
|
|
|
|
function isBrowserRuntime() {
|
|
return typeof window !== 'undefined';
|
|
}
|
|
|
|
export function isNewRelicBrowserMonitoringEnabled() {
|
|
return readEnvValue('VITE_NEW_RELIC_ENABLED') !== 'false';
|
|
}
|
|
|
|
export function createNewRelicBrowserAgentOptions(): AgentOptions | null {
|
|
if (!isNewRelicBrowserMonitoringEnabled()) return null;
|
|
|
|
const applicationID = readEnvValue('VITE_NEW_RELIC_APPLICATION_ID');
|
|
const accountID = readEnvNumber('VITE_NEW_RELIC_ACCOUNT_ID');
|
|
const licenseKey = readEnvValue('VITE_NEW_RELIC_LICENSE_KEY');
|
|
const beacon = readEnvValue('VITE_NEW_RELIC_BEACON') ?? DEFAULT_BEACON;
|
|
const agentID = readEnvNumber('VITE_NEW_RELIC_AGENT_ID') ?? Number(applicationID);
|
|
const applicationNumericID = Number(applicationID);
|
|
const trustKey = readEnvNumber('VITE_NEW_RELIC_TRUST_KEY') ?? accountID;
|
|
|
|
if (
|
|
!applicationID ||
|
|
!accountID ||
|
|
!licenseKey ||
|
|
!Number.isFinite(agentID) ||
|
|
!Number.isFinite(applicationNumericID)
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
info: {
|
|
applicationID,
|
|
beacon,
|
|
errorBeacon: beacon,
|
|
licenseKey,
|
|
sa: 1,
|
|
},
|
|
init: {
|
|
ajax: {
|
|
deny_list: [beacon],
|
|
},
|
|
browser_consent_mode: {
|
|
enabled: false,
|
|
},
|
|
distributed_tracing: {
|
|
enabled: true,
|
|
},
|
|
performance: {
|
|
capture_detail: false,
|
|
capture_marks: false,
|
|
capture_measures: true,
|
|
},
|
|
privacy: {
|
|
cookies_enabled: true,
|
|
},
|
|
session_replay: {
|
|
enabled: false,
|
|
autoStart: false,
|
|
},
|
|
},
|
|
loader_config: {
|
|
accountID,
|
|
agentID,
|
|
applicationID: applicationNumericID,
|
|
licenseKey,
|
|
trustKey,
|
|
},
|
|
};
|
|
}
|
|
|
|
function applyReleaseMetadata(agent: NewRelicBrowserAgent) {
|
|
const applicationVersion =
|
|
readEnvValue('VITE_NEW_RELIC_APPLICATION_VERSION') ??
|
|
readEnvValue('VITE_APP_VERSION') ??
|
|
readEnvValue('VITE_BUILD_NUMBER');
|
|
if (!applicationVersion) return;
|
|
|
|
agent.setApplicationVersion(applicationVersion);
|
|
agent.addRelease(
|
|
readEnvValue('VITE_NEW_RELIC_RELEASE_NAME') ?? DEFAULT_RELEASE_NAME,
|
|
readEnvValue('VITE_NEW_RELIC_RELEASE_ID') ?? applicationVersion,
|
|
);
|
|
}
|
|
|
|
export function initializeNewRelicBrowserAgent(
|
|
options = createNewRelicBrowserAgentOptions(),
|
|
) {
|
|
if (!isBrowserRuntime() || !options) return null;
|
|
if (browserAgent) return browserAgent;
|
|
|
|
// New Relic recommends instantiating the npm BrowserAgent from the earliest
|
|
// browser entry point: https://docs.newrelic.com/docs/browser/browser-monitoring/installation/install-browser-monitoring-agent/
|
|
browserAgent = new BrowserAgent(options);
|
|
applyReleaseMetadata(browserAgent);
|
|
|
|
return browserAgent;
|
|
}
|
|
|
|
export function getNewRelicBrowserAgent() {
|
|
return browserAgent;
|
|
}
|
|
|
|
export function setNewRelicUserId(userId: string | null, resetSession = false) {
|
|
browserAgent?.setUserId(userId, resetSession);
|
|
}
|
|
|
|
export function setNewRelicPageViewName(name: string, host?: string) {
|
|
browserAgent?.setPageViewName(name, host);
|
|
}
|
|
|
|
export function setNewRelicCustomAttribute(
|
|
name: string,
|
|
value: NewRelicAttributeValue,
|
|
persist = false,
|
|
) {
|
|
browserAgent?.setCustomAttribute(name, value, persist);
|
|
}
|
|
|
|
export function addNewRelicPageAction(
|
|
name: string,
|
|
attributes?: NewRelicAttributes,
|
|
) {
|
|
browserAgent?.addPageAction(name, attributes);
|
|
}
|
|
|
|
export function recordNewRelicCustomEvent(
|
|
eventType: string,
|
|
attributes?: NewRelicAttributes,
|
|
) {
|
|
browserAgent?.recordCustomEvent(eventType, attributes);
|
|
}
|
|
|
|
export function noticeNewRelicError(
|
|
error: Error | string,
|
|
attributes?: NewRelicAttributes,
|
|
) {
|
|
browserAgent?.noticeError(error, attributes);
|
|
}
|