Skip to content

Commit 3ecb494

Browse files
Nr 348708: add session attributes to log common attribute (#341)
* NR-348708: session attributes are added to log common block * NR-348708: add test
1 parent 6b814a2 commit 3ecb494

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

Agent/Utilities/NRLogger.m

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,24 @@ - (NSMutableDictionary*) commonBlockDict {
330330
if (nativePlatform) [commonAttributes setObject:nativePlatform forKey:NRLogMessageInstrumentationCollectorKey];
331331
if (nrAppId) [commonAttributes setObject:nrAppId forKey:NRLogMessageAppIdKey];
332332

333+
334+
NSString* sessionAttributes = [[NewRelicAgentInternal sharedInstance].analyticsController sessionAttributeJSONString];
335+
if (sessionAttributes != nil && [sessionAttributes length] > 0) {
336+
NSDictionary* dictionary = [NSJSONSerialization JSONObjectWithData:[sessionAttributes dataUsingEncoding:NSUTF8StringEncoding]
337+
options:0
338+
error:nil];
339+
for (NSString *key in dictionary) {
340+
id value = [dictionary objectForKey:key];
341+
342+
343+
if (value) {
344+
[commonAttributes setObject:value forKey:key];
345+
}
346+
}
347+
}
348+
349+
350+
333351
return commonAttributes;
334352
}
335353

@@ -598,7 +616,6 @@ - (void) processNextUploadTask {
598616
NSData *formattedData = [self->uploadQueue firstObject];
599617

600618
if (self->debugLogs) {
601-
//NSString* logMessagesJson = [NSString stringWithFormat:@"[ %@ ]", [[NSString alloc] initWithData:formattedData encoding:NSUTF8StringEncoding]];
602619
NSArray* decode = [NSJSONSerialization JSONObjectWithData:formattedData
603620
options:0
604621
error:nil];
@@ -609,9 +626,6 @@ - (void) processNextUploadTask {
609626
NSMutableURLRequest* req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: self->logURL]];
610627
[req setValue:self->logIngestKey forHTTPHeaderField:@"X-App-License-Key"];
611628
[req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
612-
613-
//NSString* contentEncoding = message.length <= 512 ? kNRMAIdentityHeader : kNRMAGZipHeader;
614-
615629
[req setValue:kNRMAGZipHeader forHTTPHeaderField:kNRMAContentEncodingHeader];
616630

617631
req.HTTPMethod = @"POST";

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@
1818
}
1919
@property (nonatomic) int fileDescriptor;
2020
@property (nonatomic, strong) dispatch_source_t source;
21+
22+
@property id mockNewRelicInternals;
23+
2124
@end

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@
2424
#import "NRTestConstants.h"
2525
#import "NRAutoLogCollector.h"
2626
#import <os/log.h>
27+
#import "NewRelicAgentInternal.h"
28+
#import <OCMock/OCMock.h>
2729

2830
@interface NRLogger()
2931
+ (NRLogger *)logger;
3032
- (NSMutableDictionary*) commonBlockDict;
3133
@end
3234

35+
static NewRelicAgentInternal* _sharedInstance;
36+
3337
@implementation NRLoggerTests
3438
- (void) setUp
3539
{
@@ -39,9 +43,16 @@ - (void) setUp
3943

4044
[NRLogger setLogLevels:NRLogLevelDebug];
4145
[NRLogger setRemoteLogLevel:NRLogLevelDebug];
42-
4346
[NRLogger setLogEntityGuid:@"Entity-Guid-XXXX"];
4447

48+
49+
self.mockNewRelicInternals = [OCMockObject mockForClass:[NewRelicAgentInternal class]];
50+
_sharedInstance = [[NewRelicAgentInternal alloc] init];
51+
_sharedInstance.analyticsController = [[NRMAAnalytics alloc] initWithSessionStartTimeMS:0.0];
52+
[[[[self.mockNewRelicInternals stub] classMethod] andReturn:_sharedInstance] sharedInstance];
53+
54+
[_sharedInstance.analyticsController setSessionAttribute:@"myAttribute" value:@(1)];
55+
4556
NRMAAgentConfiguration *config = [[NRMAAgentConfiguration alloc] initWithAppToken:[[NRMAAppToken alloc] initWithApplicationToken:kNRMA_ENABLED_STAGING_APP_TOKEN]
4657
collectorAddress:KNRMA_TEST_COLLECTOR_HOST
4758
crashAddress:nil];
@@ -86,7 +97,8 @@ - (void) tearDown
8697

8798
[NRMAMeasurements removeMeasurementConsumer:helper];
8899
helper = nil;
89-
100+
[self.mockNewRelicInternals stopMocking];
101+
_sharedInstance = nil;
90102
[NRMAMeasurements shutdown];
91103
[NRMAFlags disableFeatures: NRFeatureFlag_LogReporting];
92104
[NRLogger setLogTargets:NRLogTargetConsole];
@@ -183,6 +195,10 @@ - (void) testNRLogger {
183195
XCTAssertTrue([[decodedCommonBlock objectForKey:NRLogMessageInstrumentationProviderKey] isEqualToString:NRLogMessageMobileValue],@"instrumentation provider set incorrectly");
184196
XCTAssertTrue([[decodedCommonBlock objectForKey:NRLogMessageInstrumentationVersionKey] isEqualToString:@"DEV"],@"instrumentation name set incorrectly");
185197

198+
// Check for added session attributes
199+
XCTAssertTrue([[decodedCommonBlock objectForKey:@"myAttribute"] isEqualToNumber:@(1)],@"session attribute set incorrectly");
200+
201+
186202
#if TARGET_OS_WATCH
187203
XCTAssertTrue([[decodedCommonBlock objectForKey:NRLogMessageInstrumentationNameKey] isEqualToString:@"watchOSAgent"],@"instrumentation name set incorrectly");
188204
#else

0 commit comments

Comments
 (0)