Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR] Send replay recording options #4015

Merged
merged 21 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Internal

- Session Replay: Allow overriding `SdkVersion` for replay events ([#4014](https://github.com/getsentry/sentry-java/pull/4014))
- Session Replay: Send replay options as tags ([#4015](https://github.com/getsentry/sentry-java/pull/4015))

## 7.19.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import io.sentry.protocol.SentryId
import io.sentry.rrweb.RRWebBreadcrumbEvent
import io.sentry.rrweb.RRWebEvent
import io.sentry.rrweb.RRWebMetaEvent
import io.sentry.rrweb.RRWebOptionsEvent
import io.sentry.rrweb.RRWebVideoEvent
import java.io.File
import java.util.Date
Expand Down Expand Up @@ -195,6 +196,10 @@ internal interface CaptureStrategy {
}
}

if (segmentId == 0) {
recordingPayload += RRWebOptionsEvent(options)
}

val recording = ReplayRecording().apply {
this.segmentId = segmentId
this.payload = recordingPayload.sortedBy { it.timestamp }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import io.sentry.ScopeCallback
import io.sentry.SentryOptions
import io.sentry.SentryReplayEvent
import io.sentry.SentryReplayEvent.ReplayType
import io.sentry.SentryReplayOptions.SentryReplayQuality.HIGH
import io.sentry.android.replay.BuildConfig
import io.sentry.android.replay.DefaultReplayBreadcrumbConverter
import io.sentry.android.replay.GeneratedVideo
import io.sentry.android.replay.ReplayCache
Expand All @@ -22,9 +24,11 @@ import io.sentry.android.replay.ReplayCache.Companion.SEGMENT_KEY_TIMESTAMP
import io.sentry.android.replay.ReplayCache.Companion.SEGMENT_KEY_WIDTH
import io.sentry.android.replay.ReplayFrame
import io.sentry.android.replay.ScreenshotRecorderConfig
import io.sentry.android.replay.maskAllImages
import io.sentry.protocol.SentryId
import io.sentry.rrweb.RRWebBreadcrumbEvent
import io.sentry.rrweb.RRWebMetaEvent
import io.sentry.rrweb.RRWebOptionsEvent
import io.sentry.transport.CurrentDateProvider
import io.sentry.transport.ICurrentDateProvider
import org.junit.Rule
Expand All @@ -43,8 +47,10 @@ import org.mockito.kotlin.whenever
import java.io.File
import java.util.Date
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNull
import kotlin.test.assertTrue

class SessionCaptureStrategyTest {
Expand Down Expand Up @@ -367,4 +373,81 @@ class SessionCaptureStrategyTest {
"the current replay cache folder is not being deleted."
)
}

@Test
fun `records replay options event for segment 0`() {
romtsn marked this conversation as resolved.
Show resolved Hide resolved
fixture.options.experimental.sessionReplay.sessionSampleRate = 1.0
fixture.options.experimental.sessionReplay.maskAllImages = false
fixture.options.experimental.sessionReplay.quality = HIGH
fixture.options.experimental.sessionReplay.addMaskViewClass("my.custom.View")

val now =
System.currentTimeMillis() + (fixture.options.experimental.sessionReplay.sessionSegmentDuration * 5)
val strategy = fixture.getSut(dateProvider = { now })
strategy.start(fixture.recorderConfig)

strategy.onScreenshotRecorded(mock<Bitmap>()) {}

verify(fixture.hub).captureReplay(
argThat { event ->
event is SentryReplayEvent && event.segmentId == 0
},
check {
val optionsEvent =
it.replayRecording?.payload?.filterIsInstance<RRWebOptionsEvent>()!!
assertEquals("sentry.java", optionsEvent[0].optionsPayload["nativeSdkName"])
assertEquals(BuildConfig.VERSION_NAME, optionsEvent[0].optionsPayload["nativeSdkVersion"])

assertEquals(null, optionsEvent[0].optionsPayload["errorSampleRate"])
assertEquals(1.0, optionsEvent[0].optionsPayload["sessionSampleRate"])
assertEquals(true, optionsEvent[0].optionsPayload["maskAllText"])
assertEquals(false, optionsEvent[0].optionsPayload["maskAllImages"])
assertEquals("high", optionsEvent[0].optionsPayload["quality"])
assertContentEquals(
listOf(
"android.widget.TextView",
"android.webkit.WebView",
"android.widget.VideoView",
"androidx.media3.ui.PlayerView",
"com.google.android.exoplayer2.ui.PlayerView",
"com.google.android.exoplayer2.ui.StyledPlayerView",
"my.custom.View"
),
optionsEvent[0].optionsPayload["maskedViewClasses"] as Collection<*>
)
assertContentEquals(
listOf("android.widget.ImageView"),
optionsEvent[0].optionsPayload["unmaskedViewClasses"] as Collection<*>
)
}
)
}

@Test
fun `does not record replay options event for segment above 0`() {
val now =
System.currentTimeMillis() + (fixture.options.experimental.sessionReplay.sessionSegmentDuration * 5)
val strategy = fixture.getSut(dateProvider = { now })
strategy.start(fixture.recorderConfig)

strategy.onScreenshotRecorded(mock<Bitmap>()) {}
verify(fixture.hub).captureReplay(
argThat { event ->
event is SentryReplayEvent && event.segmentId == 0
},
any()
)

strategy.onScreenshotRecorded(mock<Bitmap>()) {}
verify(fixture.hub).captureReplay(
argThat { event ->
event is SentryReplayEvent && event.segmentId == 1
},
check {
val optionsEvent =
it.replayRecording?.payload?.find { it is RRWebOptionsEvent }
assertNull(optionsEvent)
}
)
}
}
28 changes: 28 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -2760,6 +2760,7 @@ public final class io/sentry/SentryReplayOptions$SentryReplayQuality : java/lang
public static final field MEDIUM Lio/sentry/SentryReplayOptions$SentryReplayQuality;
public final field bitRate I
public final field sizeScale F
public fun serializedName ()Ljava/lang/String;
public static fun valueOf (Ljava/lang/String;)Lio/sentry/SentryReplayOptions$SentryReplayQuality;
public static fun values ()[Lio/sentry/SentryReplayOptions$SentryReplayQuality;
}
Expand Down Expand Up @@ -5415,6 +5416,33 @@ public final class io/sentry/rrweb/RRWebMetaEvent$JsonKeys {
public fun <init> ()V
}

public final class io/sentry/rrweb/RRWebOptionsEvent : io/sentry/rrweb/RRWebEvent, io/sentry/JsonSerializable, io/sentry/JsonUnknown {
public static final field EVENT_TAG Ljava/lang/String;
public fun <init> ()V
public fun <init> (Lio/sentry/SentryOptions;)V
public fun getDataUnknown ()Ljava/util/Map;
public fun getOptionsPayload ()Ljava/util/Map;
public fun getTag ()Ljava/lang/String;
public fun getUnknown ()Ljava/util/Map;
public fun serialize (Lio/sentry/ObjectWriter;Lio/sentry/ILogger;)V
public fun setDataUnknown (Ljava/util/Map;)V
public fun setOptionsPayload (Ljava/util/Map;)V
public fun setTag (Ljava/lang/String;)V
public fun setUnknown (Ljava/util/Map;)V
}

public final class io/sentry/rrweb/RRWebOptionsEvent$Deserializer : io/sentry/JsonDeserializer {
public fun <init> ()V
public fun deserialize (Lio/sentry/ObjectReader;Lio/sentry/ILogger;)Lio/sentry/rrweb/RRWebOptionsEvent;
public synthetic fun deserialize (Lio/sentry/ObjectReader;Lio/sentry/ILogger;)Ljava/lang/Object;
}

public final class io/sentry/rrweb/RRWebOptionsEvent$JsonKeys {
public static final field DATA Ljava/lang/String;
public static final field PAYLOAD Ljava/lang/String;
public fun <init> ()V
}

public final class io/sentry/rrweb/RRWebSpanEvent : io/sentry/rrweb/RRWebEvent, io/sentry/JsonSerializable, io/sentry/JsonUnknown {
public static final field EVENT_TAG Ljava/lang/String;
public fun <init> ()V
Expand Down
5 changes: 5 additions & 0 deletions sentry/src/main/java/io/sentry/SentryReplayOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.sentry.protocol.SdkVersion;
import io.sentry.util.SampleRateUtils;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import org.jetbrains.annotations.ApiStatus;
Expand Down Expand Up @@ -42,6 +43,10 @@ public enum SentryReplayQuality {
this.sizeScale = sizeScale;
this.bitRate = bitRate;
}

public @NotNull String serializedName() {
return name().toLowerCase(Locale.ROOT);
}
}

/**
Expand Down
Loading
Loading