Skip to content

Commit 5589ed0

Browse files
NR-331557: Common Block impl
1 parent 925dbff commit 5589ed0

File tree

1 file changed

+68
-16
lines changed

1 file changed

+68
-16
lines changed

Agent/Utilities/NRLogger.m

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,14 @@ - (void)addLogMessage:(NSDictionary *)message : (BOOL) agentLogsOn {
286286
});
287287
}
288288

289-
- (NSData*) jsonDictionary:(NSDictionary*)message {
289+
- (NSMutableDictionary*) commonBlockDict {
290290
NSString* NRSessionId = [[[NewRelicAgentInternal sharedInstance] currentSessionId] copy];
291291
NRMAHarvesterConfiguration *configuration = [NRMAHarvestController configuration];
292+
293+
// The following line generates the native platform name which is used as "collector.name" in the log payload.
292294
NSString* nativePlatform = [NewRelicInternalUtils agentName];
295+
296+
// The following code generates the variable name that is chosen between native platform name or the Hybrid platform name which is used as "instrumentation.name" in the log payload.
293297
NSString* platform = [NewRelicInternalUtils stringFromNRMAApplicationPlatform:[NRMAAgentConfiguration connectionInformation].deviceInformation.platform];
294298
NSString* name = [NRMAAgentConfiguration connectionInformation].deviceInformation.platform == NRMAPlatform_Native ? nativePlatform : platform;
295299

@@ -316,6 +320,20 @@ - (NSData*) jsonDictionary:(NSDictionary*)message {
316320
entityGuid = @"";
317321
}
318322

323+
NSMutableDictionary *commonAttributes = [NSMutableDictionary dictionary];
324+
[commonAttributes setObject:entityGuid forKey:NRLogMessageEntityGuidKey];
325+
if (NRSessionId) [commonAttributes setObject:NRSessionId forKey:NRLogMessageSessionIdKey];
326+
[commonAttributes setObject:NRLogMessageMobileValue forKey:NRLogMessageInstrumentationProviderKey];
327+
if (name) [commonAttributes setObject:name forKey:NRLogMessageInstrumentationNameKey];
328+
[commonAttributes setObject:[NRMAAgentConfiguration connectionInformation].deviceInformation.agentVersion forKey:NRLogMessageInstrumentationVersionKey];
329+
if (nativePlatform) [commonAttributes setObject:nativePlatform forKey:NRLogMessageInstrumentationCollectorKey];
330+
if (nrAppId) [commonAttributes setObject:nrAppId forKey:NRLogMessageAppIdKey];
331+
332+
return commonAttributes;
333+
}
334+
335+
- (NSData*) jsonDictionary:(NSDictionary*)message {
336+
319337
NSMutableDictionary *requiredAttributes = [NSMutableDictionary dictionary];
320338

321339
id value = [message objectForKey:NRLogMessageLevelKey];
@@ -336,19 +354,6 @@ - (NSData*) jsonDictionary:(NSDictionary*)message {
336354
value = [[message objectForKey:NRLogMessageMessageKey]stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
337355
if (value) [requiredAttributes setObject:value forKey:NRLogMessageMessageKey]; // 6
338356

339-
if (NRSessionId) [requiredAttributes setObject:NRSessionId forKey:NRLogMessageSessionIdKey]; // 7
340-
if (nrAppId) [requiredAttributes setObject:nrAppId forKey:NRLogMessageAppIdKey]; // 8
341-
if (entityGuid) [requiredAttributes setObject:entityGuid forKey:NRLogMessageEntityGuidKey]; // 9
342-
343-
[requiredAttributes setObject:NRLogMessageMobileValue forKey:NRLogMessageInstrumentationProviderKey]; // 10
344-
[requiredAttributes setObject:name forKey:NRLogMessageInstrumentationNameKey]; // 11
345-
346-
value = [NRMAAgentConfiguration connectionInformation].deviceInformation.agentVersion;
347-
if (value) [requiredAttributes setObject:value forKey:NRLogMessageInstrumentationVersionKey]; // 12
348-
349-
[requiredAttributes setObject:nativePlatform forKey:NRLogMessageInstrumentationCollectorKey]; // 13
350-
351-
352357
NSMutableDictionary *providedAttributes = [message mutableCopy];
353358
[providedAttributes removeObjectsForKeys:@[NRLogMessageLevelKey,NRLogMessageFileKey,NRLogMessageLineNumberKey,NRLogMessageMethodKey,NRLogMessageTimestampKey,NRLogMessageMessageKey]];
354359
[providedAttributes addEntriesFromDictionary:requiredAttributes];
@@ -531,9 +536,56 @@ - (void)enqueueLogUpload {
531536
return;
532537
}
533538

534-
NSString* logMessagesJson = [NSString stringWithFormat:@"[ %@ ]", [[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding]];
539+
540+
541+
// modify to contain
542+
/*
543+
[{
544+
"common": {
545+
"attributes": {
546+
"logtype": "accesslogs",
547+
"service": "login-service",
548+
"hostname": "login.example.com"
549+
}
550+
},
551+
"logs": [{
552+
"timestamp": <TIMESTAMP_IN_UNIX_EPOCH_OR_IS08601_FORMAT>,
553+
"message": "User 'xyz' logged in"
554+
},{
555+
"timestamp": <TIMESTAMP_IN_UNIX_EPOCH_OR_IS08601_FORMAT>,
556+
"message": "User 'xyz' logged out",
557+
"attributes": {
558+
"auditId": 123
559+
}
560+
}]
561+
}]
562+
563+
*/
564+
565+
// the text of the file contents is just comma separated dict objects
566+
// Add the user provided attributes to the message.
567+
NSMutableDictionary *commonBlock = [self commonBlockDict];
568+
569+
NSError* error = nil;
570+
571+
NSData *json = [NRMAJSON dataWithJSONObject:commonBlock
572+
options:0
573+
error:&error];
574+
575+
if (error) {
576+
NRLOG_AGENT_ERROR(@"Failed to create log payload w error = %@", error);
577+
}
578+
579+
// New version of the line
580+
NSString* logMessagesJson = [NSString stringWithFormat:@"[{ \"common\": { \"attributes\": %@}, \"logs\": [ %@ ] }]",
581+
[[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding],
582+
[[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding]];
583+
584+
// Old version of the line
585+
// NSString* logMessagesJson = [NSString stringWithFormat:@"[ %@ ]", [[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding]];
586+
535587
NSData* formattedData = [logMessagesJson dataUsingEncoding:NSUTF8StringEncoding];
536-
588+
537589
// We clear the log when we save the existing logs to uploadQueue.
538590
[self clearLog];
539591

0 commit comments

Comments
 (0)