@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
@@ -0,0 +1,280 @@
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 android.content.pm.PackageManager
11
+ import android.util.Log
12
+ import com.datadog.android.DatadogSite
13
+ import com.datadog.android.core.configuration.Configuration
14
+ import com.datadog.android.core.configuration.Credentials
15
+ import com.datadog.android.privacy.TrackingConsent
16
+ import com.datadog.android.rum.RumMonitor
17
+ import com.datadog.android.rum.tracking.ActivityViewTrackingStrategy
18
+ import com.facebook.react.bridge.Promise
19
+ import com.facebook.react.bridge.ReactApplicationContext
20
+ import com.facebook.react.bridge.ReactContextBaseJavaModule
21
+ import com.facebook.react.bridge.ReactMethod
22
+ import com.facebook.react.bridge.ReadableMap
23
+ import java.net.InetSocketAddress
24
+ import java.net.Proxy
25
+ import java.util.Locale
26
+
27
+ /**
28
+ * The entry point to initialize Datadog's features.
29
+ */
30
+ class DdSdk(
31
+ reactContext: ReactApplicationContext,
32
+ private val datadog: DatadogWrapper = DatadogSDKWrapper()
33
+ ) : ReactContextBaseJavaModule(reactContext) {
34
+
35
+ internal val appContext: Context = reactContext.applicationContext
36
+
37
+ override fun getName(): String = "DdSdk"
38
+
39
+ // region DdSdk
40
+
41
+ /**
42
+ * Initializes Datadog's features.
43
+ * @param configuration The configuration to use.
44
+ */
45
+ @ReactMethod
46
+ fun initialize(configuration: ReadableMap, promise: Promise) {
47
+ val ddSdkConfiguration = configuration.asDdSdkConfiguration()
48
+ val credentials = buildCredentials(ddSdkConfiguration)
49
+ val nativeConfiguration = buildConfiguration(ddSdkConfiguration)
50
+ val trackingConsent = buildTrackingConsent(ddSdkConfiguration.trackingConsent)
51
+
52
+ configureSdkVerbosity(ddSdkConfiguration)
53
+
54
+ datadog.initialize(appContext, credentials, nativeConfiguration, trackingConsent)
55
+
56
+ datadog.registerRumMonitor(RumMonitor.Builder().build())
57
+
58
+ promise.resolve(null)
59
+ }
60
+
61
+ /**
62
+ * Sets the global context (set of attributes) attached with all future Logs, Spans and RUM events.
63
+ * @param attributes The global context attributes.
64
+ */
65
+ @ReactMethod
66
+ fun setAttributes(attributes: ReadableMap, promise: Promise) {
67
+ datadog.addRumGlobalAttributes(attributes.toHashMap())
68
+ attributes.toHashMap().forEach { (k, v) ->
69
+ GlobalState.addAttribute(k, v)
70
+ }
71
+ promise.resolve(null)
72
+ }
73
+
74
+ /**
75
+ * Set the user information.
76
+ * @param user The user object (use builtin attributes: 'id', 'email', 'name', and/or any custom attribute).
77
+ */
78
+ @ReactMethod
79
+ fun setUser(user: ReadableMap, promise: Promise) {
80
+ val extraInfo = user.toHashMap().toMutableMap()
81
+ val id = extraInfo.remove("id")?.toString()
82
+ val name = extraInfo.remove("name")?.toString()
83
+ val email = extraInfo.remove("email")?.toString()
84
+ datadog.setUserInfo(id, name, email, extraInfo)
85
+ promise.resolve(null)
86
+ }
87
+
88
+ /**
89
+ * Set the tracking consent regarding the data collection.
90
+ * @param trackingConsent Consent, which can take one of the following values: 'pending', 'granted', 'not_granted'.
91
+ */
92
+ @ReactMethod
93
+ fun setTrackingConsent(trackingConsent: String, promise: Promise) {
94
+ datadog.setTrackingConsent(buildTrackingConsent(trackingConsent))
95
+ promise.resolve(null)
96
+ }
97
+
98
+ // endregion
99
+
100
+ // region Internal
101
+
102
+ private fun configureSdkVerbosity(configuration: DdSdkConfiguration) {
103
+ val verbosityConfig = configuration.additionalConfig?.get(DD_SDK_VERBOSITY) as? String
104
+ val verbosity = when (verbosityConfig?.lowercase(Locale.US)) {
105
+ "debug" -> Log.DEBUG
106
+ "info" -> Log.INFO
107
+ "warn" -> Log.WARN
108
+ "error" -> Log.ERROR
109
+ else -> null
110
+ }
111
+ if (verbosity != null) {
112
+ datadog.setVerbosity(verbosity)
113
+ }
114
+ }
115
+
116
+ private fun getDefaultAppVersion(): String {
117
+ val packageName = appContext.packageName
118
+ val packageInfo = try {
119
+ appContext.packageManager.getPackageInfo(packageName, 0)
120
+ } catch (e: PackageManager.NameNotFoundException) {
121
+ return DEFAULT_APP_VERSION
122
+ }
123
+
124
+ return packageInfo?.let {
125
+ // we need to use the deprecated method because getLongVersionCode method is only
126
+ // available from API 28 and above
127
+ @Suppress("DEPRECATION")
128
+ it.versionName ?: it.versionCode.toString()
129
+ } ?: DEFAULT_APP_VERSION
130
+ }
131
+
132
+ @Suppress("ComplexMethod", "UnsafeCallOnNullableType")
133
+ private fun buildConfiguration(configuration: DdSdkConfiguration): Configuration {
134
+ val additionalConfig = configuration.additionalConfig?.toMutableMap();
135
+
136
+ val versionSuffix = configuration.additionalConfig?.get(DD_VERSION_SUFFIX) as? String
137
+ if (versionSuffix != null && additionalConfig != null) {
138
+ val defaultVersion = getDefaultAppVersion()
139
+ additionalConfig.put(DD_VERSION, defaultVersion + versionSuffix)
140
+ }
141
+
142
+ val configBuilder = Configuration.Builder(
143
+ logsEnabled = true,
144
+ tracesEnabled = true,
145
+ crashReportsEnabled = configuration.nativeCrashReportEnabled ?: false,
146
+ rumEnabled = true
147
+ )
148
+ .setAdditionalConfiguration(
149
+ additionalConfig
150
+ ?.filterValues { it != null }
151
+ ?.mapValues { it.value!! } ?: emptyMap()
152
+ )
153
+ if (configuration.sampleRate != null) {
154
+ configBuilder.sampleRumSessions(configuration.sampleRate.toFloat())
155
+ }
156
+
157
+ configBuilder.useSite(buildSite(configuration.site))
158
+
159
+ val viewTracking = configuration.additionalConfig?.get(DD_NATIVE_VIEW_TRACKING) as? Boolean
160
+ if (viewTracking == true) {
161
+ // Use sensible default
162
+ configBuilder.useViewTrackingStrategy(ActivityViewTrackingStrategy(false))
163
+ } else {
164
+ configBuilder.useViewTrackingStrategy(NoOpViewTrackingStrategy)
165
+ }
166
+
167
+ val longTask =
168
+ (configuration.additionalConfig?.get(DD_LONG_TASK_THRESHOLD) as? Number)?.toLong()
169
+ if (longTask != null) {
170
+ configBuilder.trackLongTasks(longTask)
171
+ }
172
+
173
+ val firstPartyHosts =
174
+ (configuration.additionalConfig?.get(DD_FIRST_PARTY_HOSTS) as? List<String>)
175
+ if (firstPartyHosts != null) {
176
+ configBuilder.setFirstPartyHosts(firstPartyHosts)
177
+ }
178
+
179
+ buildProxyConfiguration(configuration)?.let { (proxy, authenticator) ->
180
+ configBuilder.setProxy(proxy, authenticator)
181
+ }
182
+
183
+ return configBuilder.build()
184
+ }
185
+
186
+ private fun buildCredentials(configuration: DdSdkConfiguration): Credentials {
187
+ val serviceName = configuration.additionalConfig?.get(DD_SERVICE_NAME) as? String
188
+ return Credentials(
189
+ clientToken = configuration.clientToken,
190
+ envName = configuration.env,
191
+ rumApplicationId = configuration.applicationId,
192
+ variant = "",
193
+ serviceName = serviceName
194
+ )
195
+ }
196
+
197
+ internal fun buildTrackingConsent(trackingConsent: String?): TrackingConsent {
198
+ return when (trackingConsent?.lowercase(Locale.US)) {
199
+ "pending" -> TrackingConsent.PENDING
200
+ "granted" -> TrackingConsent.GRANTED
201
+ "not_granted" -> TrackingConsent.NOT_GRANTED
202
+ else -> {
203
+ Log.w(
204
+ DdSdk::class.java.canonicalName,
205
+ "Unknown consent given: $trackingConsent, " +
206
+ "using ${TrackingConsent.PENDING} as default"
207
+ )
208
+ TrackingConsent.PENDING
209
+ }
210
+ }
211
+ }
212
+
213
+ internal fun buildProxyConfiguration(configuration: DdSdkConfiguration):
214
+ Pair<Proxy, ProxyAuthenticator?>? {
215
+ val additionalConfig = configuration.additionalConfig ?: return null
216
+
217
+ val address = additionalConfig[DD_PROXY_ADDRESS] as? String
218
+ val port = (additionalConfig[DD_PROXY_PORT] as? Number)?.toInt()
219
+ val type = (additionalConfig[DD_PROXY_TYPE] as? String)?.let {
220
+ when (it.lowercase(Locale.US)) {
221
+ "http", "https" -> Proxy.Type.HTTP
222
+ "socks" -> Proxy.Type.SOCKS
223
+ else -> {
224
+ Log.w(
225
+ DdSdk::class.java.canonicalName,
226
+ "Unknown proxy type given: $it, skipping proxy configuration."
227
+ )
228
+ null
229
+ }
230
+ }
231
+ }
232
+
233
+ val proxy = if (address != null && port != null && type != null) {
234
+ Proxy(type, InetSocketAddress(address, port))
235
+ } else {
236
+ return null
237
+ }
238
+
239
+ val username = additionalConfig[DD_PROXY_USERNAME] as? String
240
+ val password = additionalConfig[DD_PROXY_PASSWORD] as? String
241
+
242
+ val authenticator = if (username != null && password != null) {
243
+ ProxyAuthenticator(username, password)
244
+ } else {
245
+ null
246
+ }
247
+
248
+ return Pair(proxy, authenticator)
249
+ }
250
+
251
+ private fun buildSite(site: String?): DatadogSite {
252
+ val siteLower = site?.lowercase(Locale.US)
253
+ return when (siteLower) {
254
+ "us1", "us" -> DatadogSite.US1
255
+ "eu1", "eu" -> DatadogSite.EU1
256
+ "us3" -> DatadogSite.US3
257
+ "us5" -> DatadogSite.US5
258
+ "us1_fed", "gov" -> DatadogSite.US1_FED
259
+ else -> DatadogSite.US1
260
+ }
261
+ }
262
+
263
+ // endregion
264
+
265
+ companion object {
266
+ internal const val DEFAULT_APP_VERSION = "?"
267
+ internal const val DD_NATIVE_VIEW_TRACKING = "_dd.native_view_tracking"
268
+ internal const val DD_SDK_VERBOSITY = "_dd.sdk_verbosity"
269
+ internal const val DD_SERVICE_NAME = "_dd.service_name"
270
+ internal const val DD_LONG_TASK_THRESHOLD = "_dd.long_task.threshold"
271
+ internal const val DD_FIRST_PARTY_HOSTS = "_dd.first_party_hosts"
272
+ internal const val DD_VERSION = "_dd.version"
273
+ internal const val DD_VERSION_SUFFIX = "_dd.version_suffix"
274
+ internal const val DD_PROXY_ADDRESS = "_dd.proxy.address"
275
+ internal const val DD_PROXY_PORT = "_dd.proxy.port"
276
+ internal const val DD_PROXY_TYPE = "_dd.proxy.type"
277
+ internal const val DD_PROXY_USERNAME = "_dd.proxy.username"
278
+ internal const val DD_PROXY_PASSWORD = "_dd.proxy.password"
279
+ }
280
+ }
@@ -9,7 +9,7 @@ package com.datadog.reactnative
9
9
  import com.facebook.react.bridge.WritableNativeArray
10
10
  import com.facebook.react.bridge.WritableNativeMap
11
11
 
12
- fun List<*>.toWritableArray(): WritableNativeArray {
12
+ internal fun List<*>.toWritableArray(): WritableNativeArray {
13
13
  val list = WritableNativeArray()
14
14
  forEach {
15
15
  when (it) {
@@ -27,7 +27,7 @@ fun List<*>.toWritableArray(): WritableNativeArray {
27
27
  return list
28
28
  }
29
29
 
30
- fun Map<*, *>.toWritableMap(): WritableNativeMap {
30
+ internal fun Map<*, *>.toWritableMap(): WritableNativeMap {
31
31
  val map = WritableNativeMap()
32
32
  forEach { (k, v) ->
33
33
  val key = (k as? String) ?: k.toString()
@@ -0,0 +1,29 @@
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
+ /**
10
+ * A configuration object to initialize Datadog's features.
11
+ * @param clientToken A valid Datadog client token.
12
+ * @param env The application’s environment, for example: prod, pre-prod, staging, etc.
13
+ * @param applicationId The RUM application ID.
14
+ * @param nativeCrashReportEnabled Whether the SDK should track native (pure iOS or pure Android) crashes (default is false).
15
+ * @param sampleRate The sample rate (between 0 and 100) of RUM sessions kept.
16
+ * @param site The Datadog site of your organization (can be 'US', 'EU' or 'GOV', default is 'US').
17
+ * @param trackingConsent Consent, which can take one of the following values: 'pending', 'granted', 'not_granted'.
18
+ * @param additionalConfig Additional configuration parameters.
19
+ */
20
+ data class DdSdkConfiguration(
21
+ val clientToken: String,
22
+ val env: String,
23
+ val applicationId: String? = null,
24
+ val nativeCrashReportEnabled: Boolean? = null,
25
+ val sampleRate: Double? = null,
26
+ val site: String? = null,
27
+ val trackingConsent: String? = null,
28
+ val additionalConfig: Map<String, Any?>? = null
29
+ )
@@ -6,12 +6,10 @@
6
6
 
7
7
  package com.datadog.reactnative
8
8
 
9
- import com.datadog.android.bridge.DdSdkConfiguration
10
- import com.facebook.react.bridge.ReadableArray
11
9
  import com.facebook.react.bridge.ReadableMap
12
10
  import com.facebook.react.bridge.WritableNativeMap
13
11
 
14
- fun ReadableMap.asDdSdkConfiguration(): DdSdkConfiguration {
12
+ internal fun ReadableMap.asDdSdkConfiguration(): DdSdkConfiguration {
15
13
  return DdSdkConfiguration(
16
14
  clientToken = getString("clientToken").orEmpty(),
17
15
  env = getString("env").orEmpty(),
@@ -24,7 +22,7 @@ fun ReadableMap.asDdSdkConfiguration(): DdSdkConfiguration {
24
22
  )
25
23
  }
26
24
 
27
- fun DdSdkConfiguration.toReadableMap(): ReadableMap {
25
+ internal fun DdSdkConfiguration.toReadableMap(): ReadableMap {
28
26
  val map = WritableNativeMap()
29
27
  map.putString("clientToken", clientToken)
30
28
  map.putString("env", env)
@@ -11,6 +11,9 @@ import com.facebook.react.bridge.NativeModule
11
11
  import com.facebook.react.bridge.ReactApplicationContext
12
12
  import com.facebook.react.uimanager.ViewManager
13
13
 
14
+ /**
15
+ * Package of native dd-sdk-reactnative native modules.
16
+ */
14
17
  class DdSdkReactNativePackage : ReactPackage {
15
18
 
16
19
  override fun createViewManagers(
@@ -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 com.datadog.android.tracing.AndroidTracer
10
+ import com.facebook.react.bridge.Promise
11
+ import com.facebook.react.bridge.ReactApplicationContext
12
+ import com.facebook.react.bridge.ReactContextBaseJavaModule
13
+ import com.facebook.react.bridge.ReactMethod
14
+ import com.facebook.react.bridge.ReadableMap
15
+ import io.opentracing.Span
16
+ import io.opentracing.Tracer
17
+ import java.util.concurrent.TimeUnit
18
+
19
+ /**
20
+ * The entry point to use Datadog's Trace feature.
21
+ */
22
+ class DdTrace(
23
+ reactContext: ReactApplicationContext,
24
+ private val tracerProvider: () -> Tracer = { AndroidTracer.Builder().build() }
25
+ ) : ReactContextBaseJavaModule(reactContext) {
26
+
27
+ private val spanMap: MutableMap<String, Span> = mutableMapOf()
28
+
29
+ // lazy here is on purpose. The thing is that this class will be instantiated even
30
+ // before Sdk.initialize is called, but Tracer can be created only after SDK is initialized.
31
+ private val tracer by lazy { tracerProvider.invoke() }
32
+
33
+ override fun getName(): String = "DdTrace"
34
+
35
+ /**
36
+ * Start a span, and returns a unique identifier for the span.
37
+ * @param operation The operation name of the span.
38
+ * @param context The additional context to send.
39
+ * @param timestampMs The timestamp when the operation started (in milliseconds). If not provided, current timestamp will be used.
40
+ */
41
+ @ReactMethod
42
+ fun startSpan(operation: String, context: ReadableMap, timestampMs: Double, promise: Promise) {
43
+ val span = tracer.buildSpan(operation)
44
+ .withStartTimestamp(TimeUnit.MILLISECONDS.toMicros(timestampMs.toLong()))
45
+ .start()
46
+ val spanContext = span.context()
47
+
48
+ span.setTags(context.toHashMap())
49
+ span.setTags(GlobalState.globalAttributes)
50
+ val spanId = spanContext.toSpanId()
51
+ spanMap[spanId] = span
52
+
53
+ promise.resolve(spanId)
54
+ }
55
+
56
+ /**
57
+ * Finish a started span.
58
+ * @param spanId The unique identifier of the span.
59
+ * @param context The additional context to send.
60
+ * @param timestampMs The timestamp when the operation stopped (in milliseconds). If not provided, current timestamp will be used.
61
+ */
62
+ @ReactMethod
63
+ fun finishSpan(spanId: String, context: ReadableMap, timestampMs: Double, promise: Promise) {
64
+ val span = spanMap.remove(spanId)
65
+ if (span == null) {
66
+ promise.resolve(null)
67
+ return
68
+ }
69
+ span.setTags(context.toHashMap())
70
+ span.setTags(GlobalState.globalAttributes)
71
+ span.finish(TimeUnit.MILLISECONDS.toMicros(timestampMs.toLong()))
72
+
73
+ promise.resolve(null)
74
+ }
75
+
76
+ private fun Span.setTags(tags: Map<String, Any?>) {
77
+ tags.forEach { (key, value) ->
78
+ when (value) {
79
+ is Boolean -> setTag(key, value)
80
+ is Number -> setTag(key, value)
81
+ is String -> setTag(key, value)
82
+ else -> setTag(key, value?.toString())
83
+ }
84
+ }
85
+ }
86
+ }
@@ -0,0 +1,32 @@
1
+ package com.datadog.reactnative
2
+
3
+ import java.util.concurrent.ConcurrentHashMap
4
+
5
+ internal object GlobalState {
6
+
7
+ // Keeps a record of global attributes for logs / spans
8
+ internal val globalAttributes: MutableMap<String, Any?> = ConcurrentHashMap()
9
+
10
+ /**
11
+ * Adds a global attribute to all future RUM events.
12
+ * @param key the attribute key (non null)
13
+ * @param value the attribute value (or null)
14
+ */
15
+ @JvmStatic
16
+ fun addAttribute(key: String, value: Any?) {
17
+ if (value == null) {
18
+ globalAttributes.remove(key)
19
+ } else {
20
+ globalAttributes[key] = value
21
+ }
22
+ }
23
+
24
+ /**
25
+ * Removes a global attribute from all future RUM events.
26
+ * @param key the attribute key (non null)
27
+ */
28
+ @JvmStatic
29
+ fun removeAttribute(key: String) {
30
+ globalAttributes.remove(key)
31
+ }
32
+ }
@@ -0,0 +1,23 @@
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.rum.tracking.ViewTrackingStrategy
11
+
12
+ /**
13
+ * No-op implementation of the [ViewTrackingStrategy].
14
+ */
15
+ object NoOpViewTrackingStrategy : ViewTrackingStrategy {
16
+ override fun register(context: Context) {
17
+ // No-op
18
+ }
19
+
20
+ override fun unregister(context: Context?) {
21
+ // No-op
22
+ }
23
+ }
@@ -0,0 +1,60 @@
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.util.Log
10
+ import okhttp3.Authenticator
11
+ import okhttp3.Credentials
12
+ import okhttp3.Request
13
+ import okhttp3.Response
14
+ import okhttp3.Route
15
+
16
+ internal class ProxyAuthenticator(
17
+ internal val username: String,
18
+ internal val password: String
19
+ ) : Authenticator {
20
+ override fun authenticate(route: Route?, response: Response): Request? {
21
+ val proxyAuthorization = response.code == PROXY_AUTHORIZATION_REQUIRED_STATUS_CODE
22
+
23
+ if (!proxyAuthorization) {
24
+ Log.w(
25
+ ProxyAuthenticator::class.java.canonicalName,
26
+ "Unexpected response code=${response.code}" +
27
+ " received during proxy authentication request."
28
+ )
29
+ return null
30
+ }
31
+
32
+ val challenges = response.challenges()
33
+ for (challenge in challenges) {
34
+ val scheme = challenge.scheme
35
+ if ("Basic".equals(scheme, ignoreCase = true) ||
36
+ "OkHttp-Preemptive".equals(scheme, ignoreCase = true)
37
+ ) {
38
+ val credential = Credentials.basic(
39
+ username,
40
+ password,
41
+ challenge.charset
42
+ )
43
+ return response.request.newBuilder()
44
+ .header("Proxy-Authorization", credential)
45
+ .build()
46
+ }
47
+ }
48
+
49
+ Log.w(
50
+ ProxyAuthenticator::class.java.canonicalName,
51
+ "No known challenges are satisfied during proxy authentication request."
52
+ )
53
+
54
+ return null
55
+ }
56
+
57
+ private companion object {
58
+ const val PROXY_AUTHORIZATION_REQUIRED_STATUS_CODE = 407
59
+ }
60
+ }