Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nr 348708: add session attributes to log common attribute #341

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading