Skip to content

Commit

Permalink
feat: Add in_foreground AppContext to transactions (#4561)
Browse files Browse the repository at this point in the history
The SDK already attaches the in_foreground to events, but it didn't for
transactions. This is fixed now.

Fixes GH-4559
  • Loading branch information
philipphofmann authored Nov 22, 2024
1 parent d70a7e8 commit 07ea386
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Features

- Add in_foreground app context to transactions (#4561)

### Improvements

- impr: Speed up getBinaryImages V2 (#4539). Follow up on (#4435)
Expand Down
40 changes: 20 additions & 20 deletions Sources/Sentry/SentryClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -683,26 +683,6 @@ - (SentryEvent *_Nullable)prepareEvent:(SentryEvent *)event
event.threads = [self.threadInspector getCurrentThreads];
}

#if SENTRY_HAS_UIKIT
if (!isCrashEvent) {
NSMutableDictionary *context =
[event.context mutableCopy] ?: [NSMutableDictionary dictionary];
if (context[@"app"] == nil
|| ([context[@"app"] isKindOfClass:NSDictionary.self]
&& context[@"app"][@"in_foreground"] == nil)) {
NSMutableDictionary *app = [(NSDictionary *)context[@"app"] mutableCopy]
?: [NSMutableDictionary dictionary];
context[@"app"] = app;

UIApplicationState appState =
[SentryDependencyContainer sharedInstance].application.applicationState;
BOOL inForeground = appState == UIApplicationStateActive;
app[@"in_foreground"] = @(inForeground);
event.context = context;
}
}
#endif

BOOL debugMetaNotAttached = !(nil != event.debugMeta && event.debugMeta.count > 0);
if (!isCrashEvent && shouldAttachStacktrace && debugMetaNotAttached
&& event.threads != nil) {
Expand All @@ -711,6 +691,26 @@ - (SentryEvent *_Nullable)prepareEvent:(SentryEvent *)event
}
}

#if SENTRY_HAS_UIKIT
if (!isCrashEvent && eventIsNotReplay) {
NSMutableDictionary *context =
[event.context mutableCopy] ?: [NSMutableDictionary dictionary];
if (context[@"app"] == nil
|| ([context[@"app"] isKindOfClass:NSDictionary.self]
&& context[@"app"][@"in_foreground"] == nil)) {
NSMutableDictionary *app =
[(NSDictionary *)context[@"app"] mutableCopy] ?: [NSMutableDictionary dictionary];
context[@"app"] = app;

UIApplicationState appState =
[SentryDependencyContainer sharedInstance].application.applicationState;
BOOL inForeground = appState == UIApplicationStateActive;
app[@"in_foreground"] = @(inForeground);
event.context = context;
}
}
#endif

event = [scope applyToEvent:event maxBreadcrumb:self.options.maxBreadcrumbs];

if (!eventIsNotReplay) {
Expand Down
12 changes: 12 additions & 0 deletions Tests/SentryTests/SentryClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,18 @@ class SentryClientTest: XCTestCase {
let inForeground = actual.context?["app"]?["in_foreground"] as? Bool
XCTAssertEqual(inForeground, true)
}

func testCaptureTransaction_WithAppStateInForegroudWhenAppIsInForeground() throws {
let app = TestSentryUIApplication()
app.applicationState = .active
SentryDependencyContainer.sharedInstance().application = app

let event = fixture.transaction
fixture.getSut().capture(event: event)
let actual = try lastSentEvent()
let inForeground = actual.context?["app"]?["in_foreground"] as? Bool
XCTAssertEqual(inForeground, true)
}

func testCaptureExceptionWithAppStateInForegroudWhenAppIsInBackground() throws {
let app = TestSentryUIApplication()
Expand Down

0 comments on commit 07ea386

Please sign in to comment.