-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1678 from DataDog/nogorodnikov/rum-321/reduce-vie…
…w-events-in-upload-pipeline RUM-321: Introduce view event filtering in upload pipeline, remove view event throttling in write pipeline
- Loading branch information
Showing
28 changed files
with
759 additions
and
555 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...android-rum/src/main/kotlin/com/datadog/android/rum/internal/domain/event/RumEventMeta.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* 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.event | ||
|
||
import com.datadog.android.api.InternalLogger | ||
import com.google.gson.JsonObject | ||
import com.google.gson.JsonParseException | ||
import com.google.gson.JsonParser | ||
import java.util.Locale | ||
import kotlin.jvm.Throws | ||
|
||
internal sealed class RumEventMeta { | ||
|
||
abstract val type: String | ||
|
||
open fun toJson(): JsonObject { | ||
val model = JsonObject() | ||
|
||
model.addProperty(TYPE_KEY, type) | ||
|
||
return model | ||
} | ||
|
||
data class View( | ||
val viewId: String, | ||
val documentVersion: Long | ||
) : RumEventMeta() { | ||
|
||
override val type: String = VIEW_TYPE_VALUE | ||
|
||
override fun toJson(): JsonObject { | ||
val model = super.toJson() | ||
|
||
model.addProperty(VIEW_ID_KEY, viewId) | ||
model.addProperty(DOCUMENT_VERSION_KEY, documentVersion) | ||
|
||
return model | ||
} | ||
} | ||
|
||
companion object { | ||
|
||
private const val UNKNOWN_RUM_EVENT_META_TYPE_ERROR = "Unknown RUM event meta type value [%s]" | ||
private const val UNABLE_TO_PARSE_JSON_INTO_META = "Unable to parse json into RUM event meta" | ||
|
||
const val TYPE_KEY = "type" | ||
const val VIEW_TYPE_VALUE = "view" | ||
const val VIEW_ID_KEY = "viewId" | ||
const val DOCUMENT_VERSION_KEY = "documentVersion" | ||
|
||
@Suppress("ThrowsCount", "ThrowingInternalException") | ||
@Throws(JsonParseException::class) | ||
fun fromJson(jsonString: String, internalLogger: InternalLogger): RumEventMeta? { | ||
return try { | ||
@Suppress("UnsafeThirdPartyFunctionCall") // JsonParseException is handled by the caller | ||
val model = JsonParser.parseString(jsonString).asJsonObject | ||
when (val type = model.get(TYPE_KEY).asString) { | ||
VIEW_TYPE_VALUE -> { | ||
val viewId = model.get(VIEW_ID_KEY).asString | ||
val docVersion = model.get(DOCUMENT_VERSION_KEY).asLong | ||
|
||
View(viewId, docVersion) | ||
} | ||
|
||
else -> { | ||
internalLogger.log( | ||
InternalLogger.Level.ERROR, | ||
InternalLogger.Target.USER, | ||
{ UNKNOWN_RUM_EVENT_META_TYPE_ERROR.format(Locale.US, type) } | ||
) | ||
null | ||
} | ||
} | ||
} catch (@Suppress("TooGenericExceptionCaught") e: NullPointerException) { | ||
throw JsonParseException(UNABLE_TO_PARSE_JSON_INTO_META, e) | ||
} catch (e: ClassCastException) { | ||
throw JsonParseException(UNABLE_TO_PARSE_JSON_INTO_META, e) | ||
} catch (e: IllegalStateException) { | ||
throw JsonParseException(UNABLE_TO_PARSE_JSON_INTO_META, e) | ||
} catch (e: NumberFormatException) { | ||
throw JsonParseException(UNABLE_TO_PARSE_JSON_INTO_META, e) | ||
} | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...src/main/kotlin/com/datadog/android/rum/internal/domain/event/RumEventMetaDeserializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* 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.event | ||
|
||
import com.datadog.android.api.InternalLogger | ||
import com.datadog.android.core.internal.persistence.Deserializer | ||
import com.google.gson.JsonParseException | ||
|
||
internal class RumEventMetaDeserializer( | ||
private val internalLogger: InternalLogger | ||
) : Deserializer<ByteArray, RumEventMeta> { | ||
override fun deserialize(model: ByteArray): RumEventMeta? { | ||
if (model.isEmpty()) return null | ||
|
||
return try { | ||
RumEventMeta.fromJson(String(model, Charsets.UTF_8), internalLogger) | ||
} catch (e: JsonParseException) { | ||
internalLogger.log( | ||
InternalLogger.Level.ERROR, | ||
InternalLogger.Target.USER, | ||
{ DESERIALIZATION_ERROR }, | ||
e | ||
) | ||
null | ||
} | ||
} | ||
|
||
companion object { | ||
const val DESERIALIZATION_ERROR = "Failed to deserialize RUM event meta" | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...m/src/main/kotlin/com/datadog/android/rum/internal/domain/event/RumEventMetaSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* 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.event | ||
|
||
import com.datadog.android.core.persistence.Serializer | ||
|
||
internal class RumEventMetaSerializer : Serializer<RumEventMeta> { | ||
override fun serialize(model: RumEventMeta): String { | ||
return when (model) { | ||
is RumEventMeta.View -> model.toJson().toString() | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...d-rum/src/main/kotlin/com/datadog/android/rum/internal/domain/event/RumViewEventFilter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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.event | ||
|
||
import com.datadog.android.api.storage.RawBatchEvent | ||
import com.datadog.android.core.internal.persistence.Deserializer | ||
import kotlin.math.max | ||
|
||
internal class RumViewEventFilter( | ||
private val eventMetaDeserializer: Deserializer<ByteArray, RumEventMeta> | ||
) { | ||
|
||
fun filterOutRedundantViewEvents(batch: List<RawBatchEvent>): List<RawBatchEvent> { | ||
val maxDocVersionByViewId = mutableMapOf<String, Long>() | ||
val viewMetaByEvent = mutableMapOf<RawBatchEvent, RumEventMeta.View>() | ||
|
||
batch.forEach { | ||
val eventMeta = eventMetaDeserializer.deserialize(it.metadata) | ||
if (eventMeta is RumEventMeta.View) { | ||
viewMetaByEvent += it to eventMeta | ||
val viewId = eventMeta.viewId | ||
val documentVersion = eventMeta.documentVersion | ||
val maxDocVersionSeen = maxDocVersionByViewId[viewId] | ||
if (maxDocVersionSeen == null) { | ||
maxDocVersionByViewId[viewId] = documentVersion | ||
} else { | ||
maxDocVersionByViewId[viewId] = max(documentVersion, maxDocVersionSeen) | ||
} | ||
} | ||
} | ||
|
||
return batch.filter { | ||
if (viewMetaByEvent.containsKey(it)) { | ||
@Suppress("UnsafeThirdPartyFunctionCall") // we checked the key before | ||
val viewMeta = viewMetaByEvent.getValue(it) | ||
// we need to leave only view event with a max doc version for a give viewId in the | ||
// batch, because backend will do the same during the reduce process | ||
@Suppress("UnsafeThirdPartyFunctionCall") // if there is a meta, there is a max doc version | ||
viewMeta.documentVersion == maxDocVersionByViewId.getValue(viewMeta.viewId) | ||
} else { | ||
true | ||
} | ||
} | ||
} | ||
} |
32 changes: 0 additions & 32 deletions
32
...c/main/kotlin/com/datadog/android/rum/internal/domain/scope/DefaultViewUpdatePredicate.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
...-rum/src/main/kotlin/com/datadog/android/rum/internal/domain/scope/ViewUpdatePredicate.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.