Skip to content

fix: Fix default values and dictionary values for session replay options - #5210

Merged
philprime merged 18 commits into
mainfrom
philprime/fix-sr-options
May 12, 2025
Merged

philprime merged 18 commits into
mainfrom
philprime/fix-sr-options

Conversation

@philprime

@philprime philprime commented May 8, 2025 •

Copy link
Copy Markdown
Member

📜 Description

This commit introduces several enhancements to the SentryReplayOptions class, including new properties for better configuration and handling of session replay options. Key changes include:

  • Added internal methods for converting raw values to SentryReplayQuality.
  • Introduced new properties for bitrate and size scale calculations.
  • Updated initializers to support dictionary-based configuration.
  • Added comprehensive unit tests for SentryReplayOptions and related classes, ensuring default values and custom configurations are correctly handled.
  • Introduced new test files for Objective-C compatibility and SwiftUI integration.

Covered cases:

  • Init without args in Swift
  • Init with all args in Swift
  • Init with some args ommitted in Swift (fallback to default value)
  • No nullable args
  • Init without args in Objective-C
  • Init with all args in Objective-C
  • Init with missing value in dictionary, fallback to default value

💡 Motivation and Context

  • The default values are inconsistent (i.e. enableViewRendererV2 was not enabled by default everywhere)
  • The default values set in the Swift initializer made an initializer without arguments unavailable to Objective-C, and the replacement fix (empty init) was setting default values inconsistently.

💚 How did you test it?

📝 Checklist

You have to check all boxes before merging:

  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

@philprime philprime self-assigned this May 8, 2025
@philprime

Copy link
Copy Markdown
Member Author

cc @kahest so you know this PR exists.

@codecov

codecov Bot commented May 8, 2025 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 99.20530% with 6 lines in your changes missing coverage. Please review.

Project coverage is 92.902%. Comparing base (7206904) to head (1d4f2bc).
Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
...tions/SessionReplay/SentryReplayOptionsTests.swift 99.218% 4 Missing ⚠️
...SentrySwiftUITests/PreviewRedactOptionsTests.swift 98.019% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##              main     #5210       +/-   ##
=============================================
+ Coverage   92.746%   92.902%   +0.155%     
=============================================
  Files          677       680        +3     
  Lines        84695     85368      +673     
  Branches     30806     30982      +176     
=============================================
+ Hits         78552     79309      +757     
+ Misses        6041      5957       -84     
  Partials       102       102               
Files with missing lines Coverage Δ
...s/SentrySwiftUI/Preview/PreviewRedactOptions.swift 100.000% <100.000%> (+100.000%) ⬆️
...tegrations/SessionReplay/SentryReplayOptions.swift 100.000% <100.000%> (+3.797%) ⬆️
...tions/SessionReplay/SentryReplayOptionsObjcTests.m 100.000% <100.000%> (ø)
...sts/Protocol/SentryRedactDefaultOptionsTests.swift 100.000% <100.000%> (ø)
...SentrySwiftUITests/PreviewRedactOptionsTests.swift 98.019% <98.019%> (ø)
...tions/SessionReplay/SentryReplayOptionsTests.swift 99.471% <99.218%> (-0.529%) ⬇️

... and 19 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7206904...1d4f2bc. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

github-actions Bot commented May 8, 2025 •

Copy link
Copy Markdown
Contributor

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 1233.71 ms 1245.98 ms 12.27 ms
Size 23.76 KiB 862.55 KiB 838.79 KiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
1ee5a37 1209.37 ms 1222.04 ms 12.68 ms
7f691b5 1233.94 ms 1243.80 ms 9.86 ms
e070f8a 1236.47 ms 1250.50 ms 14.03 ms
c8dbe73 1215.37 ms 1237.31 ms 21.94 ms
aeec206 1229.27 ms 1253.70 ms 24.43 ms
c810e58 1221.57 ms 1250.45 ms 28.88 ms
034be1c 1222.67 ms 1236.22 ms 13.55 ms
533859f 1237.78 ms 1249.76 ms 11.98 ms
4977fbc 1231.55 ms 1239.80 ms 8.25 ms
742d4b6 1204.00 ms 1217.90 ms 13.90 ms

App size

Revision Plain With Sentry Diff
1ee5a37 21.58 KiB 670.39 KiB 648.81 KiB
7f691b5 20.76 KiB 420.55 KiB 399.79 KiB
e070f8a 21.58 KiB 546.20 KiB 524.62 KiB
c8dbe73 21.58 KiB 615.91 KiB 594.33 KiB
aeec206 20.76 KiB 434.88 KiB 414.12 KiB
c810e58 22.32 KiB 761.10 KiB 738.78 KiB
034be1c 20.76 KiB 436.66 KiB 415.90 KiB
533859f 22.85 KiB 408.84 KiB 386.00 KiB
4977fbc 20.76 KiB 419.86 KiB 399.10 KiB
742d4b6 21.58 KiB 546.20 KiB 524.61 KiB

Previous results on branch: philprime/fix-sr-options

Startup times

Revision Plain With Sentry Diff
f6a2998 1225.73 ms 1247.45 ms 21.72 ms
ed3f5f1 1223.53 ms 1241.73 ms 18.20 ms
78dae36 1235.55 ms 1253.69 ms 18.14 ms
20e0a2f 1232.69 ms 1242.06 ms 9.37 ms

App size

Revision Plain With Sentry Diff
f6a2998 23.76 KiB 862.54 KiB 838.78 KiB
ed3f5f1 23.76 KiB 862.54 KiB 838.78 KiB
78dae36 23.76 KiB 862.54 KiB 838.78 KiB
20e0a2f 23.76 KiB 861.54 KiB 837.78 KiB

@armcknight armcknight left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this change entirely motivated by the inconsistency in the default value being set for enableViewRendererV2? Personally, I find these changes confusing and potentially dangerous. We cannot rely on comments to enforce consistency in the business logic, those kinds of constraints need to be coded. Yes, you can catch them in unit tests, but this introduces almost 1,000 lines of test; that is also code that is prone to errors, requires its own maintenance, and slows down future changes. A lot of that checking can be done by the compiler if we keep the target code simpler. And we wouldn't need the hacks to avoid recursive convenience init calls.

My approach here would probably involve deduplicating magic values that are used as defaults in variable declarations and default parameter values into static variables or even into a structure called Defaults. One source of truth. Here's what that would look like: #5214. A lot of your tests might still provide value so I don't necessarily think you need to throw all that effort away.

It's unfortunate that this class was ever written with publicly available initializers and mutable properties, as this is the kind of mess it leads to. Better would have been to use a builder pattern instead of offering multiple types of initializers. We use that elsewhere so I'm not sure why we didn't here. Sorry you have to deal with that now 😭

@philprime

Copy link
Copy Markdown
Member Author

Thanks for the feedback @armcknight, I already had a discussion with @philipphofmann yesterday, that this situation is quite complex and due the some of these initializers being public and already shipped to production made it even harder and frustrating to find a solution that covers all.

Is this change entirely motivated by the inconsistency in the default value being set for enableViewRendererV2? Personally, I find these changes confusing and potentially dangerous. We cannot rely on comments to enforce consistency in the business logic, those kinds of constraints need to be coded. Yes, you can catch them in unit tests, but this introduces almost 1,000 lines of test; that is also code that is prone to errors, requires its own maintenance, and slows down future changes. A lot of that checking can be done by the compiler if we keep the target code simpler. And we wouldn't need the hacks to avoid recursive convenience init calls.

This is the second time that a default value was only partially updated and therefore not consistent, so it must be covered somehow. I agree with most of your statements, these options are just so badly designed that any change can cause public facing issues. IMHO some of the initializers should not have been public in the first place.

My approach here would probably involve deduplicating magic values that are used as defaults in variable declarations and default parameter values into static variables or even into a structure called Defaults. One source of truth. Here's what that would look like: #5214. A lot of your tests might still provide value so I don't necessarily think you need to throw all that effort away.

Yes, I had this one approach in my drafts as well but it had two additional downsides:

  1. We would introduce another public type for the constants, something I wanted to avoid because SDK users should not care about the default values like that (we can not use an internal type, the type must be public).
  2. A person looking at the Xcode documentation of the initializer will not see anymore what the default value is:
image

It's unfortunate that this class was ever written with publicly available initializers and mutable properties, as this is the kind of mess it leads to. Better would have been to use a builder pattern instead of offering multiple types of initializers. We use that elsewhere so I'm not sure why we didn't here. Sorry you have to deal with that now 😭

Yes, this is terrible to maintain, but it is what is now. I added a note for v9

@philippsuess

Copy link
Copy Markdown
Contributor

I don't believe this code will be terrible to maintain, because we're not going to change that code often, as it's a public API. The tests can only change when the requirements change, which should only be the case in the next major release. As these APIs are a bit of a mess and we can't fix that now, let's agree on a somewhat OK solution and fix it later.

@philippsuess philippsuess left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, we need so many tests because the logic is so complex. Without all these tests, we can't be sure everything works. We can reduce the number of tests to omit some of the parameters, but the rest is required, IMO. I included my view on your concerns in #5210 (comment), @armcknight.

Thanks for adding all the tests, @philprime

Comment thread CHANGELOG.md Resolved
@philippsuess

philippsuess commented May 9, 2025 •

Copy link
Copy Markdown
Contributor

It's unfortunate that this class was ever written with publicly available initializers and mutable properties, as this is the kind of mess it leads to. Better would have been to use a builder pattern instead of offering multiple types of initializers. We use that elsewhere so I'm not sure why we didn't here. Sorry you have to deal with that now 😭

And yes, that's the root cause, @armcknight. We should fix this in V9.

@philprime

Copy link
Copy Markdown
Member Author

Based on your review feedback I refactored the initializers to use the default values in the new public type SessionReplayOptions.DefaultValues. I added a reference to all of the initializers and properties so SDK users can find more details

@antonis antonis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for all the changes @philprime 🙇
The changes on this PR LGTM and worked as expected with RN in my tests 🚀

@philippsuess philippsuess left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, with a couple of suggestions. Thanks

Comment thread CHANGELOG.md Resolved

@philippsuess philippsuess left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks

@philprime
philprime merged commit 369f7a1 into main May 12, 2025
@philprime
philprime deleted the philprime/fix-sr-options branch May 12, 2025 14:52
@armcknight

Copy link
Copy Markdown
Member

Nice work getting this across the finish line @philprime! 🥇

philippsuess added a commit that referenced this pull request May 13, 2025
#5210 isn't released yet.
@philippsuess philippsuess mentioned this pull request May 13, 2025
philippsuess added a commit that referenced this pull request May 14, 2025
#5210 hasn't been released yet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants