Skip to content

Commit

Permalink
NR-313973: Add Instrumentation Insights to /log (#307)
Browse files Browse the repository at this point in the history
* NR-313973: Add Instrumentation Insights to /log

* NR-313973: fix

* NR-313973: adjust NRLogerTests
  • Loading branch information
cdillard-NewRelic authored Sep 19, 2024
1 parent d513b70 commit 41b0633
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 17 deletions.
9 changes: 9 additions & 0 deletions Agent/Public/NRLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ typedef enum _NRLogTargets {
#define NRLogMessageMethodKey @"method"
#define NRLogMessageTimestampKey @"timestamp"
#define NRLogMessageMessageKey @"message"
#define NRLogMessageSessionIdKey @"sessionId"
#define NRLogMessageAppIdKey @"appId"

#define NRLogMessageEntityGuidKey @"entity.guid"
#define NRLogMessageInstrumentationProviderKey @"instrumentation.provider"
#define NRLogMessageMobileValue @"mobile"
#define NRLogMessageInstrumentationNameKey @"instrumentation.name"
#define NRLogMessageInstrumentationVersionKey @"instrumentation.version"
#define NRLogMessageInstrumentationCollectorKey @"collector.name"

/*******************************************************************************
* Log targets used in the agent's internal logger
Expand Down
26 changes: 20 additions & 6 deletions Agent/Utilities/NRLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ - (void)addLogMessage:(NSDictionary *)message : (BOOL) agentLogsOn {
- (NSData*) jsonDictionary:(NSDictionary*)message {
NSString* NRSessionId = [[[NewRelicAgentInternal sharedInstance] currentSessionId] copy];
NRMAHarvesterConfiguration *configuration = [NRMAHarvestController configuration];
NSString* nativePlatform = [NewRelicInternalUtils agentName];
NSString* platform = [NewRelicInternalUtils stringFromNRMAApplicationPlatform:[NRMAAgentConfiguration connectionInformation].deviceInformation.platform];
NSString* name = [NRMAAgentConfiguration connectionInformation].deviceInformation.platform == NRMAPlatform_Native ? nativePlatform : platform;

NSString* nrAppId = [NSString stringWithFormat:@"%lld", configuration.application_id];
NSString* entityGuid = [NSString stringWithFormat:@"%@", configuration.entity_guid];
Expand All @@ -326,17 +329,21 @@ - (NSData*) jsonDictionary:(NSDictionary*)message {
if (!entityGuid) {
entityGuid = @"";
}
// TODO: LogReporting logAttributes: Refactor to construct 9 required attributes and tack on the remaining API provided attributes.

NSDictionary *requiredAttributes = @{NRLogMessageLevelKey: [message objectForKey:NRLogMessageLevelKey], // 1
NRLogMessageFileKey: [[message objectForKey:NRLogMessageFileKey]stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""], // 2
NRLogMessageLineNumberKey: [message objectForKey:NRLogMessageLineNumberKey], // 3
NRLogMessageMethodKey: [[message objectForKey:NRLogMessageMethodKey]stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""], // 4
NRLogMessageTimestampKey: [message objectForKey:NRLogMessageTimestampKey], // 5
NRLogMessageMessageKey: [[message objectForKey:NRLogMessageMessageKey]stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""],// 6
@"sessionId": NRSessionId, // 7
@"appId": nrAppId, // 8
@"entity.guid": entityGuid}; // 9
NRLogMessageSessionIdKey: NRSessionId, // 7
NRLogMessageAppIdKey: nrAppId, // 8
NRLogMessageEntityGuidKey: entityGuid, // 9
NRLogMessageInstrumentationProviderKey: NRLogMessageMobileValue, // 10
NRLogMessageInstrumentationNameKey: name, // 11
NRLogMessageInstrumentationVersionKey: [NRMAAgentConfiguration connectionInformation].deviceInformation.agentVersion, // 12
NRLogMessageInstrumentationCollectorKey: name}; // 13


NSMutableDictionary *providedAttributes = [message mutableCopy];
[providedAttributes removeObjectsForKeys:@[NRLogMessageLevelKey,NRLogMessageFileKey,NRLogMessageLineNumberKey,NRLogMessageMethodKey,NRLogMessageTimestampKey,NRLogMessageMessageKey]];
Expand All @@ -346,8 +353,15 @@ - (NSData*) jsonDictionary:(NSDictionary*)message {
NSData *logJsonData = [NRMAJSON dataWithJSONObject:providedAttributes
options:0
error:&error];

return logJsonData;
if (!error) {
return logJsonData;

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

return nil;
}
}

- (void)setLogLevels:(unsigned int)levels {
Expand Down
40 changes: 29 additions & 11 deletions Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ - (void) setUp
[NRLogger setLogEntityGuid:@"Entity-Guid-XXXX"];

NRMAAgentConfiguration *config = [[NRMAAgentConfiguration alloc] initWithAppToken:[[NRMAAppToken alloc] initWithApplicationToken:kNRMA_ENABLED_STAGING_APP_TOKEN]
collectorAddress:KNRMA_TEST_COLLECTOR_HOST
crashAddress:nil];
collectorAddress:KNRMA_TEST_COLLECTOR_HOST
crashAddress:nil];
[NRMAHarvestController initialize:config];

category = @"hello";
name = @"world";

helper = [[NRMAMeasurementConsumerHelper alloc] initWithType:NRMAMT_NamedValue];

[NRMAMeasurements initializeMeasurements];
Expand Down Expand Up @@ -102,16 +102,20 @@ - (void) testNRLogger {
XCTFail(@"Timeout error");
}
}];


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

NSString* logMessagesJson = [NSString stringWithFormat:@"[ %@ ]", [[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding]];
NSData* formattedData = [logMessagesJson dataUsingEncoding:NSUTF8StringEncoding];

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

NSArray * expectedValues = @[
Expand All @@ -134,6 +138,20 @@ - (void) testNRLogger {
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 @@ -148,7 +166,7 @@ - (void) testNRLogger {


- (void) testRemoteLogLevels {

// Set the remote log level to warning.
[NRLogger setRemoteLogLevel:NRLogLevelWarning];

Expand All @@ -163,7 +181,7 @@ - (void) testRemoteLogLevels {
XCTFail(@"Timeout error");
}
}];

// Three messages should reach the remote log file for upload.

[NewRelic logInfo: @"Info Log..."];
Expand Down Expand Up @@ -199,8 +217,8 @@ - (void) testRemoteLogLevels {
NSString* logMessagesJson = [NSString stringWithFormat:@"[ %@ ]", [[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding]];
NSData* formattedData = [logMessagesJson dataUsingEncoding:NSUTF8StringEncoding];
NSArray* decode = [NSJSONSerialization JSONObjectWithData:formattedData
options:0
error:nil];
options:0
error:nil];
NSLog(@"decode=%@", decode);

NSArray * expectedValues = @[
Expand Down

0 comments on commit 41b0633

Please sign in to comment.