@datadog/mobile-react-native 1.0.0 → 1.1.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 (99) 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/ios/Sources/AnyEncodable.swift +138 -0
  34. package/ios/Sources/Attributes.swift +101 -0
  35. package/ios/Sources/DdLogs.swift +35 -11
  36. package/ios/Sources/DdRum.m +3 -1
  37. package/ios/Sources/DdRum.swift +184 -22
  38. package/ios/Sources/DdSdk.swift +190 -10
  39. package/ios/Sources/DdSdkConfiguration.swift +51 -0
  40. package/ios/Sources/DdTrace.swift +41 -6
  41. package/ios/Sources/GlobalState.swift +28 -0
  42. package/ios/Sources/QueueHolder.swift +10 -0
  43. package/ios/Sources/RNDdSdkConfiguration.swift +5 -6
  44. package/lib/commonjs/DdSdkReactNative.js +14 -0
  45. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  46. package/lib/commonjs/DdSdkReactNativeConfiguration.js +24 -0
  47. package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
  48. package/lib/commonjs/foundation.js +36 -2
  49. package/lib/commonjs/foundation.js.map +1 -1
  50. package/lib/commonjs/nativeModulesTypes.js +36 -0
  51. package/lib/commonjs/nativeModulesTypes.js.map +1 -0
  52. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js +4 -17
  53. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  54. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js +4 -4
  55. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  56. package/lib/commonjs/types.js +26 -1
  57. package/lib/commonjs/types.js.map +1 -1
  58. package/lib/commonjs/version.js +1 -1
  59. package/lib/commonjs/version.js.map +1 -1
  60. package/lib/module/DdSdkReactNative.js +14 -0
  61. package/lib/module/DdSdkReactNative.js.map +1 -1
  62. package/lib/module/DdSdkReactNativeConfiguration.js +24 -0
  63. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  64. package/lib/module/foundation.js +36 -2
  65. package/lib/module/foundation.js.map +1 -1
  66. package/lib/module/nativeModulesTypes.js +26 -0
  67. package/lib/module/nativeModulesTypes.js.map +1 -0
  68. package/lib/module/rum/instrumentation/DdEventsInterceptor.js +2 -14
  69. package/lib/module/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  70. package/lib/module/rum/instrumentation/DdRumErrorTracking.js +3 -4
  71. package/lib/module/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  72. package/lib/module/types.js +23 -0
  73. package/lib/module/types.js.map +1 -1
  74. package/lib/module/version.js +1 -1
  75. package/lib/module/version.js.map +1 -1
  76. package/lib/typescript/DdSdkReactNative.d.ts +2 -0
  77. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +20 -0
  78. package/lib/typescript/foundation.d.ts +3 -2
  79. package/lib/typescript/nativeModulesTypes.d.ts +168 -0
  80. package/lib/typescript/rum/instrumentation/DdEventsInterceptor.d.ts +0 -15
  81. package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.d.ts +2 -1
  82. package/lib/typescript/types.d.ts +42 -7
  83. package/lib/typescript/version.d.ts +1 -1
  84. package/package.json +3 -3
  85. package/src/DdSdkReactNative.tsx +20 -0
  86. package/src/DdSdkReactNativeConfiguration.tsx +20 -0
  87. package/src/__tests__/DdSdkReactNative.test.tsx +76 -0
  88. package/src/__tests__/foundation.test.ts +79 -0
  89. package/src/__tests__/rum/instrumentation/DdEventsInterceptor.test.tsx +8 -8
  90. package/src/foundation.tsx +40 -11
  91. package/src/nativeModulesTypes.ts +258 -0
  92. package/src/rum/instrumentation/DdEventsInterceptor.tsx +2 -17
  93. package/src/rum/instrumentation/DdRumErrorTracking.tsx +3 -4
  94. package/src/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.ts +2 -1
  95. package/src/types.tsx +62 -7
  96. package/src/version.ts +1 -1
  97. package/android/src/main/java/com/datadog/reactnative/DdRum.kt +0 -142
  98. package/android/src/main/java/com/datadog/reactnative/DdSdk.kt +0 -67
  99. package/android/src/main/java/com/datadog/reactnative/DdTrace.kt +0 -51
@@ -5,7 +5,15 @@
5
5
  */
6
6
 
7
7
  import Foundation
8
- import DatadogSDKBridge
8
+ import Datadog
9
+
10
+ extension Logger: NativeLogger { }
11
+ internal protocol NativeLogger {
12
+ func debug(_ message: String, error: Error?, attributes: [String: Encodable]?)
13
+ func info(_ message: String, error: Error?, attributes: [String: Encodable]?)
14
+ func warn(_ message: String, error: Error?, attributes: [String: Encodable]?)
15
+ func error(_ message: String, error: Error?, attributes: [String: Encodable]?)
16
+ }
9
17
 
10
18
  @objc(DdLogs)
11
19
  class RNDdLogs: NSObject {
@@ -15,32 +23,48 @@ class RNDdLogs: NSObject {
15
23
  return false
16
24
  }
17
25
 
18
- let nativeInstance: DdLogs = Bridge.getDdLogs()
19
-
20
26
  @objc(methodQueue)
21
27
  let methodQueue: DispatchQueue = sharedQueue
22
28
 
29
+ private lazy var logger: NativeLogger = loggerProvider()
30
+ private let loggerProvider: () -> NativeLogger
31
+
32
+ internal init(_ loggerProvider: @escaping () -> NativeLogger) {
33
+ self.loggerProvider = loggerProvider
34
+ }
35
+
36
+ override public convenience init() {
37
+ let builder = Logger.builder
38
+ .sendNetworkInfo(true)
39
+ .printLogsToConsole(true)
40
+ self.init { builder.build() }
41
+ }
42
+
23
43
  @objc(debug:withContext:withResolver:withRejecter:)
24
- func debug(message: NSString, context: NSDictionary, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
25
- nativeInstance.debug(message: message, context: context)
44
+ func debug(message: String, context: NSDictionary, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
45
+ let attributes = castAttributesToSwift(context).mergeWithGlobalAttributes()
46
+ logger.debug(message, error: nil, attributes: attributes)
26
47
  resolve(nil)
27
48
  }
28
49
 
29
50
  @objc(info:withContext:withResolver:withRejecter:)
30
- func info(message: NSString, context: NSDictionary, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
31
- nativeInstance.info(message: message, context: context)
51
+ func info(message: String, context: NSDictionary, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
52
+ let attributes = castAttributesToSwift(context).mergeWithGlobalAttributes()
53
+ logger.info(message, error: nil, attributes: attributes)
32
54
  resolve(nil)
33
55
  }
34
56
 
35
57
  @objc(warn:withContext:withResolver:withRejecter:)
36
- func warn(message: NSString, context: NSDictionary, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
37
- nativeInstance.warn(message: message, context: context)
58
+ func warn(message: String, context: NSDictionary, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
59
+ let attributes = castAttributesToSwift(context).mergeWithGlobalAttributes()
60
+ logger.warn(message, error: nil, attributes: attributes)
38
61
  resolve(nil)
39
62
  }
40
63
 
41
64
  @objc(error:withContext:withResolver:withRejecter:)
42
- func error(message: NSString, context: NSDictionary, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
43
- nativeInstance.error(message: message, context: context)
65
+ func error(message: String, context: NSDictionary, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
66
+ let attributes = castAttributesToSwift(context).mergeWithGlobalAttributes()
67
+ logger.error(message, error: nil, attributes: attributes)
44
68
  resolve(nil)
45
69
  }
46
70
 
@@ -28,7 +28,9 @@ RCT_EXTERN_METHOD(startAction:(NSString)type
28
28
  withResolver:(RCTPromiseResolveBlock)resolve
29
29
  withRejecter:(RCTPromiseRejectBlock)reject)
30
30
 
31
- RCT_EXTERN_METHOD(stopAction:(NSDictionary)context
31
+ RCT_EXTERN_METHOD(stopAction:(NSString)type
32
+ withName:(NSString)name
33
+ withContext:(NSDictionary)context
32
34
  withTimestampms:(NSInteger)timestampMs
33
35
  withResolver:(RCTPromiseResolveBlock)resolve
34
36
  withRejecter:(RCTPromiseRejectBlock)reject)
@@ -5,73 +5,235 @@
5
5
  */
6
6
 
7
7
  import Foundation
8
- import DatadogSDKBridge
8
+ import Datadog
9
+
10
+ extension DDRUMMonitor: NativeRUM { }
11
+ internal protocol NativeRUM {
12
+ func startView(key: String, name: String?, attributes: [String: Encodable])
13
+ func stopView(key: String, attributes: [String: Encodable])
14
+ func addError(message: String, type: String?, source: RUMErrorSource, stack: String?, attributes: [String: Encodable], file: StaticString?, line: UInt?)
15
+ func startResourceLoading(resourceKey: String, httpMethod: RUMMethod, urlString: String, attributes: [String: Encodable])
16
+ func stopResourceLoading(resourceKey: String, statusCode: Int?, kind: RUMResourceType, size: Int64?, attributes: [String: Encodable])
17
+ func startUserAction(type: RUMUserActionType, name: String, attributes: [String: Encodable])
18
+ func stopUserAction(type: RUMUserActionType, name: String?, attributes: [String: Encodable])
19
+ func addUserAction(type: RUMUserActionType, name: String, attributes: [String: Encodable])
20
+ func addTiming(name: String)
21
+ func addResourceMetrics(resourceKey: String,
22
+ fetch: (start: Date, end: Date),
23
+ redirection: (start: Date, end: Date)?,
24
+ dns: (start: Date, end: Date)?,
25
+ connect: (start: Date, end: Date)?,
26
+ ssl: (start: Date, end: Date)?,
27
+ firstByte: (start: Date, end: Date)?,
28
+ download: (start: Date, end: Date)?,
29
+ responseSize: Int64?,
30
+ attributes: [AttributeKey: AttributeValue])
31
+ }
32
+
33
+ private extension RUMUserActionType {
34
+ init(from string: String) {
35
+ switch string.lowercased() {
36
+ case "tap": self = .tap
37
+ case "scroll": self = .scroll
38
+ case "swipe": self = .swipe
39
+ default: self = .custom
40
+ }
41
+ }
42
+ }
43
+
44
+ internal extension RUMErrorSource {
45
+ init(from string: String) {
46
+ switch string.lowercased() {
47
+ case "source": self = .source
48
+ case "network": self = .network
49
+ case "webview": self = .webview
50
+ case "console": self = .console
51
+ default: self = .custom
52
+ }
53
+ }
54
+ }
55
+
56
+ private extension RUMResourceType {
57
+ init(from string: String) {
58
+ switch string {
59
+ case "image": self = .image
60
+ case "xhr": self = .xhr
61
+ case "beacon": self = .beacon
62
+ case "css": self = .css
63
+ case "document": self = .document
64
+ case "fetch": self = .fetch
65
+ case "font": self = .font
66
+ case "js": self = .js
67
+ case "media": self = .media
68
+ default: self = .other
69
+ }
70
+ }
71
+ }
72
+
73
+ private extension RUMMethod {
74
+ init(from string: String) {
75
+ switch string.uppercased() {
76
+ case "POST": self = .post
77
+ case "GET": self = .get
78
+ case "HEAD": self = .head
79
+ case "PUT": self = .put
80
+ case "DELETE": self = .delete
81
+ case "PATCH": self = .patch
82
+ default: self = .get
83
+ }
84
+ }
85
+ }
9
86
 
10
87
  @objc(DdRum)
11
88
  class RNDdRum: NSObject {
89
+ internal static let timestampKey = "_dd.timestamp"
90
+ internal static let resourceTimingsKey = "_dd.resource_timings"
91
+
92
+ internal static let fetchTimingKey = "fetch"
93
+ internal static let redirectTimingKey = "redirect"
94
+ internal static let dnsTimingKey = "dns"
95
+ internal static let connectTimingKey = "connect"
96
+ internal static let sslTimingKey = "ssl"
97
+ internal static let firstByteTimingKey = "firstByte"
98
+ internal static let downloadTimingKey = "download"
99
+
100
+ internal static let missingResourceSize = -1
101
+
102
+ lazy var nativeRUM: NativeRUM = rumProvider()
103
+ private let rumProvider: () -> NativeRUM
104
+
105
+ private typealias UserAction = (type: RUMUserActionType, name: String?)
106
+
107
+ internal init(_ rumProvider: @escaping () -> NativeRUM) {
108
+ self.rumProvider = rumProvider
109
+ }
110
+
111
+ override convenience init() {
112
+ self.init { Global.rum }
113
+ }
12
114
 
13
115
  @objc(requiresMainQueueSetup)
14
116
  static func requiresMainQueueSetup() -> Bool {
15
117
  return false
16
118
  }
17
119
 
18
- let nativeInstance: DdRum = Bridge.getDdRum()
19
-
20
120
  @objc(methodQueue)
21
121
  let methodQueue: DispatchQueue = sharedQueue
22
122
 
23
123
  @objc(startView:withName:withContext:withTimestampms:withResolver:withRejecter:)
24
- func startView(key: NSString, name: NSString, context: NSDictionary, timestampMs: Int64, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
25
- nativeInstance.startView(key: key, name: name, context: context, timestampMs: timestampMs)
124
+ func startView(key: String, name: String, context: NSDictionary, timestampMs: Int64, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
125
+ nativeRUM.startView(key: key, name: name, attributes: attributes(from: context, with: timestampMs))
26
126
  resolve(nil)
27
127
  }
28
128
 
29
129
  @objc(stopView:withContext:withTimestampms:withResolver:withRejecter:)
30
- func stopView(key: NSString, context: NSDictionary, timestampMs: Int64, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
31
- nativeInstance.stopView(key: key, context: context, timestampMs: timestampMs)
130
+ func stopView(key: String, context: NSDictionary, timestampMs: Int64, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
131
+ nativeRUM.stopView(key: key, attributes: attributes(from: context, with: timestampMs))
32
132
  resolve(nil)
33
133
  }
34
134
 
35
135
  @objc(startAction:withName:withContext:withTimestampms:withResolver:withRejecter:)
36
- func startAction(type: NSString, name: NSString, context: NSDictionary, timestampMs: Int64, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
37
- nativeInstance.startAction(type: type, name: name, context: context, timestampMs: timestampMs)
136
+ func startAction(type: String, name: String, context: NSDictionary, timestampMs: Int64, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
137
+ nativeRUM.startUserAction(type: RUMUserActionType(from: type), name: name, attributes: attributes(from: context, with: timestampMs))
38
138
  resolve(nil)
39
139
  }
40
140
 
41
- @objc(stopAction:withTimestampms:withResolver:withRejecter:)
42
- func stopAction(context: NSDictionary, timestampMs: Int64, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
43
- nativeInstance.stopAction(context: context, timestampMs: timestampMs)
141
+ @objc(stopAction:withName:withContext:withTimestampms:withResolver:withRejecter:)
142
+ func stopAction(type: String, name: String, context: NSDictionary, timestampMs: Int64, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
143
+ nativeRUM.stopUserAction(type: RUMUserActionType(from: type), name: name, attributes: attributes(from: context, with: timestampMs))
44
144
  resolve(nil)
45
145
  }
46
146
 
47
147
  @objc(addAction:withName:withContext:withTimestampms:withResolver:withRejecter:)
48
- func addAction(type: NSString, name: NSString, context: NSDictionary, timestampMs: Int64, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
49
- nativeInstance.addAction(type: type, name: name, context: context, timestampMs: timestampMs)
148
+ func addAction(type: String, name: String, context: NSDictionary, timestampMs: Int64, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
149
+ nativeRUM.addUserAction(type: RUMUserActionType(from: type), name: name, attributes: attributes(from: context, with: timestampMs))
50
150
  resolve(nil)
51
151
  }
52
152
 
53
153
  @objc(startResource:withMethod:withUrl:withContext:withTimestampms:withResolver:withRejecter:)
54
- func startResource(key: NSString, method: NSString, url: NSString, context: NSDictionary, timestampMs: Int64, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
55
- nativeInstance.startResource(key: key, method: method, url: url, context: context, timestampMs: timestampMs)
154
+ func startResource(key: String, method: String, url: String, context: NSDictionary, timestampMs: Int64, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
155
+ nativeRUM.startResourceLoading(resourceKey: key, httpMethod: RUMMethod(from: method), urlString: url, attributes: attributes(from: context, with: timestampMs))
56
156
  resolve(nil)
57
157
  }
58
158
 
59
159
  @objc(stopResource:withStatuscode:withKind:withSize:withContext:withTimestampms:withResolver:withRejecter:)
60
- func stopResource(key: NSString, statusCode: Int64, kind: NSString, size: Int64, context: NSDictionary, timestampMs: Int64, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
61
- nativeInstance.stopResource(key: key, statusCode: statusCode, kind: kind, size: size, context: context, timestampMs: timestampMs)
160
+ func stopResource(key: String, statusCode: Int64, kind: String, size: Int64, context: NSDictionary, timestampMs: Int64, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
161
+ let mutableContext = NSMutableDictionary(dictionary: context)
162
+ if let resourceTimings = mutableContext.object(forKey: Self.resourceTimingsKey) as? [String: Any] {
163
+ mutableContext.removeObject(forKey: Self.resourceTimingsKey)
164
+
165
+ addResourceMetrics(key: key, resourceTimings: resourceTimings)
166
+ }
167
+
168
+ nativeRUM.stopResourceLoading(
169
+ resourceKey: key,
170
+ statusCode: Int(statusCode),
171
+ kind: RUMResourceType(from: kind),
172
+ size: size == Self.missingResourceSize ? nil : size,
173
+ attributes: attributes(from: mutableContext, with: timestampMs)
174
+ )
62
175
  resolve(nil)
63
176
  }
64
177
 
65
178
  @objc(addError:withSource:withStacktrace:withContext:withTimestampms:withResolver:withRejecter:)
66
- func addError(message: NSString, source: NSString, stacktrace: NSString, context: NSDictionary, timestampMs: Int64, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
67
- nativeInstance.addError(message: message, source: source, stacktrace: stacktrace, context: context, timestampMs: timestampMs)
179
+ func addError(message: String, source: String, stacktrace: String, context: NSDictionary, timestampMs: Int64, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
180
+ nativeRUM.addError(message: message, type: nil, source: RUMErrorSource(from: source), stack: stacktrace, attributes: attributes(from: context, with: timestampMs), file: nil, line: nil)
68
181
  resolve(nil)
69
182
  }
70
183
 
71
184
  @objc(addTiming:withResolver:withRejecter:)
72
- func addTiming(name: NSString, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
73
- nativeInstance.addTiming(name: name)
185
+ func addTiming(name: String, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
186
+ nativeRUM.addTiming(name: name)
74
187
  resolve(nil)
75
188
  }
76
189
 
190
+ // MARK: - Private methods
191
+
192
+ private func attributes(from context: NSDictionary, with timestampMs: Int64) -> [String: Encodable] {
193
+ var context = context as? [String: Any] ?? [:]
194
+ context[Self.timestampKey] = timestampMs
195
+ return castAttributesToSwift(context)
196
+ }
197
+
198
+ private func addResourceMetrics(key: String, resourceTimings: [String: Any]) {
199
+ let fetch = timingValue(from: resourceTimings, for: Self.fetchTimingKey)
200
+ let redirect = timingValue(from: resourceTimings, for: Self.redirectTimingKey)
201
+ let dns = timingValue(from: resourceTimings, for: Self.dnsTimingKey)
202
+ let connect = timingValue(from: resourceTimings, for: Self.connectTimingKey)
203
+ let ssl = timingValue(from: resourceTimings, for: Self.sslTimingKey)
204
+ let firstByte = timingValue(from: resourceTimings, for: Self.firstByteTimingKey)
205
+ let download = timingValue(from: resourceTimings, for: Self.downloadTimingKey)
206
+
207
+ if let fetch = fetch {
208
+ nativeRUM.addResourceMetrics(
209
+ resourceKey: key,
210
+ fetch: fetch,
211
+ redirection: redirect,
212
+ dns: dns,
213
+ connect: connect,
214
+ ssl: ssl,
215
+ firstByte: firstByte,
216
+ download: download,
217
+ // no need to define the size here, because if it is missing,
218
+ // it will be taken from the command
219
+ responseSize: nil,
220
+ attributes: [:]
221
+ )
222
+ }
223
+ }
224
+
225
+ private func timingValue(from timings: [String: Any], for timingName: String) -> (start: Date, end: Date)? {
226
+ let timing = timings[timingName] as? [String: NSNumber]
227
+ if let startInNs = timing?["startTime"]?.int64Value, let durationInNs = timing?["duration"]?.int64Value {
228
+ return (
229
+ Date(timeIntervalSince1970: TimeInterval(fromNs: startInNs)),
230
+ Date(timeIntervalSince1970: TimeInterval(fromNs: startInNs + durationInNs))
231
+ )
232
+ }
233
+ return nil
234
+ }
235
+ }
236
+
237
+ internal extension TimeInterval {
238
+ init(fromNs ns: Int64) { self = TimeInterval(Double(ns) / 1_000_000_000) }
77
239
  }
@@ -5,46 +5,226 @@
5
5
  */
6
6
 
7
7
  import Foundation
8
- import DatadogSDKBridge
8
+ import Datadog
9
+ import DatadogCrashReporting
10
+
11
+ func getDefaultAppVersion() -> String {
12
+ let bundleShortVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
13
+ let bundleVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String
14
+ return bundleShortVersion ?? bundleVersion ?? "0.0.0"
15
+ }
9
16
 
10
17
  @objc(DdSdk)
11
18
  class RNDdSdk: NSObject {
12
-
13
19
  @objc(requiresMainQueueSetup)
14
20
  static func requiresMainQueueSetup() -> Bool {
15
21
  return false
16
22
  }
17
-
18
- let nativeInstance: DdSdk = Bridge.getDdSdk()
19
-
23
+
24
+ let mainDispatchQueue: DispatchQueueType
20
25
  @objc(methodQueue)
21
26
  let methodQueue: DispatchQueue = sharedQueue
27
+
28
+ convenience override init() {
29
+ self.init(mainDispatchQueue: DispatchQueue.main)
30
+ }
31
+
32
+ init(mainDispatchQueue: DispatchQueueType) {
33
+ self.mainDispatchQueue = mainDispatchQueue
34
+ super.init()
35
+ }
36
+
22
37
 
23
38
  @objc(initialize:withResolver:withRejecter:)
24
39
  func initialize(configuration: NSDictionary, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
25
40
  // Datadog SDK init needs to happen on the main thread: https://github.com/DataDog/dd-sdk-reactnative/issues/198
26
- DispatchQueue.main.async {
27
- self.nativeInstance.initialize(configuration: configuration.asDdSdkConfiguration())
41
+ self.mainDispatchQueue.async {
42
+ if Datadog.isInitialized {
43
+ // Initializing the SDK twice results in Global.rum and
44
+ // Global.sharedTracer to be set to no-op instances
45
+ consolePrint("Datadog SDK is already initialized, skipping initialization.")
46
+ return
47
+ }
48
+ let sdkConfiguration = configuration.asDdSdkConfiguration()
49
+ self.setVerbosityLevel(additionalConfig: sdkConfiguration.additionalConfig)
50
+
51
+ let ddConfig = self.buildConfiguration(configuration: sdkConfiguration)
52
+ let consent = self.buildTrackingConsent(consent: sdkConfiguration.trackingConsent)
53
+ Datadog.initialize(appContext: Datadog.AppContext(), trackingConsent: consent, configuration: ddConfig)
54
+
55
+ Global.rum = RUMMonitor.initialize()
56
+
28
57
  resolve(nil)
29
58
  }
30
59
  }
31
60
 
32
61
  @objc(setAttributes:withResolver:withRejecter:)
33
62
  func setAttributes(attributes: NSDictionary, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
34
- nativeInstance.setAttributes(attributes: attributes)
63
+ let castedAttributes = castAttributesToSwift(attributes)
64
+ for (key, value) in castedAttributes {
65
+ Global.rum.addAttribute(forKey: key, value: value)
66
+ GlobalState.addAttribute(forKey: key, value: value)
67
+ }
68
+
35
69
  resolve(nil)
36
70
  }
37
71
 
38
72
  @objc(setUser:withResolver:withRejecter:)
39
73
  func setUser(user: NSDictionary, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
40
- nativeInstance.setUser(user: user)
74
+ var castedUser = castAttributesToSwift(user)
75
+ let id = castedUser.removeValue(forKey: "id") as? String
76
+ let name = castedUser.removeValue(forKey: "name") as? String
77
+ let email = castedUser.removeValue(forKey: "email") as? String
78
+ let extraInfo: [String: Encodable] = castedUser // everything what's left is an `extraInfo`
79
+
80
+ Datadog.setUserInfo(id: id, name: name, email: email, extraInfo: extraInfo)
41
81
  resolve(nil)
42
82
  }
43
83
 
44
84
  @objc(setTrackingConsent:withResolver:withRejecter:)
45
85
  func setTrackingConsent(trackingConsent: NSString, resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
46
- nativeInstance.setTrackingConsent(trackingConsent: trackingConsent)
86
+ Datadog.set(trackingConsent: buildTrackingConsent(consent: trackingConsent))
47
87
  resolve(nil)
48
88
  }
49
89
 
90
+ func buildConfiguration(configuration: DdSdkConfiguration, defaultAppVersion: String = getDefaultAppVersion()) -> Datadog.Configuration {
91
+ let ddConfigBuilder: Datadog.Configuration.Builder
92
+ if let rumAppID = configuration.applicationId {
93
+ ddConfigBuilder = Datadog.Configuration.builderUsing(
94
+ rumApplicationID: rumAppID,
95
+ clientToken: configuration.clientToken,
96
+ environment: configuration.env
97
+ )
98
+ .set(rumSessionsSamplingRate: Float(configuration.sampleRate ?? 100.0))
99
+ } else {
100
+ ddConfigBuilder = Datadog.Configuration.builderUsing(
101
+ clientToken: configuration.clientToken,
102
+ environment: configuration.env
103
+ )
104
+ }
105
+
106
+ switch configuration.site?.lowercased ?? "us" {
107
+ case "us1", "us":
108
+ _ = ddConfigBuilder.set(endpoint: .us1)
109
+ case "eu1", "eu":
110
+ _ = ddConfigBuilder.set(endpoint: .eu1)
111
+ case "us3":
112
+ _ = ddConfigBuilder.set(endpoint: .us3)
113
+ case "us5":
114
+ _ = ddConfigBuilder.set(endpoint: .us5)
115
+ case "us1_fed", "gov":
116
+ _ = ddConfigBuilder.set(endpoint: .us1_fed)
117
+ default:
118
+ _ = ddConfigBuilder.set(endpoint: .us1)
119
+ }
120
+
121
+ let additionalConfig = configuration.additionalConfig
122
+
123
+ if var additionalConfiguration = additionalConfig as? [String: Any] {
124
+ if let versionSuffix = additionalConfig?[InternalConfigurationAttributes.versionSuffix] as? String {
125
+ let datadogVersion = defaultAppVersion + versionSuffix
126
+ additionalConfiguration[CrossPlatformAttributes.version] = datadogVersion
127
+ }
128
+
129
+ _ = ddConfigBuilder.set(additionalConfiguration: additionalConfiguration)
130
+ }
131
+
132
+ if let enableViewTracking = additionalConfig?[InternalConfigurationAttributes.nativeViewTracking] as? Bool, enableViewTracking {
133
+ _ = ddConfigBuilder.trackUIKitRUMViews()
134
+ }
135
+
136
+ if let serviceName = additionalConfig?[InternalConfigurationAttributes.serviceName] as? String {
137
+ _ = ddConfigBuilder.set(serviceName: serviceName)
138
+ }
139
+
140
+ if let threshold = additionalConfig?[InternalConfigurationAttributes.longTaskThreshold] as? TimeInterval {
141
+ // `_dd.long_task.threshold` attribute is in milliseconds
142
+ _ = ddConfigBuilder.trackRUMLongTasks(threshold: threshold / 1_000)
143
+ }
144
+
145
+ if let firstPartyHosts = additionalConfig?[InternalConfigurationAttributes.firstPartyHosts] as? [String] {
146
+ _ = ddConfigBuilder.trackURLSession(firstPartyHosts: Set(firstPartyHosts))
147
+ }
148
+
149
+ if let proxyConfiguration = buildProxyConfiguration(config: additionalConfig) {
150
+ _ = ddConfigBuilder.set(proxyConfiguration: proxyConfiguration)
151
+ }
152
+
153
+ if configuration.nativeCrashReportEnabled ?? false {
154
+ _ = ddConfigBuilder.enableCrashReporting(using: DDCrashReportingPlugin())
155
+ }
156
+
157
+ return ddConfigBuilder.build()
158
+ }
159
+
160
+ func buildProxyConfiguration(config: NSDictionary?) -> [AnyHashable: Any]? {
161
+ guard let address = config?[ProxyAttributes.address] as? String else {
162
+ return nil
163
+ }
164
+
165
+ var proxy: [AnyHashable: Any] = [:]
166
+ proxy[kCFProxyUsernameKey] = config?[ProxyAttributes.username]
167
+ proxy[kCFProxyPasswordKey] = config?[ProxyAttributes.password]
168
+
169
+ let type = config?[ProxyAttributes.type] as? String
170
+ var port = config?[ProxyAttributes.port] as? Int
171
+ if let string = config?[ProxyAttributes.port] as? String {
172
+ port = Int(string)
173
+ }
174
+
175
+ switch type {
176
+ case "http", "https":
177
+ // CFNetwork support HTTP and tunneling HTTPS proxies.
178
+ // As intakes will most likely be https, we enable both channels.
179
+ //
180
+ // We use constants string keys because there is an issue with
181
+ // cross-platform availability for proxy configuration symbols.
182
+ // see. https://developer.apple.com/forums/thread/19356?answerId=131709022#131709022
183
+ proxy["HTTPEnable"] = 1
184
+ proxy["HTTPProxy"] = address
185
+ proxy["HTTPPort"] = port
186
+ proxy["HTTPSEnable"] = 1
187
+ proxy["HTTPSProxy"] = address
188
+ proxy["HTTPSPort"] = port
189
+ case "socks":
190
+ proxy["SOCKSEnable"] = 1
191
+ proxy["SOCKSProxy"] = address
192
+ proxy["SOCKSPort"] = port
193
+ default:
194
+ break
195
+ }
196
+
197
+ return proxy
198
+ }
199
+
200
+ func buildTrackingConsent(consent: NSString?) -> TrackingConsent {
201
+ let trackingConsent: TrackingConsent
202
+ switch consent?.lowercased {
203
+ case "pending":
204
+ trackingConsent = .pending
205
+ case "granted":
206
+ trackingConsent = .granted
207
+ case "not_granted":
208
+ trackingConsent = .notGranted
209
+ default:
210
+ trackingConsent = .pending
211
+ }
212
+ return trackingConsent
213
+ }
214
+
215
+ func setVerbosityLevel(additionalConfig: NSDictionary?) {
216
+ let verbosityLevel = (additionalConfig?[InternalConfigurationAttributes.sdkVerbosity]) as? NSString
217
+ switch verbosityLevel?.lowercased {
218
+ case "debug":
219
+ Datadog.verbosityLevel = .debug
220
+ case "info":
221
+ Datadog.verbosityLevel = .info
222
+ case "warn":
223
+ Datadog.verbosityLevel = .warn
224
+ case "error":
225
+ Datadog.verbosityLevel = .error
226
+ default:
227
+ Datadog.verbosityLevel = nil
228
+ }
229
+ }
50
230
  }
@@ -0,0 +1,51 @@
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
+ import Foundation
8
+
9
+ /**
10
+ A configuration object to initialize Datadog's features.
11
+ - Parameters:
12
+ - clientToken: A valid Datadog client token.
13
+ - env: The application’s environment, for example: prod, pre-prod, staging, etc.
14
+ - applicationId: The RUM application ID.
15
+ - nativeCrashReportEnabled: Whether the SDK should track native (pure iOS or pure Android) crashes (default is false).
16
+ - sampleRate: The sample rate (between 0 and 100) of RUM sessions kept.
17
+ - site: The Datadog site of your organization (can be 'US1', 'US1_FED', 'US3', 'US5', or 'EU1', default is 'US1').
18
+ - trackingConsent: Consent, which can take one of the following values: 'pending', 'granted', 'not_granted'.
19
+ - additionalConfig: Additional configuration parameters.
20
+ */
21
+ @objc(DdSdkConfiguration)
22
+ public class DdSdkConfiguration: NSObject {
23
+ public var clientToken: String = ""
24
+ public var env: String = ""
25
+ public var applicationId: String? = nil
26
+ public var nativeCrashReportEnabled: Bool? = nil
27
+ public var sampleRate: Double? = nil
28
+ public var site: NSString? = nil
29
+ public var trackingConsent: NSString? = nil
30
+ public var additionalConfig: NSDictionary? = nil
31
+
32
+ public init(
33
+ clientToken: String,
34
+ env: String,
35
+ applicationId: String?,
36
+ nativeCrashReportEnabled: Bool?,
37
+ sampleRate: Double?,
38
+ site: NSString?,
39
+ trackingConsent: NSString?,
40
+ additionalConfig: NSDictionary?
41
+ ) {
42
+ self.clientToken = clientToken
43
+ self.env = env
44
+ self.applicationId = applicationId
45
+ self.nativeCrashReportEnabled = nativeCrashReportEnabled
46
+ self.sampleRate = sampleRate
47
+ self.site = site
48
+ self.trackingConsent = trackingConsent
49
+ self.additionalConfig = additionalConfig
50
+ }
51
+ }