Skip to content

Commit

Permalink
added an offline attribute to events created offline and added an off…
Browse files Browse the repository at this point in the history
…line data sent support metric
  • Loading branch information
mbruin-NR committed Dec 14, 2023
1 parent 8b876f3 commit 97cb3bf
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 8 deletions.
10 changes: 9 additions & 1 deletion Agent/Analytics/Events/NRMAMobileEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "NRMAMobileEvent.h"
#import "Constants.h"
#import "NewRelicInternalUtils.h"

static NSString* const kTimestampKey = @"Timestamp";
static NSString* const kSessionElapsedTimeKey = @"SessionElapsedTime";
Expand Down Expand Up @@ -56,7 +57,14 @@ - (id)JSONObject {
dict[kNRMA_RA_timestamp] = @(self.timestamp);
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
8 changes: 5 additions & 3 deletions Agent/Harvester/NRMAHarvester.mm
Original file line number Diff line number Diff line change
Expand Up @@ -333,18 +333,20 @@ - (void) connected
//TODO: add addition collector response processing.
if (response.isError) {
// failure
if([self checkResponseAndPersist:response]) {
if([self checkOfflineAndPersist:response]) {
// If the harvest was persisted for offline storage clear the harvest.
[self.harvestData clear];
} else {
[self fireOnHarvestFailure];
}
} else {
// success
[self.harvestData clear];
// If there was a successful harvest upload send the persisted offline payloads.
[connection sendOfflineStorage];
}
//Supportability/MobileAgent/Collector/Harvest
continues:

[harvestTimer stopTimer];
#ifndef DISABLE_NRMA_EXCEPTION_WRAPPER
@try {
Expand All @@ -362,7 +364,7 @@ - (void) connected
[self fireOnHarvestComplete];
}

- (BOOL) checkResponseAndPersist:(NRMAHarvestResponse*) response {
- (BOOL) checkOfflineAndPersist:(NRMAHarvestResponse*) response {
if([NRMAOfflineStorage checkErrorToPersist:response.error]) {
NSError* error = nil;
NSData* jsonData = [NRMAJSON dataWithJSONABLEObject:self.harvestData options:0 error:&error];
Expand Down
5 changes: 3 additions & 2 deletions Agent/Harvester/NRMAHarvesterConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ -(void) sendOfflineStorage {
NRLOG_ERROR(@"Failed to create data POST");
return;
}

[NRMASupportMetricHelper enqueueOfflinePayloadMetric:@"data" size:[post.HTTPBody length]];

NRMAHarvestResponse* response = [self send:post];

if([NRMAOfflineStorage checkErrorToPersist:response.error]) {
[_offlineStorage persistDataToDisk:jsonData];
// [_offlineStorage persistDataToDisk:jsonData]; Re-save if failed to send again?
}
}];
}
Expand Down
1 change: 1 addition & 0 deletions Agent/Measurements/NRMASupportMetricHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ static NSMutableArray *deferredMetrics = NULL;
+ (void) enqueueUpgradeMetric;
+ (void) enqueueStopAgentMetric;
+ (void) processDeferredMetrics;
+ (void) enqueueOfflinePayloadMetric:(NSString*)endpoint size:(long)size;
@end
8 changes: 8 additions & 0 deletions Agent/Measurements/NRMASupportMetricHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ + (void) enqueueMaxPayloadSizeLimitMetric:(NSString*)endpoint {
scope:nil]];
}

+ (void) enqueueOfflinePayloadMetric:(long)size {
NSString* nativePlatform = [NewRelicInternalUtils osName];
NSString* platform = [NewRelicInternalUtils stringFromNRMAApplicationPlatform:[NRMAAgentConfiguration connectionInformation].deviceInformation.platform];
[NRMATaskQueue queue:[[NRMAMetric alloc] initWithName:[NSString stringWithFormat: kNRMAOfflineSupportabilityFormatString, nativePlatform, platform, kNRMACollectorDest]
value:[NSNumber numberWithLongLong:size]
scope:nil]];
}

+ (void) enqueueUpgradeMetric {
if (deferredMetrics == nil) {
deferredMetrics = [NSMutableArray array];
Expand Down
2 changes: 2 additions & 0 deletions Agent/Public/NRConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ typedef NSString NRMetricUnit;
#define kNRMAMaxPayloadSizeLimitSupportabilityFormatString @"Supportability/Mobile/%@/%@/%@/MaxPayloadSizeLimit/%@"
#define kNRMAMaxPayloadSizeLimit 1000000 // bytes

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

#define kNRMABytesOutConnectAPIString @"/connect/Output/Bytes"
#define kNRMABytesOutDataAPIString @"/data/Output/Bytes"
#define kNRMABytesOutFAPIString @"/f/Output/Bytes"
Expand Down
4 changes: 2 additions & 2 deletions Agent/Utilities/NRMAOfflineStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "NRMAOfflineStorage.h"
#import "Constants.h"
#import "NRMAJSON.h"
#import "NRMASupportMetricHelper.h"

@implementation NRMAOfflineStorage {
}
Expand Down Expand Up @@ -92,8 +93,7 @@ + (NSString*)newOfflineFilePath {
}

+ (BOOL)checkErrorToPersist:(NSError*) error {
return (error.code == NSURLErrorNotConnectedToInternet || error.code == NSURLErrorTimedOut);

return (error.code == NSURLErrorNotConnectedToInternet || error.code == NSURLErrorTimedOut || error.code == NSURLErrorCannotFindHost || error.code == NSURLErrorNetworkConnectionLost || error.code == NSURLErrorCannotConnectToHost);
}

@end
2 changes: 2 additions & 0 deletions Agent/Utilities/NewRelicInternalUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,7 @@ NSTimeInterval NRMAMillisecondTimestamp(void);

+ (NRMANetworkMonitor*) networkMonitor;

+ (NRMAReachability*) reachability;

@end

0 comments on commit 97cb3bf

Please sign in to comment.