Skip to content

Commit 41b0633

Browse files
NR-313973: Add Instrumentation Insights to /log (#307)
* NR-313973: Add Instrumentation Insights to /log * NR-313973: fix * NR-313973: adjust NRLogerTests
1 parent d513b70 commit 41b0633

File tree

3 files changed

+58
-17
lines changed

3 files changed

+58
-17
lines changed

Agent/Public/NRLogger.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ typedef enum _NRLogTargets {
7272
#define NRLogMessageMethodKey @"method"
7373
#define NRLogMessageTimestampKey @"timestamp"
7474
#define NRLogMessageMessageKey @"message"
75+
#define NRLogMessageSessionIdKey @"sessionId"
76+
#define NRLogMessageAppIdKey @"appId"
77+
78+
#define NRLogMessageEntityGuidKey @"entity.guid"
79+
#define NRLogMessageInstrumentationProviderKey @"instrumentation.provider"
80+
#define NRLogMessageMobileValue @"mobile"
81+
#define NRLogMessageInstrumentationNameKey @"instrumentation.name"
82+
#define NRLogMessageInstrumentationVersionKey @"instrumentation.version"
83+
#define NRLogMessageInstrumentationCollectorKey @"collector.name"
7584

7685
/*******************************************************************************
7786
* Log targets used in the agent's internal logger

Agent/Utilities/NRLogger.m

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ - (void)addLogMessage:(NSDictionary *)message : (BOOL) agentLogsOn {
303303
- (NSData*) jsonDictionary:(NSDictionary*)message {
304304
NSString* NRSessionId = [[[NewRelicAgentInternal sharedInstance] currentSessionId] copy];
305305
NRMAHarvesterConfiguration *configuration = [NRMAHarvestController configuration];
306+
NSString* nativePlatform = [NewRelicInternalUtils agentName];
307+
NSString* platform = [NewRelicInternalUtils stringFromNRMAApplicationPlatform:[NRMAAgentConfiguration connectionInformation].deviceInformation.platform];
308+
NSString* name = [NRMAAgentConfiguration connectionInformation].deviceInformation.platform == NRMAPlatform_Native ? nativePlatform : platform;
306309

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

331333
NSDictionary *requiredAttributes = @{NRLogMessageLevelKey: [message objectForKey:NRLogMessageLevelKey], // 1
332334
NRLogMessageFileKey: [[message objectForKey:NRLogMessageFileKey]stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""], // 2
333335
NRLogMessageLineNumberKey: [message objectForKey:NRLogMessageLineNumberKey], // 3
334336
NRLogMessageMethodKey: [[message objectForKey:NRLogMessageMethodKey]stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""], // 4
335337
NRLogMessageTimestampKey: [message objectForKey:NRLogMessageTimestampKey], // 5
336338
NRLogMessageMessageKey: [[message objectForKey:NRLogMessageMessageKey]stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""],// 6
337-
@"sessionId": NRSessionId, // 7
338-
@"appId": nrAppId, // 8
339-
@"entity.guid": entityGuid}; // 9
339+
NRLogMessageSessionIdKey: NRSessionId, // 7
340+
NRLogMessageAppIdKey: nrAppId, // 8
341+
NRLogMessageEntityGuidKey: entityGuid, // 9
342+
NRLogMessageInstrumentationProviderKey: NRLogMessageMobileValue, // 10
343+
NRLogMessageInstrumentationNameKey: name, // 11
344+
NRLogMessageInstrumentationVersionKey: [NRMAAgentConfiguration connectionInformation].deviceInformation.agentVersion, // 12
345+
NRLogMessageInstrumentationCollectorKey: name}; // 13
346+
340347

341348
NSMutableDictionary *providedAttributes = [message mutableCopy];
342349
[providedAttributes removeObjectsForKeys:@[NRLogMessageLevelKey,NRLogMessageFileKey,NRLogMessageLineNumberKey,NRLogMessageMethodKey,NRLogMessageTimestampKey,NRLogMessageMessageKey]];
@@ -346,8 +353,15 @@ - (NSData*) jsonDictionary:(NSDictionary*)message {
346353
NSData *logJsonData = [NRMAJSON dataWithJSONObject:providedAttributes
347354
options:0
348355
error:&error];
349-
350-
return logJsonData;
356+
if (!error) {
357+
return logJsonData;
358+
359+
}
360+
else {
361+
NRLOG_AGENT_ERROR(@"Failed to create log payload w error = %@", error);
362+
363+
return nil;
364+
}
351365
}
352366

353367
- (void)setLogLevels:(unsigned int)levels {

Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.m

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ - (void) setUp
3636
[NRLogger setLogEntityGuid:@"Entity-Guid-XXXX"];
3737

3838
NRMAAgentConfiguration *config = [[NRMAAgentConfiguration alloc] initWithAppToken:[[NRMAAppToken alloc] initWithApplicationToken:kNRMA_ENABLED_STAGING_APP_TOKEN]
39-
collectorAddress:KNRMA_TEST_COLLECTOR_HOST
40-
crashAddress:nil];
39+
collectorAddress:KNRMA_TEST_COLLECTOR_HOST
40+
crashAddress:nil];
4141
[NRMAHarvestController initialize:config];
4242

4343
category = @"hello";
4444
name = @"world";
45-
45+
4646
helper = [[NRMAMeasurementConsumerHelper alloc] initWithType:NRMAMT_NamedValue];
4747

4848
[NRMAMeasurements initializeMeasurements];
@@ -102,16 +102,20 @@ - (void) testNRLogger {
102102
XCTFail(@"Timeout error");
103103
}
104104
}];
105-
105+
106+
NSError* error;
106107
NSString *path = [NRLogger logFilePath];
107-
NSData* logData = [NSData dataWithContentsOfFile:path];
108+
NSData* logData = [NRLogger logFileData:&error];
109+
if(error){
110+
NSLog(@"%@", error.localizedDescription);
111+
}
108112

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

112116
NSArray* decode = [NSJSONSerialization JSONObjectWithData:formattedData
113-
options:0
114-
error:nil];
117+
options:0
118+
error:nil];
115119
NSLog(@"decode=%@", decode);
116120

117121
NSArray * expectedValues = @[
@@ -134,6 +138,20 @@ - (void) testNRLogger {
134138
if ([[dict2 objectForKey:@"message"] isEqualToString: currentMessage]) {
135139
foundCount += 1;
136140
XCTAssertTrue([[dict2 objectForKey:@"entity.guid"] isEqualToString:@"Entity-Guid-XXXX"],@"entity.guid set incorrectly");
141+
XCTAssertTrue([[dict2 objectForKey:NRLogMessageInstrumentationProviderKey] isEqualToString:NRLogMessageMobileValue],@"instrumentation provider set incorrectly");
142+
XCTAssertTrue([[dict2 objectForKey:NRLogMessageInstrumentationVersionKey] isEqualToString:@"DEV"],@"instrumentation name set incorrectly");
143+
144+
#if TARGET_OS_WATCH
145+
XCTAssertTrue([[dict2 objectForKey:NRLogMessageInstrumentationNameKey] isEqualToString:@"watchOSAgent"],@"instrumentation name set incorrectly");
146+
#else
147+
if ([[[UIDevice currentDevice] systemName] isEqualToString:@"tvOS"]) {
148+
XCTAssertTrue([[dict2 objectForKey:NRLogMessageInstrumentationNameKey] isEqualToString:@"tvOSAgent"],@"instrumentation name set incorrectly");
149+
150+
}
151+
else {
152+
XCTAssertTrue([[dict2 objectForKey:NRLogMessageInstrumentationNameKey] isEqualToString:@"iOSAgent"],@"instrumentation name set incorrectly");
153+
}
154+
#endif
137155
}
138156
// Verify added attributes with logAttributes.
139157
if ([[dict2 objectForKey:@"message"] isEqualToString:@"This is a test message for the New Relic logging system."]) {
@@ -148,7 +166,7 @@ - (void) testNRLogger {
148166

149167

150168
- (void) testRemoteLogLevels {
151-
169+
152170
// Set the remote log level to warning.
153171
[NRLogger setRemoteLogLevel:NRLogLevelWarning];
154172

@@ -163,7 +181,7 @@ - (void) testRemoteLogLevels {
163181
XCTFail(@"Timeout error");
164182
}
165183
}];
166-
184+
167185
// Three messages should reach the remote log file for upload.
168186

169187
[NewRelic logInfo: @"Info Log..."];
@@ -199,8 +217,8 @@ - (void) testRemoteLogLevels {
199217
NSString* logMessagesJson = [NSString stringWithFormat:@"[ %@ ]", [[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding]];
200218
NSData* formattedData = [logMessagesJson dataUsingEncoding:NSUTF8StringEncoding];
201219
NSArray* decode = [NSJSONSerialization JSONObjectWithData:formattedData
202-
options:0
203-
error:nil];
220+
options:0
221+
error:nil];
204222
NSLog(@"decode=%@", decode);
205223

206224
NSArray * expectedValues = @[

0 commit comments

Comments
 (0)