Skip to content

Commit

Permalink
RUMM-2353: Report frustration count on views
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnm committed Sep 1, 2022
1 parent 639d58e commit 157e679
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.datadog.android.core.internal.utils.sdkLogger
import com.datadog.android.log.Logger
import com.datadog.android.rum.GlobalRum
import com.datadog.android.rum.internal.monitor.AdvancedRumMonitor
import com.datadog.android.rum.internal.monitor.EventType
import com.datadog.android.rum.internal.monitor.StorageEvent
import com.datadog.android.rum.model.ActionEvent
import com.datadog.android.rum.model.ErrorEvent
import com.datadog.android.rum.model.LongTaskEvent
Expand All @@ -43,18 +43,21 @@ internal class RumDataWriter(
override fun onDataWritten(data: Any, rawData: ByteArray) {
when (data) {
is ViewEvent -> persistViewEvent(rawData)
is ActionEvent -> notifyEventSent(data.view.id, EventType.ACTION)
is ResourceEvent -> notifyEventSent(data.view.id, EventType.RESOURCE)
is ActionEvent -> notifyEventSent(
data.view.id,
StorageEvent.Action(data.action.frustration?.type?.size ?: 0)
)
is ResourceEvent -> notifyEventSent(data.view.id, StorageEvent.Resource)
is ErrorEvent -> {
if (data.error.isCrash != true) {
notifyEventSent(data.view.id, EventType.ERROR)
notifyEventSent(data.view.id, StorageEvent.Error)
}
}
is LongTaskEvent -> {
if (data.longTask.isFrozenFrame == true) {
notifyEventSent(data.view.id, EventType.FROZEN_FRAME)
notifyEventSent(data.view.id, StorageEvent.FrozenFrame)
} else {
notifyEventSent(data.view.id, EventType.LONG_TASK)
notifyEventSent(data.view.id, StorageEvent.LongTask)
}
}
}
Expand All @@ -77,10 +80,10 @@ internal class RumDataWriter(
}
}

private fun notifyEventSent(viewId: String, eventType: EventType) {
private fun notifyEventSent(viewId: String, storageEvent: StorageEvent) {
val rumMonitor = GlobalRum.get()
if (rumMonitor is AdvancedRumMonitor) {
rumMonitor.eventSent(viewId, eventType)
rumMonitor.eventSent(viewId, storageEvent)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.datadog.android.event.EventMapper
import com.datadog.android.log.internal.utils.warningWithTelemetry
import com.datadog.android.rum.GlobalRum
import com.datadog.android.rum.internal.monitor.AdvancedRumMonitor
import com.datadog.android.rum.internal.monitor.EventType
import com.datadog.android.rum.internal.monitor.StorageEvent
import com.datadog.android.rum.model.ActionEvent
import com.datadog.android.rum.model.ErrorEvent
import com.datadog.android.rum.model.LongTaskEvent
Expand Down Expand Up @@ -98,14 +98,17 @@ internal data class RumEventMapper(
private fun notifyEventDropped(event: Any) {
val monitor = (GlobalRum.get() as? AdvancedRumMonitor) ?: return
when (event) {
is ActionEvent -> monitor.eventDropped(event.view.id, EventType.ACTION)
is ResourceEvent -> monitor.eventDropped(event.view.id, EventType.RESOURCE)
is ErrorEvent -> monitor.eventDropped(event.view.id, EventType.ERROR)
is ActionEvent -> monitor.eventDropped(
event.view.id,
StorageEvent.Action(frustrationCount = event.action.frustration?.type?.size ?: 0)
)
is ResourceEvent -> monitor.eventDropped(event.view.id, StorageEvent.Resource)
is ErrorEvent -> monitor.eventDropped(event.view.id, StorageEvent.Error)
is LongTaskEvent -> {
if (event.longTask.isFrozenFrame == true) {
monitor.eventDropped(event.view.id, EventType.FROZEN_FRAME)
monitor.eventDropped(event.view.id, StorageEvent.FrozenFrame)
} else {
monitor.eventDropped(event.view.id, EventType.LONG_TASK)
monitor.eventDropped(event.view.id, StorageEvent.LongTask)
}
}
else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ internal sealed class RumRawEvent {

internal data class ActionSent(
val viewId: String,
val frustrationCount: Int,
override val eventTime: Time = Time()
) : RumRawEvent()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ internal open class RumViewScope(

private var resourceCount: Long = 0
private var actionCount: Long = 0
private var frustrationCount: Int = 0
private var errorCount: Long = 0
private var crashCount: Long = 0
private var longTaskCount: Long = 0
Expand Down Expand Up @@ -490,6 +491,7 @@ internal open class RumViewScope(
if (event.viewId == viewId) {
pendingActionCount--
actionCount++
frustrationCount += event.frustrationCount
sendViewUpdate(event, writer)
}
}
Expand Down Expand Up @@ -585,7 +587,8 @@ internal open class RumViewScope(
memoryMax = memoryInfo?.maxValue,
refreshRateAverage = refreshRateInfo?.meanValue?.let { it * refreshRateScale },
refreshRateMin = refreshRateInfo?.minValue?.let { it * refreshRateScale },
isSlowRendered = isSlowRendered
isSlowRendered = isSlowRendered,
frustration = ViewEvent.Frustration(frustrationCount.toLong())
),
usr = ViewEvent.Usr(
id = user.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ internal interface AdvancedRumMonitor : RumMonitor {
throwable: Throwable
)

fun eventSent(viewId: String, type: EventType)
fun eventSent(viewId: String, event: StorageEvent)

fun eventDropped(viewId: String, type: EventType)
fun eventDropped(viewId: String, event: StorageEvent)

fun setDebugListener(listener: RumDebugListener?)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,27 +298,32 @@ internal class DatadogRumMonitor(
)
}

override fun eventSent(viewId: String, type: EventType) {
when (type) {
EventType.ACTION -> handleEvent(RumRawEvent.ActionSent(viewId))
EventType.RESOURCE -> handleEvent(RumRawEvent.ResourceSent(viewId))
EventType.ERROR -> handleEvent(RumRawEvent.ErrorSent(viewId))
EventType.LONG_TASK -> handleEvent(RumRawEvent.LongTaskSent(viewId, false))
EventType.FROZEN_FRAME -> handleEvent(RumRawEvent.LongTaskSent(viewId, true))
EventType.VIEW -> {
override fun eventSent(viewId: String, event: StorageEvent) {
when (event) {
is StorageEvent.Action -> handleEvent(
RumRawEvent.ActionSent(
viewId,
event.frustrationCount
)
)
is StorageEvent.Resource -> handleEvent(RumRawEvent.ResourceSent(viewId))
is StorageEvent.Error -> handleEvent(RumRawEvent.ErrorSent(viewId))
is StorageEvent.LongTask -> handleEvent(RumRawEvent.LongTaskSent(viewId, false))
is StorageEvent.FrozenFrame -> handleEvent(RumRawEvent.LongTaskSent(viewId, true))
is StorageEvent.View -> {
// Nothing to do
}
}
}

override fun eventDropped(viewId: String, type: EventType) {
when (type) {
EventType.ACTION -> handleEvent(RumRawEvent.ActionDropped(viewId))
EventType.RESOURCE -> handleEvent(RumRawEvent.ResourceDropped(viewId))
EventType.ERROR -> handleEvent(RumRawEvent.ErrorDropped(viewId))
EventType.LONG_TASK -> handleEvent(RumRawEvent.LongTaskDropped(viewId, false))
EventType.FROZEN_FRAME -> handleEvent(RumRawEvent.LongTaskDropped(viewId, true))
EventType.VIEW -> {
override fun eventDropped(viewId: String, event: StorageEvent) {
when (event) {
is StorageEvent.Action -> handleEvent(RumRawEvent.ActionDropped(viewId))
is StorageEvent.Resource -> handleEvent(RumRawEvent.ResourceDropped(viewId))
is StorageEvent.Error -> handleEvent(RumRawEvent.ErrorDropped(viewId))
is StorageEvent.LongTask -> handleEvent(RumRawEvent.LongTaskDropped(viewId, false))
is StorageEvent.FrozenFrame -> handleEvent(RumRawEvent.LongTaskDropped(viewId, true))
is StorageEvent.View -> {
// Nothing to do
}
}
Expand All @@ -333,8 +338,8 @@ internal class DatadogRumMonitor(
}

override fun sendErrorTelemetryEvent(message: String, throwable: Throwable?) {
var stack: String? = throwable?.loggableStackTrace()
var kind: String? = throwable?.javaClass?.canonicalName ?: throwable?.javaClass?.simpleName
val stack: String? = throwable?.loggableStackTrace()
val kind: String? = throwable?.javaClass?.canonicalName ?: throwable?.javaClass?.simpleName
handleEvent(RumRawEvent.SendTelemetry(TelemetryType.ERROR, message, stack, kind))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

package com.datadog.android.rum.internal.monitor

internal enum class EventType {
VIEW,
ACTION,
RESOURCE,
ERROR,
LONG_TASK,
FROZEN_FRAME
internal sealed class StorageEvent {
object View : StorageEvent()
data class Action(val frustrationCount: Int) : StorageEvent()
object Resource : StorageEvent()
object Error : StorageEvent()
object LongTask : StorageEvent()
object FrozenFrame : StorageEvent()
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ internal class ViewEventAssert(actual: ViewEvent) :
return this
}

fun hasFrustrationCount(expected: Long?): ViewEventAssert {
assertThat(actual.view.frustration?.count)
.overridingErrorMessage(
"Expected event data to have view.frustration.count $expected " +
"but was ${actual.view.frustration?.count}"
)
.isEqualTo(expected)
return this
}

fun hasCrashCount(expected: Long?): ViewEventAssert {
assertThat(actual.view.crash?.count)
.overridingErrorMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.datadog.android.core.internal.persistence.file.FileHandler
import com.datadog.android.core.internal.persistence.file.FileOrchestrator
import com.datadog.android.log.Logger
import com.datadog.android.log.internal.logger.LogHandler
import com.datadog.android.rum.internal.monitor.EventType
import com.datadog.android.rum.internal.monitor.StorageEvent
import com.datadog.android.rum.model.ActionEvent
import com.datadog.android.rum.model.ErrorEvent
import com.datadog.android.rum.model.LongTaskEvent
Expand Down Expand Up @@ -165,7 +165,10 @@ internal class RumDataWriterTest {
testedWriter.onDataWritten(actionEvent, fakeSerializedData)

// Then
verify(rumMonitor.mockInstance).eventSent(actionEvent.view.id, EventType.ACTION)
verify(rumMonitor.mockInstance).eventSent(
actionEvent.view.id,
StorageEvent.Action(frustrationCount = actionEvent.action.frustration?.type?.size ?: 0)
)
verifyZeroInteractions(mockFileHandler)
}

Expand All @@ -188,7 +191,7 @@ internal class RumDataWriterTest {
testedWriter.onDataWritten(resourceEvent, fakeSerializedData)

// Then
verify(rumMonitor.mockInstance).eventSent(resourceEvent.view.id, EventType.RESOURCE)
verify(rumMonitor.mockInstance).eventSent(resourceEvent.view.id, StorageEvent.Resource)
verifyZeroInteractions(mockFileHandler)
}

Expand All @@ -214,7 +217,7 @@ internal class RumDataWriterTest {
testedWriter.onDataWritten(errorEvent, fakeSerializedData)

// Then
verify(rumMonitor.mockInstance).eventSent(fakeEvent.view.id, EventType.ERROR)
verify(rumMonitor.mockInstance).eventSent(fakeEvent.view.id, StorageEvent.Error)
verifyZeroInteractions(mockFileHandler)
}

Expand Down Expand Up @@ -261,7 +264,7 @@ internal class RumDataWriterTest {
testedWriter.onDataWritten(longTaskEvent, fakeSerializedData)

// Then
verify(rumMonitor.mockInstance).eventSent(longTaskEvent.view.id, EventType.LONG_TASK)
verify(rumMonitor.mockInstance).eventSent(longTaskEvent.view.id, StorageEvent.LongTask)
verifyZeroInteractions(mockFileHandler)
}

Expand All @@ -282,7 +285,10 @@ internal class RumDataWriterTest {
testedWriter.onDataWritten(frozenFrameEvent, fakeSerializedData)

// Then
verify(rumMonitor.mockInstance).eventSent(frozenFrameEvent.view.id, EventType.FROZEN_FRAME)
verify(rumMonitor.mockInstance).eventSent(
frozenFrameEvent.view.id,
StorageEvent.FrozenFrame
)
verifyZeroInteractions(mockFileHandler)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package com.datadog.android.rum.internal.domain.event
import android.util.Log
import com.datadog.android.event.EventMapper
import com.datadog.android.log.internal.utils.WARN_WITH_TELEMETRY_LEVEL
import com.datadog.android.rum.internal.monitor.EventType
import com.datadog.android.rum.internal.monitor.StorageEvent
import com.datadog.android.rum.model.ActionEvent
import com.datadog.android.rum.model.ErrorEvent
import com.datadog.android.rum.model.LongTaskEvent
Expand Down Expand Up @@ -436,7 +436,13 @@ internal class RumEventMapperTest {

// THEN
assertThat(mappedRumEvent).isNull()
verify(rumMonitor.mockInstance).eventDropped(fakeRumEvent.view.id, EventType.ACTION)
verify(rumMonitor.mockInstance)
.eventDropped(
fakeRumEvent.view.id,
StorageEvent.Action(
frustrationCount = fakeRumEvent.action.frustration?.type?.size ?: 0
)
)
}

@Test
Expand All @@ -451,7 +457,7 @@ internal class RumEventMapperTest {

// THEN
assertThat(mappedRumEvent).isNull()
verify(rumMonitor.mockInstance).eventDropped(fakeRumEvent.view.id, EventType.RESOURCE)
verify(rumMonitor.mockInstance).eventDropped(fakeRumEvent.view.id, StorageEvent.Resource)
}

@Test
Expand All @@ -469,7 +475,7 @@ internal class RumEventMapperTest {

// THEN
assertThat(mappedRumEvent).isNull()
verify(rumMonitor.mockInstance).eventDropped(fakeNoCrashEvent.view.id, EventType.ERROR)
verify(rumMonitor.mockInstance).eventDropped(fakeNoCrashEvent.view.id, StorageEvent.Error)
}

@Test
Expand All @@ -490,7 +496,7 @@ internal class RumEventMapperTest {

// THEN
assertThat(mappedRumEvent).isNull()
verify(rumMonitor.mockInstance).eventDropped(longTaskEvent.view.id, EventType.LONG_TASK)
verify(rumMonitor.mockInstance).eventDropped(longTaskEvent.view.id, StorageEvent.LongTask)
}

@Test
Expand All @@ -511,7 +517,10 @@ internal class RumEventMapperTest {

// THEN
assertThat(mappedRumEvent).isNull()
verify(rumMonitor.mockInstance).eventDropped(longTaskEvent.view.id, EventType.FROZEN_FRAME)
verify(rumMonitor.mockInstance).eventDropped(
longTaskEvent.view.id,
StorageEvent.FrozenFrame
)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ internal fun Forge.silentOrphanEvent(): RumRawEvent {
RumRawEvent.ResetSession(),
RumRawEvent.KeepAlive(),
RumRawEvent.StopView(anAlphabeticalString(), emptyMap()),
RumRawEvent.ActionSent(fakeId),
RumRawEvent.ActionSent(fakeId, aPositiveInt()),
RumRawEvent.ErrorSent(fakeId),
RumRawEvent.LongTaskSent(fakeId),
RumRawEvent.ResourceSent(fakeId),
Expand Down
Loading

0 comments on commit 157e679

Please sign in to comment.