Skip to content

Commit

Permalink
Added the offline attribute to sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
mbruin-NR committed Dec 19, 2023
1 parent 97cb3bf commit 252780c
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 25 deletions.
7 changes: 0 additions & 7 deletions Agent/Analytics/Events/NRMAMobileEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@ - (id)JSONObject {
dict[kNRMA_RA_sessionElapsedTime] = @(self.sessionElapsedTimeSeconds);
dict[kNRMA_RA_eventType] = self.eventType;

NRMAReachability* r = [NewRelicInternalUtils reachability];
@synchronized(r) {
NRMANetworkStatus status = [r currentReachabilityStatus];
if (status == NotReachable) {
dict[@"Offline"] = [[NSNumber alloc] initWithBool:TRUE];
}
}
return [NSDictionary dictionaryWithDictionary:dict];
}

Expand Down
16 changes: 13 additions & 3 deletions Agent/General/NewRelicAgentInternal.m
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ - (void) onSessionStart {

// Initializing analytics take a while. Take care executing time sensitive code after this point the since initializeAnalytics method will delay its execution.
[self initializeAnalytics];
NRMAReachability* r = [NewRelicInternalUtils reachability];

if ([NRMAFlags shouldEnableHandledExceptionEvents]) {
self.handledExceptionsController = [[NRMAHandledExceptions alloc] initWithAnalyticsController:self.analyticsController
Expand All @@ -483,7 +484,12 @@ - (void) onSessionStart {
platform:[NewRelicInternalUtils osName]
sessionId:[self currentSessionId]];

[self.handledExceptionsController processAndPublishPersistedReports];
@synchronized(r) {
NRMANetworkStatus status = [r currentReachabilityStatus];
if (status != NotReachable) {
[self.handledExceptionsController processAndPublishPersistedReports];
}
}

[NRMAHarvestController addHarvestListener:self.handledExceptionsController];

Expand All @@ -493,8 +499,12 @@ - (void) onSessionStart {

// Attempt to upload crash report files (if any exist)
if ([NRMAFlags shouldEnableCrashReporting]) {

[[NRMAExceptionHandlerManager manager].uploader uploadCrashReports];
@synchronized(r) {
NRMANetworkStatus status = [r currentReachabilityStatus];
if (status != NotReachable) {
[[NRMAExceptionHandlerManager manager].uploader uploadCrashReports];
}
}
}

if([NRMAFlags shouldEnableGestureInstrumentation])
Expand Down
2 changes: 1 addition & 1 deletion Agent/HandledException/NRMAHexUploader.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#import "NRLogger.h"
#include <libkern/OSAtomic.h>
#import "NRMASupportMetricHelper.h"
#import "NRConstants.h"

#define kNRMARetryLimit 2 // this will result in 2 additional upload attempts.
#define kNRMAMaxPayloadSizeLimit 1000000

@interface NRMAHexUploader()
@property(strong) NSString* host;
Expand Down
4 changes: 4 additions & 0 deletions Agent/Harvester/NRMAHarvester.mm
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ - (void) connected

- (BOOL) checkOfflineAndPersist:(NRMAHarvestResponse*) response {
if([NRMAOfflineStorage checkErrorToPersist:response.error]) {
NSMutableDictionary *tempAnalyticsAttributes = [[NSMutableDictionary alloc] initWithDictionary:self.harvestData.analyticsAttributes];
[tempAnalyticsAttributes setValue:[NSNumber numberWithBool:YES] forKey:@"offline"];
[self.harvestData setAnalyticsAttributes:[[NSDictionary alloc] initWithDictionary:tempAnalyticsAttributes]];

NSError* error = nil;
NSData* jsonData = [NRMAJSON dataWithJSONABLEObject:self.harvestData options:0 error:&error];
if (error) {
Expand Down
2 changes: 1 addition & 1 deletion Agent/Harvester/NRMAHarvesterConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ -(void) sendOfflineStorage {
NRLOG_ERROR(@"Failed to create data POST");
return;
}
[NRMASupportMetricHelper enqueueOfflinePayloadMetric:@"data" size:[post.HTTPBody length]];
[NRMASupportMetricHelper enqueueOfflinePayloadMetric:[post.HTTPBody length]];

NRMAHarvestResponse* response = [self send:post];

Expand Down
2 changes: 1 addition & 1 deletion Agent/Measurements/NRMASupportMetricHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ static NSMutableArray *deferredMetrics = NULL;
+ (void) enqueueUpgradeMetric;
+ (void) enqueueStopAgentMetric;
+ (void) processDeferredMetrics;
+ (void) enqueueOfflinePayloadMetric:(NSString*)endpoint size:(long)size;
+ (void) enqueueOfflinePayloadMetric:(long)size;
@end
2 changes: 1 addition & 1 deletion Agent/Public/NRConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ typedef NSString NRMetricUnit;
#define kNRMAMaxPayloadSizeLimitSupportabilityFormatString @"Supportability/Mobile/%@/%@/%@/MaxPayloadSizeLimit/%@"
#define kNRMAMaxPayloadSizeLimit 1000000 // bytes

#define kNRMAOfflineSupportabilityFormatString @"Supportability/Mobile/%@/%@/%@/Offline/bytes"
#define kNRMAOfflineSupportabilityFormatString @"Supportability/Mobile/%@/%@/%@/OfflinePayload/bytes"

#define kNRMABytesOutConnectAPIString @"/connect/Output/Bytes"
#define kNRMABytesOutDataAPIString @"/data/Output/Bytes"
Expand Down
20 changes: 10 additions & 10 deletions Agent/Utilities/NRMAOfflineStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ - (id)initWithEndpoint:(NSString*) name {
}

- (void) createDirectory {
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[NRMAOfflineStorage offlineDirectoryPath] isDirectory:nil];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[self offlineDirectoryPath] isDirectory:nil];
if(!fileExists){
NSError *error = nil;
if(![[NSFileManager defaultManager] createDirectoryAtPath:[NRMAOfflineStorage offlineDirectoryPath] withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@"Failed to create directory \"%@\". Error: %@", [NRMAOfflineStorage offlineDirectoryPath], error);
if(![[NSFileManager defaultManager] createDirectoryAtPath:[self offlineDirectoryPath] withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@"Failed to create directory \"%@\". Error: %@", [self offlineDirectoryPath], error);
}
}
}
Expand All @@ -43,7 +43,7 @@ - (BOOL) persistDataToDisk:(NSData*) data {

NSError *error = nil;
if (data) {
if ([data writeToFile:[NRMAOfflineStorage newOfflineFilePath] options:NSDataWritingAtomic error:&error]) {
if ([data writeToFile:[self newOfflineFilePath] options:NSDataWritingAtomic error:&error]) {
NRLOG_VERBOSE(@"Successfully persisted failed upload data to disk for offline storage.");
return YES;
}
Expand All @@ -58,11 +58,11 @@ - (BOOL) persistDataToDisk:(NSData*) data {
@synchronized (self) {
NSMutableArray<NSData *> *combinedPosts = [NSMutableArray array];

NSArray* dirs = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NRMAOfflineStorage offlineDirectoryPath]
NSArray* dirs = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self offlineDirectoryPath]
error:NULL];
[dirs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *filename = (NSString *)obj;
NSData * data = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",[NRMAOfflineStorage offlineDirectoryPath],filename]];
NSData * data = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",[self offlineDirectoryPath],filename]];
NRLOG_VERBOSE(@"Offline storage to be uploaded from %@", filename);

[combinedPosts addObject:data];
Expand All @@ -77,19 +77,19 @@ - (BOOL) persistDataToDisk:(NSData*) data {
}

- (BOOL) clearAllOfflineFiles {
return [[NSFileManager defaultManager] removeItemAtPath:[NRMAOfflineStorage offlineDirectoryPath] error:NULL];
return [[NSFileManager defaultManager] removeItemAtPath:[self offlineDirectoryPath] error:NULL];
}

+ (NSString*)offlineDirectoryPath {
- (NSString*)offlineDirectoryPath {
return [NSString stringWithFormat:@"%@/%@/%@",[NewRelicInternalUtils getStorePath],kNRMA_Offline_file,_name];
}

+ (NSString*)newOfflineFilePath {
- (NSString*)newOfflineFilePath {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
NSString *date = [dateFormatter stringFromDate:[NSDate date]];

return [NSString stringWithFormat:@"%@/%@%@",[NRMAOfflineStorage offlineDirectoryPath],date,@".txt"];
return [NSString stringWithFormat:@"%@/%@%@",[self offlineDirectoryPath],date,@".txt"];
}

+ (BOOL)checkErrorToPersist:(NSError*) error {
Expand Down
2 changes: 1 addition & 1 deletion Agent/Utilities/NewRelicInternalUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ + (NRMANetworkMonitor*) networkMonitor {
static NRMANetworkMonitor* nm = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (@available(tvOS 12.0, *)) {
if (@available(iOS 12.0, tvOS 12.0, *)) {
nm = [[NRMANetworkMonitor alloc] init];
}
});
Expand Down

0 comments on commit 252780c

Please sign in to comment.