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

NR-352010 Changed NRMASAM to add the persisted attributes individually so that they get re-persisted #342

Merged
merged 7 commits into from
Jan 31, 2025
25 changes: 13 additions & 12 deletions Agent/Analytics/NRMASAM.mm
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,31 @@ @implementation NRMASAM {
- (id)initWithAttributeValidator:(__nullable id<AttributeValidatorProtocol>)validator {
self = [super init];
if (self) {
attributeValidator = validator;

_attributePersistentStore = [[PersistentEventStore alloc] initWithFilename:[NRMASAM attributeFilePath] andMinimumDelay:.025];

_privateAttributePersistentStore = [[PersistentEventStore alloc] initWithFilename:[NRMASAM privateAttributeFilePath] andMinimumDelay:.025];

// Load public attributes from file.
NSDictionary *lastSessionAttributes = [PersistentEventStore getLastSessionEventsFromFilename:[NRMASAM attributeFilePath]];
attributeDict = [[NSMutableDictionary alloc] init];
if (lastSessionAttributes != nil) {
attributeDict = [lastSessionAttributes mutableCopy];
}
if (!attributeDict) {
attributeDict = [[NSMutableDictionary alloc] init];
for(NSString* key in [lastSessionAttributes allKeys]) {
[self setAttribute:key value:[lastSessionAttributes valueForKey:key]];
}
}

// Load private attributes from file.
NSDictionary *lastSessionPrivateAttributes = [PersistentEventStore getLastSessionEventsFromFilename:[NRMASAM privateAttributeFilePath]];

privateAttributeDict = [[NSMutableDictionary alloc] init];

if (lastSessionPrivateAttributes != nil) {
privateAttributeDict = [lastSessionPrivateAttributes mutableCopy];
}
if (!privateAttributeDict) {
privateAttributeDict = [[NSMutableDictionary alloc] init];
for(NSString* key in [lastSessionPrivateAttributes allKeys]) {
[self setNRSessionAttribute:key value:[lastSessionPrivateAttributes valueForKey:key]];
}
}

attributeValidator = validator;

}
return self;
}
Expand Down Expand Up @@ -221,7 +222,7 @@ - (NSString*) sessionAttributeJSONString {
+ (NSString*) getLastSessionsAttributes {
NSError *error;
NSString *lastSessionAttributesJsonString = nil;
NSDictionary *lastSessionAttributes = [PersistentEventStore getLastSessionEventsFromFilename:[self attributeFilePath]];
NSDictionary *lastSessionAttributes = [PersistentEventStore getLastSessionEventsFromFilename:[NRMASAM attributeFilePath]];
NSDictionary *lastSessionPrivateAttributes = [PersistentEventStore getLastSessionEventsFromFilename:[NRMASAM privateAttributeFilePath]];

NSMutableDictionary *mergedDictionary = [NSMutableDictionary dictionary];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,21 @@ @implementation NRMASAMTest

- (void)setUp {
[super setUp];
NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:[NSString stringWithFormat:@"%@/%@",[NewRelicInternalUtils getStorePath],kNRMA_Attrib_file]]) {
[fileManager removeItemAtPath:[NSString stringWithFormat:@"%@/%@",[NewRelicInternalUtils getStorePath],kNRMA_Attrib_file] error:nil];
}
if([fileManager fileExistsAtPath:[NSString stringWithFormat:@"%@/%@",[NewRelicInternalUtils getStorePath],kNRMA_Attrib_file_private]]) {
[fileManager removeItemAtPath:[NSString stringWithFormat:@"%@/%@",[NewRelicInternalUtils getStorePath],kNRMA_Attrib_file_private] error:nil];
}

// [NRLogger setLogLevels:NRLogLevelDebug];

manager = [self samTest];
}

- (void) tearDown {
[super tearDown];
[manager removeAllSessionAttributes];
}

Expand Down Expand Up @@ -129,6 +142,7 @@ - (void) testIncrementSessionAttribute {
error:nil];
XCTAssertTrue([decode[attribute] isEqual:@(2)]);
}

- (void) testIncrementSessionAttributeDiffTypes {
NSString* attribute = @"incrementableAttribute";
float initialValue = 1.2;
Expand Down Expand Up @@ -266,8 +280,68 @@ - (void) testClearLastSessionsAnalytics {

}

- (void)waitForAttributesToPersist:(NSArray<NSString *> *)expectedAttributes timeout:(NSTimeInterval)timeout {
NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeout];
while ([timeoutDate timeIntervalSinceNow] > 0) {
NSString *attributes = [NRMASAM getLastSessionsAttributes];
NSDictionary *decode = [NSJSONSerialization JSONObjectWithData:[attributes dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];

BOOL allAttributesPersisted = YES;
for (NSString *attribute in expectedAttributes) {
if (![decode objectForKey:attribute]) {
allAttributesPersisted = NO;
break;
}
}

if (allAttributesPersisted) {
return;
}

// Wait a short period before retrying
[NSThread sleepForTimeInterval:0.1];
}
XCTFail(@"Failed to persist expected attributes.");
}

- (void)testPersistedSessionAnalytics {

NSString *attribute = @"blarg";
NSString *attribute2 = @"blarg2";
NSString *attribute3 = @"privateBlarg3";
NSString *attribute4 = @"Blarg4";
NSString *attribute5 = @"privateBlarg5";

// Set attributes
XCTAssertTrue([manager setSessionAttribute:attribute value:@"blurg"], @"Failed to successfully set session attribute");
XCTAssertTrue([manager setSessionAttribute:attribute2 value:@"blurg2"], @"Failed to successfully set session attribute");
XCTAssertTrue([manager setNRSessionAttribute:attribute3 value:@"blurg2"], @"Failed to successfully set private session attribute");

NSString *attributes = [manager sessionAttributeJSONString];
NSDictionary *decode = [NSJSONSerialization JSONObjectWithData:[attributes dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];

XCTAssertEqual(decode.count, 3);

manager = nil;
// Wait for persistence
[self waitForAttributesToPersist:@[attribute, attribute2, attribute3] timeout:10];

manager = [self samTest];

XCTAssertTrue([manager setSessionAttribute:attribute4 value:@"blurg"], @"Failed to successfully set session attribute");
XCTAssertTrue([manager setNRSessionAttribute:attribute5 value:@"blurg2"], @"Failed to successfully set private session attribute");

attributes = [manager sessionAttributeJSONString];
decode = [NSJSONSerialization JSONObjectWithData:[attributes dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];

XCTAssertTrue([[decode allKeys] containsObject:attribute], @"Should have persisted and new attribute 1.");
XCTAssertTrue([[decode allKeys] containsObject:attribute2], @"Should have persisted and new attribute 2.");
XCTAssertTrue([[decode allKeys] containsObject:attribute3], @"Should have persisted and new private attribute 3.");
XCTAssertTrue([[decode allKeys] containsObject:attribute4], @"Should have persisted and new attribute 4.");
XCTAssertTrue([[decode allKeys] containsObject:attribute5], @"Should have persisted and new private attribute 5.");
}

- (void) testClearPersistedSessionAnalytics {
NRMASAM *manager = [self samTest];
NSString *attribute = @"blarg";
NSString *attribute2 = @"blarg2";

Expand Down
Loading