forked from DataDog/dd-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRUMCommand.swift
555 lines (488 loc) · 21.3 KB
/
RUMCommand.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*
* 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
/// Command processed through the tree of `RUMScopes`.
internal protocol RUMCommand {
/// The time of command issue.
var time: Date { set get }
/// Global attributes when the command was issued.
var globalAttributes: [AttributeKey: AttributeValue] { set get }
/// Attributes associated with the command.
var attributes: [AttributeKey: AttributeValue] { set get }
/// Whether or not receiving this command should start the "Background" view if no view is active
/// and ``RUM.Configuration.trackBackgroundEvents`` is enabled.
var canStartBackgroundView: Bool { get }
/// Whether or not this command is considered a user interaction
var isUserInteraction: Bool { get }
/// A type of event missed upon receiving this command in case of absence of an active view; `nil` if none or N/A.
var missedEventType: SessionEndedMetric.MissedEventType? { get }
}
internal struct RUMSDKInitCommand: RUMCommand {
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue] = [:]
var canStartBackgroundView = false
var isUserInteraction = false
let missedEventType: SessionEndedMetric.MissedEventType? = nil
}
internal struct RUMApplicationStartCommand: RUMCommand {
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue]
var canStartBackgroundView = false
var isUserInteraction = false
let missedEventType: SessionEndedMetric.MissedEventType? = nil
}
internal struct RUMStopSessionCommand: RUMCommand {
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue] = [:]
let canStartBackgroundView = false // no, stopping a session should not start a backgorund session
let isUserInteraction = false
let missedEventType: SessionEndedMetric.MissedEventType? = nil
init(time: Date) {
self.time = time
}
}
// MARK: - RUM View related commands
internal struct RUMStartViewCommand: RUMCommand, RUMViewScopePropagatableAttributes {
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue]
let canStartBackgroundView = false // no, it should start its own view, not the "Background"
let isUserInteraction = true // a new View means there was a navigation, it's considered a User interaction
/// The value holding stable identity of the RUM View.
let identity: ViewIdentifier
/// The name of this View, rendered in RUM Explorer as `VIEW NAME`.
let name: String
/// The path of this View, rendered in RUM Explorer as `VIEW URL`.
let path: String
/// The type of instrumentation that started this view.
let instrumentationType: SessionEndedMetric.ViewInstrumentationType
let missedEventType: SessionEndedMetric.MissedEventType? = nil
init(
time: Date,
identity: ViewIdentifier,
name: String,
path: String,
attributes: [AttributeKey: AttributeValue],
instrumentationType: SessionEndedMetric.ViewInstrumentationType
) {
self.time = time
self.attributes = attributes
self.identity = identity
self.name = name
self.path = path
self.instrumentationType = instrumentationType
}
}
internal struct RUMStopViewCommand: RUMCommand, RUMViewScopePropagatableAttributes {
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue]
let canStartBackgroundView = false // no, we don't expect receiving it without an active view
let isUserInteraction = false // a view can be stopped and in most cases should not be considered an interaction (if it's stopped because the user navigate inside the same app, the startView will happen shortly after this)
/// The value holding stable identity of the RUM View.
let identity: ViewIdentifier
let missedEventType: SessionEndedMetric.MissedEventType? = nil
}
/// Any error command, like exception or App Hang.
internal protocol RUMErrorCommand: RUMCommand {
/// The error message.
var message: String { get }
/// Error type.
var type: String? { get }
/// Error stacktrace.
var stack: String? { get }
/// Mobile-specific category of the error to empower high-level grouping of different types of errors.
var category: RUMErrorCategory { get }
/// Whether this error has crashed the host application
var isCrash: Bool? { get }
/// The origin of this error.
var source: RUMInternalErrorSource { get }
/// The platform type of the error (iOS, React Native, ...)
var errorSourceType: RUMErrorEvent.Error.SourceType { get }
/// An information about the threads currently running in the process.
var threads: [DDThread]? { get }
/// The list of binary images referenced from `stack` and `threads`.
var binaryImages: [BinaryImage]? { get }
/// Indicates whether any stack trace information in `stack` or `threads` was truncated due to stack trace minimization.
var isStackTraceTruncated: Bool? { get }
}
/// Adds exception error to current view.
///
/// Using this command results with classifying the error as "Exception" in Datadog app (`@error.category: Exception`).
internal struct RUMAddCurrentViewErrorCommand: RUMErrorCommand {
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue]
let canStartBackgroundView = true // yes, we want to track errors in "Background" view
let isUserInteraction = false // an error is not an interactive event
let message: String
let type: String?
let stack: String?
let category: RUMErrorCategory = .exception
let isCrash: Bool?
let source: RUMInternalErrorSource
let errorSourceType: RUMErrorEvent.Error.SourceType
let threads: [DDThread]?
let binaryImages: [BinaryImage]?
let isStackTraceTruncated: Bool?
let missedEventType: SessionEndedMetric.MissedEventType? = .error
/// Constructor dedicated to errors defined by message, type and stack.
init(
time: Date,
message: String,
type: String?,
stack: String?,
source: RUMInternalErrorSource,
attributes: [AttributeKey: AttributeValue]
) {
self.init(
time: time,
message: message,
type: type,
stack: stack,
source: source,
isCrash: nil,
threads: nil,
binaryImages: nil,
isStackTraceTruncated: nil,
attributes: attributes
)
}
/// Constructor dedicated to errors defined by `Error` object.
init(
time: Date,
error: Error,
source: RUMInternalErrorSource,
attributes: [AttributeKey: AttributeValue]
) {
let dderror = DDError(error: error)
self.init(
time: time,
message: dderror.message,
type: dderror.type,
stack: dderror.stack,
source: source,
isCrash: nil,
threads: nil,
binaryImages: nil,
isStackTraceTruncated: nil,
attributes: attributes
)
}
/// Broad constructor for all kinds of errors.
init(
time: Date,
message: String,
type: String?,
stack: String?,
source: RUMInternalErrorSource,
isCrash: Bool?,
threads: [DDThread]?,
binaryImages: [BinaryImage]?,
isStackTraceTruncated: Bool?,
attributes: [AttributeKey: AttributeValue]
) {
var attributes = attributes
let isCrossPlatformCrash: Bool? = attributes.removeValue(forKey: CrossPlatformAttributes.errorIsCrash)?.dd.decode()
let crossPlatformSourceType = RUMErrorSourceType.extract(from: &attributes)
self.time = time
self.attributes = attributes
self.message = message
self.type = type
self.stack = stack
self.isCrash = isCrossPlatformCrash ?? isCrash
self.source = source
self.errorSourceType = crossPlatformSourceType ?? .ios
self.threads = threads
self.binaryImages = binaryImages
self.isStackTraceTruncated = isStackTraceTruncated
}
}
/// Adds App Hang error to current view.
///
/// Using this command results with classifying the error as "App Hang" in Datadog app (`@error.category: App Hang`).
internal struct RUMAddCurrentViewAppHangCommand: RUMErrorCommand {
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue]
let canStartBackgroundView = false // no, we don't want to track App Hangs in "Background" view
let isUserInteraction = false // an error is not an interactive event
let message: String
let type: String?
let stack: String?
let category: RUMErrorCategory = .appHang
let isCrash: Bool? = false
let source: RUMInternalErrorSource = .source
let errorSourceType: RUMErrorEvent.Error.SourceType = .ios
let threads: [DDThread]?
let binaryImages: [BinaryImage]?
let isStackTraceTruncated: Bool?
/// The duration of hang.
let hangDuration: TimeInterval
let missedEventType: SessionEndedMetric.MissedEventType? = .error
}
internal struct RUMAddCurrentViewMemoryWarningCommand: RUMErrorCommand {
var time: Date
var globalAttributes: [AttributeKey: AttributeValue]
var attributes: [AttributeKey: AttributeValue]
let canStartBackgroundView = false
let isUserInteraction = false
let message: String
let type: String?
let stack: String?
let category: RUMErrorCategory = .memoryWarning
let isCrash: Bool? = false
let source: RUMInternalErrorSource = .source
let errorSourceType: RUMErrorEvent.Error.SourceType = .ios
let threads: [DDThread]?
let binaryImages: [BinaryImage]?
let isStackTraceTruncated: Bool?
let missedEventType: SessionEndedMetric.MissedEventType? = .error
}
internal struct RUMAddViewLoadingTime: RUMCommand, RUMViewScopePropagatableAttributes {
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue]
let canStartBackgroundView = false // no, it doesn't make sense to start "Background" view on receiving custom timing, as it will be `0ns` timing
let isUserInteraction = false // a custom view timing is not an interactive event
let missedEventType: SessionEndedMetric.MissedEventType? = .viewLoadingTime
let overwrite: Bool
}
internal struct RUMAddViewTimingCommand: RUMCommand, RUMViewScopePropagatableAttributes {
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue]
let canStartBackgroundView = false // no, it doesn't make sense to start "Background" view on receiving custom timing, as it will be `0ns` timing
let isUserInteraction = false // a custom view timing is not an interactive event
/// The name of the timing. It will be used as a JSON key, whereas the value will be the timing duration,
/// measured since the start of the View.
let timingName: String
let missedEventType: SessionEndedMetric.MissedEventType? = nil
}
// MARK: - RUM Resource related commands
internal protocol RUMResourceCommand: RUMCommand {
/// The key identifying the RUM Resource.
var resourceKey: String { get }
}
/// Tracing information propagated by Tracing to the underlying `URLRequest`. It is passed to the RUM backend
/// in order to create the APM span. The actual `Span` is not send by the SDK.
internal struct RUMSpanContext {
/// The trace ID injected to `URLRequest` that issues RUM resource.
let traceID: TraceID
/// The span ID injected to `URLRequest` that issues RUM resource.
let spanID: SpanID
/// The sampling rate applied to the trace (a value between `0.0` and `1.0`).
let samplingRate: Double
}
internal struct RUMStartResourceCommand: RUMResourceCommand {
let resourceKey: String
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue]
let canStartBackgroundView = true // yes, we want to track resources in "Background" view
let isUserInteraction = false // a resource is not an interactive event
/// Resource url
let url: String
/// HTTP method used to load the Resource
let httpMethod: RUMMethod
/// A type of the Resource if it's possible to determine on start (when the response MIME is not yet known).
let kind: RUMResourceType?
/// Span context passed to the RUM backend in order to generate the APM span for underlying resource.
let spanContext: RUMSpanContext?
let missedEventType: SessionEndedMetric.MissedEventType? = .resource
}
internal struct RUMAddResourceMetricsCommand: RUMResourceCommand {
let resourceKey: String
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue]
let canStartBackgroundView = false // no, we don't expect receiving it without an active view (started earlier on `RUMStartResourceCommand`)
let isUserInteraction = false // an error is not an interactive event
/// Resource metrics.
let metrics: ResourceMetrics
let missedEventType: SessionEndedMetric.MissedEventType? = nil
}
internal struct RUMStopResourceCommand: RUMResourceCommand {
let resourceKey: String
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue]
let canStartBackgroundView = false // no, we don't expect receiving it without an active view (started earlier on `RUMStartResourceCommand`)
let isUserInteraction = false // a resource is not an interactive event
/// A type of the Resource
let kind: RUMResourceType
/// HTTP status code of loading the Ressource
let httpStatusCode: Int?
/// The size of loaded Resource
let size: Int64?
let missedEventType: SessionEndedMetric.MissedEventType? = nil
}
internal struct RUMStopResourceWithErrorCommand: RUMResourceCommand {
let resourceKey: String
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue]
let canStartBackgroundView = false // no, we don't expect receiving it without an active view (started earlier on `RUMStartResourceCommand`)
let isUserInteraction = false // a resource is not an interactive event
/// The error message.
let errorMessage: String
/// Error type.
let errorType: String?
/// The origin of the error (network, webview, ...)
let errorSource: RUMInternalErrorSource
/// The platform type of the error (iOS, React Native, ...)
let errorSourceType: RUMErrorEvent.Error.SourceType
/// Error stacktrace.
let stack: String?
/// HTTP status code of the Ressource error.
let httpStatusCode: Int?
let missedEventType: SessionEndedMetric.MissedEventType? = .error
init(
resourceKey: String,
time: Date,
message: String,
type: String?,
source: RUMInternalErrorSource,
httpStatusCode: Int?,
attributes: [AttributeKey: AttributeValue]
) {
self.resourceKey = resourceKey
self.time = time
self.errorMessage = message
self.errorType = type
self.errorSource = source
self.attributes = attributes
self.httpStatusCode = httpStatusCode
// The stack will be meaningless in most cases as it will go down to the networking code:
self.stack = nil
self.errorSourceType = RUMErrorSourceType.extract(from: &self.attributes) ?? .ios
}
init(
resourceKey: String,
time: Date,
error: Error,
source: RUMInternalErrorSource,
httpStatusCode: Int?,
attributes: [AttributeKey: AttributeValue]
) {
self.resourceKey = resourceKey
self.time = time
self.errorSource = source
self.attributes = attributes
self.httpStatusCode = httpStatusCode
let dderror = DDError(error: error)
self.errorMessage = dderror.message
self.errorType = dderror.type
// The stack will give the networking error (`NSError`) description in most cases:
self.stack = dderror.stack
self.errorSourceType = RUMErrorSourceType.extract(from: &self.attributes) ?? .ios
}
}
// MARK: - RUM User Action related commands
internal protocol RUMUserActionCommand: RUMCommand {
/// The action identifying the RUM User Action.
var actionType: RUMActionType { get }
}
/// Starts continuous User Action.
internal struct RUMStartUserActionCommand: RUMUserActionCommand {
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue]
let canStartBackgroundView = true // yes, we want to track actions in "Background" view (e.g. it makes sense for custom actions)
let isUserInteraction = true // a user action definitely is a User Interacgion
/// The type of instrumentation used to create this command.
let instrumentation: InstrumentationType
let actionType: RUMActionType
let name: String
let missedEventType: SessionEndedMetric.MissedEventType? = .action
}
/// Stops continuous User Action.
internal struct RUMStopUserActionCommand: RUMUserActionCommand {
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue]
let canStartBackgroundView = false // no, we don't expect receiving it without an active view (started earlier on `RUMStartUserActionCommand`)
let isUserInteraction = true // a user action definitely is a User Interacgion
let actionType: RUMActionType
let name: String?
let missedEventType: SessionEndedMetric.MissedEventType? = nil
}
/// Adds discrete (discontinuous) User Action.
internal struct RUMAddUserActionCommand: RUMUserActionCommand {
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue]
let canStartBackgroundView = true // yes, we want to track actions in "Background" view (e.g. it makes sense for custom actions)
let isUserInteraction = true // a user action definitely is a User Interacgion
/// The type of instrumentation used to create this command.
let instrumentation: InstrumentationType
let actionType: RUMActionType
let name: String
let missedEventType: SessionEndedMetric.MissedEventType? = .action
}
/// Adds that a feature flag has been evaluated to the view
internal struct RUMAddFeatureFlagEvaluationCommand: RUMCommand {
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue]
let canStartBackgroundView = true // yes, we don't want to miss evaluation of flags that may affect background tasks
let isUserInteraction = false
let name: String
let value: Encodable
let missedEventType: SessionEndedMetric.MissedEventType? = nil
init(time: Date, name: String, value: Encodable) {
self.time = time
self.globalAttributes = [:]
self.attributes = [:]
self.name = name
self.value = value
}
}
// MARK: - RUM Long Task related commands
internal struct RUMAddLongTaskCommand: RUMCommand {
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue]
let canStartBackgroundView = false // no, we don't expect receiving long tasks in "Background" view
let isUserInteraction = false // a long task is not an interactive event
let duration: TimeInterval
let missedEventType: SessionEndedMetric.MissedEventType? = .longTask
}
// MARK: - RUM Web Events related commands
/// RUM Events received from WebView should keep the active session alive, therefore they fire this command to do so. (ref: RUMM-1793)
internal struct RUMKeepSessionAliveCommand: RUMCommand {
let canStartBackgroundView = false
let isUserInteraction = false
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue]
let missedEventType: SessionEndedMetric.MissedEventType? = nil
}
// MARK: - Cross-platform attributes
internal struct RUMUpdatePerformanceMetric: RUMCommand {
let canStartBackgroundView = false
let isUserInteraction = false
let metric: PerformanceMetric
let value: Double
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue]
let missedEventType: SessionEndedMetric.MissedEventType? = nil
}
internal struct RUMSetInternalViewAttributeCommand: RUMCommand {
let canStartBackgroundView = false
let isUserInteraction = false
var time: Date
var globalAttributes: [AttributeKey: AttributeValue] = [:]
var attributes: [AttributeKey: AttributeValue] = [:]
let missedEventType: SessionEndedMetric.MissedEventType? = nil
let key: AttributeKey
let value: AttributeValue
}