@datadog/mobile-react-native 1.0.0-rc9 → 1.1.1

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 (112) hide show
  1. package/DatadogSDKReactNative.podspec +2 -1
  2. package/android/build.gradle +73 -3
  3. package/android/gradle.properties +1 -0
  4. package/android/src/main/kotlin/com/datadog/reactnative/DatadogSDKWrapper.kt +54 -0
  5. package/android/src/main/kotlin/com/datadog/reactnative/DatadogWrapper.kt +86 -0
  6. package/android/src/main/{java → kotlin}/com/datadog/reactnative/DdLogs.kt +27 -11
  7. package/android/src/main/kotlin/com/datadog/reactnative/DdRum.kt +305 -0
  8. package/android/src/main/kotlin/com/datadog/reactnative/DdSdk.kt +280 -0
  9. package/android/src/main/{java → kotlin}/com/datadog/reactnative/DdSdkBridgeExt.kt +2 -2
  10. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkConfiguration.kt +29 -0
  11. package/android/src/main/{java → kotlin}/com/datadog/reactnative/DdSdkConfigurationExt.kt +2 -4
  12. package/android/src/main/{java → kotlin}/com/datadog/reactnative/DdSdkReactNativePackage.kt +3 -0
  13. package/android/src/main/kotlin/com/datadog/reactnative/DdTrace.kt +86 -0
  14. package/android/src/main/kotlin/com/datadog/reactnative/GlobalState.kt +32 -0
  15. package/android/src/main/kotlin/com/datadog/reactnative/NoOpViewTrackingStrategy.kt +23 -0
  16. package/android/src/main/kotlin/com/datadog/reactnative/ProxyAuthenticator.kt +60 -0
  17. package/android/src/test/kotlin/com/datadog/reactnative/DdLogsTest.kt +170 -0
  18. package/android/src/test/kotlin/com/datadog/reactnative/DdRumTest.kt +408 -0
  19. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt +1547 -0
  20. package/android/src/test/kotlin/com/datadog/reactnative/DdTraceTest.kt +306 -0
  21. package/android/src/test/kotlin/com/datadog/reactnative/GlobalStateTest.kt +118 -0
  22. package/android/src/test/kotlin/com/datadog/reactnative/ProxyAuthenticatorTest.kt +123 -0
  23. package/android/src/test/kotlin/com/datadog/tools/unit/DdSdkConfigurationExt.kt +33 -0
  24. package/android/src/test/kotlin/com/datadog/tools/unit/GenericAssert.kt +43 -0
  25. package/android/src/test/kotlin/com/datadog/tools/unit/MapExt.kt +22 -0
  26. package/android/src/test/kotlin/com/datadog/tools/unit/ReflectUtils.kt +267 -0
  27. package/android/src/test/kotlin/com/datadog/tools/unit/forge/BaseConfigurator.kt +26 -0
  28. package/android/src/test/kotlin/com/datadog/tools/unit/forge/DdSdkConfigurationForgeryFactory.kt +33 -0
  29. package/android/src/test/kotlin/com/datadog/tools/unit/forge/PackageInfoForgeryFactory.kt +20 -0
  30. package/android/src/test/kotlin/com/datadog/tools/unit/forge/Throwable.kt +31 -0
  31. package/android/src/test/kotlin/com/datadog/tools/unit/forge/ThrowableForgeryFactory.kt +21 -0
  32. package/android/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker +1 -0
  33. package/datadog-sourcemaps.gradle +137 -0
  34. package/ios/Sources/AnyEncodable.swift +138 -0
  35. package/ios/Sources/Attributes.swift +101 -0
  36. package/ios/Sources/DdLogs.swift +35 -11
  37. package/ios/Sources/DdRum.m +3 -1
  38. package/ios/Sources/DdRum.swift +184 -22
  39. package/ios/Sources/DdSdk.swift +190 -10
  40. package/ios/Sources/DdSdkConfiguration.swift +51 -0
  41. package/ios/Sources/DdTrace.swift +41 -6
  42. package/ios/Sources/GlobalState.swift +28 -0
  43. package/ios/Sources/QueueHolder.swift +10 -0
  44. package/ios/Sources/RNDdSdkConfiguration.swift +5 -6
  45. package/lib/commonjs/DdSdkReactNative.js +14 -0
  46. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  47. package/lib/commonjs/DdSdkReactNativeConfiguration.js +24 -0
  48. package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
  49. package/lib/commonjs/foundation.js +36 -2
  50. package/lib/commonjs/foundation.js.map +1 -1
  51. package/lib/commonjs/index.js +14 -0
  52. package/lib/commonjs/index.js.map +1 -1
  53. package/lib/commonjs/nativeModulesTypes.js +36 -0
  54. package/lib/commonjs/nativeModulesTypes.js.map +1 -0
  55. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js +4 -17
  56. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  57. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js +4 -4
  58. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  59. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +7 -2
  60. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -1
  61. package/lib/commonjs/types.js +26 -1
  62. package/lib/commonjs/types.js.map +1 -1
  63. package/lib/commonjs/version.js +1 -1
  64. package/lib/commonjs/version.js.map +1 -1
  65. package/lib/module/DdSdkReactNative.js +14 -0
  66. package/lib/module/DdSdkReactNative.js.map +1 -1
  67. package/lib/module/DdSdkReactNativeConfiguration.js +24 -0
  68. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  69. package/lib/module/foundation.js +36 -2
  70. package/lib/module/foundation.js.map +1 -1
  71. package/lib/module/index.js +2 -1
  72. package/lib/module/index.js.map +1 -1
  73. package/lib/module/nativeModulesTypes.js +26 -0
  74. package/lib/module/nativeModulesTypes.js.map +1 -0
  75. package/lib/module/rum/instrumentation/DdEventsInterceptor.js +2 -14
  76. package/lib/module/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  77. package/lib/module/rum/instrumentation/DdRumErrorTracking.js +3 -4
  78. package/lib/module/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  79. package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +7 -2
  80. package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -1
  81. package/lib/module/types.js +23 -0
  82. package/lib/module/types.js.map +1 -1
  83. package/lib/module/version.js +1 -1
  84. package/lib/module/version.js.map +1 -1
  85. package/lib/typescript/DdSdkReactNative.d.ts +2 -0
  86. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +20 -0
  87. package/lib/typescript/foundation.d.ts +3 -2
  88. package/lib/typescript/index.d.ts +2 -1
  89. package/lib/typescript/nativeModulesTypes.d.ts +168 -0
  90. package/lib/typescript/rum/instrumentation/DdEventsInterceptor.d.ts +0 -15
  91. package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.d.ts +2 -1
  92. package/lib/typescript/types.d.ts +42 -7
  93. package/lib/typescript/version.d.ts +1 -1
  94. package/package.json +5 -4
  95. package/src/DdSdkReactNative.tsx +20 -0
  96. package/src/DdSdkReactNativeConfiguration.tsx +20 -0
  97. package/src/__tests__/DdSdkReactNative.test.tsx +76 -0
  98. package/src/__tests__/foundation.test.ts +79 -0
  99. package/src/__tests__/rum/instrumentation/DdEventsInterceptor.test.tsx +8 -8
  100. package/src/foundation.tsx +40 -11
  101. package/src/index.tsx +3 -0
  102. package/src/nativeModulesTypes.ts +258 -0
  103. package/src/rum/instrumentation/DdEventsInterceptor.tsx +2 -17
  104. package/src/rum/instrumentation/DdRumErrorTracking.tsx +3 -4
  105. package/src/rum/instrumentation/resourceTracking/domain/__tests__/firstPartyHosts.test.ts +22 -0
  106. package/src/rum/instrumentation/resourceTracking/domain/firstPartyHosts.ts +8 -1
  107. package/src/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.ts +2 -1
  108. package/src/types.tsx +62 -7
  109. package/src/version.ts +1 -1
  110. package/android/src/main/java/com/datadog/reactnative/DdRum.kt +0 -142
  111. package/android/src/main/java/com/datadog/reactnative/DdSdk.kt +0 -67
  112. package/android/src/main/java/com/datadog/reactnative/DdTrace.kt +0 -51
@@ -17,7 +17,8 @@ Pod::Spec.new do |s|
17
17
  s.source_files = "ios/Sources/*.{h,m,mm,swift}"
18
18
 
19
19
  s.dependency "React-Core"
20
- s.dependency "DatadogSDKBridge", "0.5.5"
20
+ s.dependency 'DatadogSDK', '~> 1.12.0-beta3'
21
+ s.dependency 'DatadogSDKCrashReporting', '~> 1.12.0-beta3'
21
22
 
22
23
  s.test_spec 'Tests' do |test_spec|
23
24
  test_spec.source_files = 'ios/Tests/*.swift'
@@ -5,17 +5,22 @@ buildscript {
5
5
  repositories {
6
6
  mavenCentral()
7
7
  google()
8
+ gradlePluginPortal()
8
9
  }
9
10
 
10
11
  dependencies {
11
- classpath 'com.android.tools.build:gradle:3.2.1'
12
+ classpath 'com.android.tools.build:gradle:7.2.2'
12
13
  // noinspection DifferentKotlinGradleVersion
13
14
  classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
15
+ classpath "org.jlleitschuh.gradle:ktlint-gradle:10.2.1"
16
+ classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.18.0"
14
17
  }
15
18
  }
16
19
 
17
20
  apply plugin: 'com.android.library'
18
21
  apply plugin: 'kotlin-android'
22
+ apply plugin: 'org.jlleitschuh.gradle.ktlint'
23
+ apply plugin: "io.gitlab.arturbosch.detekt"
19
24
 
20
25
  def getExtOrDefault(name) {
21
26
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['DdSdkReactNative_' + name]
@@ -35,6 +40,20 @@ android {
35
40
  versionName "1.0"
36
41
 
37
42
  }
43
+
44
+ sourceSets.named("main") {
45
+ java.srcDir("src/main/kotlin")
46
+ }
47
+
48
+ sourceSets.named("test") {
49
+ java.srcDir("src/test/kotlin")
50
+ }
51
+
52
+ testOptions {
53
+ unitTests {
54
+ returnDefaultValues = true
55
+ }
56
+ }
38
57
 
39
58
  buildTypes {
40
59
  release {
@@ -127,8 +146,59 @@ def kotlin_version = getExtOrDefault('kotlinVersion')
127
146
  dependencies {
128
147
  // noinspection GradleDynamicVersion
129
148
  api 'com.facebook.react:react-native:+'
149
+ implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
150
+ compileOnly "com.squareup.okhttp3:okhttp:3.12.13"
130
151
 
131
- implementation "com.datadoghq:dd-bridge-android:0.5.4"
152
+ implementation "com.datadoghq:dd-sdk-android:1.14.0-beta2"
153
+ testImplementation "org.junit.platform:junit-platform-launcher:1.6.2"
154
+ testImplementation "org.junit.jupiter:junit-jupiter-api:5.6.2"
155
+ testImplementation "org.junit.jupiter:junit-jupiter-engine:5.6.2"
156
+ testImplementation "org.mockito:mockito-junit-jupiter:3.4.6"
157
+ testImplementation "org.assertj:assertj-core:3.18.1"
158
+ testImplementation "com.github.xgouchet.Elmyr:core:1.3.1"
159
+ testImplementation "com.github.xgouchet.Elmyr:inject:1.3.1"
160
+ testImplementation "com.github.xgouchet.Elmyr:junit5:1.3.1"
161
+ testImplementation "com.github.xgouchet.Elmyr:jvm:1.3.1"
162
+ testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
163
+ testImplementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
164
+ }
132
165
 
133
- implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
166
+ tasks.withType(Test) {
167
+ useJUnitPlatform {
168
+ includeEngines("spek", "junit-jupiter", "junit-vintage")
169
+ }
170
+ reports {
171
+ junitXml.required.set(true)
172
+ html.required.set(true)
173
+ }
174
+ }
175
+
176
+ tasks.named("check") {
177
+ dependsOn("ktlintCheck")
178
+ dependsOn("detekt")
179
+ }
180
+
181
+ ktlint {
182
+ debug.set(false)
183
+ android.set(true)
184
+ outputToConsole.set(true)
185
+ ignoreFailures.set(false)
186
+ enableExperimentalRules.set(false)
187
+ additionalEditorconfigFile.set(file("${project.rootDir}/script/config/.editorconfig"))
188
+ filter {
189
+ exclude("**/generated/**")
190
+ include("**/kotlin/**")
191
+ }
192
+ }
193
+
194
+ detekt {
195
+ input = files("$projectDir/src/main/kotlin")
196
+ config = files("${project.rootDir}/detekt.yml")
197
+
198
+ reports {
199
+ xml {
200
+ enabled = true
201
+ destination = file("build/reports/detekt.xml")
202
+ }
203
+ }
134
204
  }
@@ -2,3 +2,4 @@ DdSdkReactNative_kotlinVersion=1.5.31
2
2
  DdSdkReactNative_compileSdkVersion=31
3
3
  DdSdkReactNative_buildToolsVersion=31.0.0
4
4
  DdSdkReactNative_targetSdkVersion=31
5
+ android.useAndroidX=true
@@ -0,0 +1,54 @@
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.reactnative
8
+
9
+ import android.content.Context
10
+ import com.datadog.android.Datadog
11
+ import com.datadog.android.core.configuration.Configuration
12
+ import com.datadog.android.core.configuration.Credentials
13
+ import com.datadog.android.privacy.TrackingConsent
14
+ import com.datadog.android.rum.GlobalRum
15
+ import com.datadog.android.rum.RumMonitor
16
+
17
+ internal class DatadogSDKWrapper : DatadogWrapper {
18
+
19
+ override fun setVerbosity(level: Int) {
20
+ Datadog.setVerbosity(level)
21
+ }
22
+
23
+ override fun initialize(
24
+ context: Context,
25
+ credentials: Credentials,
26
+ configuration: Configuration,
27
+ consent: TrackingConsent
28
+ ) {
29
+ Datadog.initialize(context, credentials, configuration, consent)
30
+ }
31
+
32
+ override fun setUserInfo(
33
+ id: String?,
34
+ name: String?,
35
+ email: String?,
36
+ extraInfo: Map<String, Any?>
37
+ ) {
38
+ Datadog.setUserInfo(id, name, email, extraInfo)
39
+ }
40
+
41
+ override fun registerRumMonitor(rumMonitor: RumMonitor) {
42
+ GlobalRum.registerIfAbsent(rumMonitor)
43
+ }
44
+
45
+ override fun addRumGlobalAttributes(attributes: Map<String, Any?>) {
46
+ attributes.forEach {
47
+ GlobalRum.addAttribute(it.key, it.value)
48
+ }
49
+ }
50
+
51
+ override fun setTrackingConsent(trackingConsent: TrackingConsent) {
52
+ Datadog.setTrackingConsent(trackingConsent)
53
+ }
54
+ }
@@ -0,0 +1,86 @@
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.reactnative
8
+
9
+ import android.content.Context
10
+ import com.datadog.android.core.configuration.Configuration
11
+ import com.datadog.android.core.configuration.Credentials
12
+ import com.datadog.android.privacy.TrackingConsent
13
+ import com.datadog.android.rum.RumMonitor
14
+ import java.lang.IllegalArgumentException
15
+
16
+ /**
17
+ * Wrapper around [Datadog].
18
+ */
19
+ interface DatadogWrapper {
20
+
21
+ /**
22
+ * Sets the verbosity of the Datadog library.
23
+ *
24
+ * Messages with a priority level equal or above the given level will be sent to Android's
25
+ * Logcat.
26
+ *
27
+ * @param level one of the Android [android.util.Log] constants
28
+ * ([android.util.Log.VERBOSE], [android.util.Log.DEBUG], [android.util.Log.INFO],
29
+ * [android.util.Log.WARN], [android.util.Log.ERROR], [android.util.Log.ASSERT]).
30
+ */
31
+ fun setVerbosity(level: Int)
32
+
33
+ /**
34
+ * Initializes the Datadog SDK.
35
+ * @param context your application context
36
+ * @param credentials your organization credentials
37
+ * @param configuration the configuration for the SDK library
38
+ * @param trackingConsent as the initial state of the tracking consent flag.
39
+ * @see [Credentials]
40
+ * @see [Configuration]
41
+ * @see [TrackingConsent]
42
+ * @throws IllegalArgumentException if the env name is using illegal characters and your
43
+ * application is in debug mode otherwise returns false and stops initializing the SDK
44
+ */
45
+ fun initialize(
46
+ context: Context,
47
+ credentials: Credentials,
48
+ configuration: Configuration,
49
+ consent: TrackingConsent
50
+ )
51
+
52
+ /**
53
+ * Sets the user information.
54
+ *
55
+ * @param id (nullable) a unique user identifier (relevant to your business domain)
56
+ * @param name (nullable) the user name or alias
57
+ * @param email (nullable) the user email
58
+ * @param extraInfo additional information. An extra information can be
59
+ * nested up to 8 levels deep. Keys using more than 8 levels will be sanitized by SDK.
60
+ */
61
+ fun setUserInfo(
62
+ id: String?,
63
+ name: String?,
64
+ email: String?,
65
+ extraInfo: Map<String, Any?>
66
+ )
67
+
68
+ /**
69
+ * Registers a given monitor in [GlobalRum].
70
+ *
71
+ * @param rumMonitor to register
72
+ */
73
+ fun registerRumMonitor(rumMonitor: RumMonitor)
74
+
75
+ /**
76
+ * Adds global attributes.
77
+ *
78
+ * @param attributes Attributes to add
79
+ */
80
+ fun addRumGlobalAttributes(attributes: Map<String, Any?>)
81
+
82
+ /**
83
+ * Sets tracking consent.
84
+ */
85
+ fun setTrackingConsent(trackingConsent: TrackingConsent)
86
+ }
@@ -6,24 +6,29 @@
6
6
 
7
7
  package com.datadog.reactnative
8
8
 
9
- import com.datadog.android.bridge.DdBridge
10
- import com.datadog.android.bridge.DdLogs as SDKDdLogs
9
+ import com.datadog.android.log.Logger
11
10
  import com.facebook.react.bridge.Promise
12
11
  import com.facebook.react.bridge.ReactApplicationContext
13
12
  import com.facebook.react.bridge.ReactContextBaseJavaModule
14
13
  import com.facebook.react.bridge.ReactMethod
15
- import com.facebook.react.bridge.ReadableArray
16
14
  import com.facebook.react.bridge.ReadableMap
17
15
 
18
16
  /**
19
17
  * The entry point to use Datadog's Logs feature.
20
18
  */
21
- class DdLogs(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
22
-
23
- private val nativeInstance: SDKDdLogs = DdBridge.getDdLogs(reactContext)
19
+ class DdLogs(reactContext: ReactApplicationContext, logger: Logger? = null) :
20
+ ReactContextBaseJavaModule(reactContext) {
24
21
 
25
22
  override fun getName(): String = "DdLogs"
26
23
 
24
+ private val reactNativeLogger: Logger by lazy {
25
+ logger ?: Logger.Builder()
26
+ .setDatadogLogsEnabled(true)
27
+ .setLogcatLogsEnabled(true)
28
+ .setLoggerName("DdLogs")
29
+ .build()
30
+ }
31
+
27
32
  /**
28
33
  * Send a log with level debug.
29
34
  * @param message The message to send.
@@ -31,7 +36,10 @@ class DdLogs(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule
31
36
  */
32
37
  @ReactMethod
33
38
  fun debug(message: String, context: ReadableMap, promise: Promise) {
34
- nativeInstance.debug(message, context.toHashMap())
39
+ reactNativeLogger.d(
40
+ message = message,
41
+ attributes = context.toHashMap() + GlobalState.globalAttributes
42
+ )
35
43
  promise.resolve(null)
36
44
  }
37
45
 
@@ -42,7 +50,10 @@ class DdLogs(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule
42
50
  */
43
51
  @ReactMethod
44
52
  fun info(message: String, context: ReadableMap, promise: Promise) {
45
- nativeInstance.info(message, context.toHashMap())
53
+ reactNativeLogger.i(
54
+ message = message,
55
+ attributes = context.toHashMap() + GlobalState.globalAttributes
56
+ )
46
57
  promise.resolve(null)
47
58
  }
48
59
 
@@ -53,7 +64,10 @@ class DdLogs(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule
53
64
  */
54
65
  @ReactMethod
55
66
  fun warn(message: String, context: ReadableMap, promise: Promise) {
56
- nativeInstance.warn(message, context.toHashMap())
67
+ reactNativeLogger.w(
68
+ message = message,
69
+ attributes = context.toHashMap() + GlobalState.globalAttributes
70
+ )
57
71
  promise.resolve(null)
58
72
  }
59
73
 
@@ -64,8 +78,10 @@ class DdLogs(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule
64
78
  */
65
79
  @ReactMethod
66
80
  fun error(message: String, context: ReadableMap, promise: Promise) {
67
- nativeInstance.error(message, context.toHashMap())
81
+ reactNativeLogger.e(
82
+ message = message,
83
+ attributes = context.toHashMap() + GlobalState.globalAttributes
84
+ )
68
85
  promise.resolve(null)
69
86
  }
70
-
71
87
  }
@@ -0,0 +1,305 @@
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.reactnative
8
+
9
+ import com.datadog.android.rum.GlobalRum
10
+ import com.datadog.android.rum.RumActionType
11
+ import com.datadog.android.rum.RumAttributes
12
+ import com.datadog.android.rum.RumErrorSource
13
+ import com.datadog.android.rum.RumResourceKind
14
+ import com.facebook.react.bridge.Promise
15
+ import com.facebook.react.bridge.ReactApplicationContext
16
+ import com.facebook.react.bridge.ReactContextBaseJavaModule
17
+ import com.facebook.react.bridge.ReactMethod
18
+ import com.facebook.react.bridge.ReadableMap
19
+ import java.util.Locale
20
+
21
+ /**
22
+ * The entry point to use Datadog's RUM feature.
23
+ */
24
+ class DdRum(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
25
+
26
+ override fun getName(): String = "DdRum"
27
+
28
+ /**
29
+ * Start tracking a RUM View.
30
+ * @param key The view unique key identifier.
31
+ * @param name The view name.
32
+ * @param context The additional context to send.
33
+ * @param timestampMs The timestamp when the view started (in milliseconds). If not provided, current timestamp will be used.
34
+ */
35
+ @ReactMethod
36
+ fun startView(
37
+ key: String,
38
+ name: String,
39
+ context: ReadableMap,
40
+ timestampMs: Double,
41
+ promise: Promise
42
+ ) {
43
+ val attributes = context.toHashMap().toMutableMap().apply {
44
+ put(RumAttributes.INTERNAL_TIMESTAMP, timestampMs.toLong())
45
+ }
46
+ GlobalRum.get().startView(
47
+ key = key,
48
+ name = name,
49
+ attributes = attributes
50
+ )
51
+ promise.resolve(null)
52
+ }
53
+
54
+ /**
55
+ * Stop tracking a RUM View.
56
+ * @param key The view unique key identifier.
57
+ * @param context The additional context to send.
58
+ * @param timestampMs The timestamp when the view stopped (in milliseconds). If not provided, current timestamp will be used.
59
+ */
60
+ @ReactMethod
61
+ fun stopView(key: String, context: ReadableMap, timestampMs: Double, promise: Promise) {
62
+ val attributes = context.toHashMap().toMutableMap().apply {
63
+ put(RumAttributes.INTERNAL_TIMESTAMP, timestampMs.toLong())
64
+ }
65
+ GlobalRum.get().stopView(
66
+ key = key,
67
+ attributes = attributes
68
+ )
69
+ promise.resolve(null)
70
+ }
71
+
72
+ /**
73
+ * Start tracking a RUM Action.
74
+ * @param type The action type (tap, scroll, swipe, click, custom).
75
+ * @param name The action name.
76
+ * @param context The additional context to send.
77
+ * @param timestampMs The timestamp when the action started (in milliseconds). If not provided, current timestamp will be used.
78
+ */
79
+ @ReactMethod
80
+ fun startAction(
81
+ type: String,
82
+ name: String,
83
+ context: ReadableMap,
84
+ timestampMs: Double,
85
+ promise: Promise
86
+ ) {
87
+ val attributes = context.toHashMap().toMutableMap().apply {
88
+ put(RumAttributes.INTERNAL_TIMESTAMP, timestampMs.toLong())
89
+ }
90
+ GlobalRum.get().startUserAction(
91
+ type = type.asRumActionType(),
92
+ name = name,
93
+ attributes = attributes
94
+ )
95
+ promise.resolve(null)
96
+ }
97
+
98
+ /**
99
+ * Stop tracking the ongoing RUM Action.
100
+ * @param type The action type (tap, scroll, swipe, click, custom).
101
+ * @param name The action name.
102
+ * @param context The additional context to send.
103
+ * @param timestampMs The timestamp when the action stopped (in milliseconds). If not provided, current timestamp will be used.
104
+ */
105
+ @ReactMethod
106
+ fun stopAction(
107
+ type: String,
108
+ name: String,
109
+ context: ReadableMap,
110
+ timestampMs: Double,
111
+ promise: Promise
112
+ ) {
113
+ val attributes = context.toHashMap().toMutableMap().apply {
114
+ put(RumAttributes.INTERNAL_TIMESTAMP, timestampMs.toLong())
115
+ }
116
+ GlobalRum.get().stopUserAction(
117
+ type = type.asRumActionType(),
118
+ name = name,
119
+ attributes = attributes
120
+ )
121
+ promise.resolve(null)
122
+ }
123
+
124
+ /**
125
+ * Add a RUM Action.
126
+ * @param type The action type (tap, scroll, swipe, click, custom).
127
+ * @param name The action name.
128
+ * @param context The additional context to send.
129
+ * @param timestampMs The timestamp when the action occurred (in milliseconds). If not provided, current timestamp will be used.
130
+ */
131
+ @ReactMethod
132
+ fun addAction(
133
+ type: String,
134
+ name: String,
135
+ context: ReadableMap,
136
+ timestampMs: Double,
137
+ promise: Promise
138
+ ) {
139
+ val attributes = context.toHashMap().toMutableMap().apply {
140
+ put(RumAttributes.INTERNAL_TIMESTAMP, timestampMs.toLong())
141
+ }
142
+ GlobalRum.get().addUserAction(
143
+ type = type.asRumActionType(),
144
+ name = name,
145
+ attributes = attributes
146
+ )
147
+ promise.resolve(null)
148
+ }
149
+
150
+ /**
151
+ * Start tracking a RUM Resource.
152
+ * @param key The resource unique key identifier.
153
+ * @param method The resource method (GET, POST, …).
154
+ * @param url The resource url.
155
+ * @param context The additional context to send.
156
+ * @param timestampMs The timestamp when the resource started (in milliseconds). If not provided, current timestamp will be used.
157
+ */
158
+ @Suppress("LongParameterList")
159
+ @ReactMethod
160
+ fun startResource(
161
+ key: String,
162
+ method: String,
163
+ url: String,
164
+ context: ReadableMap,
165
+ timestampMs: Double,
166
+ promise: Promise
167
+ ) {
168
+ val attributes = context.toHashMap().toMutableMap().apply {
169
+ put(RumAttributes.INTERNAL_TIMESTAMP, timestampMs.toLong())
170
+ }
171
+ GlobalRum.get().startResource(
172
+ key = key,
173
+ method = method,
174
+ url = url,
175
+ attributes = attributes
176
+ )
177
+ promise.resolve(null)
178
+ }
179
+
180
+ /**
181
+ * Stop tracking a RUM Resource.
182
+ * @param key The resource unique key identifier.
183
+ * @param statusCode The resource status code.
184
+ * @param kind The resource's kind (xhr, document, image, css, font, …).
185
+ * @param size The resource size in bytes.
186
+ * @param context The additional context to send.
187
+ * @param timestampMs The timestamp when the resource stopped (in milliseconds). If not provided, current timestamp will be used.
188
+ */
189
+ @Suppress("LongParameterList")
190
+ @ReactMethod
191
+ fun stopResource(
192
+ key: String,
193
+ statusCode: Double,
194
+ kind: String,
195
+ size: Double,
196
+ context: ReadableMap,
197
+ timestampMs: Double,
198
+ promise: Promise
199
+ ) {
200
+ val attributes = context.toHashMap().toMutableMap().apply {
201
+ put(RumAttributes.INTERNAL_TIMESTAMP, timestampMs.toLong())
202
+ }
203
+ val resourceSize = if (size.toLong() == MISSING_RESOURCE_SIZE) {
204
+ null
205
+ } else {
206
+ size.toLong()
207
+ }
208
+ GlobalRum.get().stopResource(
209
+ key = key,
210
+ statusCode = statusCode.toInt(),
211
+ kind = kind.asRumResourceKind(),
212
+ size = resourceSize,
213
+ attributes = attributes
214
+ )
215
+ promise.resolve(null)
216
+ }
217
+
218
+ /**
219
+ * Add a RUM Error.
220
+ * @param message The error message.
221
+ * @param source The error source (network, source, console, logger, …).
222
+ * @param stacktrace The error stacktrace.
223
+ * @param context The additional context to send.
224
+ * @param timestampMs The timestamp when the error occurred (in milliseconds). If not provided, current timestamp will be used.
225
+ */
226
+ @Suppress("LongParameterList")
227
+ @ReactMethod
228
+ fun addError(
229
+ message: String,
230
+ source: String,
231
+ stacktrace: String,
232
+ context: ReadableMap,
233
+ timestampMs: Double,
234
+ promise: Promise
235
+ ) {
236
+ val attributes = context.toHashMap().toMutableMap().apply {
237
+ put(RumAttributes.INTERNAL_TIMESTAMP, timestampMs.toLong())
238
+ }
239
+ GlobalRum.get().addErrorWithStacktrace(
240
+ message = message,
241
+ source = source.asErrorSource(),
242
+ stacktrace = stacktrace,
243
+ attributes = attributes
244
+ )
245
+ promise.resolve(null)
246
+ }
247
+
248
+ /**
249
+ * Adds a specific timing in the active View. The timing duration will be computed as the difference between the time the View was started and the time this function was called.
250
+ * @param name The name of the new custom timing attribute. Timings can be nested up to 8 levels deep. Names using more than 8 levels will be sanitized by SDK.
251
+ */
252
+ @ReactMethod
253
+ fun addTiming(name: String, promise: Promise) {
254
+ GlobalRum.get().addTiming(name)
255
+ promise.resolve(null)
256
+ }
257
+
258
+ // region Internal
259
+
260
+ private fun String.asRumActionType(): RumActionType {
261
+ return when (lowercase(Locale.US)) {
262
+ "tap" -> RumActionType.TAP
263
+ "scroll" -> RumActionType.SCROLL
264
+ "swipe" -> RumActionType.SWIPE
265
+ "click" -> RumActionType.CLICK
266
+ "back" -> RumActionType.BACK
267
+ else -> RumActionType.CUSTOM
268
+ }
269
+ }
270
+
271
+ private fun String.asRumResourceKind(): RumResourceKind {
272
+ return when (lowercase(Locale.US)) {
273
+ "xhr" -> RumResourceKind.XHR
274
+ "native" -> RumResourceKind.NATIVE
275
+ "fetch" -> RumResourceKind.FETCH
276
+ "document" -> RumResourceKind.DOCUMENT
277
+ "beacon" -> RumResourceKind.BEACON
278
+ "js" -> RumResourceKind.JS
279
+ "image" -> RumResourceKind.IMAGE
280
+ "font" -> RumResourceKind.FONT
281
+ "css" -> RumResourceKind.CSS
282
+ "media" -> RumResourceKind.MEDIA
283
+ "other" -> RumResourceKind.OTHER
284
+ else -> RumResourceKind.UNKNOWN
285
+ }
286
+ }
287
+
288
+ private fun String.asErrorSource(): RumErrorSource {
289
+ return when (lowercase(Locale.US)) {
290
+ "agent" -> RumErrorSource.AGENT
291
+ "console" -> RumErrorSource.CONSOLE
292
+ "logger" -> RumErrorSource.LOGGER
293
+ "network" -> RumErrorSource.NETWORK
294
+ "source" -> RumErrorSource.SOURCE
295
+ "webview" -> RumErrorSource.WEBVIEW
296
+ else -> RumErrorSource.SOURCE
297
+ }
298
+ }
299
+
300
+ // endregion
301
+
302
+ companion object {
303
+ private const val MISSING_RESOURCE_SIZE = -1L
304
+ }
305
+ }