Skip to content

Commit

Permalink
Nr 348708: add session attributes to log common attribute (#341)
Browse files Browse the repository at this point in the history
* NR-348708: session attributes are added to log common block

* NR-348708: add test
  • Loading branch information
cdillard-NewRelic authored Jan 29, 2025
1 parent 6b814a2 commit 3ecb494
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
22 changes: 18 additions & 4 deletions Agent/Utilities/NRLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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];
Expand All @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@
}
@property (nonatomic) int fileDescriptor;
@property (nonatomic, strong) dispatch_source_t source;

@property id mockNewRelicInternals;

@end
20 changes: 18 additions & 2 deletions Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@
#import "NRTestConstants.h"
#import "NRAutoLogCollector.h"
#import <os/log.h>
#import "NewRelicAgentInternal.h"
#import <OCMock/OCMock.h>

@interface NRLogger()
+ (NRLogger *)logger;
- (NSMutableDictionary*) commonBlockDict;
@end

static NewRelicAgentInternal* _sharedInstance;

@implementation NRLoggerTests
- (void) setUp
{
Expand All @@ -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];
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3ecb494

Please sign in to comment.