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

5371 max persisted sessions #943

Merged
merged 1 commit into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion Bugsnag/BugsnagSessionTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ - (instancetype)initWithConfig:(BugsnagConfiguration *)config
if (!storePath) {
BSG_KSLOG_ERROR(@"Failed to initialize session store.");
}
_sessionStore = [BugsnagSessionFileStore storeWithPath:storePath];

_sessionStore = [BugsnagSessionFileStore storeWithPath:storePath maxPersistedSessions:config.maxPersistedSessions];
_extraRuntimeInfo = [NSMutableDictionary new];
}
return self;
Expand Down
2 changes: 2 additions & 0 deletions Bugsnag/Configuration/BSGConfigurationBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ + (BugsnagConfiguration *)configurationFromOptions:(NSDictionary *)options {
BSGKeyEndpoints,
BSGKeyMaxBreadcrumbs,
BSGKeyMaxPersistedEvents,
BSGKeyMaxPersistedSessions,
BSGKeyPersistUser,
BSGKeyRedactedKeys,
BSGKeyReleaseStage,
Expand All @@ -57,6 +58,7 @@ + (BugsnagConfiguration *)configurationFromOptions:(NSDictionary *)options {

[self loadNumber:config options:options key:BSGKeyMaxBreadcrumbs];
[self loadNumber:config options:options key:BSGKeyMaxPersistedEvents];
[self loadNumber:config options:options key:BSGKeyMaxPersistedSessions];
[self loadSendThreads:config options:options];
return config;
}
Expand Down
22 changes: 22 additions & 0 deletions Bugsnag/Configuration/BugsnagConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ - (nonnull id)copyWithZone:(nullable NSZone *)zone {
copy.discardClasses = self.discardClasses;
[copy setRedactedKeys:self.redactedKeys];
[copy setMaxPersistedEvents:self.maxPersistedEvents];
[copy setMaxPersistedSessions:self.maxPersistedSessions];
[copy setMaxBreadcrumbs:self.maxBreadcrumbs];
copy->_metadata = [[BugsnagMetadata alloc] initWithDictionary:[[self.metadata toDictionary] mutableCopy]];
[copy setEndpoints:self.endpoints];
Expand Down Expand Up @@ -162,6 +163,7 @@ - (instancetype)initWithApiKey:(NSString *)apiKey {
_redactedKeys = [NSSet setWithArray:@[@"password"]];
_enabledBreadcrumbTypes = BSGEnabledBreadcrumbTypeAll;
_maxPersistedEvents = 12;
_maxPersistedSessions = 32;
_maxBreadcrumbs = 25;
_autoTrackSessions = YES;
_sendThreads = BSGThreadSendPolicyAlways;
Expand Down Expand Up @@ -438,6 +440,26 @@ - (void)setMaxPersistedEvents:(NSUInteger)maxPersistedEvents {
}
}

@synthesize maxPersistedSessions = _maxPersistedSessions;

- (NSUInteger)maxPersistedSessions {
@synchronized (self) {
return _maxPersistedSessions;
}
}

- (void)setMaxPersistedSessions:(NSUInteger)maxPersistedSessions {
@synchronized (self) {
if (maxPersistedSessions >= 1 && maxPersistedSessions <= 100) {
_maxPersistedSessions = maxPersistedSessions;
} else {
bsg_log_err(@"Invalid configuration value detected. Option maxPersistedSessions "
"should be an integer between 1-100. Supplied value is %lu",
(unsigned long) maxPersistedSessions);
}
}
}

@synthesize maxBreadcrumbs = _maxBreadcrumbs;

- (NSUInteger)maxBreadcrumbs {
Expand Down
1 change: 1 addition & 0 deletions Bugsnag/Helpers/BugsnagKeys.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ extern NSString *const BSGKeyMachoUUID;
extern NSString *const BSGKeyMachoVMAddress;
extern NSString *const BSGKeyMaxBreadcrumbs;
extern NSString *const BSGKeyMaxPersistedEvents;
extern NSString *const BSGKeyMaxPersistedSessions;
extern NSString *const BSGKeyMessage;
extern NSString *const BSGKeyMetadata;
extern NSString *const BSGKeyMethod;
Expand Down
1 change: 1 addition & 0 deletions Bugsnag/Helpers/BugsnagKeys.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
NSString *const BSGKeyMachoVMAddress = @"machoVMAddress";
NSString *const BSGKeyMaxBreadcrumbs = @"maxBreadcrumbs";
NSString *const BSGKeyMaxPersistedEvents = @"maxPersistedEvents";
NSString *const BSGKeyMaxPersistedSessions = @"maxPersistedSessions";
NSString *const BSGKeyMessage = @"message";
NSString *const BSGKeyMetadata = @"metaData";
NSString *const BSGKeyMethod = @"method";
Expand Down
3 changes: 2 additions & 1 deletion Bugsnag/Storage/BugsnagSessionFileStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
#import "BugsnagSession.h"

@interface BugsnagSessionFileStore : BugsnagFileStore
+ (BugsnagSessionFileStore *)storeWithPath:(NSString *)path;
+ (BugsnagSessionFileStore *)storeWithPath:(NSString *)path
maxPersistedSessions:(NSUInteger)maxPersistedSessions;

- (void)write:(BugsnagSession *)session;

Expand Down
22 changes: 20 additions & 2 deletions Bugsnag/Storage/BugsnagSessionFileStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,27 @@

static NSString *const kSessionStoreSuffix = @"-Session-";

@interface BugsnagSessionFileStore ()

@property NSUInteger maxPersistedSessions;

@end

@implementation BugsnagSessionFileStore

+ (BugsnagSessionFileStore *)storeWithPath:(NSString *)path {
+ (BugsnagSessionFileStore *)storeWithPath:(NSString *)path
maxPersistedSessions:(NSUInteger)maxPersistedSessions {
return [[self alloc] initWithPath:path
filenameSuffix:kSessionStoreSuffix];
maxPersistedSessions:maxPersistedSessions];
}

- (instancetype) initWithPath:(NSString *)path
maxPersistedSessions:(NSUInteger)maxPersistedSessions {
if ((self = [super initWithPath:path
filenameSuffix:kSessionStoreSuffix])) {
_maxPersistedSessions = maxPersistedSessions;
}
return self;
}

- (void)write:(BugsnagSession *)session {
Expand All @@ -30,6 +46,8 @@ - (void)write:(BugsnagSession *)session {
BSG_KSLOG_ERROR(@"Failed to write session %@", error);
return;
}

[self pruneFilesLeaving:(int)self.maxPersistedSessions];
}


Expand Down
8 changes: 8 additions & 0 deletions Bugsnag/include/Bugsnag/BugsnagConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ typedef BOOL (^BugsnagOnSessionBlock)(BugsnagSession *_Nonnull session);
*/
@property (nonatomic) NSUInteger maxPersistedEvents;

/**
* Sets the maximum number of sessions which will be stored. Once the threshold is reached,
* the oldest sessions will be deleted.
*
* By default, 32 sessions are stored: this can be amended up to a maximum of 100.
*/
@property (nonatomic) NSUInteger maxPersistedSessions;

/**
* Sets the maximum number of breadcrumbs which will be stored. Once the threshold is reached,
* the oldest breadcrumbs will be deleted.
Expand Down
4 changes: 4 additions & 0 deletions Tests/BSGConfigurationBuilderTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ - (void)testDecodeDefaultValues {
XCTAssertTrue(config.autoDetectErrors);
XCTAssertTrue(config.autoTrackSessions);
XCTAssertEqual(12, config.maxPersistedEvents);
XCTAssertEqual(32, config.maxPersistedSessions);
XCTAssertEqual(25, config.maxBreadcrumbs);
XCTAssertTrue(config.persistUser);
XCTAssertEqualObjects(@[@"password"], [config.redactedKeys allObjects]);
Expand Down Expand Up @@ -94,6 +95,7 @@ - (void)testDecodeFullConfig {
},
@"enabledReleaseStages": @[@"beta2", @"prod"],
@"maxPersistedEvents": @29,
@"maxPersistedSessions": @19,
@"maxBreadcrumbs": @27,
@"persistUser": @NO,
@"redactedKeys": @[@"foo"],
Expand All @@ -108,6 +110,7 @@ - (void)testDecodeFullConfig {
XCTAssertFalse(config.autoTrackSessions);
XCTAssertEqualObjects(@"7.22", config.bundleVersion);
XCTAssertEqual(29, config.maxPersistedEvents);
XCTAssertEqual(19, config.maxPersistedSessions);
XCTAssertEqual(27, config.maxBreadcrumbs);
XCTAssertFalse(config.persistUser);
XCTAssertEqualObjects(@[@"foo"], config.redactedKeys);
Expand Down Expand Up @@ -142,6 +145,7 @@ - (void)testInvalidConfigOptions {
@"enabledReleaseStages": @[@"beta2", @"prod"],
@"enabledErrorTypes": @[@"ooms", @"signals"],
@"maxPersistedEvents": @29,
@"maxPersistedSessions": @19,
@"maxBreadcrumbs": @27,
@"persistUser": @"pomelo",
@"redactedKeys": @[@77],
Expand Down
4 changes: 4 additions & 0 deletions Tests/BugsnagClientTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ - (void)assertEqualConfiguration:(BugsnagConfiguration *)expected withActual:(Bu
XCTAssertEqualObjects(expected.endpoints.notify, actual.endpoints.notify);
XCTAssertEqualObjects(expected.endpoints.sessions, actual.endpoints.sessions);
XCTAssertEqual(expected.maxPersistedEvents, actual.maxPersistedEvents);
XCTAssertEqual(expected.maxPersistedSessions, actual.maxPersistedSessions);
XCTAssertEqual(expected.maxBreadcrumbs, actual.maxBreadcrumbs);
XCTAssertEqual(expected.persistUser, actual.persistUser);
XCTAssertEqual([expected.redactedKeys count], [actual.redactedKeys count]);
Expand All @@ -211,12 +212,14 @@ - (void)testChangesToConfigurationAreIgnoredAfterCallingStart {
// Modify some arbitrary properties
config.persistUser = !config.persistUser;
config.maxPersistedEvents = config.maxPersistedEvents * 2;
config.maxPersistedSessions = config.maxPersistedSessions * 2;
config.maxBreadcrumbs = config.maxBreadcrumbs * 2;
config.appVersion = @"99.99.99";

// Ensure the changes haven't been reflected in our copy
XCTAssertNotEqual(initialConfig.persistUser, config.persistUser);
XCTAssertNotEqual(initialConfig.maxPersistedEvents, config.maxPersistedEvents);
XCTAssertNotEqual(initialConfig.maxPersistedSessions, config.maxPersistedSessions);
XCTAssertNotEqual(initialConfig.maxBreadcrumbs, config.maxBreadcrumbs);
XCTAssertNotEqualObjects(initialConfig.appVersion, config.appVersion);

Expand All @@ -235,6 +238,7 @@ - (void)testStartingBugsnagTwiceLogsAWarningAndIgnoresNewConfiguration {
updatedConfig.persistUser = !initialConfig.persistUser;
updatedConfig.maxBreadcrumbs = initialConfig.maxBreadcrumbs * 2;
updatedConfig.maxPersistedEvents = initialConfig.maxPersistedEvents * 2;
updatedConfig.maxPersistedSessions = initialConfig.maxPersistedSessions * 2;
updatedConfig.appVersion = @"99.99.99";

[Bugsnag startWithConfiguration:updatedConfig];
Expand Down
32 changes: 32 additions & 0 deletions Tests/BugsnagConfigurationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,38 @@ - (void)testMaxPersistedEvents {
XCTAssertEqual(1, config.maxPersistedEvents);
}

// =============================================================================
// MARK: - Max Persisted Sessions
// =============================================================================

- (void)testMaxPersistedSessions {
BugsnagConfiguration *config = [[BugsnagConfiguration alloc] initWithApiKey:DUMMY_APIKEY_32CHAR_1];
XCTAssertEqual(32, config.maxPersistedSessions);

// alter to valid value
config.maxPersistedSessions = 10;
XCTAssertEqual(10, config.maxPersistedSessions);

// alter to max value
config.maxPersistedSessions = 100;
XCTAssertEqual(100, config.maxPersistedSessions);

// alter to min value
config.maxPersistedSessions = 1;
XCTAssertEqual(1, config.maxPersistedSessions);

config.maxPersistedSessions = 0;
XCTAssertEqual(1, config.maxPersistedSessions);

// alter to negative value
config.maxPersistedSessions = -1;
XCTAssertEqual(1, config.maxPersistedSessions);

// alter to > max value
config.maxPersistedSessions = 500;
XCTAssertEqual(1, config.maxPersistedSessions);
}

// =============================================================================
// MARK: - Max Breadcrumb
// =============================================================================
Expand Down
19 changes: 19 additions & 0 deletions Tests/ConfigurationApiValidationTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,25 @@ - (void)testInvalidMaxPersistedEvents {
XCTAssertEqual(1, self.config.maxPersistedEvents);
}

- (void)testValidMaxPersistedSessions {
self.config.maxPersistedSessions = 1;
XCTAssertEqual(1, self.config.maxPersistedSessions);
self.config.maxPersistedSessions = 100;
XCTAssertEqual(100, self.config.maxPersistedSessions);
self.config.maxPersistedSessions = 40;
XCTAssertEqual(40, self.config.maxPersistedSessions);
}

- (void)testInvalidMaxPersistedSessions {
self.config.maxPersistedSessions = 1;
self.config.maxPersistedSessions = 0;
XCTAssertEqual(1, self.config.maxPersistedSessions);
self.config.maxPersistedSessions = -1;
XCTAssertEqual(1, self.config.maxPersistedSessions);
self.config.maxPersistedSessions = 590;
XCTAssertEqual(1, self.config.maxPersistedSessions);
}

- (void)testValidMaxBreadcrumbs {
self.config.maxBreadcrumbs = 0;
XCTAssertEqual(0, self.config.maxBreadcrumbs);
Expand Down