Skip to content

Commit

Permalink
NR-331577: Impl Log common block
Browse files Browse the repository at this point in the history
  • Loading branch information
cdillard-NewRelic committed Dec 19, 2024
1 parent 5589ed0 commit 5a50928
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 78 deletions.
56 changes: 15 additions & 41 deletions Agent/Utilities/NRLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -536,50 +536,24 @@ - (void)enqueueLogUpload {
return;
}

// the text of the file contents is just comma separated dict objects
// Add the user provided attributes to the message.
NSMutableDictionary *commonBlock = [self commonBlockDict];

NSError* error = nil;

// modify to contain
/*
[{
"common": {
"attributes": {
"logtype": "accesslogs",
"service": "login-service",
"hostname": "login.example.com"
}
},
"logs": [{
"timestamp": <TIMESTAMP_IN_UNIX_EPOCH_OR_IS08601_FORMAT>,
"message": "User 'xyz' logged in"
},{
"timestamp": <TIMESTAMP_IN_UNIX_EPOCH_OR_IS08601_FORMAT>,
"message": "User 'xyz' logged out",
"attributes": {
"auditId": 123
}
}]
}]
*/

// the text of the file contents is just comma separated dict objects
// Add the user provided attributes to the message.
NSMutableDictionary *commonBlock = [self commonBlockDict];

NSError* error = nil;

NSData *json = [NRMAJSON dataWithJSONObject:commonBlock
options:0
error:&error];

if (error) {
NRLOG_AGENT_ERROR(@"Failed to create log payload w error = %@", error);
}
NSData *json = [NRMAJSON dataWithJSONObject:commonBlock
options:0
error:&error];

if (error) {
NRLOG_AGENT_ERROR(@"Failed to create log payload w error = %@", error);
}

// New version of the line
NSString* logMessagesJson = [NSString stringWithFormat:@"[{ \"common\": { \"attributes\": %@}, \"logs\": [ %@ ] }]",
[[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding],
[[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding]];
// New version of the line
NSString* logMessagesJson = [NSString stringWithFormat:@"[{ \"common\": { \"attributes\": %@}, \"logs\": [ %@ ] }]",
[[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding],
[[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding]];

// Old version of the line
// NSString* logMessagesJson = [NSString stringWithFormat:@"[ %@ ]", [[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding]];
Expand Down
162 changes: 125 additions & 37 deletions Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
#import "NRAutoLogCollector.h"
#import <os/log.h>

@interface NRLogger()
+ (NRLogger *)logger;
- (NSMutableDictionary*) commonBlockDict;
@end

@implementation NRLoggerTests
- (void) setUp
{
Expand Down Expand Up @@ -69,8 +74,6 @@ - (void) setUp
weakSelf.fileDescriptor = 0;
}
});


}
- (void) tearDown
{
Expand Down Expand Up @@ -108,20 +111,40 @@ - (void) testNRLogger {

sleep(5);

NSError* error;
NSError* error = nil;
NSData* logData = [NRLogger logFileData:&error];
if(error){
NSLog(@"%@", error.localizedDescription);
}

NSString* logMessagesJson = [NSString stringWithFormat:@"[ %@ ]", [[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding]];
NSMutableDictionary *commonBlock = [[NRLogger logger] commonBlockDict];

NSData *json = [NRMAJSON dataWithJSONObject:commonBlock
options:0
error:&error];

if (error) {
NRLOG_AGENT_ERROR(@"Failed to create log payload w error = %@", error);
XCTAssertNil(error, @"Error creating log payload");
return;
}

error = nil;
// New version of the line
NSString* logMessagesJson = [NSString stringWithFormat:@"[{ \"common\": { \"attributes\": %@}, \"logs\": [ %@ ] }]",
[[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding],
[[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding]];

NSData* formattedData = [logMessagesJson dataUsingEncoding:NSUTF8StringEncoding];

NSArray* decode = [NSJSONSerialization JSONObjectWithData:formattedData
NSDictionary* decode = [NSJSONSerialization JSONObjectWithData:formattedData
options:0
error:nil];
error:&error];
NSLog(@"decode=%@", decode);

NSArray *decodedArray = [[decode valueForKey:@"logs"] objectAtIndex:0];
NSDictionary *decodedCommonBlock = [[[decode valueForKey:@"common"] objectAtIndex:0] valueForKey:@"attributes"];

NSArray * expectedValues = @[
@{@"message": @"Info Log..."},
@{@"message": @"Error Log..."},
Expand All @@ -131,31 +154,19 @@ - (void) testNRLogger {
@{@"message": @"Debug Log..."},
@{@"message": @"This is a test message for the New Relic logging system."},
];

// check for existence of 6 logs.
int foundCount = 0;
// For each expected message.
for (NSDictionary *dict in expectedValues) {
// Iterate through the collected message logs.
for (NSDictionary *dict2 in decode) {
for (NSDictionary *dict2 in decodedArray) {
//
NSString* currentMessage = [dict objectForKey:@"message"];

// Check the logs entries
if ([[dict2 objectForKey:@"message"] isEqualToString: currentMessage]) {
foundCount += 1;
XCTAssertTrue([[dict2 objectForKey:@"entity.guid"] isEqualToString:@"Entity-Guid-XXXX"],@"entity.guid set incorrectly");
XCTAssertTrue([[dict2 objectForKey:NRLogMessageInstrumentationProviderKey] isEqualToString:NRLogMessageMobileValue],@"instrumentation provider set incorrectly");
XCTAssertTrue([[dict2 objectForKey:NRLogMessageInstrumentationVersionKey] isEqualToString:@"DEV"],@"instrumentation name set incorrectly");

#if TARGET_OS_WATCH
XCTAssertTrue([[dict2 objectForKey:NRLogMessageInstrumentationNameKey] isEqualToString:@"watchOSAgent"],@"instrumentation name set incorrectly");
#else
if ([[[UIDevice currentDevice] systemName] isEqualToString:@"tvOS"]) {
XCTAssertTrue([[dict2 objectForKey:NRLogMessageInstrumentationNameKey] isEqualToString:@"tvOSAgent"],@"instrumentation name set incorrectly");

}
else {
XCTAssertTrue([[dict2 objectForKey:NRLogMessageInstrumentationNameKey] isEqualToString:@"iOSAgent"],@"instrumentation name set incorrectly");
}
#endif
}
// Verify added attributes with logAttributes.
if ([[dict2 objectForKey:@"message"] isEqualToString:@"This is a test message for the New Relic logging system."]) {
Expand All @@ -166,6 +177,22 @@ - (void) testNRLogger {
}

XCTAssertEqual(foundCount, 7, @"Seven messages should be found.");

// Verify Common Block
XCTAssertTrue([[decodedCommonBlock objectForKey:@"entity.guid"] isEqualToString:@"Entity-Guid-XXXX"],@"entity.guid set incorrectly");
XCTAssertTrue([[decodedCommonBlock objectForKey:NRLogMessageInstrumentationProviderKey] isEqualToString:NRLogMessageMobileValue],@"instrumentation provider set incorrectly");
XCTAssertTrue([[decodedCommonBlock objectForKey:NRLogMessageInstrumentationVersionKey] isEqualToString:@"DEV"],@"instrumentation name set incorrectly");

#if TARGET_OS_WATCH
XCTAssertTrue([[decodedCommonBlock objectForKey:NRLogMessageInstrumentationNameKey] isEqualToString:@"watchOSAgent"],@"instrumentation name set incorrectly");
#else
if ([[[UIDevice currentDevice] systemName] isEqualToString:@"tvOS"]) {
XCTAssertTrue([[decodedCommonBlock objectForKey:NRLogMessageInstrumentationNameKey] isEqualToString:@"tvOSAgent"],@"instrumentation name set incorrectly");
}
else {
XCTAssertTrue([[decodedCommonBlock objectForKey:NRLogMessageInstrumentationNameKey] isEqualToString:@"iOSAgent"],@"instrumentation name set incorrectly");
}
#endif
}


Expand Down Expand Up @@ -223,13 +250,34 @@ - (void) testRemoteLogLevels {
if(error){
NSLog(@"%@", error.localizedDescription);
}
NSString* logMessagesJson = [NSString stringWithFormat:@"[ %@ ]", [[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding]];
NSMutableDictionary *commonBlock = [[NRLogger logger] commonBlockDict];

NSData *json = [NRMAJSON dataWithJSONObject:commonBlock
options:0
error:&error];

if (error) {
NRLOG_AGENT_ERROR(@"Failed to create log payload w error = %@", error);
XCTAssertNil(error, @"Error creating log payload");
return;
}

error = nil;
// New version of the line
NSString* logMessagesJson = [NSString stringWithFormat:@"[{ \"common\": { \"attributes\": %@}, \"logs\": [ %@ ] }]",
[[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding],
[[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding]];

NSData* formattedData = [logMessagesJson dataUsingEncoding:NSUTF8StringEncoding];
NSArray* decode = [NSJSONSerialization JSONObjectWithData:formattedData

NSDictionary* decode = [NSJSONSerialization JSONObjectWithData:formattedData
options:0
error:nil];
error:&error];
NSLog(@"decode=%@", decode);

NSArray *decodedArray = [[decode valueForKey:@"logs"] objectAtIndex:0];
NSDictionary *decodedCommonBlock = [[[decode valueForKey:@"common"] objectAtIndex:0] valueForKey:@"attributes"];

NSArray * expectedValues = @[
@{@"message": @"Info Log..."},
@{@"message": @"Error Log..."},
Expand All @@ -244,12 +292,11 @@ - (void) testRemoteLogLevels {
// For each expected message.
for (NSDictionary *dict in expectedValues) {
// Iterate through the collected message logs.
for (NSDictionary *dict2 in decode) {
for (NSDictionary *dict2 in decodedArray) {
//
NSString* currentMessage = [dict objectForKey:@"message"];
if ([[dict2 objectForKey:@"message"] isEqualToString: currentMessage]) {
foundCount += 1;
XCTAssertTrue([[dict2 objectForKey:@"entity.guid"] isEqualToString:@"Entity-Guid-XXXX"],@"entity.guid set incorrectly");
}
// Verify added attributes with logAttributes.
if ([[dict2 objectForKey:@"message"] isEqualToString:@"This is a test message for the New Relic logging system."]) {
Expand Down Expand Up @@ -314,13 +361,34 @@ - (void) testLocalLogLevels {
if(error){
NSLog(@"%@", error.localizedDescription);
}
NSString* logMessagesJson = [NSString stringWithFormat:@"[ %@ ]", [[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding]];
NSMutableDictionary *commonBlock = [[NRLogger logger] commonBlockDict];

NSData *json = [NRMAJSON dataWithJSONObject:commonBlock
options:0
error:&error];

if (error) {
NRLOG_AGENT_ERROR(@"Failed to create log payload w error = %@", error);
XCTAssertNil(error, @"Error creating log payload");
return;
}

error = nil;
// New version of the line
NSString* logMessagesJson = [NSString stringWithFormat:@"[{ \"common\": { \"attributes\": %@}, \"logs\": [ %@ ] }]",
[[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding],
[[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding]];

NSData* formattedData = [logMessagesJson dataUsingEncoding:NSUTF8StringEncoding];
NSArray* decode = [NSJSONSerialization JSONObjectWithData:formattedData

NSDictionary* decode = [NSJSONSerialization JSONObjectWithData:formattedData
options:0
error:nil];
error:&error];
NSLog(@"decode=%@", decode);

NSArray *decodedArray = [[decode valueForKey:@"logs"] objectAtIndex:0];
NSDictionary *decodedCommonBlock = [[[decode valueForKey:@"common"] objectAtIndex:0] valueForKey:@"attributes"];

NSArray * expectedValues = @[
@{@"message": @"Info Log..."},
@{@"message": @"Error Log..."},
Expand All @@ -335,12 +403,11 @@ - (void) testLocalLogLevels {
// For each expected message.
for (NSDictionary *dict in expectedValues) {
// Iterate through the collected message logs.
for (NSDictionary *dict2 in decode) {
for (NSDictionary *dict2 in decodedArray) {
//
NSString* currentMessage = [dict objectForKey:@"message"];
if ([[dict2 objectForKey:@"message"] isEqualToString: currentMessage]) {
foundCount += 1;
XCTAssertTrue([[dict2 objectForKey:@"entity.guid"] isEqualToString:@"Entity-Guid-XXXX"],@"entity.guid set incorrectly");
}
// Verify added attributes with logAttributes.
if ([[dict2 objectForKey:@"message"] isEqualToString:@"This is a test message for the New Relic logging system."]) {
Expand Down Expand Up @@ -400,13 +467,35 @@ - (void) testAutoCollectedLogs {
if(error){
NSLog(@"%@", error.localizedDescription);
}
NSString* logMessagesJson = [NSString stringWithFormat:@"[ %@ ]", [[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding]];
NSMutableDictionary *commonBlock = [[NRLogger logger] commonBlockDict];

NSData *json = [NRMAJSON dataWithJSONObject:commonBlock
options:0
error:&error];

if (error) {
NRLOG_AGENT_ERROR(@"Failed to create log payload w error = %@", error);
XCTAssertNil(error, @"Error creating log payload");
return;
}

error = nil;
// New version of the line
NSString* logMessagesJson = [NSString stringWithFormat:@"[{ \"common\": { \"attributes\": %@}, \"logs\": [ %@ ] }]",
[[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding],
[[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding]];

NSData* formattedData = [logMessagesJson dataUsingEncoding:NSUTF8StringEncoding];
NSArray* decode = [NSJSONSerialization JSONObjectWithData:formattedData

NSDictionary* decode = [NSJSONSerialization JSONObjectWithData:formattedData
options:0
error:nil];
error:&error];
NSLog(@"decode=%@", decode);

NSArray *decodedArray = [[decode valueForKey:@"logs"] objectAtIndex:0];
NSDictionary *decodedCommonBlock = [[[decode valueForKey:@"common"] objectAtIndex:0] valueForKey:@"attributes"];


NSArray * expectedValues = @[
@{@"message": @"NSLog Test"},
@{@"message": @"This is a default os_log message."},
Expand All @@ -419,12 +508,11 @@ - (void) testAutoCollectedLogs {
// For each expected message.
for (NSDictionary *dict in expectedValues) {
// Iterate through the collected message logs.
for (NSDictionary *dict2 in decode) {
for (NSDictionary *dict2 in decodedArray) {
//
NSString* currentMessage = [dict objectForKey:@"message"];
if ([[dict2 objectForKey:@"message"] containsString: currentMessage]) {
foundCount += 1;
XCTAssertTrue([[dict2 objectForKey:@"entity.guid"] isEqualToString:@"Entity-Guid-XXXX"],@"entity.guid set incorrectly");
}
// Verify added attributes with logAttributes.
if ([[dict2 objectForKey:@"message"] isEqualToString:@"This is a test message for the New Relic logging system."]) {
Expand Down

0 comments on commit 5a50928

Please sign in to comment.