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 16 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

- Fix warm start detection ([#3937](https://github.com/getsentry/sentry-java/pull/3937))

### 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

### Fixes
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 Down Expand Up @@ -367,4 +371,39 @@ 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"])
assertEquals("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"])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIU from your definition of optionsPayload value, we could add a list here instead of a concatenated string. Would it make sense to do this by default so we can potentially have a better UI for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah just tried it and it looks fine! I will ask about the frontend change
Google Chrome 2024-12-30 23 07 44

assertEquals("android.widget.ImageView", optionsEvent[0].optionsPayload["unmaskedViewClasses"])
}
)
}
}
37 changes: 34 additions & 3 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public abstract interface class io/sentry/EventProcessor {
}

public final class io/sentry/ExperimentalOptions {
public fun <init> (Z)V
public fun <init> (ZLio/sentry/protocol/SdkVersion;)V
public fun getSessionReplay ()Lio/sentry/SentryReplayOptions;
public fun setSessionReplay (Lio/sentry/SentryReplayOptions;)V
}
Expand Down Expand Up @@ -2724,8 +2724,8 @@ public final class io/sentry/SentryReplayOptions {
public static final field TEXT_VIEW_CLASS_NAME Ljava/lang/String;
public static final field VIDEO_VIEW_CLASS_NAME Ljava/lang/String;
public static final field WEB_VIEW_CLASS_NAME Ljava/lang/String;
public fun <init> (Ljava/lang/Double;Ljava/lang/Double;)V
public fun <init> (Z)V
public fun <init> (Ljava/lang/Double;Ljava/lang/Double;Lio/sentry/protocol/SdkVersion;)V
public fun <init> (ZLio/sentry/protocol/SdkVersion;)V
public fun addMaskViewClass (Ljava/lang/String;)V
public fun addUnmaskViewClass (Ljava/lang/String;)V
public fun getErrorReplayDuration ()J
Expand All @@ -2734,6 +2734,7 @@ public final class io/sentry/SentryReplayOptions {
public fun getMaskViewContainerClass ()Ljava/lang/String;
public fun getOnErrorSampleRate ()Ljava/lang/Double;
public fun getQuality ()Lio/sentry/SentryReplayOptions$SentryReplayQuality;
public fun getSdkVersion ()Lio/sentry/protocol/SdkVersion;
public fun getSessionDuration ()J
public fun getSessionSampleRate ()Ljava/lang/Double;
public fun getSessionSegmentDuration ()J
Expand All @@ -2747,6 +2748,7 @@ public final class io/sentry/SentryReplayOptions {
public fun setMaskViewContainerClass (Ljava/lang/String;)V
public fun setOnErrorSampleRate (Ljava/lang/Double;)V
public fun setQuality (Lio/sentry/SentryReplayOptions$SentryReplayQuality;)V
public fun setSdkVersion (Lio/sentry/protocol/SdkVersion;)V
public fun setSessionSampleRate (Ljava/lang/Double;)V
public fun setTrackOrientationChange (Z)V
public fun setUnmaskViewContainerClass (Ljava/lang/String;)V
Expand All @@ -2758,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 @@ -5413,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 Expand Up @@ -5635,6 +5665,7 @@ public final class io/sentry/util/CollectionUtils {
public static fun contains ([Ljava/lang/Object;Ljava/lang/Object;)Z
public static fun filterListEntries (Ljava/util/List;Lio/sentry/util/CollectionUtils$Predicate;)Ljava/util/List;
public static fun filterMapEntries (Ljava/util/Map;Lio/sentry/util/CollectionUtils$Predicate;)Ljava/util/Map;
public static fun joinToString (Ljava/util/Set;Ljava/lang/String;)Ljava/lang/String;
public static fun map (Ljava/util/List;Lio/sentry/util/CollectionUtils$Mapper;)Ljava/util/List;
public static fun newArrayList (Ljava/util/List;)Ljava/util/List;
public static fun newConcurrentHashMap (Ljava/util/Map;)Ljava/util/Map;
Expand Down
6 changes: 4 additions & 2 deletions sentry/src/main/java/io/sentry/ExperimentalOptions.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.sentry;

import io.sentry.protocol.SdkVersion;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Experimental options for new features, these options are going to be promoted to SentryOptions
Expand All @@ -11,8 +13,8 @@
public final class ExperimentalOptions {
private @NotNull SentryReplayOptions sessionReplay;

public ExperimentalOptions(final boolean empty) {
this.sessionReplay = new SentryReplayOptions(empty);
public ExperimentalOptions(final boolean empty, final @Nullable SdkVersion sdkVersion) {
this.sessionReplay = new SentryReplayOptions(empty, sdkVersion);
}

@NotNull
Expand Down
7 changes: 7 additions & 0 deletions sentry/src/main/java/io/sentry/MainEventProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.sentry.hints.Cached;
import io.sentry.protocol.DebugImage;
import io.sentry.protocol.DebugMeta;
import io.sentry.protocol.SdkVersion;
import io.sentry.protocol.SentryException;
import io.sentry.protocol.SentryTransaction;
import io.sentry.protocol.User;
Expand Down Expand Up @@ -159,6 +160,12 @@ private void processNonCachedEvent(final @NotNull SentryBaseEvent event) {

if (shouldApplyScopeData(event, hint)) {
processNonCachedEvent(event);
final @Nullable SdkVersion replaySdkVersion =
options.getExperimental().getSessionReplay().getSdkVersion();
if (replaySdkVersion != null) {
// we override the SdkVersion only for replay events as those may come from Hybrid SDKs
event.setSdk(replaySdkVersion);
}
}
return event;
}
Expand Down
3 changes: 2 additions & 1 deletion sentry/src/main/java/io/sentry/SentryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,8 @@ public void captureUserFeedback(final @NotNull UserFeedback userFeedback) {
final SentryId sentryId = event.getEventId();

final SentryEnvelopeHeader envelopeHeader =
new SentryEnvelopeHeader(sentryId, options.getSdkVersion(), traceContext);
new SentryEnvelopeHeader(
sentryId, options.getExperimental().getSessionReplay().getSdkVersion(), traceContext);

return new SentryEnvelope(envelopeHeader, envelopeItems);
}
Expand Down
12 changes: 10 additions & 2 deletions sentry/src/main/java/io/sentry/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,13 @@ public void setSslSocketFactory(final @Nullable SSLSocketFactory sslSocketFactor
*/
@ApiStatus.Internal
public void setSdkVersion(final @Nullable SdkVersion sdkVersion) {
final @Nullable SdkVersion replaySdkVersion = experimental.getSessionReplay().getSdkVersion();
if (this.sdkVersion != null
&& replaySdkVersion != null
&& this.sdkVersion.equals(replaySdkVersion)) {
// if sdkVersion = sessionReplay.sdkVersion we override it, as it means no one else set it
experimental.getSessionReplay().setSdkVersion(sdkVersion);
}
this.sdkVersion = sdkVersion;
}

Expand Down Expand Up @@ -2626,7 +2633,8 @@ public SentryOptions() {
* @param empty if options should be empty.
*/
private SentryOptions(final boolean empty) {
experimental = new ExperimentalOptions(empty);
final @NotNull SdkVersion sdkVersion = createSdkVersion();
experimental = new ExperimentalOptions(empty, sdkVersion);
if (!empty) {
// SentryExecutorService should be initialized before any
// SendCachedEventFireAndForgetIntegration
Expand All @@ -2647,7 +2655,7 @@ private SentryOptions(final boolean empty) {
}

setSentryClientName(BuildConfig.SENTRY_JAVA_SDK_NAME + "/" + BuildConfig.VERSION_NAME);
setSdkVersion(createSdkVersion());
setSdkVersion(sdkVersion);
addPackageInfo();
}
}
Expand Down
32 changes: 29 additions & 3 deletions sentry/src/main/java/io/sentry/SentryReplayOptions.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.sentry;

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 @@ -41,6 +43,10 @@ public enum SentryReplayQuality {
this.sizeScale = sizeScale;
this.bitRate = bitRate;
}

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

/**
Expand Down Expand Up @@ -114,7 +120,13 @@ public enum SentryReplayQuality {
*/
private boolean trackOrientationChange = true;

public SentryReplayOptions(final boolean empty) {
/**
* SdkVersion object that contains the Sentry Client Name and its version. This object is only
* applied to {@link SentryReplayEvent}s.
*/
private @Nullable SdkVersion sdkVersion;

public SentryReplayOptions(final boolean empty, final @Nullable SdkVersion sdkVersion) {
if (!empty) {
setMaskAllText(true);
setMaskAllImages(true);
Expand All @@ -123,14 +135,18 @@ public SentryReplayOptions(final boolean empty) {
maskViewClasses.add(ANDROIDX_MEDIA_VIEW_CLASS_NAME);
maskViewClasses.add(EXOPLAYER_CLASS_NAME);
maskViewClasses.add(EXOPLAYER_STYLED_CLASS_NAME);
this.sdkVersion = sdkVersion;
}
}

public SentryReplayOptions(
final @Nullable Double sessionSampleRate, final @Nullable Double onErrorSampleRate) {
this(false);
final @Nullable Double sessionSampleRate,
final @Nullable Double onErrorSampleRate,
final @Nullable SdkVersion sdkVersion) {
this(false, sdkVersion);
this.sessionSampleRate = sessionSampleRate;
this.onErrorSampleRate = onErrorSampleRate;
this.sdkVersion = sdkVersion;
}

@Nullable
Expand Down Expand Up @@ -282,4 +298,14 @@ public boolean isTrackOrientationChange() {
public void setTrackOrientationChange(final boolean trackOrientationChange) {
this.trackOrientationChange = trackOrientationChange;
}

@ApiStatus.Internal
public @Nullable SdkVersion getSdkVersion() {
return sdkVersion;
}

@ApiStatus.Internal
public void setSdkVersion(final @Nullable SdkVersion sdkVersion) {
this.sdkVersion = sdkVersion;
}
}
Loading
Loading