forked from DataDog/dd-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatadogConfiguration+objc.swift
289 lines (244 loc) · 9.88 KB
/
DatadogConfiguration+objc.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2019-Present Datadog, Inc.
*/
import Foundation
import DatadogInternal
import DatadogCore
@objc
public class DDSite: NSObject {
internal let sdkSite: DatadogSite
internal init(sdkSite: DatadogSite) {
self.sdkSite = sdkSite
}
// MARK: - Public
@objc
public static func us1() -> DDSite { .init(sdkSite: .us1) }
@objc
public static func us3() -> DDSite { .init(sdkSite: .us3) }
@objc
public static func us5() -> DDSite { .init(sdkSite: .us5) }
@objc
public static func eu1() -> DDSite { .init(sdkSite: .eu1) }
@objc
public static func ap1() -> DDSite { .init(sdkSite: .ap1) }
@objc
public static func us1_fed() -> DDSite { .init(sdkSite: .us1_fed) }
}
@objc
public enum DDBatchSize: Int {
case small
case medium
case large
internal var swiftType: Datadog.Configuration.BatchSize {
switch self {
case .small: return .small
case .medium: return .medium
case .large: return .large
}
}
internal init(swiftType: Datadog.Configuration.BatchSize) {
switch swiftType {
case .small: self = .small
case .medium: self = .medium
case .large: self = .large
}
}
}
@objc
public enum DDUploadFrequency: Int {
case frequent
case average
case rare
internal var swiftType: Datadog.Configuration.UploadFrequency {
switch self {
case .frequent: return .frequent
case .average: return .average
case .rare: return .rare
}
}
internal init(swiftType: Datadog.Configuration.UploadFrequency) {
switch swiftType {
case .frequent: self = .frequent
case .average: self = .average
case .rare: self = .rare
}
}
}
@objc
public enum DDBatchProcessingLevel: Int {
case low
case medium
case high
internal var swiftType: Datadog.Configuration.BatchProcessingLevel {
switch self {
case .low: return .low
case .medium: return .medium
case .high: return .high
}
}
internal init(swiftType: Datadog.Configuration.BatchProcessingLevel) {
switch swiftType {
case .low: self = .low
case .medium: self = .medium
case .high: self = .high
}
}
}
@objc
public class DDTracingHeaderType: NSObject {
internal let swiftType: TracingHeaderType
private init(_ swiftType: TracingHeaderType) {
self.swiftType = swiftType
}
@objc public static let datadog = DDTracingHeaderType(.datadog)
@objc public static let b3multi = DDTracingHeaderType(.b3multi)
@objc public static let b3 = DDTracingHeaderType(.b3)
@objc public static let tracecontext = DDTracingHeaderType(.tracecontext)
}
@objc
public protocol DDDataEncryption: AnyObject {
/// Encrypts given `Data` with user-chosen encryption.
///
/// - Parameter data: Data to encrypt.
/// - Returns: The encrypted data.
func encrypt(data: Data) throws -> Data
/// Decrypts given `Data` with user-chosen encryption.
///
/// Beware that data to decrypt could be encrypted in a previous
/// app launch, so implementation should be aware of the case when decryption could
/// fail (for example, key used for encryption is different from key used for decryption, if
/// they are unique for every app launch).
///
/// - Parameter data: Data to decrypt.
/// - Returns: The decrypted data.
func decrypt(data: Data) throws -> Data
}
internal struct DDDataEncryptionBridge: DataEncryption {
let objcEncryption: DDDataEncryption
func encrypt(data: Data) throws -> Data {
return try objcEncryption.encrypt(data: data)
}
func decrypt(data: Data) throws -> Data {
return try objcEncryption.decrypt(data: data)
}
}
@objc
public protocol DDServerDateProvider: AnyObject {
/// Start the clock synchronisation with NTP server.
///
/// Calls the `completion` by passing it the server time offset when the synchronization succeeds or`nil` if it fails.
func synchronize(update: @escaping (TimeInterval) -> Void)
}
internal struct DDServerDateProviderBridge: ServerDateProvider {
let objcProvider: DDServerDateProvider
func synchronize(update: @escaping (TimeInterval) -> Void) {
objcProvider.synchronize(update: update)
}
}
@objc
public class DDConfiguration: NSObject {
internal var sdkConfiguration: Datadog.Configuration
/// Either the RUM client token (which supports RUM, Logging and APM) or regular client token, only for Logging and APM.
@objc public var clientToken: String {
get { sdkConfiguration.clientToken }
set { sdkConfiguration.clientToken = newValue }
}
/// The environment name which will be sent to Datadog. This can be used
/// To filter events on different environments (e.g. "staging" or "production").
@objc public var env: String {
get { sdkConfiguration.env }
set { sdkConfiguration.env = newValue }
}
/// The Datadog server site where data is sent.
///
/// Default value is `.us1`.
@objc public var site: DDSite {
get { DDSite(sdkSite: sdkConfiguration.site) }
set { sdkConfiguration.site = newValue.sdkSite }
}
/// The service name associated with data send to Datadog.
///
/// Default value is set to application bundle identifier.
@objc public var service: String? {
get { sdkConfiguration.service }
set { sdkConfiguration.service = newValue }
}
/// The preferred size of batched data uploaded to Datadog servers.
/// This value impacts the size and number of requests performed by the SDK.
///
/// `.medium` by default.
@objc public var batchSize: DDBatchSize {
get { DDBatchSize(swiftType: sdkConfiguration.batchSize) }
set { sdkConfiguration.batchSize = newValue.swiftType }
}
/// The preferred frequency of uploading data to Datadog servers.
/// This value impacts the frequency of performing network requests by the SDK.
///
/// `.average` by default.
@objc public var uploadFrequency: DDUploadFrequency {
get { DDUploadFrequency(swiftType: sdkConfiguration.uploadFrequency) }
set { sdkConfiguration.uploadFrequency = newValue.swiftType }
}
///
@objc public var batchProcessingLevel: DDBatchProcessingLevel {
get { DDBatchProcessingLevel(swiftType: sdkConfiguration.batchProcessingLevel) }
set { sdkConfiguration.batchProcessingLevel = newValue.swiftType }
}
/// Proxy configuration attributes.
/// This can be used to a enable a custom proxy for uploading tracked data to Datadog's intake.
@objc public var proxyConfiguration: [AnyHashable: Any]? {
get { sdkConfiguration.proxyConfiguration }
set { sdkConfiguration.proxyConfiguration = newValue }
}
/// Sets Data encryption to use for on-disk data persistency by providing an object
/// complying with `DataEncryption` protocol.
@objc
public func setEncryption(_ encryption: DDDataEncryption) {
sdkConfiguration.encryption = DDDataEncryptionBridge(objcEncryption: encryption)
}
/// A custom NTP synchronization interface.
///
/// By default, the Datadog SDK synchronizes with dedicated NTP pools provided by the
/// https://www.ntppool.org/ . Using different pools or setting a no-op `ServerDateProvider`
/// implementation will result in desynchronization of the SDK instance and the Datadog servers.
/// This can lead to significant time shift in RUM sessions or distributed traces.
@objc
public func setServerDateProvider(_ serverDateProvider: DDServerDateProvider) {
sdkConfiguration.serverDateProvider = DDServerDateProviderBridge(objcProvider: serverDateProvider)
}
/// The bundle object that contains the current executable.
@objc public var bundle: Bundle {
get { sdkConfiguration.bundle }
set { sdkConfiguration.bundle = newValue }
}
/// Sets additional configuration attributes.
/// This can be used to tweak internal features of the SDK and shouldn't be considered as a part of public API.
@objc public var additionalConfiguration: [String: Any] {
get { sdkConfiguration._internal.additionalConfiguration }
set { sdkConfiguration._internal_mutation { $0.additionalConfiguration = newValue } }
}
/// Flag that determines if UIApplication methods [`beginBackgroundTask(expirationHandler:)`](https://developer.apple.com/documentation/uikit/uiapplication/1623031-beginbackgroundtaskwithexpiratio) and [`endBackgroundTask:`](https://developer.apple.com/documentation/uikit/uiapplication/1622970-endbackgroundtask)
/// are utilized to perform background uploads. It may extend the amount of time the app is operating in background by 30 seconds.
///
/// Tasks are normally stopped when there's nothing to upload or when encountering any upload blocker such us no internet connection or low battery.
///
/// `false` by default.
@objc public var backgroundTasksEnabled: Bool {
get { sdkConfiguration.backgroundTasksEnabled }
set { sdkConfiguration.backgroundTasksEnabled = newValue }
}
/// Creates a Datadog SDK Configuration object.
///
/// - Parameters:
/// - clientToken: Either the RUM client token (which supports RUM, Logging and APM) or regular client token,
/// only for Logging and APM.
///
/// - env: The environment name which will be sent to Datadog. This can be used
/// To filter events on different environments (e.g. "staging" or "production").
@objc
public init(clientToken: String, env: String) {
sdkConfiguration = .init(clientToken: clientToken, env: env)
}
}