diff --git a/Agent/Utilities/NRLogger.m b/Agent/Utilities/NRLogger.m index c1ad8c5e..43ed95e0 100644 --- a/Agent/Utilities/NRLogger.m +++ b/Agent/Utilities/NRLogger.m @@ -330,6 +330,24 @@ - (NSMutableDictionary*) commonBlockDict { if (nativePlatform) [commonAttributes setObject:nativePlatform forKey:NRLogMessageInstrumentationCollectorKey]; if (nrAppId) [commonAttributes setObject:nrAppId forKey:NRLogMessageAppIdKey]; + + NSString* sessionAttributes = [[NewRelicAgentInternal sharedInstance].analyticsController sessionAttributeJSONString]; + if (sessionAttributes != nil && [sessionAttributes length] > 0) { + NSDictionary* dictionary = [NSJSONSerialization JSONObjectWithData:[sessionAttributes dataUsingEncoding:NSUTF8StringEncoding] + options:0 + error:nil]; + for (NSString *key in dictionary) { + id value = [dictionary objectForKey:key]; + + + if (value) { + [commonAttributes setObject:value forKey:key]; + } + } + } + + + return commonAttributes; } @@ -598,7 +616,6 @@ - (void) processNextUploadTask { NSData *formattedData = [self->uploadQueue firstObject]; if (self->debugLogs) { - //NSString* logMessagesJson = [NSString stringWithFormat:@"[ %@ ]", [[NSString alloc] initWithData:formattedData encoding:NSUTF8StringEncoding]]; NSArray* decode = [NSJSONSerialization JSONObjectWithData:formattedData options:0 error:nil]; @@ -609,9 +626,6 @@ - (void) processNextUploadTask { NSMutableURLRequest* req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: self->logURL]]; [req setValue:self->logIngestKey forHTTPHeaderField:@"X-App-License-Key"]; [req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; - - //NSString* contentEncoding = message.length <= 512 ? kNRMAIdentityHeader : kNRMAGZipHeader; - [req setValue:kNRMAGZipHeader forHTTPHeaderField:kNRMAContentEncodingHeader]; req.HTTPMethod = @"POST"; diff --git a/Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.h b/Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.h index 71f918bf..6150a793 100644 --- a/Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.h +++ b/Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.h @@ -18,4 +18,7 @@ } @property (nonatomic) int fileDescriptor; @property (nonatomic, strong) dispatch_source_t source; + +@property id mockNewRelicInternals; + @end diff --git a/Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.m b/Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.m index da02f03c..f89990c1 100644 --- a/Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.m +++ b/Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.m @@ -24,12 +24,16 @@ #import "NRTestConstants.h" #import "NRAutoLogCollector.h" #import +#import "NewRelicAgentInternal.h" +#import @interface NRLogger() + (NRLogger *)logger; - (NSMutableDictionary*) commonBlockDict; @end +static NewRelicAgentInternal* _sharedInstance; + @implementation NRLoggerTests - (void) setUp { @@ -39,9 +43,16 @@ - (void) setUp [NRLogger setLogLevels:NRLogLevelDebug]; [NRLogger setRemoteLogLevel:NRLogLevelDebug]; - [NRLogger setLogEntityGuid:@"Entity-Guid-XXXX"]; + + self.mockNewRelicInternals = [OCMockObject mockForClass:[NewRelicAgentInternal class]]; + _sharedInstance = [[NewRelicAgentInternal alloc] init]; + _sharedInstance.analyticsController = [[NRMAAnalytics alloc] initWithSessionStartTimeMS:0.0]; + [[[[self.mockNewRelicInternals stub] classMethod] andReturn:_sharedInstance] sharedInstance]; + + [_sharedInstance.analyticsController setSessionAttribute:@"myAttribute" value:@(1)]; + NRMAAgentConfiguration *config = [[NRMAAgentConfiguration alloc] initWithAppToken:[[NRMAAppToken alloc] initWithApplicationToken:kNRMA_ENABLED_STAGING_APP_TOKEN] collectorAddress:KNRMA_TEST_COLLECTOR_HOST crashAddress:nil]; @@ -86,7 +97,8 @@ - (void) tearDown [NRMAMeasurements removeMeasurementConsumer:helper]; helper = nil; - + [self.mockNewRelicInternals stopMocking]; + _sharedInstance = nil; [NRMAMeasurements shutdown]; [NRMAFlags disableFeatures: NRFeatureFlag_LogReporting]; [NRLogger setLogTargets:NRLogTargetConsole]; @@ -183,6 +195,10 @@ - (void) testNRLogger { XCTAssertTrue([[decodedCommonBlock objectForKey:NRLogMessageInstrumentationProviderKey] isEqualToString:NRLogMessageMobileValue],@"instrumentation provider set incorrectly"); XCTAssertTrue([[decodedCommonBlock objectForKey:NRLogMessageInstrumentationVersionKey] isEqualToString:@"DEV"],@"instrumentation name set incorrectly"); + // Check for added session attributes + XCTAssertTrue([[decodedCommonBlock objectForKey:@"myAttribute"] isEqualToNumber:@(1)],@"session attribute set incorrectly"); + + #if TARGET_OS_WATCH XCTAssertTrue([[decodedCommonBlock objectForKey:NRLogMessageInstrumentationNameKey] isEqualToString:@"watchOSAgent"],@"instrumentation name set incorrectly"); #else