This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
SwiftDatadogFlutterPlugin.swift
378 lines (333 loc) · 12.3 KB
/
SwiftDatadogFlutterPlugin.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import Flutter
import UIKit
import Datadog
import Foundation
public class SwiftDatadogFlutterPlugin: NSObject, FlutterPlugin {
private var loggers: [String: Logger] = [:]
private var traces: [String: OTSpan] = [:]
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "plugins.greenbits.com/datadog_flutter", binaryMessenger: registrar.messenger())
let instance = SwiftDatadogFlutterPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
let args = call.arguments as? [String : Any?]
switch call.method {
case "initWithClientToken":
let rumApplicationId = args?["iosRumApplicationId"] as? String
let config = buildConfiguration(
clientToken: args!["clientToken"] as! String,
environment: args!["environment"] as! String,
serviceName: args!["serviceName"] as! String,
rumApplicationId: rumApplicationId,
useEUEndpoints: args?["useEUEndpoints"] as? NSNumber == 1
)
let trackingConsent = numberToTrackingConsent(args?["trackingConsent"] as? NSNumber)
Datadog.initialize(
appContext: .init(),
trackingConsent: trackingConsent,
configuration: config.build()
)
Datadog.verbosityLevel = .debug
if rumApplicationId != nil {
Global.rum = RUMMonitor.initialize()
}
result(true)
case "loggerLog":
let logLevel = args!["level"] as! String
let logMessage = args!["message"] as! String
let attributes = encodeAttributes(args?["attributes"] as? [AttributeKey : Any])
switch logLevel {
case "debug":
getLogger(args)?.debug(logMessage, attributes: attributes)
case "info":
getLogger(args)?.info(logMessage, attributes: attributes)
case "notice":
getLogger(args)?.notice(logMessage, attributes: attributes)
case "warn":
getLogger(args)?.warn(logMessage, attributes: attributes)
case "error":
getLogger(args)?.error(logMessage, attributes: attributes)
case "critical":
getLogger(args)?.critical(logMessage, attributes: attributes)
default:
return result(false);
}
result(true);
case "loggerAddAttribute":
getLogger(args)?.addAttribute(
forKey: args!["key"] as! String,
value: args!["value"] as! String
)
result(true)
case "loggerAddTag":
getLogger(args)?.addTag(
withKey: args!["key"] as! String,
value: args!["value"] as! String
)
result(true)
case "loggerCreateLogger":
var builder = Logger.builder
if let loggerName = args?["loggerName"] as? String {
builder = builder.set(loggerName: loggerName)
}
loggers[args!["identifier"] as! String] = builder.build()
result(true)
case "loggerRemoveAttribute":
getLogger(args)?.removeAttribute(forKey: args!["key"] as! String)
result(true)
case "loggerRemoveTag":
getLogger(args)?.removeTag(withKey: args!["key"] as! String)
result(true)
case "resourceStartLoading":
guard let key = args?["key"] as? String,
let method = args?["method"] as? String,
let url = args?["url"] as? String,
let rumMethod = RUMMethod.init(rawValue: method) else {
return result(false)
}
let attributes = args?["attributes"] as? Dictionary<String, Encodable>
if attributes?.isEmpty ?? true {
Global.rum.startResourceLoading(resourceKey: key, httpMethod: rumMethod, urlString: url)
} else {
Global.rum.startResourceLoading(
resourceKey: key,
httpMethod: rumMethod,
urlString: url,
attributes: attributes!
)
}
result(true)
case "resourceStopLoading":
guard let key = args?["key"] as? String else {
return result(false)
}
let attributes = args?["attributes"] as? Dictionary<String, Encodable>
if let errorMessage = args?["errorMessage"] as? String {
if attributes?.isEmpty ?? true {
Global.rum.stopResourceLoadingWithError(
resourceKey: key,
errorMessage: errorMessage
)
} else {
Global.rum.stopResourceLoadingWithError(
resourceKey: key,
errorMessage: errorMessage,
attributes: attributes!
)
}
} else {
if let kind = args?["kind"] as? String,
let resourceType = RUMResourceType.init(rawValue: kind) {
if attributes?.isEmpty ?? true {
Global.rum.stopResourceLoading(
resourceKey: key,
statusCode: args?["statusCode"] as? Int,
kind: resourceType
)
} else {
Global.rum.stopResourceLoading(
resourceKey: key,
statusCode: args?["statusCode"] as? Int,
kind: resourceType,
attributes: attributes!
)
}
}
}
result(true)
case "rumAddAttribute":
Global.rum.addAttribute(
forKey: args!["key"] as! String,
value: args!["value"] as! String
)
result(true)
case "rumAddError":
Global.rum.addError(
message: args!["message"] as! String,
source: .source,
stack: args?["stack"] as? String
)
result(true)
case "rumAddTiming":
Global.rum.addTiming(name: args!["name"] as! String)
result(true)
case "rumAddUserAction" :
let type = numberToRumActionType(args?["type"] as? NSNumber)
let attributes = encodeAttributes(args?["attributes"] as? [AttributeKey : Any])
if attributes?.isEmpty ?? true {
Global.rum.addUserAction(
type: type,
name: args!["name"] as! String
)
} else {
Global.rum.addUserAction(
type: type,
name: args!["name"] as! String,
attributes: attributes!
)
}
result(true)
case "rumRemoveAttribute":
Global.rum.removeAttribute(forKey: args!["key"] as! String)
result(true)
case "rumStartUserAction":
let type = numberToRumActionType(args?["type"] as? NSNumber)
let attributes = encodeAttributes(args?["attributes"] as? [AttributeKey : Any])
if attributes?.isEmpty ?? true {
Global.rum.startUserAction(
type: type,
name: args!["name"] as! String
)
} else {
Global.rum.startUserAction(
type: type,
name: args!["name"] as! String,
attributes: attributes!
)
}
result(true)
case "rumStartView":
Global.rum.startView(key: args!["key"] as! String)
result(true)
case "rumStopUserAction":
let type = numberToRumActionType(args?["type"] as? NSNumber)
let attributes = encodeAttributes(args?["attributes"] as? [AttributeKey : Any])
if attributes?.isEmpty ?? true {
Global.rum.stopUserAction(
type: type,
name: args!["name"] as? String
)
} else {
Global.rum.stopUserAction(
type: type,
name: args!["name"] as? String,
attributes: attributes!
)
}
result(true)
case "rumStopView":
Global.rum.stopView(key: args!["key"] as! String)
result(true)
case "setUserInfo":
let attributes = encodeAttributes(args?["attributes"] as? [AttributeKey : Any])
Datadog.setUserInfo(
id: args?["id"] as? String,
name: args?["name"] as? String,
email: args?["email"] as? String,
extraInfo: attributes ?? [AttributeKey : AttributeValue]()
)
result(true)
case "tracingCreateHeadersForRequest":
guard let tracer = Global.sharedTracer as? Tracer else {
return result([String : String]())
}
let writer = HTTPHeadersWriter()
let span = tracer.startSpan(operationName: args!["resourceName"] as! String)
if let method = args?["method"] as? String {
span.setTag(key: "http.method", value: method)
}
if let url = args?["url"] as? String {
span.setTag(key: "http.url", value: url)
}
tracer.inject(spanContext: span.context, writer: writer)
let headers = writer.tracePropagationHTTPHeaders
traces[headers["x-datadog-parent-id"]!] = span
result(writer.tracePropagationHTTPHeaders)
case "tracingFinishSpan":
let spanId = args!["spanId"] as! String
let span = traces[spanId]
if let statusCode = args?["statusCode"] as? NSNumber {
span?.setTag(key: "http.status_code", value: statusCode.intValue)
}
span?.finish()
traces.removeValue(forKey: spanId)
result(true)
case "tracingInitialize":
Global.sharedTracer = Tracer.initialize(
configuration: Tracer.Configuration(
sendNetworkInfo: true
)
)
result(true)
case "updateTrackingConsent":
let trackingConsent = numberToTrackingConsent(args?["trackingConsent"] as? NSNumber)
Datadog.set(trackingConsent: trackingConsent)
result(true)
default:
result(FlutterMethodNotImplemented);
}
}
private func buildConfiguration(clientToken: String, environment: String, serviceName: String, rumApplicationId: String?, useEUEndpoints: Bool) -> Datadog.Configuration.Builder {
var config: Datadog.Configuration.Builder
if rumApplicationId != nil {
config = Datadog.Configuration.builderUsing(
rumApplicationID: rumApplicationId!,
clientToken: clientToken,
environment: environment
)
} else {
config = Datadog.Configuration.builderUsing(
clientToken: clientToken,
environment: environment
)
}
config = config.set(serviceName: serviceName)
if useEUEndpoints {
config = config.set(endpoint: .eu)
}
return config
}
// Flutter sends ints, bools, and doubles as NSNumer
// but NSNumber does not conform to Encodable, so the number must
// be converted back to an Encodable format
private func encodeAttributes(_ attributes: [AttributeKey : Any]?) -> [AttributeKey : AttributeValue]? {
guard var unencodedAttributes = attributes else {
return nil
}
for key in unencodedAttributes.keys {
let value = unencodedAttributes[key]
if value is NSNumber {
// (decimals will be lost with Int so Double is used.
unencodedAttributes[key] = value as? Double
// Since bools are NSNumbers in platform channel, boolean
// attributes are 1 or 0; however, this could be intended as a
// number so documentation will encourage sending the boolean
// as a string and recasting from it
} else if value as? String == "true" {
unencodedAttributes[key] = true
} else if value as? String == "false" {
unencodedAttributes[key] = false
} else if value is String {
unencodedAttributes[key] = value as? String
} else if value is [String : Any] {
unencodedAttributes[key] = encodeAttributes(value as? [AttributeKey : Any])
}
}
return unencodedAttributes as? [AttributeKey : AttributeValue]
}
private func getLogger(_ args: [String : Any?]?) -> Logger? {
guard let identifier = args?["identifier"] as? String else {
return nil
}
return loggers[identifier]
}
private func numberToRumActionType(_ index: NSNumber?) -> RUMUserActionType {
if index == 0 {
return RUMUserActionType.tap
} else if index == 1 {
return RUMUserActionType.scroll
} else if index == 2 {
return RUMUserActionType.swipe
}
return RUMUserActionType.custom
}
private func numberToTrackingConsent(_ index: NSNumber?) -> TrackingConsent {
if index == 0 {
return TrackingConsent.granted
} else if index == 1 {
return TrackingConsent.notGranted
}
return TrackingConsent.pending
}
}