@datadog/mobile-react-native 1.2.0-beta1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/DatadogSDKReactNative.podspec +2 -2
  2. package/android/build.gradle +28 -3
  3. package/android/detekt.yml +572 -0
  4. package/android/src/main/kotlin/com/datadog/reactnative/DatadogSDKWrapper.kt +4 -0
  5. package/android/src/main/kotlin/com/datadog/reactnative/DatadogWrapper.kt +7 -2
  6. package/android/src/main/kotlin/com/datadog/reactnative/DdSdk.kt +118 -8
  7. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkConfiguration.kt +23 -1
  8. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkConfigurationExt.kt +29 -1
  9. package/android/src/main/kotlin/com/datadog/reactnative/VitalFrameCallback.kt +41 -0
  10. package/android/src/test/kotlin/com/datadog/reactnative/DdRumTest.kt +7 -2
  11. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt +377 -27
  12. package/android/src/test/kotlin/com/datadog/reactnative/DdTraceTest.kt +5 -1
  13. package/android/src/test/kotlin/com/datadog/tools/unit/DdSdkConfigurationExt.kt +33 -1
  14. package/android/src/test/kotlin/com/datadog/tools/unit/GenericAssert.kt +21 -1
  15. package/android/src/test/kotlin/com/datadog/tools/unit/MapExt.kt +1 -1
  16. package/android/src/test/kotlin/com/datadog/tools/unit/forge/BaseConfigurator.kt +1 -0
  17. package/android/src/test/kotlin/com/datadog/tools/unit/forge/DdSdkConfigurationForgeryFactory.kt +21 -2
  18. package/android/src/test/kotlin/com/datadog/tools/unit/forge/PackageInfoForgeryFactory.kt +2 -3
  19. package/android/src/test/kotlin/com/datadog/tools/unit/forge/TelemetryConfigurationEventForgeryFactory.kt +94 -0
  20. package/datadog-sourcemaps.gradle +85 -28
  21. package/ios/Sources/Attributes.swift +0 -4
  22. package/ios/Sources/DdSdk.swift +99 -13
  23. package/ios/Sources/DdSdkConfiguration.swift +34 -1
  24. package/ios/Sources/JSRefreshRateListener.swift +86 -0
  25. package/ios/Sources/QueueHolder.swift +7 -0
  26. package/ios/Sources/RNDdSdkConfiguration.swift +23 -1
  27. package/lib/commonjs/DdSdkReactNative.js +24 -25
  28. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  29. package/lib/commonjs/DdSdkReactNativeConfiguration.js +66 -2
  30. package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
  31. package/lib/commonjs/errorUtils.js +12 -3
  32. package/lib/commonjs/errorUtils.js.map +1 -1
  33. package/lib/commonjs/longTasksUtils.js +40 -0
  34. package/lib/commonjs/longTasksUtils.js.map +1 -0
  35. package/lib/commonjs/rum/instrumentation/getJsxRuntime.js +11 -8
  36. package/lib/commonjs/rum/instrumentation/getJsxRuntime.js.map +1 -1
  37. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/distributedTracing.js +2 -1
  38. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -1
  39. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +2 -1
  40. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -1
  41. package/lib/commonjs/types.js +5 -1
  42. package/lib/commonjs/types.js.map +1 -1
  43. package/lib/commonjs/version.js +1 -1
  44. package/lib/commonjs/version.js.map +1 -1
  45. package/lib/module/DdSdkReactNative.js +23 -24
  46. package/lib/module/DdSdkReactNative.js.map +1 -1
  47. package/lib/module/DdSdkReactNativeConfiguration.js +64 -1
  48. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  49. package/lib/module/errorUtils.js +12 -3
  50. package/lib/module/errorUtils.js.map +1 -1
  51. package/lib/module/longTasksUtils.js +31 -0
  52. package/lib/module/longTasksUtils.js.map +1 -0
  53. package/lib/module/rum/instrumentation/getJsxRuntime.js +11 -8
  54. package/lib/module/rum/instrumentation/getJsxRuntime.js.map +1 -1
  55. package/lib/module/rum/instrumentation/resourceTracking/domain/distributedTracing.js +2 -1
  56. package/lib/module/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -1
  57. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +2 -1
  58. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -1
  59. package/lib/module/types.js +5 -1
  60. package/lib/module/types.js.map +1 -1
  61. package/lib/module/version.js +1 -1
  62. package/lib/module/version.js.map +1 -1
  63. package/lib/typescript/DdSdkReactNative.d.ts +0 -7
  64. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +51 -0
  65. package/lib/typescript/errorUtils.d.ts +8 -0
  66. package/lib/typescript/longTasksUtils.d.ts +6 -0
  67. package/lib/typescript/rum/instrumentation/resourceTracking/domain/distributedTracing.d.ts +1 -0
  68. package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.d.ts +1 -1
  69. package/lib/typescript/types.d.ts +15 -1
  70. package/lib/typescript/version.d.ts +1 -1
  71. package/package.json +6 -3
  72. package/scripts/replace-react-require.js +78 -0
  73. package/src/DatadogProvider/__tests__/initialization.test.tsx +9 -1
  74. package/src/DdSdkReactNative.tsx +31 -34
  75. package/src/DdSdkReactNativeConfiguration.tsx +66 -1
  76. package/src/__tests__/DdSdkReactNative.test.tsx +56 -13
  77. package/src/__tests__/DdSdkReactNativeConfiguration.test.ts +15 -2
  78. package/src/__tests__/longTasksUtils.ts +23 -0
  79. package/src/errorUtils.ts +11 -3
  80. package/src/longTasksUtils.ts +30 -0
  81. package/src/rum/instrumentation/getJsxRuntime.ts +11 -11
  82. package/src/rum/instrumentation/resourceTracking/domain/distributedTracing.tsx +3 -1
  83. package/src/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.ts +1 -1
  84. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.ts +2 -1
  85. package/src/rum/instrumentation/resourceTracking/implementation/__tests__/XHRProxy.test.ts +9 -47
  86. package/src/types.tsx +10 -1
  87. package/src/version.ts +1 -1
@@ -3,9 +3,10 @@
3
3
  * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
-
6
+
7
7
  package com.datadog.tools.unit.forge
8
8
 
9
+ import com.datadog.reactnative.ConfigurationForTelemetry
9
10
  import com.datadog.reactnative.DdSdkConfiguration
10
11
  import fr.xgouchet.elmyr.Forge
11
12
  import fr.xgouchet.elmyr.ForgeryFactory
@@ -18,8 +19,18 @@ class DdSdkConfigurationForgeryFactory : ForgeryFactory<DdSdkConfiguration> {
18
19
  env = forge.anAlphabeticalString(),
19
20
  applicationId = forge.aNullable { getForgery<UUID>().toString() },
20
21
  nativeCrashReportEnabled = forge.aNullable { aBool() },
22
+ nativeLongTaskThresholdMs = forge.aNullable { aDouble(100.0, 5000.0) },
23
+ longTaskThresholdMs = forge.aDouble(0.0, 100.0),
21
24
  sampleRate = forge.aNullable { aDouble(0.0, 100.0) },
22
25
  telemetrySampleRate = forge.aNullable { aDouble(0.0, 100.0) },
26
+ vitalsUpdateFrequency = forge.aNullable {
27
+ anElementFrom(
28
+ "RARE",
29
+ "NEVER",
30
+ "FREQUENT",
31
+ "AVERAGE"
32
+ )
33
+ },
23
34
  site = forge.aNullable { anElementFrom("US", "EU", "GOV") },
24
35
  additionalConfig = forge.aMap {
25
36
  forge.anAsciiString() to forge.anElementFrom(
@@ -28,7 +39,15 @@ class DdSdkConfigurationForgeryFactory : ForgeryFactory<DdSdkConfiguration> {
28
39
  null
29
40
  )
30
41
  },
31
- trackingConsent = forge.aNullable { anElementFrom("pending", "granted", "not_granted") }
42
+ trackingConsent = forge.aNullable {
43
+ anElementFrom("pending", "granted", "not_granted")
44
+ },
45
+ configurationForTelemetry = ConfigurationForTelemetry(
46
+ initializationType = forge.anAlphabeticalString(),
47
+ trackErrors = forge.aBool(),
48
+ trackInteractions = forge.aBool(),
49
+ trackNetworkRequests = forge.aBool()
50
+ )
32
51
  )
33
52
  }
34
53
  }
@@ -3,17 +3,16 @@
3
3
  * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
-
6
+
7
7
  package com.datadog.tools.unit.forge
8
8
 
9
9
  import android.content.pm.PackageInfo
10
10
  import fr.xgouchet.elmyr.Forge
11
11
  import fr.xgouchet.elmyr.ForgeryFactory
12
- import java.util.UUID
13
12
 
14
13
  class PackageInfoForgeryFactory : ForgeryFactory<PackageInfo> {
15
14
  override fun getForgery(forge: Forge): PackageInfo {
16
- val packageInfo = PackageInfo();
15
+ val packageInfo = PackageInfo()
17
16
  packageInfo.versionName = forge.aStringMatching("[0-9]\\.[0-9]\\.[0-9]")
18
17
  return packageInfo
19
18
  }
@@ -0,0 +1,94 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ package com.datadog.tools.unit.forge
8
+
9
+ import com.datadog.android.telemetry.model.TelemetryConfigurationEvent
10
+ import fr.xgouchet.elmyr.Forge
11
+ import fr.xgouchet.elmyr.ForgeryFactory
12
+ import fr.xgouchet.elmyr.jvm.ext.aTimestamp
13
+ import java.util.UUID
14
+
15
+ internal class TelemetryConfigurationEventForgeryFactory :
16
+ ForgeryFactory<TelemetryConfigurationEvent> {
17
+
18
+ override fun getForgery(forge: Forge): TelemetryConfigurationEvent {
19
+ return TelemetryConfigurationEvent(
20
+ date = forge.aTimestamp(),
21
+ source = forge.aValueFrom(TelemetryConfigurationEvent.Source::class.java),
22
+ service = forge.anAlphabeticalString(),
23
+ version = forge.anAlphabeticalString(),
24
+ application = forge.aNullable {
25
+ TelemetryConfigurationEvent.Application(
26
+ forge.getForgery<UUID>().toString()
27
+ )
28
+ },
29
+ session = forge.aNullable {
30
+ TelemetryConfigurationEvent.Session(
31
+ forge.getForgery<UUID>().toString()
32
+ )
33
+ },
34
+ view = forge.aNullable {
35
+ TelemetryConfigurationEvent.View(
36
+ forge.getForgery<UUID>().toString()
37
+ )
38
+ },
39
+ action = forge.aNullable {
40
+ TelemetryConfigurationEvent.Action(
41
+ forge.getForgery<UUID>().toString()
42
+ )
43
+ },
44
+ dd = TelemetryConfigurationEvent.Dd(),
45
+ telemetry = TelemetryConfigurationEvent.Telemetry(
46
+ configuration = TelemetryConfigurationEvent.Configuration(
47
+ forge.aNullable { aLong() },
48
+ forge.aNullable { aLong() },
49
+ forge.aNullable { aLong() },
50
+ forge.aNullable { aLong() },
51
+ forge.aNullable { aLong() },
52
+ forge.aNullable { aLong() },
53
+ forge.aNullable { aLong() },
54
+ forge.aNullable { aBool() },
55
+ forge.aNullable { aBool() },
56
+ forge.aNullable { aBool() },
57
+ forge.aNullable { aBool() },
58
+ forge.aNullable { aBool() },
59
+ forge.aNullable { aBool() },
60
+ forge.aNullable { aBool() },
61
+ forge.aNullable { aBool() },
62
+ forge.aNullable { aString() },
63
+ forge.aNullable { aBool() },
64
+ forge.aNullable { aString() },
65
+ forge.aNullable { aBool() },
66
+ forge.aNullable { aBool() },
67
+ forge.aNullable { aBool() },
68
+ forge.aNullable { aBool() },
69
+ forge.aNullable { aBool() },
70
+ forge.aNullable { aList { aString() } },
71
+ forge.aNullable { aList { aString() } },
72
+ forge.aNullable { aBool() },
73
+ forge.aNullable {
74
+ aValueFrom(TelemetryConfigurationEvent.ViewTrackingStrategy::class.java)
75
+ },
76
+ forge.aNullable { aBool() },
77
+ forge.aNullable { aLong() },
78
+ forge.aNullable { aBool() },
79
+ forge.aNullable { aBool() },
80
+ forge.aNullable { aBool() },
81
+ forge.aNullable { aBool() },
82
+ forge.aNullable { aBool() },
83
+ forge.aNullable { aBool() },
84
+ forge.aNullable { aBool() },
85
+ forge.aNullable { aBool() },
86
+ forge.aNullable { aString() },
87
+ forge.aNullable { aBool() },
88
+ forge.aNullable { aLong() },
89
+ forge.aNullable { aLong() }
90
+ )
91
+ )
92
+ )
93
+ }
94
+ }
@@ -6,9 +6,6 @@
6
6
 
7
7
  import org.apache.tools.ant.taskdefs.condition.Os
8
8
 
9
- def reactConfig = project.hasProperty("react") ? project.react : [:]
10
- def reactRoot = file(reactConfig.root ?: "../../")
11
-
12
9
  afterEvaluate {
13
10
 
14
11
  def androidExtension = extensions.findByName("android")
@@ -33,7 +30,10 @@ afterEvaluate {
33
30
  def targetName = variant.name.capitalize()
34
31
  def targetPath = variant.dirName
35
32
 
36
- def bundleTask = tasks.findByName("bundle${targetName}JsAndAssets")
33
+ def reactConfig = getReactConfig(buildDir, targetName)
34
+ def reactRoot = file(reactConfig.root)
35
+
36
+ def bundleTask = tasks.findByName(reactConfig.bundleTaskName)
37
37
 
38
38
  if (bundleTask == null) {
39
39
  logger.info("Cannot find JS bundle task for variant=${targetName}.")
@@ -47,10 +47,7 @@ afterEvaluate {
47
47
  def serviceName = getServiceName(variant)
48
48
  logger.info("Service name used for the upload of variant=${targetName} is ${serviceName}.")
49
49
 
50
- def bundleAssetName = reactConfig.bundleAssetName ?: "index.android.bundle"
51
-
52
- def jsBundleDir = file("$buildDir/generated/assets/react/${targetPath}")
53
- def jsBundleFile = file("$jsBundleDir/$bundleAssetName")
50
+ def bundleAssetName = reactConfig.bundleAssetName
54
51
 
55
52
  def jsSourceMapsDir = file("$buildDir/generated/sourcemaps/react/${targetPath}")
56
53
  def jsOutputSourceMapFile = file("$jsSourceMapsDir/${bundleAssetName}.map")
@@ -59,27 +56,30 @@ afterEvaluate {
59
56
  group = "datadog"
60
57
  description = "Uploads sourcemaps to Datadog."
61
58
 
62
- def execCommand = [
63
- "yarn",
64
- "datadog-ci",
65
- "react-native",
66
- "upload",
67
- "--platform",
68
- "android",
69
- "--service",
70
- serviceName,
71
- "--bundle",
72
- jsBundleFile.absolutePath,
73
- "--sourcemap",
74
- jsOutputSourceMapFile.absolutePath,
75
- "--release-version",
76
- releaseVersion,
77
- "--build-version",
78
- buildVersion
79
- ]
59
+ def execCommand = { jsBundleFile ->
60
+ return [
61
+ "yarn",
62
+ "datadog-ci",
63
+ "react-native",
64
+ "upload",
65
+ "--platform",
66
+ "android",
67
+ "--service",
68
+ serviceName,
69
+ "--bundle",
70
+ jsBundleFile.absolutePath,
71
+ "--sourcemap",
72
+ jsOutputSourceMapFile.absolutePath,
73
+ "--release-version",
74
+ releaseVersion,
75
+ "--build-version",
76
+ buildVersion
77
+ ]
78
+ }
80
79
 
81
80
  doFirst {
82
- if (!jsBundleFile.exists()) {
81
+ def jsBundleFile = reactConfig.bundleFileResolver()
82
+ if (jsBundleFile == null) {
83
83
  throw new GradleException("JS bundle file doesn't exist, aborting upload.")
84
84
  }
85
85
 
@@ -87,7 +87,7 @@ afterEvaluate {
87
87
  throw new GradleException("JS sourcemap file doesn't exist, aborting upload.")
88
88
  }
89
89
 
90
- runShellCommand(execCommand, reactRoot)
90
+ runShellCommand(execCommand(jsBundleFile), reactRoot)
91
91
 
92
92
  }
93
93
  }
@@ -98,6 +98,63 @@ afterEvaluate {
98
98
  }
99
99
  }
100
100
 
101
+ /**
102
+ * We use a function here to resolve the correct bundle location after the bundle task
103
+ * is over, as its location can depend on the Android Gradle Plugin version.
104
+ */
105
+ private def getBundleFileResolver(String[] jsBundleDirs, String bundleAssetName) {
106
+ def resolver = { ->
107
+ return jsBundleDirs.collect {
108
+ file("$it/$bundleAssetName")
109
+ }.find {
110
+ it.exists()
111
+ }
112
+ }
113
+
114
+ return resolver
115
+ }
116
+
117
+ /**
118
+ * From RN 0.71, the following things changed in the RN gradle plugin:
119
+ * - bundle task name changed from "bundle${targetName}JsAndAssets" to "createBundle${targetName}JsAndAssets"
120
+ * - bundle dir changed from "$buildDir/generated/assets/react/${targetPath}" to "$buildDir/ASSETS/createBundle${targetPath}JsAndAssets"
121
+ * (and then "$buildDir/generated/assets/createBundle${targetPath}JsAndAssets" with the release of Android Gradle Plugin 7.4, see https://github.com/facebook/react-native/issues/35439)
122
+ * - config was in "project.react" and is now in "project.extensions.react"
123
+ * - accessing parameters values requires calling a getter
124
+ */
125
+ private def getReactConfig(File buildDir, String targetName) {
126
+ def reactConfig = [:]
127
+
128
+ if (project.extensions.findByName("react")) {
129
+ // From RN 0.71
130
+ def bundleAssetName = project.extensions.react.bundleAssetName.get() ?: "index.android.bundle"
131
+ reactConfig['bundleTaskName'] = "createBundle${targetName}JsAndAssets"
132
+ reactConfig['bundleFileResolver'] = getBundleFileResolver([
133
+ "$buildDir/ASSETS/createBundle${targetName}JsAndAssets", // Android Gradle Plugin 7.3
134
+ "$buildDir/generated/assets/createBundle${targetName}JsAndAssets" // Android Gradle Plugin 7.4 and up
135
+ ] as String[], bundleAssetName)
136
+ reactConfig['bundleAssetName'] = bundleAssetName
137
+ reactConfig['root'] = project.extensions.react.root.get() ?: "../../"
138
+ } else if (project.hasProperty("react")) {
139
+ // WARNING: on RN 0.71, `project.react` is an empty map, so this `if` must go after the previous one.
140
+ // Legacy way, before RN 0.71
141
+ def bundleAssetName = project.react.bundleAssetName ?: "index.android.bundle"
142
+ reactConfig['bundleTaskName'] = "bundle${targetName}JsAndAssets"
143
+ reactConfig['bundleFileResolver'] = getBundleFileResolver(["$buildDir/generated/assets/react/${targetName}"] as String[], bundleAssetName)
144
+ reactConfig['bundleAssetName'] = bundleAssetName
145
+ reactConfig['root'] = project.react.root ?: "../../"
146
+ } else {
147
+ // We assume this cannot happen with RN >= 0.71, so we use the legacy default values
148
+ def bundleAssetName = "index.android.bundle"
149
+ reactConfig['bundleTaskName'] = "bundle${targetName}JsAndAssets"
150
+ reactConfig['bundleFileResolver'] = getBundleFileResolver(["$buildDir/generated/assets/react/${targetName}"] as String[], bundleAssetName)
151
+ reactConfig['bundleAssetName'] = bundleAssetName
152
+ reactConfig['root'] = "../../"
153
+ }
154
+
155
+ return reactConfig
156
+ }
157
+
101
158
  private def runShellCommand(
102
159
  List<String> command,
103
160
  File workingDirectory
@@ -81,10 +81,6 @@ internal struct InternalConfigurationAttributes {
81
81
  /// Expects `String?` value
82
82
  static let serviceName = "_dd.service_name"
83
83
 
84
- /// Enable overriding the long task threshold for long task detection.
85
- /// Expects `Double?` value
86
- static let longTaskThreshold = "_dd.long_task.threshold"
87
-
88
84
  /// Specify first party hosts for distributed tracing.
89
85
  /// Even if the requests are intercepted at the javascript level, we have to pass this parameter to make sure the headers are
90
86
  /// correctly set when sending the traces to the intake.
@@ -7,6 +7,7 @@
7
7
  import Foundation
8
8
  import Datadog
9
9
  import DatadogCrashReporting
10
+ import React
10
11
 
11
12
  func getDefaultAppVersion() -> String {
12
13
  let bundleShortVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
@@ -16,46 +17,60 @@ func getDefaultAppVersion() -> String {
16
17
 
17
18
  @objc(DdSdk)
18
19
  class RNDdSdk: NSObject {
20
+ @objc var bridge: RCTBridge!
21
+
19
22
  @objc(requiresMainQueueSetup)
20
23
  static func requiresMainQueueSetup() -> Bool {
21
24
  return false
22
25
  }
23
26
 
27
+ let jsRefreshRateMonitor: RefreshRateMonitor
24
28
  let mainDispatchQueue: DispatchQueueType
25
29
  @objc(methodQueue)
26
30
  let methodQueue: DispatchQueue = sharedQueue
27
31
 
32
+ private let jsLongTaskThresholdInSeconds: TimeInterval = 0.1;
33
+
28
34
  convenience override init() {
29
- self.init(mainDispatchQueue: DispatchQueue.main)
35
+ self.init(mainDispatchQueue: DispatchQueue.main, jsRefreshRateMonitor: JSRefreshRateMonitor.init())
30
36
  }
31
37
 
32
- init(mainDispatchQueue: DispatchQueueType) {
38
+ init(mainDispatchQueue: DispatchQueueType, jsRefreshRateMonitor: RefreshRateMonitor) {
33
39
  self.mainDispatchQueue = mainDispatchQueue
40
+ self.jsRefreshRateMonitor = jsRefreshRateMonitor
34
41
  super.init()
35
42
  }
36
-
37
-
43
+
38
44
  @objc(initialize:withResolver:withRejecter:)
39
45
  func initialize(configuration: NSDictionary, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
40
46
  // Datadog SDK init needs to happen on the main thread: https://github.com/DataDog/dd-sdk-reactnative/issues/198
41
47
  self.mainDispatchQueue.async {
48
+ let sdkConfiguration = configuration.asDdSdkConfiguration()
49
+
42
50
  if Datadog.isInitialized {
43
51
  // Initializing the SDK twice results in Global.rum and
44
52
  // Global.sharedTracer to be set to no-op instances
45
53
  consolePrint("Datadog SDK is already initialized, skipping initialization.")
46
- Datadog._internal._telemtry.debug(id: "datadog_react_native: RN SDK was already initialized in native", message: "RN SDK was already initialized in native")
54
+ Datadog._internal.telemetry.debug(id: "datadog_react_native: RN SDK was already initialized in native", message: "RN SDK was already initialized in native")
55
+
56
+ // This block is called when SDK is reinitialized and the javascript has been wiped out.
57
+ // In this case, we need to restart the refresh rate monitor, as the javascript thread
58
+ // appears to change at that moment.
59
+ self.startJSRefreshRateMonitoring(sdkConfiguration: sdkConfiguration)
47
60
  resolve(nil)
48
61
  return
49
62
  }
50
- let sdkConfiguration = configuration.asDdSdkConfiguration()
51
63
  self.setVerbosityLevel(additionalConfig: sdkConfiguration.additionalConfig)
52
64
 
53
65
  let ddConfig = self.buildConfiguration(configuration: sdkConfiguration)
54
66
  let consent = self.buildTrackingConsent(consent: sdkConfiguration.trackingConsent)
55
67
  Datadog.initialize(appContext: Datadog.AppContext(), trackingConsent: consent, configuration: ddConfig)
68
+ self.sendConfigurationAsTelemetry(rnConfiguration: sdkConfiguration)
56
69
 
57
70
  Global.rum = RUMMonitor.initialize()
58
71
 
72
+ self.startJSRefreshRateMonitoring(sdkConfiguration: sdkConfiguration)
73
+
59
74
  resolve(nil)
60
75
  }
61
76
  }
@@ -91,15 +106,37 @@ class RNDdSdk: NSObject {
91
106
 
92
107
  @objc(telemetryDebug:withResolver:withRejecter:)
93
108
  func telemetryDebug(message: NSString, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
94
- Datadog._internal._telemtry.debug(id: "datadog_react_native:\(message)", message: message as String)
109
+ Datadog._internal.telemetry.debug(id: "datadog_react_native:\(message)", message: message as String)
95
110
  resolve(nil)
96
111
  }
97
112
 
98
113
  @objc(telemetryError:withStack:withKind:withResolver:withRejecter:)
99
114
  func telemetryDebug(message: NSString, stack: NSString, kind: NSString, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
100
- Datadog._internal._telemtry.error(id: "datadog_react_native:\(String(describing: kind)):\(message)", message: message as String, kind: kind as? String, stack: stack as? String)
115
+ Datadog._internal.telemetry.error(id: "datadog_react_native:\(String(describing: kind)):\(message)", message: message as String, kind: kind as? String, stack: stack as? String)
101
116
  resolve(nil)
102
117
  }
118
+
119
+ func sendConfigurationAsTelemetry(rnConfiguration: DdSdkConfiguration) -> Void {
120
+ Datadog._internal.telemetry.setConfigurationMapper { event in
121
+ var event = event
122
+
123
+ var configuration = event.telemetry.configuration
124
+ configuration.initializationType = rnConfiguration.configurationForTelemetry?.initializationType as? String
125
+ configuration.trackErrors = rnConfiguration.configurationForTelemetry?.trackErrors
126
+ configuration.trackInteractions = rnConfiguration.configurationForTelemetry?.trackInteractions
127
+ configuration.trackResources = rnConfiguration.configurationForTelemetry?.trackNetworkRequests
128
+ configuration.trackNetworkRequests = rnConfiguration.configurationForTelemetry?.trackNetworkRequests
129
+
130
+ // trackCrossPlatformLongTasks will be deprecated for trackLongTask
131
+ configuration.trackCrossPlatformLongTasks = rnConfiguration.longTaskThresholdMs != 0
132
+ configuration.trackLongTask = rnConfiguration.longTaskThresholdMs != 0
133
+ configuration.trackNativeErrors = rnConfiguration.nativeCrashReportEnabled
134
+ configuration.trackNativeLongTasks = rnConfiguration.nativeLongTaskThresholdMs != 0
135
+ event.telemetry.configuration = configuration
136
+
137
+ return event
138
+ }
139
+ }
103
140
 
104
141
  func buildConfiguration(configuration: DdSdkConfiguration, defaultAppVersion: String = getDefaultAppVersion()) -> Datadog.Configuration {
105
142
  let ddConfigBuilder: Datadog.Configuration.Builder
@@ -131,11 +168,20 @@ class RNDdSdk: NSObject {
131
168
  default:
132
169
  _ = ddConfigBuilder.set(endpoint: .us1)
133
170
  }
171
+
172
+ _ = ddConfigBuilder.set(mobileVitalsFrequency: buildVitalsUpdateFrequency(frequency: configuration.vitalsUpdateFrequency))
134
173
 
135
174
  if var telemetrySampleRate = (configuration.telemetrySampleRate as? NSNumber)?.floatValue {
136
175
  _ = ddConfigBuilder.set(sampleTelemetry: telemetrySampleRate)
137
176
  }
138
177
 
178
+ if let threshold = configuration.nativeLongTaskThresholdMs as? TimeInterval {
179
+ if (threshold != 0) {
180
+ // `nativeLongTaskThresholdMs` attribute is in milliseconds
181
+ _ = ddConfigBuilder.trackRUMLongTasks(threshold: threshold / 1_000)
182
+ }
183
+ }
184
+
139
185
  let additionalConfig = configuration.additionalConfig
140
186
 
141
187
  if var additionalConfiguration = additionalConfig as? [String: Any] {
@@ -155,11 +201,6 @@ class RNDdSdk: NSObject {
155
201
  _ = ddConfigBuilder.set(serviceName: serviceName)
156
202
  }
157
203
 
158
- if let threshold = additionalConfig?[InternalConfigurationAttributes.longTaskThreshold] as? TimeInterval {
159
- // `_dd.long_task.threshold` attribute is in milliseconds
160
- _ = ddConfigBuilder.trackRUMLongTasks(threshold: threshold / 1_000)
161
- }
162
-
163
204
  if let firstPartyHosts = additionalConfig?[InternalConfigurationAttributes.firstPartyHosts] as? [String] {
164
205
  _ = ddConfigBuilder.trackURLSession(firstPartyHosts: Set(firstPartyHosts))
165
206
  }
@@ -230,6 +271,23 @@ class RNDdSdk: NSObject {
230
271
  return trackingConsent
231
272
  }
232
273
 
274
+ func buildVitalsUpdateFrequency(frequency: NSString?) -> Datadog.Configuration.VitalsFrequency {
275
+ let vitalsFrequency: Datadog.Configuration.VitalsFrequency
276
+ switch frequency?.lowercased {
277
+ case "never":
278
+ vitalsFrequency = .never
279
+ case "rare":
280
+ vitalsFrequency = .rare
281
+ case "average":
282
+ vitalsFrequency = .average
283
+ case "frequent":
284
+ vitalsFrequency = .frequent
285
+ default:
286
+ vitalsFrequency = .average
287
+ }
288
+ return vitalsFrequency
289
+ }
290
+
233
291
  func setVerbosityLevel(additionalConfig: NSDictionary?) {
234
292
  let verbosityLevel = (additionalConfig?[InternalConfigurationAttributes.sdkVerbosity]) as? NSString
235
293
  switch verbosityLevel?.lowercased {
@@ -245,4 +303,32 @@ class RNDdSdk: NSObject {
245
303
  Datadog.verbosityLevel = nil
246
304
  }
247
305
  }
306
+
307
+ func startJSRefreshRateMonitoring(sdkConfiguration: DdSdkConfiguration) {
308
+ if let frameTimeCallback = buildFrameTimeCallback(sdkConfiguration: sdkConfiguration) {
309
+ // Falling back to mainDispatchQueue if bridge is nil is only useful for tests
310
+ self.jsRefreshRateMonitor.startMonitoring(jsQueue: bridge ?? mainDispatchQueue, frameTimeCallback: frameTimeCallback)
311
+ }
312
+ }
313
+
314
+ func buildFrameTimeCallback(sdkConfiguration: DdSdkConfiguration)-> ((Double) -> ())? {
315
+ let jsRefreshRateMonitoringEnabled = buildVitalsUpdateFrequency(frequency: sdkConfiguration.vitalsUpdateFrequency) != .never
316
+ let jsLongTaskMonitoringEnabled = sdkConfiguration.longTaskThresholdMs != 0
317
+
318
+ if (!jsRefreshRateMonitoringEnabled && !jsLongTaskMonitoringEnabled) {
319
+ return nil
320
+ }
321
+
322
+ func frameTimeCallback(frameTime: Double) {
323
+ if (jsRefreshRateMonitoringEnabled && frameTime > 0) {
324
+ Global.rum._internal.updatePerformanceMetric(at: Date(), metric: .jsFrameTimeSeconds, value: frameTime)
325
+ }
326
+ if (jsLongTaskMonitoringEnabled && frameTime > sdkConfiguration.longTaskThresholdMs / 1_000) {
327
+ Global.rum._internal.addLongTask(at: Date(), duration: frameTime, attributes: ["long_task.target": "javascript"])
328
+ }
329
+ }
330
+
331
+ return frameTimeCallback
332
+ }
333
+
248
334
  }
@@ -13,6 +13,8 @@ import Foundation
13
13
  - env: The application’s environment, for example: prod, pre-prod, staging, etc.
14
14
  - applicationId: The RUM application ID.
15
15
  - nativeCrashReportEnabled: Whether the SDK should track native (pure iOS or pure Android) crashes (default is false).
16
+ - nativeLongTaskThresholdMs: The threshold for native long tasks reporting in milliseconds.
17
+ - longTaskThresholdMs: The threshold for javascript long tasks reporting in milliseconds.
16
18
  - sampleRate: The sample rate (between 0 and 100) of RUM sessions kept.
17
19
  - site: The Datadog site of your organization (can be 'US1', 'US1_FED', 'US3', 'US5', or 'EU1', default is 'US1').
18
20
  - trackingConsent: Consent, which can take one of the following values: 'pending', 'granted', 'not_granted'.
@@ -25,31 +27,62 @@ public class DdSdkConfiguration: NSObject {
25
27
  public var env: String = ""
26
28
  public var applicationId: String? = nil
27
29
  public var nativeCrashReportEnabled: Bool? = nil
30
+ public var nativeLongTaskThresholdMs: Double? = nil
31
+ public var longTaskThresholdMs: Double = 0.0
28
32
  public var sampleRate: Double? = nil
29
33
  public var site: NSString? = nil
30
34
  public var trackingConsent: NSString? = nil
31
35
  public var telemetrySampleRate: Double? = nil
36
+ public var vitalsUpdateFrequency: NSString? = nil
32
37
  public var additionalConfig: NSDictionary? = nil
38
+ public var configurationForTelemetry: ConfigurationForTelemetry? = nil
33
39
 
34
40
  public init(
35
41
  clientToken: String,
36
42
  env: String,
37
43
  applicationId: String?,
38
44
  nativeCrashReportEnabled: Bool?,
45
+ nativeLongTaskThresholdMs: Double?,
46
+ longTaskThresholdMs: Double,
39
47
  sampleRate: Double?,
40
48
  site: NSString?,
41
49
  trackingConsent: NSString?,
42
50
  telemetrySampleRate: Double?,
43
- additionalConfig: NSDictionary?
51
+ vitalsUpdateFrequency: NSString?,
52
+ additionalConfig: NSDictionary?,
53
+ configurationForTelemetry: ConfigurationForTelemetry?
44
54
  ) {
45
55
  self.clientToken = clientToken
46
56
  self.env = env
47
57
  self.applicationId = applicationId
48
58
  self.nativeCrashReportEnabled = nativeCrashReportEnabled
59
+ self.nativeLongTaskThresholdMs = nativeLongTaskThresholdMs
60
+ self.longTaskThresholdMs = longTaskThresholdMs
49
61
  self.sampleRate = sampleRate
50
62
  self.site = site
51
63
  self.trackingConsent = trackingConsent
52
64
  self.telemetrySampleRate = telemetrySampleRate
65
+ self.vitalsUpdateFrequency = vitalsUpdateFrequency
53
66
  self.additionalConfig = additionalConfig
67
+ self.configurationForTelemetry = configurationForTelemetry
68
+ }
69
+ }
70
+
71
+ public class ConfigurationForTelemetry: NSObject {
72
+ public var initializationType: NSString?
73
+ public var trackErrors: Bool?
74
+ public var trackInteractions: Bool?
75
+ public var trackNetworkRequests: Bool?
76
+
77
+ public init(
78
+ initializationType: NSString?,
79
+ trackErrors: Bool?,
80
+ trackInteractions: Bool?,
81
+ trackNetworkRequests: Bool?
82
+ ) {
83
+ self.initializationType = initializationType
84
+ self.trackErrors = trackErrors
85
+ self.trackInteractions = trackInteractions
86
+ self.trackNetworkRequests = trackNetworkRequests
54
87
  }
55
88
  }