Skip to content

Commit bb8965c

Browse files
authored
Upload fatal MK reports on the next run of the app (#8688)
1 parent a550e68 commit bb8965c

File tree

6 files changed

+44
-9
lines changed

6 files changed

+44
-9
lines changed

Crashlytics/Crashlytics/Controllers/FIRCLSExistingReportManager.m

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
#import "Crashlytics/Crashlytics/Controllers/FIRCLSManagerData.h"
1818
#import "Crashlytics/Crashlytics/Controllers/FIRCLSReportUploader.h"
19+
#import "Crashlytics/Crashlytics/DataCollection/FIRCLSDataCollectionArbiter.h"
1920
#import "Crashlytics/Crashlytics/DataCollection/FIRCLSDataCollectionToken.h"
2021
#import "Crashlytics/Crashlytics/Helpers/FIRCLSLogger.h"
2122
#import "Crashlytics/Crashlytics/Models/FIRCLSFileManager.h"
2223
#import "Crashlytics/Crashlytics/Models/FIRCLSInternalReport.h"
24+
#import "Crashlytics/Crashlytics/Models/FIRCLSSettings.h"
2325
#import "Crashlytics/Crashlytics/Private/FIRCrashlyticsReport_Private.h"
2426
#import "Crashlytics/Crashlytics/Public/FirebaseCrashlytics/FIRCrashlyticsReport.h"
2527

@@ -31,6 +33,8 @@ @interface FIRCLSExistingReportManager ()
3133
@property(nonatomic, strong) FIRCLSFileManager *fileManager;
3234
@property(nonatomic, strong) FIRCLSReportUploader *reportUploader;
3335
@property(nonatomic, strong) NSOperationQueue *operationQueue;
36+
@property(nonatomic, strong) FIRCLSSettings *settings;
37+
@property(nonatomic, strong) FIRCLSDataCollectionArbiter *dataArbiter;
3438

3539
// This list of active reports excludes the brand new active report that will be created this run of
3640
// the app.
@@ -52,7 +56,9 @@ - (instancetype)initWithManagerData:(FIRCLSManagerData *)managerData
5256
}
5357

5458
_fileManager = managerData.fileManager;
59+
_settings = managerData.settings;
5560
_operationQueue = managerData.operationQueue;
61+
_dataArbiter = managerData.dataArbiter;
5662
_reportUploader = reportUploader;
5763

5864
return self;
@@ -91,16 +97,36 @@ - (NSUInteger)unsentReportsCount {
9197
*/
9298
- (NSArray<NSString *> *)getUnsentActiveReportsAndDeleteEmptyOrOld:(NSArray *)reportPaths {
9399
NSMutableArray<FIRCLSInternalReport *> *validReports = [NSMutableArray array];
100+
NSMutableArray<FIRCLSInternalReport *> *reports = [NSMutableArray array];
101+
94102
for (NSString *path in reportPaths) {
95103
FIRCLSInternalReport *_Nullable report = [FIRCLSInternalReport reportWithPath:path];
96104
if (!report) {
97105
continue;
98106
}
99107

108+
[reports addObject:report];
109+
}
110+
111+
if (reports.count == 0) {
112+
return @[];
113+
}
114+
115+
[reports sortUsingFunction:compareNewer context:nil];
116+
NSString *newestReportPath = [reports firstObject].path;
117+
118+
// If there was a MetricKit event recorded on the last run of the app, add it to the newest
119+
// report.
120+
if (self.settings.metricKitCollectionEnabled &&
121+
[self.fileManager metricKitDiagnosticFileExists]) {
122+
[self.fileManager createEmptyMetricKitFile:newestReportPath];
123+
}
124+
125+
for (FIRCLSInternalReport *report in reports) {
100126
// Delete reports without any crashes or non-fatals
101127
if (![report hasAnyEvents]) {
102128
[self.operationQueue addOperationWithBlock:^{
103-
[self.fileManager removeItemAtPath:path];
129+
[self.fileManager removeItemAtPath:report.path];
104130
}];
105131
continue;
106132
}

Crashlytics/Crashlytics/Controllers/FIRCLSMetricKitManager.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,10 @@ - (BOOL)processMetricKitPayload:(MXDiagnosticPayload *)diagnosticPayload
133133
return false;
134134
}
135135

136-
// TODO: Time stamp information is only available in begin and end time periods. Hopefully this
137-
// is updated with iOS 15.
138-
NSTimeInterval beginSecondsSince1970 = [diagnosticPayload.timeStampBegin timeIntervalSince1970];
136+
// Time stamp information from MetricKit is only available in begin and end time periods. For now,
137+
// we rely on the current timestamp to set the event timestamp, since this is likely more accurate
138+
// that the 24 hour block we'd otherwise have.
139+
NSTimeInterval beginSecondsSince1970 = [[NSDate date] timeIntervalSince1970];
139140
NSTimeInterval endSecondsSince1970 = [diagnosticPayload.timeStampEnd timeIntervalSince1970];
140141

141142
// Get file path for the active reports directory.

Crashlytics/Crashlytics/Controllers/FIRCLSReportManager.m

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ - (instancetype)initWithManagerData:(FIRCLSManagerData *)managerData
182182
googleAppID:self.googleAppID];
183183

184184
_notificationManager = [[FIRCLSNotificationManager alloc] init];
185+
186+
// This needs to be called before any values are read from settings
187+
NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
188+
[self.settings reloadFromCacheWithGoogleAppID:self.googleAppID currentTimestamp:currentTimestamp];
189+
185190
#if CLS_METRICKIT_SUPPORTED
186191
if (@available(iOS 15, *)) {
187192
if (self.settings.metricKitCollectionEnabled) {
@@ -263,10 +268,6 @@ - (FBLPromise *)deleteUnsentReports {
263268
- (FBLPromise<NSNumber *> *)startWithProfilingMark:(FIRCLSProfileMark)mark {
264269
NSString *executionIdentifier = self.executionIDModel.executionID;
265270

266-
// This needs to be called before any values are read from settings
267-
NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
268-
[self.settings reloadFromCacheWithGoogleAppID:self.googleAppID currentTimestamp:currentTimestamp];
269-
270271
// This needs to be called before the new report is created for
271272
// this run of the app.
272273
[self.existingReportManager collectExistingReports];

Crashlytics/Crashlytics/Models/FIRCLSFileManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
- (BOOL)moveItemAtPath:(NSString *)path toDirectory:(NSString *)destDir;
5959
- (BOOL)didCrashOnPreviousExecution;
6060
- (BOOL)metricKitDiagnosticFileExists;
61+
- (void)createEmptyMetricKitFile:(NSString *)reportPath;
6162
- (void)enumerateFilesInDirectory:(NSString *)directory
6263
usingBlock:(void (^)(NSString *filePath, NSString *extension))block;
6364
- (NSNumber *)fileSizeAtPath:(NSString *)path;

Crashlytics/Crashlytics/Models/FIRCLSFileManager.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ - (BOOL)metricKitDiagnosticFileExists {
147147
return ([contentsOfMetricKitDirectory count] > 0);
148148
}
149149

150+
- (void)createEmptyMetricKitFile:(NSString *)reportPath {
151+
NSString *metricKitFile =
152+
[reportPath stringByAppendingPathComponent:FIRCLSMetricKitFatalReportFile];
153+
[self createFileAtPath:metricKitFile contents:nil attributes:nil];
154+
}
155+
150156
- (void)enumerateFilesInDirectory:(NSString *)directory
151157
usingBlock:(void (^)(NSString *filePath, NSString *extension))block {
152158
for (NSString *path in [[self underlyingFileManager] contentsOfDirectoryAtPath:directory

Crashlytics/Crashlytics/Models/FIRCLSSettings.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ - (BOOL)metricKitCollectionEnabled {
286286
return value.boolValue;
287287
}
288288

289-
return YES;
289+
return NO;
290290
}
291291

292292
#pragma mark - Optional Limit Overrides

0 commit comments

Comments
 (0)