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

Offline storage to file system on failed upload #189

Merged
merged 23 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0672057
Offline storing the data that would be sent to the data endpoint
mbruin-NR Nov 21, 2023
22f23af
Added some better logic for when to record the json for offline storage
mbruin-NR Nov 30, 2023
120f3e0
removed unneeded change
mbruin-NR Nov 30, 2023
ea1b939
Merge branch 'develop' into offlineStoragePOC
mbruin-NR Nov 30, 2023
8b876f3
Added tests and better logging
mbruin-NR Dec 13, 2023
97cb3bf
added an offline attribute to events created offline and added an off…
mbruin-NR Dec 14, 2023
252780c
Added the offline attribute to sessions
mbruin-NR Dec 19, 2023
3f40154
Cleaned up some unused stuff
mbruin-NR Dec 20, 2023
2a641f0
Added a feature flag for offline storage and a maximum amount of stor…
mbruin-NR Jan 5, 2024
69a40aa
Merge branch 'develop' into offlineStoragePOC
mbruin-NR Jan 5, 2024
4fa0b18
Fixing some mistakes
mbruin-NR Jan 10, 2024
1394872
trying to fix a test
mbruin-NR Jan 10, 2024
c5c102d
Some changes after review
mbruin-NR Jan 11, 2024
13b0073
Update Agent/Public/NewRelic.h
mbruin-NR Jan 11, 2024
907d730
Fixed not adding the attribute to metrics payload
mbruin-NR Jan 11, 2024
1efd215
Merge commit '13b0073e7a7bdabfa7be4dcc8b27f2e8acb70b4d' into offlineS…
mbruin-NR Jan 11, 2024
e300cfa
Changed offline to enabled by default and fix a few place to check fo…
mbruin-NR Jan 25, 2024
a87bd97
Changing offline to only care about the persisted reports to eliminat…
mbruin-NR Jan 29, 2024
e35b3fa
Merge branch 'develop' into offlineStoragePOC
mbruin-NR Jan 31, 2024
1f432a3
moving the offline attribute to be per event and adding it to handled…
mbruin-NR Feb 9, 2024
eb8f5ff
Merge branch 'develop' into offlineStoragePOC
mbruin-NR Feb 9, 2024
02b80c2
Mixed up my offline check
mbruin-NR Feb 9, 2024
e783273
Merge commit 'eb8f5ff988606a39c5ac0d74f8569a83298680a2' into offlineS…
mbruin-NR Feb 9, 2024
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
14 changes: 14 additions & 0 deletions Agent.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,8 @@
F824A43129AEAD63000886A6 /* NRViewModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = F824A43029AEAD63000886A6 /* NRViewModifier.swift */; };
F824A43229AEAD63000886A6 /* NRViewModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = F824A43029AEAD63000886A6 /* NRViewModifier.swift */; };
F825FAE329C8F52800E1C1DC /* NRMAWKFakeNavigationAction.m in Sources */ = {isa = PBXBuildFile; fileRef = F825FAE229C8F52800E1C1DC /* NRMAWKFakeNavigationAction.m */; };
F83A77B02B48810900B3D180 /* NRMAOfflineStorageTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F83A77AF2B48810900B3D180 /* NRMAOfflineStorageTests.m */; };
F83A77B12B48810900B3D180 /* NRMAOfflineStorageTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F83A77AF2B48810900B3D180 /* NRMAOfflineStorageTests.m */; };
F848CDC02AA133EA0082052F /* NRMAInteractionEvent.m in Sources */ = {isa = PBXBuildFile; fileRef = F848CDBF2AA133EA0082052F /* NRMAInteractionEvent.m */; };
F848CDC12AA133EA0082052F /* NRMAInteractionEvent.m in Sources */ = {isa = PBXBuildFile; fileRef = F848CDBF2AA133EA0082052F /* NRMAInteractionEvent.m */; };
F858F3602AE04B0C00CF9EB5 /* NRMAURLSessionHeaderTrackingTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F858F35F2AE04B0C00CF9EB5 /* NRMAURLSessionHeaderTrackingTests.m */; };
Expand All @@ -1046,6 +1048,8 @@
F8A455492AFBE31E0057B1E0 /* NRMAURLSessionHeaderTrackingTestsOldEventSystem.m in Sources */ = {isa = PBXBuildFile; fileRef = F8A455472AFBE31E0057B1E0 /* NRMAURLSessionHeaderTrackingTestsOldEventSystem.m */; };
F8AC3E932938FD6C002B4AA8 /* NRMAFakeDataHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = F8AC3E922938FD6C002B4AA8 /* NRMAFakeDataHelper.m */; };
F8AC3E942938FD6C002B4AA8 /* NRMAFakeDataHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = F8AC3E922938FD6C002B4AA8 /* NRMAFakeDataHelper.m */; };
F8E202DE2B07BA61008E0B7B /* NRMAOfflineStorage.m in Sources */ = {isa = PBXBuildFile; fileRef = F8E202DD2B07BA61008E0B7B /* NRMAOfflineStorage.m */; };
F8E202DF2B07BA61008E0B7B /* NRMAOfflineStorage.m in Sources */ = {isa = PBXBuildFile; fileRef = F8E202DD2B07BA61008E0B7B /* NRMAOfflineStorage.m */; };
F8FBFA512A71ACB200CDC8C5 /* NRMARequestEvent.m in Sources */ = {isa = PBXBuildFile; fileRef = F8FBFA502A71ACB200CDC8C5 /* NRMARequestEvent.m */; };
F8FBFA522A71ACB200CDC8C5 /* NRMARequestEvent.m in Sources */ = {isa = PBXBuildFile; fileRef = F8FBFA502A71ACB200CDC8C5 /* NRMARequestEvent.m */; };
F8FBFA552A71BA2900CDC8C5 /* NRMAPayload.m in Sources */ = {isa = PBXBuildFile; fileRef = F8FBFA542A71BA2900CDC8C5 /* NRMAPayload.m */; };
Expand Down Expand Up @@ -1957,6 +1961,7 @@
F824A43029AEAD63000886A6 /* NRViewModifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NRViewModifier.swift; sourceTree = "<group>"; };
F825FAE229C8F52800E1C1DC /* NRMAWKFakeNavigationAction.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NRMAWKFakeNavigationAction.m; sourceTree = "<group>"; };
F825FAF729C8F5BA00E1C1DC /* NRMAWKFakeNavigationAction.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NRMAWKFakeNavigationAction.h; sourceTree = "<group>"; };
F83A77AF2B48810900B3D180 /* NRMAOfflineStorageTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NRMAOfflineStorageTests.m; sourceTree = "<group>"; };
F848CDBF2AA133EA0082052F /* NRMAInteractionEvent.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NRMAInteractionEvent.m; sourceTree = "<group>"; };
F848CDD52AA133FB0082052F /* NRMAInteractionEvent.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NRMAInteractionEvent.h; sourceTree = "<group>"; };
F858F35F2AE04B0C00CF9EB5 /* NRMAURLSessionHeaderTrackingTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NRMAURLSessionHeaderTrackingTests.m; sourceTree = "<group>"; };
Expand All @@ -1966,6 +1971,8 @@
F8A455472AFBE31E0057B1E0 /* NRMAURLSessionHeaderTrackingTestsOldEventSystem.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NRMAURLSessionHeaderTrackingTestsOldEventSystem.m; sourceTree = "<group>"; };
F8AC3E922938FD6C002B4AA8 /* NRMAFakeDataHelper.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NRMAFakeDataHelper.m; sourceTree = "<group>"; };
F8AC3EA72938FDDB002B4AA8 /* NRMAFakeDataHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NRMAFakeDataHelper.h; sourceTree = "<group>"; };
F8E202DD2B07BA61008E0B7B /* NRMAOfflineStorage.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NRMAOfflineStorage.m; sourceTree = "<group>"; };
F8E202F32B07BA6E008E0B7B /* NRMAOfflineStorage.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NRMAOfflineStorage.h; sourceTree = "<group>"; };
F8FBFA502A71ACB200CDC8C5 /* NRMARequestEvent.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NRMARequestEvent.m; sourceTree = "<group>"; };
F8FBFA532A71ACC000CDC8C5 /* NRMARequestEvent.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NRMARequestEvent.h; sourceTree = "<group>"; };
F8FBFA542A71BA2900CDC8C5 /* NRMAPayload.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NRMAPayload.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2376,6 +2383,7 @@
F8AC3E922938FD6C002B4AA8 /* NRMAFakeDataHelper.m */,
F8AC3EA72938FDDB002B4AA8 /* NRMAFakeDataHelper.h */,
2B4226E529DB99740068BB8A /* NRMAHTTPUtilitiesTests.m */,
F83A77AF2B48810900B3D180 /* NRMAOfflineStorageTests.m */,
);
path = Uncategorized;
sourceTree = "<group>";
Expand Down Expand Up @@ -3126,6 +3134,8 @@
02FF4A8424DC652C00115469 /* NRMAMemoryVitals.m */,
02FF4A8624DC652C00115469 /* NRMANonARCMethods.h */,
02FF4A8524DC652C00115469 /* NRMANonARCMethods.m */,
F8E202F32B07BA6E008E0B7B /* NRMAOfflineStorage.h */,
F8E202DD2B07BA61008E0B7B /* NRMAOfflineStorage.m */,
02FF4A8324DC652C00115469 /* NRMAUDIDManager.h */,
02FF4A8D24DC652D00115469 /* NRMAUDIDManager.m */,
02FF4A8B24DC652D00115469 /* NRMAUUIDStore.h */,
Expand Down Expand Up @@ -4334,6 +4344,7 @@
0209AC1224E7071500E45C90 /* NRMAAPIHelperTests.m in Sources */,
0209AC4524E7078000E45C90 /* NRMAFeatureFlagsTests.m in Sources */,
2B02DB86293E630B001A59D6 /* NRMAURLSessionAsyncTests.swift in Sources */,
F83A77B02B48810900B3D180 /* NRMAOfflineStorageTests.m in Sources */,
0209AC3224E7073800E45C90 /* NRMAHarvestableAnalyticsTest.m in Sources */,
0209AC9924E7393300E45C90 /* NSURLConnectionTests.m in Sources */,
0209ABFE24E7070900E45C90 /* NRURLSessionDelegateTests.m in Sources */,
Expand Down Expand Up @@ -4492,6 +4503,7 @@
02FF4A2324DC648600115469 /* NRMAMeasurementConsumer.m in Sources */,
02FF4AA824DC652E00115469 /* NRMAUDIDManager.m in Sources */,
02FF48DC24DC622700115469 /* NRMACrashDataUploader.m in Sources */,
F8E202DE2B07BA61008E0B7B /* NRMAOfflineStorage.m in Sources */,
02FF49E824DC644900115469 /* NRMAKeyAttributes.m in Sources */,
02FF49EF24DC645B00115469 /* NRMAAppInstallMetricGenerator.m in Sources */,
02FF4A7724DC64FC00115469 /* NRTimer.m in Sources */,
Expand Down Expand Up @@ -4583,6 +4595,7 @@
025657F924EB19BF00FE3125 /* NSURLSessionOverrideTest.m in Sources */,
F8AC3E942938FD6C002B4AA8 /* NRMAFakeDataHelper.m in Sources */,
025657FA24EB19BF00FE3125 /* NRActivityTracesTest.m in Sources */,
F83A77B12B48810900B3D180 /* NRMAOfflineStorageTests.m in Sources */,
025657FB24EB19BF00FE3125 /* NRMeasurementTest.m in Sources */,
2B40F4FF28BEB95E00EAD248 /* NRMAStartTimerTests.m in Sources */,
025657FC24EB19BF00FE3125 /* TestUITableViewInstrumentation.m in Sources */,
Expand Down Expand Up @@ -4725,6 +4738,7 @@
2B33E5D32AA9160E00AEB7B4 /* NRMASessionEvent.m in Sources */,
F8FBFA632A78416300CDC8C5 /* NRMAMobileEvent.m in Sources */,
02FF4C0524E3201400115469 /* NRMAMetric.m in Sources */,
F8E202DF2B07BA61008E0B7B /* NRMAOfflineStorage.m in Sources */,
02FF4C0624E3201400115469 /* NRMAInteractionHistory.c in Sources */,
02FF4C0724E3201400115469 /* NRMAActivityNameGenerator.m in Sources */,
02FF4C0824E3201400115469 /* NRMAScopedMeasurement.m in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions Agent/Analytics/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ extern NSString *const kNRMA_Attrib_dtGuid;
extern NSString *const kNRMA_Attrib_dtId;
extern NSString *const kNRMA_Attrib_dtTraceId;
extern NSString *const kNRMA_Attrib_name;
extern NSString *const kNRMA_Attrib_offline;

extern NSString *const kNRMA_Val_errorType_HTTP;
extern NSString *const kNRMA_Val_errorType_Network;

extern NSString *const kNRMA_Attrib_file;
extern NSString *const kNRMA_Attrib_file_private;

extern NSString *const kNRMA_Offline_folder;

// Integer Analytics Constants
static int kNRMA_Attrib_Max_Name_Length = 256;
Expand Down
3 changes: 3 additions & 0 deletions Agent/Analytics/Constants.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@
NSString * const kNRMA_Attrib_dtId = @"id";
NSString * const kNRMA_Attrib_dtTraceId = @"trace.id";
NSString * const kNRMA_Attrib_name = @"name";
NSString * const kNRMA_Attrib_offline = @"offline";

NSString * const kNRMA_Val_errorType_HTTP = @"HTTPError";
NSString * const kNRMA_Val_errorType_Network = @"NetworkFailure";


NSString * const kNRMA_Attrib_file = @"attributes.txt";
NSString * const kNRMA_Attrib_file_private = @"privateAttributes.txt";

NSString * const kNRMA_Offline_folder = @"offlineStorage";
1 change: 0 additions & 1 deletion Agent/Analytics/Events/NRMAMobileEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ - (id)JSONObject {
dict[kNRMA_RA_timestamp] = @(self.timestamp);
dict[kNRMA_RA_sessionElapsedTime] = @(self.sessionElapsedTimeSeconds);
dict[kNRMA_RA_eventType] = self.eventType;

return [NSDictionary dictionaryWithDictionary:dict];
}

Expand Down
8 changes: 7 additions & 1 deletion Agent/CrashHandler/NRMAExceptionHandlerManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,13 @@ - (instancetype) initWithLastSessionsAttributes:(NSDictionary*)attributes
[_reportManager processReportsWithSessionAttributes:attributes
analyticsEvents:events];

[self.uploader uploadCrashReports];
NRMAReachability* r = [NewRelicInternalUtils reachability];
@synchronized(r) {
NRMANetworkStatus status = [r currentReachabilityStatus];
if (status != NotReachable) { // Because we support offline mode check if we're online before sending the crash reports
[self.uploader uploadCrashReports];
}
}
}

self.handler = [[NRMAUncaughtExceptionHandler alloc] initWithCrashReporter:_crashReporter];
Expand Down
2 changes: 2 additions & 0 deletions Agent/FeatureFlags/NRMAFlags.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

+ (BOOL) shouldEnableLogReporting;

+ (BOOL) shouldEnableOfflineStorage;

+ (BOOL) shouldEnableNewEventSystem;

+ (NSArray<NSString*>*) namesForFlags:(NRMAFeatureFlags)flags;
Expand Down
11 changes: 10 additions & 1 deletion Agent/FeatureFlags/NRMAFlags.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ + (NRMAFeatureFlags) featureFlags
NRFeatureFlag_NetworkRequestEvents |
NRFeatureFlag_RequestErrorEvents |
NRFeatureFlag_DistributedTracing |
NRFeatureFlag_AppStartMetrics;
NRFeatureFlag_AppStartMetrics |
NRFeatureFlag_OfflineStorage;
});
return __flags;
}
Expand Down Expand Up @@ -161,6 +162,11 @@ + (BOOL) shouldEnableFedRampSupport {
+ (BOOL) shouldEnableSwiftAsyncURLSessionSupport {
return ([NRMAFlags featureFlags] & NRFeatureFlag_SwiftAsyncURLSessionSupport) != 0;
}

+ (BOOL) shouldEnableOfflineStorage {
return ([NRMAFlags featureFlags] & NRFeatureFlag_OfflineStorage) != 0;
}

+ (BOOL) shouldEnableLogReporting {
return NO;
//return ([NRMAFlags featureFlags] & NRFeatureFlag_LogReporting) != 0;
Expand Down Expand Up @@ -218,6 +224,9 @@ + (BOOL) shouldEnableNewEventSystem {
if ((flags & NRFeatureFlag_SwiftAsyncURLSessionSupport) == NRFeatureFlag_SwiftAsyncURLSessionSupport) {
[retArray addObject:@"SwiftAsyncURLSessionSupport"];
}
if ((flags & NRFeatureFlag_OfflineStorage) == NRFeatureFlag_OfflineStorage) {
[retArray addObject:@"OfflineStorage"];
}

// NOTE: Temporarily removed NRFeatureFlag_LogReporting
// if ((flags & NRFeatureFlag_LogReporting) == NRFeatureFlag_LogReporting) {
Expand Down
16 changes: 12 additions & 4 deletions Agent/General/NewRelicAgentInternal.m
Original file line number Diff line number Diff line change
Expand Up @@ -475,15 +475,22 @@ - (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];
NRMANetworkStatus status;
@synchronized(r) {
status = [r currentReachabilityStatus];
}
if ([NRMAFlags shouldEnableHandledExceptionEvents]) {
self.handledExceptionsController = [[NRMAHandledExceptions alloc] initWithAnalyticsController:self.analyticsController
sessionStartTime:self.appSessionStartDate
agentConfiguration:self.agentConfiguration
platform:[NewRelicInternalUtils osName]
sessionId:[self currentSessionId]];

[self.handledExceptionsController processAndPublishPersistedReports];

if (status != NotReachable) { // Because we support offline mode check if we're online before sending the handled exceptions
[self.handledExceptionsController processAndPublishPersistedReports];
}

[NRMAHarvestController addHarvestListener:self.handledExceptionsController];

Expand All @@ -493,8 +500,9 @@ - (void) onSessionStart {

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

[[NRMAExceptionHandlerManager manager].uploader uploadCrashReports];
if (status != NotReachable) { // Because we support offline mode check if we're online before sending the crash reports
[[NRMAExceptionHandlerManager manager].uploader uploadCrashReports];
}
}

if([NRMAFlags shouldEnableGestureInstrumentation])
Expand Down
18 changes: 15 additions & 3 deletions Agent/HandledException/NRMAHandledExceptions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,21 @@ - (instancetype) initWithAnalyticsController:(NRMAAnalytics*)analytics
}

- (void) onHarvest {
_controller->publish();
_store->clear();
_publisher->retry();
if([NRMAFlags shouldEnableOfflineStorage]) {
NRMAReachability* r = [NewRelicInternalUtils reachability];
@synchronized(r) {
NRMANetworkStatus status = [r currentReachabilityStatus];
if (status != NotReachable) {
[self processAndPublishPersistedReports]; // When using offline we always want to send from persisted because the keyContext doesn't persist.
_controller->resetKeyContext();
_publisher->retry();
}
}
} else {
_controller->publish();
_store->clear();
_publisher->retry();
}
}

- (fbs::Platform) fbsPlatformFromString:(NSString*)platform {
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
3 changes: 3 additions & 0 deletions Agent/Harvester/DataStore/NRMAAgentConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@
+ (void) setMaxEventBufferTime:(NSUInteger)seconds;
+ (NSUInteger) getMaxEventBufferTime;

+ (void) setMaxOfflineStorageSize:(NSUInteger)megaBytes;
+ (NSUInteger) getMaxOfflineStorageSize;

@end
9 changes: 9 additions & 0 deletions Agent/Harvester/DataStore/NRMAAgentConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

// Default max event buffer time is 10 minutes (600 seconds).
static NSUInteger __NRMA__maxEventBufferTime = 600;
static NSUInteger __NRMA__maxOfflineStorageSize = 100000000; // 100 mb

@implementation NRMAAgentConfiguration

Expand Down Expand Up @@ -49,6 +50,14 @@ + (NSUInteger) getMaxEventBufferTime {
return __NRMA__maxEventBufferTime;
}

+ (void) setMaxOfflineStorageSize:(NSUInteger)megaBytes {
__NRMA__maxOfflineStorageSize = megaBytes;
}

+ (NSUInteger) getMaxOfflineStorageSize {
return __NRMA__maxOfflineStorageSize;
}

- (id) initWithAppToken:(NRMAAppToken*)token
collectorAddress:(NSString*)collectorHost
crashAddress:(NSString*)crashHost {
Expand Down
2 changes: 2 additions & 0 deletions Agent/Harvester/NRMAHarvestController.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

+ (BOOL) shouldNotCollectTraces;

+ (void) setMaxOfflineStorageSize:(NSUInteger) size;

#pragma mark - HarvestController interface

+ (NRMAHarvesterConfiguration*) configuration;
Expand Down
4 changes: 4 additions & 0 deletions Agent/Harvester/NRMAHarvestController.m
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ + (void) addHarvestableActivity:(NRMAHarvestableActivity*)activity
[harvestData.activityTraces addActivityTraces:activity];
}
}

+ (void) setMaxOfflineStorageSize:(NSUInteger) size {
[[[NRMAHarvestController harvestController] harvester] setMaxOfflineStorageSize:size];
}
@end

#ifdef __cplusplus
Expand Down
2 changes: 2 additions & 0 deletions Agent/Harvester/NRMAHarvester.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ typedef enum {
- (void) removeHarvestAwareObject:(id<NRMAHarvestAware>)harvestAware;
- (void) fireOnHarvestStart;
- (void) stop;

- (void) setMaxOfflineStorageSize:(NSUInteger) size;
@end

#ifdef __cplusplus
Expand Down
38 changes: 36 additions & 2 deletions Agent/Harvester/NRMAHarvester.mm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <Utilities/Application.hpp>
#import "NRMASupportMetricHelper.h"
#import "NRMAFlags.h"
#import "Constants.h"

#define kNRSupportabilityResponseCode kNRSupportabilityPrefix @"/Collector/ResponseStatusCodes"

Expand Down Expand Up @@ -333,13 +334,20 @@ - (void) connected
//TODO: add addition collector response processing.
if (response.isError) {
// failure
[self fireOnHarvestFailure];
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

[harvestTimer stopTimer];
#ifndef DISABLE_NRMA_EXCEPTION_WRAPPER
@try {
Expand All @@ -357,6 +365,28 @@ - (void) connected
[self fireOnHarvestComplete];
}

- (BOOL) checkOfflineAndPersist:(NRMAHarvestResponse*) response {
if (![NRMAFlags shouldEnableOfflineStorage]) {
return false;
}
if([NRMAOfflineStorage checkErrorToPersist:response.error]) {
NSMutableDictionary *tempAnalyticsAttributes = [[NSMutableDictionary alloc] initWithDictionary:self.harvestData.analyticsAttributes];
if(tempAnalyticsAttributes.count > 0){
[tempAnalyticsAttributes setValue:[NSNumber numberWithBool:YES] forKey:kNRMA_Attrib_offline];
[self.harvestData setAnalyticsAttributes:[[NSDictionary alloc] initWithDictionary:tempAnalyticsAttributes]];
}
NSError* error = nil;
NSData* jsonData = [NRMAJSON dataWithJSONABLEObject:self.harvestData options:0 error:&error];
if (error) {
NRLOG_ERROR(@"Failed to generate JSON");
return false;
}
[connection.offlineStorage persistDataToDisk:jsonData];
return true;
}
return false;
}

- (void) disconnected
{
// Handle stored config.
Expand Down Expand Up @@ -696,4 +726,8 @@ - (void) stop
}
}

- (void) setMaxOfflineStorageSize:(NSUInteger) size {
[connection setMaxOfflineStorageSize:size];
}

@end
Loading
Loading