소스 검색

Upload fatal MK reports on the next run of the app (#8688)

elenadoty 4 년 전
부모
커밋
bb8965c3ce

+ 27 - 1
Crashlytics/Crashlytics/Controllers/FIRCLSExistingReportManager.m

@@ -16,10 +16,12 @@
 
 #import "Crashlytics/Crashlytics/Controllers/FIRCLSManagerData.h"
 #import "Crashlytics/Crashlytics/Controllers/FIRCLSReportUploader.h"
+#import "Crashlytics/Crashlytics/DataCollection/FIRCLSDataCollectionArbiter.h"
 #import "Crashlytics/Crashlytics/DataCollection/FIRCLSDataCollectionToken.h"
 #import "Crashlytics/Crashlytics/Helpers/FIRCLSLogger.h"
 #import "Crashlytics/Crashlytics/Models/FIRCLSFileManager.h"
 #import "Crashlytics/Crashlytics/Models/FIRCLSInternalReport.h"
+#import "Crashlytics/Crashlytics/Models/FIRCLSSettings.h"
 #import "Crashlytics/Crashlytics/Private/FIRCrashlyticsReport_Private.h"
 #import "Crashlytics/Crashlytics/Public/FirebaseCrashlytics/FIRCrashlyticsReport.h"
 
@@ -31,6 +33,8 @@ NSUInteger const FIRCLSMaxUnsentReports = 4;
 @property(nonatomic, strong) FIRCLSFileManager *fileManager;
 @property(nonatomic, strong) FIRCLSReportUploader *reportUploader;
 @property(nonatomic, strong) NSOperationQueue *operationQueue;
+@property(nonatomic, strong) FIRCLSSettings *settings;
+@property(nonatomic, strong) FIRCLSDataCollectionArbiter *dataArbiter;
 
 // This list of active reports excludes the brand new active report that will be created this run of
 // the app.
@@ -52,7 +56,9 @@ NSUInteger const FIRCLSMaxUnsentReports = 4;
   }
 
   _fileManager = managerData.fileManager;
+  _settings = managerData.settings;
   _operationQueue = managerData.operationQueue;
+  _dataArbiter = managerData.dataArbiter;
   _reportUploader = reportUploader;
 
   return self;
@@ -91,16 +97,36 @@ NSInteger compareNewer(FIRCLSInternalReport *reportA,
  */
 - (NSArray<NSString *> *)getUnsentActiveReportsAndDeleteEmptyOrOld:(NSArray *)reportPaths {
   NSMutableArray<FIRCLSInternalReport *> *validReports = [NSMutableArray array];
+  NSMutableArray<FIRCLSInternalReport *> *reports = [NSMutableArray array];
+
   for (NSString *path in reportPaths) {
     FIRCLSInternalReport *_Nullable report = [FIRCLSInternalReport reportWithPath:path];
     if (!report) {
       continue;
     }
 
+    [reports addObject:report];
+  }
+
+  if (reports.count == 0) {
+    return @[];
+  }
+
+  [reports sortUsingFunction:compareNewer context:nil];
+  NSString *newestReportPath = [reports firstObject].path;
+
+  // If there was a MetricKit event recorded on the last run of the app, add it to the newest
+  // report.
+  if (self.settings.metricKitCollectionEnabled &&
+      [self.fileManager metricKitDiagnosticFileExists]) {
+    [self.fileManager createEmptyMetricKitFile:newestReportPath];
+  }
+
+  for (FIRCLSInternalReport *report in reports) {
     // Delete reports without any crashes or non-fatals
     if (![report hasAnyEvents]) {
       [self.operationQueue addOperationWithBlock:^{
-        [self.fileManager removeItemAtPath:path];
+        [self.fileManager removeItemAtPath:report.path];
       }];
       continue;
     }

+ 4 - 3
Crashlytics/Crashlytics/Controllers/FIRCLSMetricKitManager.m

@@ -133,9 +133,10 @@
     return false;
   }
 
-  // TODO: Time stamp information is only available in begin and end time periods. Hopefully this
-  // is updated with iOS 15.
-  NSTimeInterval beginSecondsSince1970 = [diagnosticPayload.timeStampBegin timeIntervalSince1970];
+  // Time stamp information from MetricKit is only available in begin and end time periods. For now,
+  // we rely on the current timestamp to set the event timestamp, since this is likely more accurate
+  // that the 24 hour block we'd otherwise have.
+  NSTimeInterval beginSecondsSince1970 = [[NSDate date] timeIntervalSince1970];
   NSTimeInterval endSecondsSince1970 = [diagnosticPayload.timeStampEnd timeIntervalSince1970];
 
   // Get file path for the active reports directory.

+ 5 - 4
Crashlytics/Crashlytics/Controllers/FIRCLSReportManager.m

@@ -182,6 +182,11 @@ typedef NSNumber FIRCLSWrappedReportAction;
                                                            googleAppID:self.googleAppID];
 
   _notificationManager = [[FIRCLSNotificationManager alloc] init];
+
+  // This needs to be called before any values are read from settings
+  NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
+  [self.settings reloadFromCacheWithGoogleAppID:self.googleAppID currentTimestamp:currentTimestamp];
+
 #if CLS_METRICKIT_SUPPORTED
   if (@available(iOS 15, *)) {
     if (self.settings.metricKitCollectionEnabled) {
@@ -263,10 +268,6 @@ typedef NSNumber FIRCLSWrappedReportAction;
 - (FBLPromise<NSNumber *> *)startWithProfilingMark:(FIRCLSProfileMark)mark {
   NSString *executionIdentifier = self.executionIDModel.executionID;
 
-  // This needs to be called before any values are read from settings
-  NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
-  [self.settings reloadFromCacheWithGoogleAppID:self.googleAppID currentTimestamp:currentTimestamp];
-
   // This needs to be called before the new report is created for
   // this run of the app.
   [self.existingReportManager collectExistingReports];

+ 1 - 0
Crashlytics/Crashlytics/Models/FIRCLSFileManager.h

@@ -58,6 +58,7 @@
 - (BOOL)moveItemAtPath:(NSString *)path toDirectory:(NSString *)destDir;
 - (BOOL)didCrashOnPreviousExecution;
 - (BOOL)metricKitDiagnosticFileExists;
+- (void)createEmptyMetricKitFile:(NSString *)reportPath;
 - (void)enumerateFilesInDirectory:(NSString *)directory
                        usingBlock:(void (^)(NSString *filePath, NSString *extension))block;
 - (NSNumber *)fileSizeAtPath:(NSString *)path;

+ 6 - 0
Crashlytics/Crashlytics/Models/FIRCLSFileManager.m

@@ -147,6 +147,12 @@ NSString *const FIRCLSMetricKitDiagnosticPath = @"/MetricKit/Diagnostics/";
   return ([contentsOfMetricKitDirectory count] > 0);
 }
 
+- (void)createEmptyMetricKitFile:(NSString *)reportPath {
+  NSString *metricKitFile =
+      [reportPath stringByAppendingPathComponent:FIRCLSMetricKitFatalReportFile];
+  [self createFileAtPath:metricKitFile contents:nil attributes:nil];
+}
+
 - (void)enumerateFilesInDirectory:(NSString *)directory
                        usingBlock:(void (^)(NSString *filePath, NSString *extension))block {
   for (NSString *path in [[self underlyingFileManager] contentsOfDirectoryAtPath:directory

+ 1 - 1
Crashlytics/Crashlytics/Models/FIRCLSSettings.m

@@ -286,7 +286,7 @@ NSString *const AppVersion = @"app_version";
     return value.boolValue;
   }
 
-  return YES;
+  return NO;
 }
 
 #pragma mark - Optional Limit Overrides