-
Notifications
You must be signed in to change notification settings - Fork 56
/
RumActionScope.kt
257 lines (223 loc) · 8.64 KB
/
RumActionScope.kt
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
/*
* 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 2016-Present Datadog, Inc.
*/
package com.datadog.android.rum.internal.domain.scope
import com.datadog.android.core.internal.CoreFeature
import com.datadog.android.core.internal.persistence.DataWriter
import com.datadog.android.core.internal.utils.devLogger
import com.datadog.android.rum.GlobalRum
import com.datadog.android.rum.RumActionType
import com.datadog.android.rum.internal.domain.RumContext
import com.datadog.android.rum.internal.domain.Time
import com.datadog.android.rum.internal.domain.event.RumEvent
import com.datadog.android.rum.model.ActionEvent
import java.lang.ref.WeakReference
import java.util.UUID
import java.util.concurrent.TimeUnit
import kotlin.math.max
internal class RumActionScope(
val parentScope: RumScope,
val waitForStop: Boolean,
eventTime: Time,
initialType: RumActionType,
initialName: String,
initialAttributes: Map<String, Any?>,
serverTimeOffsetInMs: Long,
inactivityThresholdMs: Long = ACTION_INACTIVITY_MS,
maxDurationMs: Long = ACTION_MAX_DURATION_MS
) : RumScope {
private val inactivityThresholdNs = TimeUnit.MILLISECONDS.toNanos(inactivityThresholdMs)
private val maxDurationNs = TimeUnit.MILLISECONDS.toNanos(maxDurationMs)
internal val eventTimestamp = eventTime.timestamp + serverTimeOffsetInMs
internal val actionId: String = UUID.randomUUID().toString()
internal var type: RumActionType = initialType
internal var name: String = initialName
private val startedNanos: Long = eventTime.nanoTime
private var lastInteractionNanos: Long = startedNanos
internal val attributes: MutableMap<String, Any?> = initialAttributes.toMutableMap().apply {
putAll(GlobalRum.globalAttributes)
}
private val ongoingResourceKeys = mutableListOf<WeakReference<Any>>()
internal var resourceCount: Long = 0
internal var errorCount: Long = 0
internal var crashCount: Long = 0
internal var longTaskCount: Long = 0
internal var viewTreeChangeCount: Int = 0
private var sent = false
private var stopped = false
// endregion
override fun handleEvent(event: RumRawEvent, writer: DataWriter<RumEvent>): RumScope? {
val now = event.eventTime.nanoTime
val isInactive = now - lastInteractionNanos > inactivityThresholdNs
val isLongDuration = now - startedNanos > maxDurationNs
ongoingResourceKeys.removeAll { it.get() == null }
val isOngoing = waitForStop && !stopped
val shouldStop = isInactive && ongoingResourceKeys.isEmpty() && !isOngoing
when {
shouldStop -> sendAction(lastInteractionNanos, writer)
isLongDuration -> sendAction(now, writer)
event is RumRawEvent.SendCustomActionNow -> sendAction(lastInteractionNanos, writer)
event is RumRawEvent.ViewTreeChanged -> onViewTreeChanged(now)
event is RumRawEvent.StopView -> onStopView(now, writer)
event is RumRawEvent.StopAction -> onStopAction(event, now)
event is RumRawEvent.StartResource -> onStartResource(event, now)
event is RumRawEvent.StopResource -> onStopResource(event, now)
event is RumRawEvent.AddError -> onError(event, now, writer)
event is RumRawEvent.StopResourceWithError -> onResourceError(event, now)
event is RumRawEvent.AddLongTask -> onLongTask(now)
}
return if (sent) null else this
}
override fun getRumContext(): RumContext {
return parentScope.getRumContext()
}
// endregion
// region Internal
private fun onViewTreeChanged(now: Long) {
lastInteractionNanos = now
viewTreeChangeCount++
}
private fun onStopView(
now: Long,
writer: DataWriter<RumEvent>
) {
ongoingResourceKeys.clear()
sendAction(now, writer)
}
private fun onStopAction(
event: RumRawEvent.StopAction,
now: Long
) {
event.type?.let { type = it }
event.name?.let { name = it }
attributes.putAll(event.attributes)
stopped = true
lastInteractionNanos = now
}
private fun onStartResource(
event: RumRawEvent.StartResource,
now: Long
) {
lastInteractionNanos = now
resourceCount++
ongoingResourceKeys.add(WeakReference(event.key))
}
private fun onStopResource(
event: RumRawEvent.StopResource,
now: Long
) {
val keyRef = ongoingResourceKeys.firstOrNull { it.get() == event.key }
if (keyRef != null) {
ongoingResourceKeys.remove(keyRef)
lastInteractionNanos = now
}
}
private fun onError(
event: RumRawEvent.AddError,
now: Long,
writer: DataWriter<RumEvent>
) {
lastInteractionNanos = now
errorCount++
if (event.isFatal) {
crashCount++
sendAction(now, writer)
}
}
private fun onResourceError(event: RumRawEvent.StopResourceWithError, now: Long) {
val keyRef = ongoingResourceKeys.firstOrNull { it.get() == event.key }
if (keyRef != null) {
ongoingResourceKeys.remove(keyRef)
lastInteractionNanos = now
resourceCount--
errorCount++
}
}
private fun onLongTask(now: Long) {
lastInteractionNanos = now
longTaskCount++
}
private fun sendAction(
endNanos: Long,
writer: DataWriter<RumEvent>
) {
if (sent) return
val actualType = type
if (actionCanBeSent(actualType)) {
attributes.putAll(GlobalRum.globalAttributes)
val context = getRumContext()
val user = CoreFeature.userInfoProvider.getUserInfo()
val actionEvent = ActionEvent(
date = eventTimestamp,
action = ActionEvent.Action(
type = actualType.toSchemaType(),
id = actionId,
target = ActionEvent.Target(name),
error = ActionEvent.Error(errorCount),
crash = ActionEvent.Crash(crashCount),
longTask = ActionEvent.LongTask(longTaskCount),
resource = ActionEvent.Resource(resourceCount),
loadingTime = max(endNanos - startedNanos, 1L)
),
view = ActionEvent.View(
id = context.viewId.orEmpty(),
name = context.viewName,
url = context.viewUrl.orEmpty()
),
application = ActionEvent.Application(context.applicationId),
session = ActionEvent.ActionEventSession(
id = context.sessionId,
type = ActionEvent.ActionEventSessionType.USER
),
usr = ActionEvent.Usr(
id = user.id,
name = user.name,
email = user.email
),
dd = ActionEvent.Dd(session = ActionEvent.DdSession(plan = ActionEvent.Plan.PLAN_1))
)
val rumEvent = RumEvent(
event = actionEvent,
globalAttributes = attributes,
userExtraAttributes = user.additionalProperties
)
writer.write(rumEvent)
} else {
parentScope.handleEvent(
RumRawEvent.ActionDropped(getRumContext().viewId.orEmpty()),
writer
)
devLogger.i(
"RUM Action $actionId ($actualType on $name) was dropped " +
"(no side effect was registered during its scope)"
)
}
sent = true
}
private fun actionCanBeSent(actualType: RumActionType): Boolean {
val sideEffectsCount = resourceCount + errorCount + viewTreeChangeCount + longTaskCount
return sideEffectsCount > 0 || actualType == RumActionType.CUSTOM
}
// endregion
companion object {
internal const val ACTION_INACTIVITY_MS = 100L
internal const val ACTION_MAX_DURATION_MS = 5000L
fun fromEvent(
parentScope: RumScope,
event: RumRawEvent.StartAction,
timestampOffset: Long
): RumScope {
return RumActionScope(
parentScope,
event.waitForStop,
event.eventTime,
event.type,
event.name,
event.attributes,
timestampOffset
)
}
}
}