@datadog/mobile-react-native
Version:
A client-side React Native module to interact with Datadog
403 lines (379 loc) • 14.6 kB
JavaScript
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/
import { TrackingConsent } from './TrackingConsent';
import { PropagatorType } from './rum/types';
export let VitalsUpdateFrequency = /*#__PURE__*/function (VitalsUpdateFrequency) {
VitalsUpdateFrequency["FREQUENT"] = "FREQUENT";
VitalsUpdateFrequency["AVERAGE"] = "AVERAGE";
VitalsUpdateFrequency["RARE"] = "RARE";
VitalsUpdateFrequency["NEVER"] = "NEVER";
return VitalsUpdateFrequency;
}({});
export let UploadFrequency = /*#__PURE__*/function (UploadFrequency) {
/**
* Upload data every 1000ms.
*/
UploadFrequency["FREQUENT"] = "FREQUENT";
/**
* Upload data every 5000ms.
*/
UploadFrequency["AVERAGE"] = "AVERAGE";
/**
* Upload data every 10000ms.
*/
UploadFrequency["RARE"] = "RARE";
return UploadFrequency;
}({});
export let BatchSize = /*#__PURE__*/function (BatchSize) {
/**
* Upload less frequent, larger batches of data
*/
BatchSize["LARGE"] = "LARGE";
/**
* Use default size for batches of data
*/
BatchSize["MEDIUM"] = "MEDIUM";
/**
* Upload more frequent, smaller batches of data
*/
BatchSize["SMALL"] = "SMALL";
return BatchSize;
}({});
export let BatchProcessingLevel = /*#__PURE__*/function (BatchProcessingLevel) {
/**
* Only 1 batch will be sent in a single upload cycle.
*/
BatchProcessingLevel["LOW"] = "LOW";
/**
* 10 batches will be sent in a single upload cycle
*/
BatchProcessingLevel["MEDIUM"] = "MEDIUM";
/**
* 100 batches will be sent in a single upload cycle.
*/
BatchProcessingLevel["HIGH"] = "HIGH";
return BatchProcessingLevel;
}({});
const isLegacyFirstPartyHost = firstPartyHost => {
return typeof firstPartyHost === 'string';
};
/**
* Defaults legacy first party hosts format to Datadog first party hosts to keep
* retro-compatibility before OTel support was introduced.
*/
export const formatFirstPartyHosts = firstPartyHosts => {
return firstPartyHosts.map(host => {
if (isLegacyFirstPartyHost(host)) {
return {
match: host,
propagatorTypes: [PropagatorType.DATADOG, PropagatorType.TRACECONTEXT]
};
}
return host;
});
};
export const DEFAULTS = {
nativeCrashReportEnabled: false,
sessionSamplingRate: 100.0,
resourceTracingSamplingRate: 20.0,
site: 'US1',
longTaskThresholdMs: 0,
nativeLongTaskThresholdMs: 200,
nativeViewTracking: false,
nativeInteractionTracking: false,
getFirstPartyHosts: () => [],
getAdditionalConfiguration: () => ({}),
trackingConsent: TrackingConsent.GRANTED,
telemetrySampleRate: 20.0,
vitalsUpdateFrequency: VitalsUpdateFrequency.AVERAGE,
logEventMapper: null,
errorEventMapper: null,
resourceEventMapper: null,
actionEventMapper: null,
trackFrustrations: true,
uploadFrequency: UploadFrequency.AVERAGE,
batchSize: BatchSize.MEDIUM,
trackBackgroundEvents: false,
getCustomEndpoints: () => ({}),
bundleLogsWithRum: true,
bundleLogsWithTraces: true,
useAccessibilityLabel: true,
trackWatchdogTerminations: false,
batchProcessingLevel: BatchProcessingLevel.MEDIUM
};
/**
* The SDK configuration class.
* It will be used to configure the SDK functionality at initialization.
*/
export class DdSdkReactNativeConfiguration {
/**
* Enables crash reporting for native platforms (iOS, Android). Default `false`.
*/
nativeCrashReportEnabled = DEFAULTS.nativeCrashReportEnabled;
/**
* @deprecated `sampleRate` has been replaced by `sessionSamplingRate` to avoid confusion with `resourceTracingSamplingRate` and will be removed in a future release.
*/
/**
* Percentage of sampled RUM sessions. Range `0`-`100`.
*/
sessionSamplingRate = DEFAULTS.sessionSamplingRate;
/**
* Percentage of tracing integrations for network calls between your app and your backend. Range `0`-`100`.
*/
resourceTracingSamplingRate = DEFAULTS.resourceTracingSamplingRate;
site = DEFAULTS.site;
/**
* Verbosity for internal SDK logging.
* Set to `SdkVerbosity.DEBUG` to debug your SDK implementation.
*/
verbosity = undefined;
/**
* Enables native views tracking.
* Set to `true` if you use a custom navigation system relying on native views.
*/
nativeViewTracking = DEFAULTS.nativeViewTracking;
/**
* Enables native interaction tracking.
* Set to `true` if you want to track interactions on native screens.
*/
nativeInteractionTracking = DEFAULTS.nativeInteractionTracking;
proxyConfig = undefined;
serviceName = undefined;
/**
* List of your backends hosts to enable tracing with.
* Regular expressions are NOT supported.
*
* Matches domains and subdomains, e.g. `['example.com']` matches `example.com` and `api.example.com`.
*/
firstPartyHosts = DEFAULTS.getFirstPartyHosts();
/**
* Overrides the reported version of the app.
* Accepted characters are alphanumerics and `_`, `-`, `:`, `.`, `/`.
* Other special characters are converted to underscores.
*
* See https://docs.datadoghq.com/getting_started/tagging/#define-tags for more information on the format.
*
* Make sure you set it correctly, as it will have to match the one specified during the upload of your source maps and other mapping files.
*/
/**
* Add a suffix to the reported version of the app.
* Accepted characters are alphanumerics and `_`, `-`, `:`, `.`, `/`.
* Other special characters are converted to underscores.
*
* See https://docs.datadoghq.com/getting_started/tagging/#define-tags for more information on the format.
*
* A dash (`-`) will be automatically added between the version and the suffix
*/
/**
* The sampling rate for Internal Telemetry (info related to the work of the
* SDK internals).
*
* The sampling rate must be a value between 0 and 100. A value of 0 means no
* telemetry will be sent, 100 means all telemetry will be sent. When
* `telemetrySampleRate` is not set, the default value from the iOS and
* Android SDK is used, which is 20.
*/
telemetrySampleRate = DEFAULTS.telemetrySampleRate;
/**
* The threshold for native long tasks reporting in milliseconds.
*
* - Setting it to `0` or `false` disables native long task reporting.
* - Values below `100` will be raised to `100`.
* - Values above `5000` will be lowered to `5000`.
*
* Default value is `200`.
*/
nativeLongTaskThresholdMs = DEFAULTS.nativeLongTaskThresholdMs;
/**
* The threshold for javascript long tasks reporting in milliseconds.
*
* - Setting it to `0` or `false` disables javascript long task reporting.
* - Values below `100` will be raised to `100`.
* - Values above `5000` will be lowered to `5000`.
*
* Default value is `0`
*/
longTaskThresholdMs = DEFAULTS.longTaskThresholdMs;
/**
* Sets the preferred frequency for collecting mobile vitals.
*/
vitalsUpdateFrequency = DEFAULTS.vitalsUpdateFrequency;
/**
* Enables tracking of frustration signals (error taps). Defaults to `true`.
*/
trackFrustrations = DEFAULTS.trackFrustrations;
/**
* Sets the preferred frequency for uploading batches of data.
*/
uploadFrequency = DEFAULTS.uploadFrequency;
/**
* Defines the Datadog SDK policy when batching data together before uploading it to Datadog servers.
* Smaller batches mean smaller but more network requests, whereas larger batches mean fewer but larger network requests.
*/
batchSize = DEFAULTS.batchSize;
/**
* Enables tracking of RUM event when no RUM View is active.
*
* By default, background events are not tracked. Enabling this feature might increase the
* number of sessions tracked and impact your billing.
*/
trackBackgroundEvents = DEFAULTS.trackBackgroundEvents;
/**
* Enables RUM correlation with logs.
*
* By default, RUM is enabled for logs.
*/
bundleLogsWithRum = DEFAULTS.bundleLogsWithRum;
/**
* Enables Traces correlation with logs.
*
* By default, Traces is enabled for logs.
*/
bundleLogsWithTraces = DEFAULTS.bundleLogsWithTraces;
/**
* Sets the preferred level for processing batches of data.
*/
batchProcessingLevel = DEFAULTS.batchProcessingLevel;
/**
* Enables tracking of non-fatal ANRs on Android.
* By default, the reporting of non-fatal ANRs on Android 30+ is disabled because it would
* create too much noise over fatal ANRs. On Android 29 and below, however,
* the reporting of non-fatal ANRs is enabled by default,
* as fatal ANRs cannot be reported on those versions.
*/
/**
* The app hang threshold in seconds for non-fatal app hangs on iOS.
*
* App hangs are an iOS-specific type of error that happens when the application is unresponsive for too long.
* By default, app hangs reporting is disabled, but you can enable it and set your
* own threshold to monitor app hangs that last more than a specified
* duration by using the this parameter.
*
* Set the `appHangThreshold` parameter to the minimal duration you want
* app hangs to be reported. For example, enter 0.25 to report hangs lasting at least 250 ms.
* See [Configure the app hang threshold](https://docs.datadoghq.com/real_user_monitoring/error_tracking/mobile/ios/?tab=cocoapods#configure-the-app-hang-threshold)
* for more guidance on what to set this value to.
*/
/**
* The amount of time after a view starts where a Resource should be
* considered when calculating Time to Network-Settled (TNS). TNS will be
* calculated using all resources that start withing the specified threshold, in seconds.
* Defaults to 0.1 seconds.
*/
/**
* Determines whether the SDK should track application termination by the watchdog on iOS. Default: `false`.
*/
trackWatchdogTerminations = DEFAULTS.trackWatchdogTerminations;
/**
* Specifies a custom prop to name RUM actions on elements having an `onPress` prop.
*
* For example if you set it to `testID`, the value of the `testID` prop is used as a custom action name:
*
* ```js
* <TouchableOpacity testID="Dismiss notification" onPress={() => dismiss()}>
* ```
*
* `dd-action-name` is favored when both attributes are present on an element.
*/
logEventMapper = DEFAULTS.logEventMapper;
errorEventMapper = DEFAULTS.errorEventMapper;
resourceEventMapper = DEFAULTS.resourceEventMapper;
actionEventMapper = DEFAULTS.actionEventMapper;
additionalConfiguration = DEFAULTS.getAdditionalConfiguration();
customEndpoints = DEFAULTS.getCustomEndpoints();
constructor(clientToken, env, applicationId, trackInteractions = false, trackResources = false, trackErrors = false, trackingConsent = DEFAULTS.trackingConsent, useAccessibilityLabel = DEFAULTS.useAccessibilityLabel // eslint-disable-next-line no-empty-function
) {
this.clientToken = clientToken;
this.env = env;
this.applicationId = applicationId;
this.trackInteractions = trackInteractions;
this.trackResources = trackResources;
this.trackErrors = trackErrors;
this.trackingConsent = trackingConsent;
this.useAccessibilityLabel = useAccessibilityLabel;
}
}
/**
* Auto Instrumentation configuration passed to DatadogProvider.
* Does not include default values.
*/
/**
* Parameters needed to start auto instrumentation. Includes default values.
*/
/**
* We could use `Proxy` instead of this function, but `Proxy` is not available on
* the older android jsc that can still be used.
*/
export const addDefaultValuesToAutoInstrumentationConfiguration = features => {
return {
...features,
firstPartyHosts: features.firstPartyHosts || DEFAULTS.getFirstPartyHosts(),
resourceTracingSamplingRate: features.resourceTracingSamplingRate === undefined ? DEFAULTS.resourceTracingSamplingRate : features.resourceTracingSamplingRate,
logEventMapper: features.logEventMapper === undefined ? DEFAULTS.logEventMapper : features.logEventMapper,
errorEventMapper: features.errorEventMapper === undefined ? DEFAULTS.errorEventMapper : features.errorEventMapper,
resourceEventMapper: features.resourceEventMapper === undefined ? DEFAULTS.resourceEventMapper : features.resourceEventMapper,
actionEventMapper: features.actionEventMapper === undefined ? DEFAULTS.actionEventMapper : features.actionEventMapper,
useAccessibilityLabel: DEFAULTS.useAccessibilityLabel
};
};
const setConfigurationAttribute = (attribute, configuration) => {
if (attribute.value !== undefined) {
configuration[attribute.name] = attribute.value;
}
};
export const buildConfigurationFromPartialConfiguration = (features, configuration) => {
const {
clientToken,
env,
applicationId,
...remainingConfiguration
} = configuration;
const SdkConfiguration = new DdSdkReactNativeConfiguration(clientToken, env, applicationId, features.trackInteractions, features.trackResources, features.trackErrors, configuration.trackingConsent, features.useAccessibilityLabel);
Object.keys(remainingConfiguration).forEach(name => {
setConfigurationAttribute({
value: remainingConfiguration[name],
name
}, SdkConfiguration);
});
setConfigurationAttribute({
name: 'resourceTracingSamplingRate',
value: features.resourceTracingSamplingRate
}, SdkConfiguration);
setConfigurationAttribute({
name: 'firstPartyHosts',
value: features.firstPartyHosts
}, SdkConfiguration);
setConfigurationAttribute({
name: 'logEventMapper',
value: features.logEventMapper
}, SdkConfiguration);
setConfigurationAttribute({
name: 'errorEventMapper',
value: features.errorEventMapper
}, SdkConfiguration);
setConfigurationAttribute({
name: 'resourceEventMapper',
value: features.resourceEventMapper
}, SdkConfiguration);
setConfigurationAttribute({
name: 'actionEventMapper',
value: features.actionEventMapper
}, SdkConfiguration);
setConfigurationAttribute({
name: 'actionNameAttribute',
value: features.actionNameAttribute
}, SdkConfiguration);
return SdkConfiguration;
};
export class DatadogProviderConfiguration extends DdSdkReactNativeConfiguration {
initializationMode = InitializationMode.SYNC;
}
export let InitializationMode = /*#__PURE__*/function (InitializationMode) {
InitializationMode["SYNC"] = "SYNC";
InitializationMode["ASYNC"] = "ASYNC";
return InitializationMode;
}({});
//# sourceMappingURL=DdSdkReactNativeConfiguration.js.map