-
Notifications
You must be signed in to change notification settings - Fork 113
/
RUMFeature.swift
181 lines (165 loc) · 7.48 KB
/
RUMFeature.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
/*
* 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-2020 Datadog, Inc.
*/
import Foundation
import DatadogInternal
internal final class RUMFeature: DatadogRemoteFeature {
static let name = "rum"
let requestBuilder: FeatureRequestBuilder
let messageReceiver: FeatureMessageReceiver
let monitor: Monitor
let instrumentation: RUMInstrumentation
let configuration: RUM.Configuration
init(
in core: DatadogCoreProtocol,
configuration: RUM.Configuration
) throws {
self.configuration = configuration
let eventsMapper = RUMEventsMapper(
viewEventMapper: configuration.viewEventMapper,
errorEventMapper: configuration.errorEventMapper,
resourceEventMapper: configuration.resourceEventMapper,
actionEventMapper: configuration.actionEventMapper,
longTaskEventMapper: configuration.longTaskEventMapper,
telemetry: core.telemetry
)
let featureScope = core.scope(for: RUMFeature.self)
let dependencies = RUMScopeDependencies(
featureScope: featureScope,
rumApplicationID: configuration.applicationID,
sessionSampler: Sampler(samplingRate: configuration.debugSDK ? 100 : configuration.sessionSampleRate),
trackBackgroundEvents: configuration.trackBackgroundEvents,
trackFrustrations: configuration.trackFrustrations,
firstPartyHosts: {
switch configuration.urlSessionTracking?.firstPartyHostsTracing {
case let .trace(hosts, _):
return FirstPartyHosts(hosts)
case let .traceWithHeaders(hostsWithHeaders, _):
return FirstPartyHosts(hostsWithHeaders)
case .none:
return nil
}
}(),
eventBuilder: RUMEventBuilder(
eventsMapper: eventsMapper
),
rumUUIDGenerator: configuration.uuidGenerator,
ciTest: configuration.ciTestExecutionID.map { RUMCITest(testExecutionId: $0) },
syntheticsTest: {
if let testId = configuration.syntheticsTestId, let resultId = configuration.syntheticsResultId {
return RUMSyntheticsTest(injected: nil, resultId: resultId, testId: testId)
} else {
return nil
}
}(),
vitalsReaders: configuration.vitalsUpdateFrequency.map {
VitalsReaders(
frequency: $0.timeInterval,
telemetry: core.telemetry
)
},
onSessionStart: configuration.onSessionStart,
viewCache: ViewCache()
)
self.monitor = Monitor(
dependencies: dependencies,
dateProvider: configuration.dateProvider
)
self.instrumentation = RUMInstrumentation(
uiKitRUMViewsPredicate: configuration.uiKitViewsPredicate,
uiKitRUMActionsPredicate: configuration.uiKitActionsPredicate,
longTaskThreshold: configuration.longTaskThreshold,
appHangThreshold: configuration.appHangThreshold,
mainQueue: configuration.mainQueue,
dateProvider: configuration.dateProvider,
backtraceReporter: core.backtraceReporter,
telemetry: core.telemetry
)
self.requestBuilder = RequestBuilder(
customIntakeURL: configuration.customEndpoint,
eventsFilter: RUMViewEventsFilter(),
telemetry: core.telemetry
)
self.messageReceiver = CombinedFeatureMessageReceiver(
TelemetryReceiver(
featureScope: featureScope,
dateProvider: configuration.dateProvider,
sampler: Sampler(samplingRate: configuration.telemetrySampleRate),
configurationExtraSampler: Sampler(samplingRate: configuration.configurationTelemetrySampleRate),
metricsExtraSampler: Sampler(samplingRate: configuration.metricsTelemetrySampleRate)
),
ErrorMessageReceiver(
featureScope: featureScope,
monitor: monitor
),
WebViewEventReceiver(
featureScope: featureScope,
dateProvider: configuration.dateProvider,
commandSubscriber: monitor,
viewCache: dependencies.viewCache
),
CrashReportReceiver(
featureScope: featureScope,
applicationID: configuration.applicationID,
dateProvider: configuration.dateProvider,
sessionSampler: Sampler(samplingRate: configuration.debugSDK ? 100 : configuration.sessionSampleRate),
trackBackgroundEvents: configuration.trackBackgroundEvents,
uuidGenerator: configuration.uuidGenerator,
ciTest: configuration.ciTestExecutionID.map { RUMCITest(testExecutionId: $0) },
syntheticsTest: {
if let testId = configuration.syntheticsTestId, let resultId = configuration.syntheticsResultId {
return RUMSyntheticsTest(injected: nil, resultId: resultId, testId: testId)
} else {
return nil
}
}(),
eventsMapper: eventsMapper
)
)
// Forward instrumentation calls to monitor:
instrumentation.publish(to: monitor)
// Send configuration telemetry:
core.telemetry.configuration(
appHangThreshold: configuration.appHangThreshold?.toInt64Milliseconds,
mobileVitalsUpdatePeriod: configuration.vitalsUpdateFrequency?.timeInterval.toInt64Milliseconds,
sessionSampleRate: Int64(withNoOverflow: configuration.sessionSampleRate),
telemetrySampleRate: Int64(withNoOverflow: configuration.telemetrySampleRate),
traceSampleRate: configuration.urlSessionTracking?.firstPartyHostsTracing.map { Int64(withNoOverflow: $0.sampleRate) },
trackBackgroundEvents: configuration.trackBackgroundEvents,
trackFrustrations: configuration.trackFrustrations,
trackInteractions: configuration.uiKitActionsPredicate != nil,
trackLongTask: configuration.longTaskThreshold != nil,
trackNativeLongTasks: configuration.longTaskThreshold != nil,
trackNativeViews: configuration.uiKitViewsPredicate != nil,
trackNetworkRequests: configuration.urlSessionTracking != nil,
useFirstPartyHosts: configuration.urlSessionTracking?.firstPartyHostsTracing != nil
)
}
}
extension RUMFeature: Flushable {
/// Awaits completion of all asynchronous operations.
///
/// **blocks the caller thread**
func flush() {
monitor.flush()
}
}
private extension RUM.Configuration.URLSessionTracking.FirstPartyHostsTracing {
var sampleRate: Float {
switch self {
case .trace(_, let sampleRate): return sampleRate
case .traceWithHeaders(_, let sampleRate): return sampleRate
}
}
}
private extension RUM.Configuration.VitalsFrequency {
var timeInterval: TimeInterval {
switch self {
case .frequent: return 0.1
case .average: return 0.5
case .rare: return 1
}
}
}