-
Notifications
You must be signed in to change notification settings - Fork 10
NR-186415 rewrote the persistence in the new event system #190
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
Changes from 3 commits
e37d766
f895338
92d90ae
64e9922
6fdb24c
1ef2247
526dd3e
3743206
602a9c5
492a5d7
dcfdf1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ | |
#import "NRMARequestEvent.h" | ||
#import "NRMANetworkErrorEvent.h" | ||
#import "NRMAAgentConfiguration.h" | ||
#import "NewRelicInternalUtils.h" | ||
#import "Constants.h" | ||
|
||
static const NSUInteger kDefaultBufferSize = 1000; | ||
static const NSUInteger kDefaultBufferTimeSeconds = 600; // 10 Minutes | ||
|
@@ -29,19 +31,38 @@ @implementation NRMAEventManager { | |
|
||
NSUInteger totalAttemptedInserts; | ||
NSTimeInterval oldestEventTimestamp; | ||
|
||
PersistentEventStore *_persistentStore; | ||
} | ||
|
||
- (nonnull instancetype)initWithPersistentStore:(PersistentEventStore *)store { | ||
static PersistentEventStore* __persistentEventStore; | ||
+ (PersistentEventStore*) persistentEventStore | ||
{ | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
NSString *filename = [[NewRelicInternalUtils getStorePath] stringByAppendingPathComponent:kNRMA_EventStoreFilename]; | ||
__persistentEventStore = [[PersistentEventStore alloc] initWithFilename:filename andMinimumDelay:.025]; | ||
}); | ||
return (__persistentEventStore); | ||
} | ||
|
||
- (nonnull instancetype)init { | ||
self = [super init]; | ||
if (self) { | ||
events = [[NSMutableArray<NRMAAnalyticEventProtocol> alloc] init]; | ||
maxBufferSize = kDefaultBufferSize; | ||
events = [NRMAEventManager getLastSessionEventsArray]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be behind a feature flag to only happen when offline storage is enabled? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is a good idea to have this as a feature flag but this is separate from offline storage. Persistence was originally intended to create an event trail for handledExceptions. Now because we believed that events were suppose to persist back into an eventManager and Android agreed they added that and now we should too. This accomplishes the goal of restoring events of an app session that ended unexpectedly. The way that offline storage currently is CDD'd to work is by storing the data of failed network request into separate files and then sending them all as soon as we send a successful request. There are talks about combining these two things but I haven't heard anything conclusive.
mbruin-NR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
maxBufferSize = [NRMAAgentConfiguration getMaxEventBufferSize]; | ||
maxBufferTimeSeconds = [NRMAAgentConfiguration getMaxEventBufferTime]; | ||
totalAttemptedInserts = 0; | ||
oldestEventTimestamp = 0; | ||
} | ||
return self; | ||
} | ||
- (nonnull instancetype)initWithFilename:(NSString*) filename { | ||
self = [super init]; | ||
if (self) { | ||
events = [NRMAEventManager getLastSessionEventsArray]; | ||
maxBufferSize = [NRMAAgentConfiguration getMaxEventBufferSize]; | ||
maxBufferTimeSeconds = [NRMAAgentConfiguration getMaxEventBufferTime]; | ||
totalAttemptedInserts = 0; | ||
oldestEventTimestamp = 0; | ||
_persistentStore = store; | ||
} | ||
mbruin-NR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return self; | ||
} | ||
|
@@ -85,7 +106,7 @@ - (BOOL)addEvent:(id<NRMAAnalyticEventProtocol>)event { | |
if (events.count < maxBufferSize) { | ||
[events addObject:event]; | ||
|
||
[_persistentStore setObject:event forKey:[self createKeyForEvent:event]]; | ||
[[NRMAEventManager persistentEventStore] setObject:event forKey:[self createKeyForEvent:event]]; | ||
|
||
if(events.count == 1) { | ||
oldestEventTimestamp = event.timestamp; | ||
|
@@ -98,7 +119,7 @@ - (BOOL)addEvent:(id<NRMAAnalyticEventProtocol>)event { | |
[events removeObjectAtIndex:evictionIndex]; | ||
[events addObject:event]; | ||
|
||
[_persistentStore removeObjectForKey:[self createKeyForEvent:event]]; | ||
[[NRMAEventManager persistentEventStore] removeObjectForKey:[self createKeyForEvent:event]]; | ||
} | ||
} | ||
} | ||
|
@@ -113,7 +134,7 @@ - (NSString *)createKeyForEvent:(id<NRMAAnalyticEventProtocol>)event { | |
- (void)empty { | ||
@synchronized (events) { | ||
[events removeAllObjects]; | ||
[_persistentStore clearAll]; | ||
[[NRMAEventManager persistentEventStore] clearAll]; | ||
oldestEventTimestamp = 0; | ||
totalAttemptedInserts = 0; | ||
} | ||
|
@@ -144,6 +165,42 @@ - (nullable NSString *)getEventJSONStringWithError:(NSError *__autoreleasing *)e | |
return eventJsonString; | ||
} | ||
|
||
+ (NSMutableArray<NRMAAnalyticEventProtocol> *)getLastSessionEventsArray | ||
{ | ||
NSDictionary *lastSessionEvents = [[NRMAEventManager persistentEventStore] getLastSessionEvents]; | ||
NSMutableArray<NRMAAnalyticEventProtocol> * events = (NSMutableArray<NRMAAnalyticEventProtocol> *)[[NSMutableArray alloc] initWithArray:[lastSessionEvents allValues]]; | ||
return events; | ||
} | ||
|
||
+ (NSString *)getLastSessionEventsString { | ||
NSDictionary *lastSessionEvents = [[NRMAEventManager persistentEventStore] getLastSessionEvents]; | ||
NSString *lastSessionEventJsonString = nil; | ||
@synchronized (lastSessionEvents) { | ||
@try { | ||
NSMutableArray *jsonEvents = [[NSMutableArray alloc] init]; | ||
for(id<NRMAAnalyticEventProtocol> event in lastSessionEvents.allValues) { | ||
[jsonEvents addObject:[event JSONObject]]; | ||
|
||
NSData *lastSessionEventJsonData = [NRMAJSON dataWithJSONObject:jsonEvents | ||
options:0 | ||
error:nil]; | ||
lastSessionEventJsonString = [[NSString alloc] initWithData:lastSessionEventJsonData | ||
encoding:NSUTF8StringEncoding]; | ||
} | ||
} | ||
@catch (NSException *e) { | ||
NRLOG_ERROR(@"FAILED TO CREATE LAST SESSION EVENT JSON: %@", e.reason); | ||
} | ||
} | ||
|
||
return lastSessionEventJsonString; | ||
} | ||
|
||
+ (void) clearDuplicationStores | ||
{ | ||
[[NRMAEventManager persistentEventStore] clearAll]; | ||
} | ||
|
||
+ (NSString *)getLastSessionEventsFromFilename:(NSString *)filename { | ||
NSDictionary *lastSessionEvents = [PersistentEventStore getLastSessionEventsFromFilename:filename]; | ||
NSString *lastSessionEventJsonString = nil; | ||
|
Uh oh!
There was an error while loading. Please reload this page.