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

fix: Errors shortly after SentrySDK.init don't affect the session #2430

Merged
merged 5 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ This version adds a dependency on Swift.

- Properly demangle Swift class name (#2162)

### Fixes

- Errors shortly after SentrySDK.init now affect the session (#2430)

### Breaking Changes

- Remove `- [SentryOptions initWithDict:didFailWithError:]` (#2404)
Expand Down
10 changes: 10 additions & 0 deletions Sources/Sentry/SentryHub.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#import "SentrySamplingContext.h"
#import "SentryScope.h"
#import "SentrySerialization.h"
#import "SentrySession+Private.h"
#import "SentryTracer.h"
#import "SentryTracesSampler.h"
#import "SentryTransaction.h"
Expand All @@ -33,6 +34,7 @@
@property (nonatomic, strong) id<SentryCurrentDateProvider> currentDateProvider;
@property (nonatomic, strong) NSMutableArray<id<SentryIntegrationProtocol>> *installedIntegrations;
@property (nonatomic, strong) NSMutableSet<NSString *> *installedIntegrationNames;
@property (nonatomic) NSUInteger errorsBeforeSession;

@end

Expand All @@ -53,6 +55,7 @@ - (instancetype)initWithClient:(nullable SentryClient *)client
_installedIntegrationNames = [[NSMutableSet alloc] init];
_crashWrapper = [SentryCrashWrapper sharedInstance];
_tracesSampler = [[SentryTracesSampler alloc] initWithOptions:client.options];
_errorsBeforeSession = 0;
#if SENTRY_TARGET_PROFILING_SUPPORTED
if (client.options.isProfilingEnabled) {
_profilesSampler = [[SentryProfilesSampler alloc] initWithOptions:client.options];
Expand Down Expand Up @@ -93,6 +96,11 @@ - (void)startSession
}
_session = [[SentrySession alloc] initWithReleaseName:options.releaseName];

if (_errorsBeforeSession > 0) {
_session.errors = _errorsBeforeSession;
_errorsBeforeSession = 0;
}

NSString *environment = options.environment;
if (nil != environment) {
_session.environment = environment;
Expand Down Expand Up @@ -447,6 +455,7 @@ - (SentryId *)captureError:(NSError *)error withScope:(SentryScope *)scope
withScope:scope
incrementSessionErrors:^(void) { return [self incrementSessionErrors]; }];
} else {
_errorsBeforeSession++;
return [client captureError:error withScope:scope];
}
}
Expand All @@ -468,6 +477,7 @@ - (SentryId *)captureException:(NSException *)exception withScope:(SentryScope *
withScope:scope
incrementSessionErrors:^(void) { return [self incrementSessionErrors]; }];
} else {
_errorsBeforeSession++;
return [client captureException:exception withScope:scope];
}
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/Sentry/SentrySession.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
}
}

@interface
SentrySession ()
@property (nonatomic) NSUInteger errors;
@end

@implementation SentrySession

@synthesize flagInit = _init;
Expand Down
2 changes: 2 additions & 0 deletions Sources/Sentry/include/SentrySession+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ NSString *nameForSentrySessionStatus(SentrySessionStatus status);
@interface
SentrySession (Private)

@property (nonatomic) NSUInteger errors;

- (void)setFlagInit;

@end
Expand Down
13 changes: 13 additions & 0 deletions Tests/SentryTests/SentryHubTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,19 @@ class SentryHubTests: XCTestCase {
XCTAssertEqual(1, fixture.client.captureSessionInvocations.count)
}

func testCaptureErrorBeforeSession() {
let sut = fixture.getSut()
sut.capture(error: fixture.error, scope: fixture.scope).assertIsNotEmpty()
sut.startSession()

XCTAssertEqual(fixture.client.captureErrorWithScopeInvocations.count, 1)
XCTAssertEqual(fixture.client.captureSessionInvocations.count, 1)

if let session = fixture.client.captureSessionInvocations.first {
XCTAssertEqual(session.errors, 1)
}
}

func testCaptureWithoutIncreasingErrorCount() {
let sut = fixture.getSut()
sut.startSession()
Expand Down