Skip to content

Conversation

@philipphofmann
Copy link
Member

@philipphofmann philipphofmann commented Jan 16, 2026

📜 Description

This PR aligns the in_foreground app context behaviour for both handled and unhandled events and also adds a is_active property to the app context.

getsentry/sentry#107047 will correctly format is_active in the issue details view.

Next steps:

💡 Motivation and Context

First step for GH-6178

💚 How did you test it?

Screenshot 2026-01-27 at 13 50 45

Unit tests and simulator

📝 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.

Fix the mismatch of the app context in foreground between fatal and
non-fatal events. Also, fix the breadcrumbs to correctly add
in_foreground and in background crumbs, and add new active crumbs for
more info for the user.

Fixes GH-6178
@github-actions
Copy link
Contributor

github-actions bot commented Jan 16, 2026

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


Bug Fixes 🐛

  • In_foreground and is_active app context by philipphofmann in #7188

🤖 This preview updates automatically when you update the PR.

@codecov
Copy link

codecov bot commented Jan 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.113%. Comparing base (9a34176) to head (d11c6c4).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##              main     #7188       +/-   ##
=============================================
+ Coverage   85.099%   85.113%   +0.013%     
=============================================
  Files          473       475        +2     
  Lines        28557     28589       +32     
  Branches     12462     12474       +12     
=============================================
+ Hits         24302     24333       +31     
+ Misses        4210      4208        -2     
- Partials        45        48        +3     
Files with missing lines Coverage Δ
SentryTestUtils/Sources/TestClient.swift 86.324% <100.000%> (+0.237%) ⬆️
...ryTestUtils/Sources/TestEventContextEnricher.swift 100.000% <100.000%> (ø)
Sources/Sentry/SentryClient.m 98.348% <100.000%> (-0.021%) ⬇️
Sources/Sentry/SentryCrashReportConverter.m 96.119% <100.000%> (+0.011%) ⬆️
...rces/Swift/Helper/SentryEventContextEnricher.swift 100.000% <100.000%> (ø)
Sources/Swift/SentryDependencyContainer.swift 97.000% <100.000%> (+0.076%) ⬆️

... and 3 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 9a34176...d11c6c4. Read the comment docs.

philipphofmann and others added 15 commits January 27, 2026 10:13
Add test coverage for the new is_active field in SentryCrashReportConverter:
- testAppContextIsActiveTrue_IsTrue: verifies is_active is true from crash report
- testAppContextIsActiveFalse_FromCrashReport: verifies is_active is false from crash report
- testAppContextIsActiveFalse_IsFalse: verifies manually set false value
- testAppContextIsActiveNil_IsNil: verifies nil handling

Also update existing tests to expect both in_foreground and is_active fields.
…r protocol

- Extract app state logic from SentryClient.m (lines 714-739) to Swift
- Create SentryEventContextEnricher protocol with SentryDefaultEventContextEnricher implementation
- Add in_foreground and is_active fields based on UIApplicationState
- Support all platforms (UIKit: enrichment, non-UIKit: no-op)
- Fix retain cycle with [weak self] in applicationStateProvider closure
- Handle nil application state with warning log
- Add comprehensive unit tests for all app states and edge cases
- Add tests for is_active field in SentryCrashReportConverter
- Add TestEventContextEnricher helper for test reusability

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
- Move eventContextEnricher property from SentryClient+Private.h to class extension in SentryClient.m
- Make property truly internal, not exposed in any header
- Remove protocol forward declaration from private header
- Rename method to be more specific about enriching with app state
- Update protocol documentation to clarify it adds in_foreground and is_active
- Update all call sites and tests to use new method name
…ntext

- Avoid nullable default pattern (?? [:])
- Make it explicit when creating new app context vs using existing one
- Change func enrichWithAppState(_ context: [String: Any]?) to non-optional parameter
- SentryClient.m always passes non-nil context (event.context ?: @{})
- Remove nil handling in enricher implementation
- Update test to verify empty dict is passed when context is nil
…oject

- Replace if-else with nil coalescing operator for appContext
- Add SentryDefaultEventContextEnricherTests.swift to Xcode project
- Add TestEventContextEnricher.swift to Xcode project
- Both test files now properly included in build targets
- Check if context["app"] exists but is not a dictionary
- Log warning when app context has invalid type
- Return context unchanged when app context type is invalid
- Update test to verify unchanged context and proper warning behavior
…entStates

- Test was redundant with individual state tests
- Each call created a fresh enricher instance (no stateful behavior tested)
- Coverage already provided by testEnrichEventContext_WhenActive/Inactive/Background
Changed non-UIKit platform tests from extension to class definition.
On macOS, the main test class is not defined (UIKit platforms only),
so the extension failed to compile. Now macOS has its own class
definition with platform-specific tests.

Fixes compilation error on macOS.
Changed from separate class definitions per platform to single class
with platform-specific tests inside #if directives. This approach is
cleaner and prevents duplication of the class definition.

- UIKit platform tests (iOS, tvOS, visionOS) in first #if block
- Non-UIKit platform tests (macOS) in second #if block
@philipphofmann philipphofmann marked this pull request as ready for review January 27, 2026 13:18
@github-actions
Copy link
Contributor

github-actions bot commented Jan 29, 2026

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 1217.51 ms 1243.78 ms 26.27 ms
Size 24.14 KiB 1.10 MiB 1.07 MiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
a424cf3 1231.36 ms 1259.62 ms 28.25 ms
58a9225 1211.40 ms 1238.88 ms 27.48 ms
013fd4d 1216.02 ms 1242.16 ms 26.14 ms
387fbe4 1226.19 ms 1251.98 ms 25.79 ms
84bc307 1211.52 ms 1237.09 ms 25.56 ms
ffe0649 1213.35 ms 1248.64 ms 35.29 ms
4e4af90 1222.89 ms 1252.15 ms 29.26 ms
7714225 1213.24 ms 1252.28 ms 39.04 ms
29b6704 1216.31 ms 1251.91 ms 35.60 ms
bed2eb7 1215.88 ms 1247.14 ms 31.27 ms

App size

Revision Plain With Sentry Diff
a424cf3 24.14 KiB 1.06 MiB 1.04 MiB
58a9225 24.14 KiB 1.06 MiB 1.04 MiB
013fd4d 24.14 KiB 1.04 MiB 1.02 MiB
387fbe4 24.14 KiB 1.07 MiB 1.04 MiB
84bc307 24.14 KiB 1.08 MiB 1.06 MiB
ffe0649 24.14 KiB 1.06 MiB 1.04 MiB
4e4af90 24.14 KiB 1.08 MiB 1.06 MiB
7714225 24.14 KiB 1.07 MiB 1.05 MiB
29b6704 24.14 KiB 1.06 MiB 1.04 MiB
bed2eb7 24.14 KiB 1.07 MiB 1.04 MiB

Previous results on branch: app-is-active-context

Startup times

Revision Plain With Sentry Diff
a194e50 1211.29 ms 1240.44 ms 29.15 ms

App size

Revision Plain With Sentry Diff
a194e50 24.14 KiB 1.10 MiB 1.07 MiB

Copy link
Member

@philprime philprime left a comment

Choose a reason for hiding this comment

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

LGTM with one feedback comment

philipphofmann and others added 4 commits January 30, 2026 10:55
Enables running specific test classes during development, speeding up iterations when using agents for refactoring. This allows targeted testing of affected areas without running the full test suite.
We received the feedback in #7316 that it was easy to miss that we fixed a bug which now requires `sendDefaultPii` to be set to `true` for user attributes to be set. This adds an alert to the changelog which I will also copy to the GitHub release notes
)

* refactor: Convert `SentryPerfromanceTrackingIntegration` to Swift

* Merge fixes

* Fix visionOS tests

* Remove SentryDependencyContainer dependency

* Fix project missing files

* Fix UITest

* Refactor UIViewController swizzling + add more tests

* Use same loop than ObjC (just in case)

* Minor improvements

* Remove `Dynamic` use

* Apply PR feedback

* Remove URL and use file path

* Fix typo
@github-actions

This comment was marked as outdated.

@philipphofmann philipphofmann enabled auto-merge (squash) January 30, 2026 10:01
@philipphofmann philipphofmann merged commit f9133d5 into main Jan 30, 2026
200 checks passed
@philipphofmann philipphofmann deleted the app-is-active-context branch January 30, 2026 11:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-merge Use this label to trigger all PR workflows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants