UNPKG

@dynatrace/react-native-plugin

Version:

This plugin gives you the ability to use the Dynatrace Mobile agent in your react native application.

360 lines (359 loc) 20.6 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Dynatrace = void 0; const Types_1 = require("../../instrumentation/model/Types"); const DynatraceSecondGenForwarder_1 = require("../next/DynatraceSecondGenForwarder"); const Dynatrace_1 = require("../next/Dynatrace"); const StringUtils_1 = require("./util/StringUtils"); const DynatraceBridge_1 = require("./DynatraceBridge"); const DataCollectionLevel_1 = require("./model/DataCollectionLevel"); const UserPrivacyOptions_1 = require("./UserPrivacyOptions"); const ConsoleLogger_1 = require("./logging/ConsoleLogger"); const ConfigurationHandler_1 = require("./configuration/ConfigurationHandler"); const DynatraceRootAction_1 = require("./DynatraceRootAction"); const NullRootAction_1 = require("./NullRootAction"); const DynatraceInternal_1 = require("./DynatraceInternal"); const DynatraceAction_1 = require("./DynatraceAction"); const NullAction_1 = require("./NullAction"); const ErrorHandler_1 = require("./ErrorHandler"); const Application_1 = require("./Application"); const logger = new ConsoleLogger_1.ConsoleLogger('Dynatrace'); exports.Dynatrace = { start: (configuration) => __awaiter(void 0, void 0, void 0, function* () { if (!ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { if (configuration !== undefined) { yield DynatraceBridge_1.DynatraceNative.start(configuration); if (configuration.errorHandler) { (0, ErrorHandler_1.startErrorHandler)(configuration.reportFatalErrorAsCrash); } else { logger.debug('Dynatrace error handler will not be enabled as errorHandler was set to false!'); } Application_1.ApplicationHandler.startup(configuration); } else { throw new Error('Configuration is empty! Not allowed for Manual Startup!'); } } else { logger.debug('Configuration is already available! Duplicated startup blocked.'); } }), withMonitoring: (Component, name) => { if (Component !== undefined) { if (Component.prototype !== undefined && Component.prototype.isReactComponent !== undefined) { Component._dtInfo = { type: Types_1.Types.ClassComponent, name, }; } else { Component._dtInfo = { type: Types_1.Types.FunctionalComponent, name, }; } } return Component; }, enterAction: (name, platform) => exports.Dynatrace.enterAutoAction(name, platform), enterManualAction: (name, platform) => { if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { let key; if (ConfigurationHandler_1.ConfigurationHandler.getBundleName() != null) { key = ConfigurationHandler_1.ConfigurationHandler.getBundleName() + '_' + DynatraceInternal_1.DynatraceInternal.getCounter(); } else { key = 'DEFAULT_' + DynatraceInternal_1.DynatraceInternal.getCounter(); } if (!StringUtils_1.StringUtils.isStringNullEmptyOrUndefined(name)) { logger.debug(`enterManualAction(${name})`); DynatraceBridge_1.DynatraceNative.enterManualAction(name, key, platform === null || platform === void 0 ? void 0 : platform.toString()); return new DynatraceRootAction_1.DynatraceRootAction(key, name); } else { logger.debug('Action Name was empty or null! Action will have no effect.'); return new NullRootAction_1.NullRootAction(); } } else { logger.info(`enterManualAction(${name}): React Native plugin has not been started yet! Action can't be created!`); return new NullRootAction_1.NullRootAction(); } }, enterAutoAction: (name, platform) => { if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { let key; if (ConfigurationHandler_1.ConfigurationHandler.getBundleName() != null) { key = ConfigurationHandler_1.ConfigurationHandler.getBundleName() + '_' + DynatraceInternal_1.DynatraceInternal.getCounter(); } else { key = 'DEFAULT_' + DynatraceInternal_1.DynatraceInternal.getCounter(); } if (!StringUtils_1.StringUtils.isStringNullEmptyOrUndefined(name)) { logger.debug(`enterAutoAction(${name})`); DynatraceBridge_1.DynatraceNative.enterAction(name, key, platform === null || platform === void 0 ? void 0 : platform.toString()); return new DynatraceAction_1.DynatraceAction(key, name, false); } else { logger.debug('Action Name was empty or null! Action will have no effect.'); return new NullAction_1.NullAction(); } } else { logger.info(`enterAutoAction(${name}): React Native plugin has not been started yet! Action can't be created!`); return new NullAction_1.NullAction(); } }, reportError: (errorName, errorCode, platform) => { if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { logger.debug(`reportError(${errorName}, ${errorCode})`); DynatraceBridge_1.DynatraceNative.reportErrorWithoutStacktrace(errorName, errorCode, platform === null || platform === void 0 ? void 0 : platform.toString()); DynatraceSecondGenForwarder_1.DynatraceSecondGenForwarder.reportError(errorName, errorCode); } else { logger.info(`reportError(${errorName}, ${errorCode}): React Native plugin has not been started yet! Error will not be reported!`); } }, reportErrorWithStacktrace: (errorName, reason, stacktrace, platform) => { if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { if (!StringUtils_1.StringUtils.isStringNullEmptyOrUndefined(reason)) { const reasonNewLineIndex = reason.indexOf('\n'); if (reasonNewLineIndex !== -1) { reason = reason.substring(0, reasonNewLineIndex); } } logger.debug(`reportErrorStacktrace(${errorName}, ${reason}, ${stacktrace})`); DynatraceBridge_1.DynatraceNative.reportError(errorName, '-', reason, stacktrace, platform === null || platform === void 0 ? void 0 : platform.toString()); DynatraceSecondGenForwarder_1.DynatraceSecondGenForwarder.reportErrorWithStacktrace(errorName, reason, stacktrace); } else { logger.info(`reportErrorStacktrace(${errorName}, ${reason}, ${stacktrace}): React Native plugin has not been started yet! Error will not be reported!`); } }, reportErrorStacktrace: (errorName, errorValue, reason, stacktrace, platform) => { if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { if (!StringUtils_1.StringUtils.isStringNullEmptyOrUndefined(reason)) { const reasonNewLineIndex = reason.indexOf('\n'); if (reasonNewLineIndex !== -1) { reason = reason.substring(0, reasonNewLineIndex); } } logger.debug(`reportErrorStacktrace(${errorName}, ${errorValue}, ${reason}, ${stacktrace})`); DynatraceBridge_1.DynatraceNative.reportError(errorName, errorValue, reason, stacktrace, platform === null || platform === void 0 ? void 0 : platform.toString()); DynatraceSecondGenForwarder_1.DynatraceSecondGenForwarder.reportErrorStacktrace(errorName, errorValue, reason, stacktrace); } else { logger.info(`reportErrorStacktrace(${errorName}, ${errorValue}, ${reason}, ${stacktrace}): React Native plugin has not been started yet! Error will not be reported!`); } }, reportCrash: (crashName, reason, stacktrace, platform) => { if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { if (!StringUtils_1.StringUtils.isStringNullEmptyOrUndefined(crashName)) { if (!StringUtils_1.StringUtils.isStringNullEmptyOrUndefined(reason)) { const reasonNewLineIndex = reason.indexOf('\n'); if (reasonNewLineIndex !== -1) { reason = reason.substring(0, reasonNewLineIndex); } } logger.debug(`reportCrash(${crashName}, ${reason}, ${stacktrace})`); DynatraceBridge_1.DynatraceNative.reportCrash(crashName, reason, stacktrace, false, platform === null || platform === void 0 ? void 0 : platform.toString()); DynatraceSecondGenForwarder_1.DynatraceSecondGenForwarder.reportCrash(crashName, reason, stacktrace); } } else { logger.info(`reportCrash(${crashName}, ${reason}, ${stacktrace}): React Native plugin has not been started yet! Crash will not be reported!`); } }, reportCrashWithException: (crashName, crash, platform) => { if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { if (!StringUtils_1.StringUtils.isStringNullEmptyOrUndefined(crashName) && !StringUtils_1.StringUtils.isStringNullEmptyOrUndefined(crash.stack)) { let crashReason = crash.message; if (!StringUtils_1.StringUtils.isStringNullEmptyOrUndefined(crashReason)) { const reasonNewLineIndex = crashReason.indexOf('\n'); if (reasonNewLineIndex !== -1) { crashReason = crashReason.substring(0, reasonNewLineIndex); } } logger.debug(`reportCrashWithException(${crashName}, ${crash.stack})`); DynatraceBridge_1.DynatraceNative.reportCrash(crashName, crashReason, crash.stack, true, platform === null || platform === void 0 ? void 0 : platform.toString()); DynatraceSecondGenForwarder_1.DynatraceSecondGenForwarder.reportCrashWithException(crashName, crash); } } else { logger.info(`reportCrashWithException(${crashName}, ${crash}): React Native plugin has not been started yet! Crash will not be reported!`); } }, sendBizEvent: (type, attributes = {}, platform) => { if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { if (StringUtils_1.StringUtils.isStringNullEmptyOrUndefined(type)) { logger.debug(`sendBizEvent(${type}, ${attributes})): Type must be a non empty string`); return; } if (typeof attributes !== 'object' || attributes === null || Array.isArray(attributes)) { logger.debug(`sendBizEvent(${type}, ${attributes})): Payload toplevel must be an object`); return; } const internalAttributes = Object.assign({}, attributes); logger.debug(`sendBizEvent(${type}, ${internalAttributes})`); DynatraceBridge_1.DynatraceNative.sendBizEvent(type, internalAttributes, platform === null || platform === void 0 ? void 0 : platform.toString()); } else { logger.info(`sendBizEvent(${type}, ${attributes}): React Native plugin has not been started yet! Biz Event will not be reported!`); } }, endSession: (platform) => { if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { logger.debug('Dynatrace endSession()'); DynatraceBridge_1.DynatraceNative.endVisit(platform === null || platform === void 0 ? void 0 : platform.toString()); } else { logger.info('endSession(): React Native plugin has not been started yet! Session will not be ended!'); } }, identifyUser: (user, platform) => { if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { logger.debug(`Dynatrace identifyUser(${user})`); DynatraceBridge_1.DynatraceNative.identifyUser(user, platform === null || platform === void 0 ? void 0 : platform.toString()); } else { logger.info(`identifyUser(${user}): React Native plugin has not been started yet! Session will not be tagged!`); } }, setGPSLocation: (latitude, longitude, platform) => { if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { logger.debug(`setGPSLocation(${latitude}, ${longitude})`); DynatraceBridge_1.DynatraceNative.setGPSLocation(latitude, longitude, platform === null || platform === void 0 ? void 0 : platform.toString()); } else { logger.info(`setGPSLocation(${latitude}, ${longitude}): React Native plugin has not been started yet! GPS Position will not be set!`); } }, isCrashReportingOptedIn: (platform) => __awaiter(void 0, void 0, void 0, function* () { if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { logger.debug('Dynatrace isCrashReportingOptedIn()'); return DynatraceBridge_1.DynatraceNative.isCrashReportingOptedIn(platform === null || platform === void 0 ? void 0 : platform.toString()); } else { logger.info('isCrashReportingOptedIn(): React Native plugin has not been started yet! Crash reporting value not available!'); return false; } }), setCrashReportingOptedIn: (crashReporting, platform) => { if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { logger.debug(`setCrashReportingOptedIn(${crashReporting})`); DynatraceBridge_1.DynatraceNative.setCrashReportingOptedIn(crashReporting, platform === null || platform === void 0 ? void 0 : platform.toString()); } else { logger.info(`setCrashReportingOptedIn(${crashReporting}): React Native plugin has not been started yet! Crash reporting value will not be set!`); } }, getDataCollectionLevel: (platform) => __awaiter(void 0, void 0, void 0, function* () { if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { logger.debug('getDataCollectionLevel()'); return (0, DataCollectionLevel_1.StringToDataCollectionLevel)(yield DynatraceBridge_1.DynatraceNative.getDataCollectionLevel(platform === null || platform === void 0 ? void 0 : platform.toString())); } else { logger.info('getDataCollectionLevel(): React Native plugin has not been started yet! Data Collection level not available!'); return DataCollectionLevel_1.DataCollectionLevel.Off; } }), setDataCollectionLevel: (dataCollectionLevel, platform) => { if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { logger.debug(`setDataCollectionLevel(${dataCollectionLevel})`); DynatraceBridge_1.DynatraceNative.setDataCollectionLevel(dataCollectionLevel.toString(), platform === null || platform === void 0 ? void 0 : platform.toString()); } else { logger.info(`setDataCollectionLevel(${dataCollectionLevel}): React Native plugin has not been started yet! Data Collection level will not be set!`); } }, getUserPrivacyOptions: (platform) => __awaiter(void 0, void 0, void 0, function* () { if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { logger.debug('getUserPrivacyOptions()'); const options = yield DynatraceBridge_1.DynatraceNative.getUserPrivacyOptions(platform === null || platform === void 0 ? void 0 : platform.toString()); const currentOptions = new UserPrivacyOptions_1.UserPrivacyOptions((0, DataCollectionLevel_1.StringToDataCollectionLevel)(options.dataCollectionLevel), options.crashReportingOptedIn); return currentOptions; } else { logger.info('getUserPrivacyOptions(): React Native plugin has not been started yet! User Privacy Options are not available!'); return new UserPrivacyOptions_1.UserPrivacyOptions(DataCollectionLevel_1.DataCollectionLevel.Off, false); } }), applyUserPrivacyOptions: (userPrivacyOptions, platform) => { if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { if ((userPrivacyOptions.crashReportingOptedIn === true || userPrivacyOptions.crashReportingOptedIn === false) && userPrivacyOptions.dataCollectionLevel in DataCollectionLevel_1.DataCollectionLevel) { logger.debug(`applyUserPrivacyOptions(${JSON.stringify(userPrivacyOptions)})`); DynatraceBridge_1.DynatraceNative.applyUserPrivacyOptions(userPrivacyOptions, platform === null || platform === void 0 ? void 0 : platform.toString()); } } else { logger.info(`applyUserPrivacyOptions(${JSON.stringify(userPrivacyOptions)}): React Native plugin has not been started yet! User Privacy Options can't be applied!`); } }, flushEvents: (platform) => { if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { logger.debug('flushEvents()'); DynatraceBridge_1.DynatraceNative.flushEvents(platform === null || platform === void 0 ? void 0 : platform.toString()); } else { logger.info('flushEvents(): React Native plugin has not been started yet! Flushing Event not possible!'); } }, setBeaconHeaders: (headers, platform) => { if (ConfigurationHandler_1.ConfigurationHandler.isConfigurationAvailable()) { logger.debug('setBeaconHeaders(headers)'); if (headers && headers.size > 0) { const obj = {}; headers.forEach((value, key) => (obj[key] = value)); DynatraceBridge_1.DynatraceNative.setBeaconHeaders(obj, platform === null || platform === void 0 ? void 0 : platform.toString()); } else if (!headers || typeof headers !== 'undefined') { DynatraceBridge_1.DynatraceNative.setBeaconHeaders(null, platform === null || platform === void 0 ? void 0 : platform.toString()); } } else { logger.info('setBeaconHeaders(headers): React Native plugin has not been started yet! Setting Beacon Headers is not possible!'); } }, addEventModifier: (eventModifier) => Dynatrace_1.Dynatrace.addEventModifier(eventModifier), removeEventModifier: (eventModifier) => Dynatrace_1.Dynatrace.removeEventModifier(eventModifier), sendEvent: (properties) => { Dynatrace_1.Dynatrace.sendEvent(properties); }, startView: (name) => { Dynatrace_1.Dynatrace.startView(name); }, stopView: () => { Dynatrace_1.Dynatrace.stopView(); }, sendSessionPropertyEvent: (properties) => { Dynatrace_1.Dynatrace.sendSessionPropertyEvent(properties); }, sendExceptionEvent: (error, fields) => { Dynatrace_1.Dynatrace.sendExceptionEvent(error, fields); }, sendHttpRequestEvent(httpRequestEventBuilder) { Dynatrace_1.Dynatrace.sendHttpRequestEvent(httpRequestEventBuilder); }, };