禁用 Firebase 效能監控

要讓您的使用者選擇加入或退出使用 Firebase 效能監控,您可能需要設定您的應用,以便可以啟用和停用效能監控。您可能還會發現此功能在應用程式開發和測試期間很有用。

以下是一些需要考慮的選項:

  • 您可以在建立應用程式時停用效能監控 SDK,並可以選擇在運行時重新啟用它。

  • 您可以在啟用效能監控 SDK 的情況下建立應用,但可以選擇使用 Firebase 遠端配置在運行時停用它。

  • 您可以完全停用效能監控 SDK,而無需選擇在運行時啟用它。

在應用程式建置過程中停用效能監控

在應用程式建置過程中停用效能監控可能有用的一種情況是,避免在應用程式開發和測試期間報告應用程式預發布版本的效能資料。

若要停用或停用效能監控,您可以將兩個按鍵之一新增至 Apple 應用程式的屬性清單檔案 ( Info.plist ):

  • 若要停用效能監控,但允許您的應用程式在執行時間啟用它,請在套用的Info.plist檔案中將firebase_performance_collection_enabled設定為false

  • 若要完全停用效能監控(無法在執行時間啟用它),請在應用程式的Info.plist檔案中將firebase_performance_collection_deactivated設為true

使用遠端配置在運行時停用您的應用程式

Firebase 遠端配置可讓您變更應用程式的行為和外觀,因此它提供了一種理想的方法來讓您在應用程式的已部署實例中停用效能監控。

若要在您的 Apple 應用程式下次啟動時停用效能監控資料收集,請使用下面所示的範例程式碼。有關在 Apple 應用程式中使用 Remote Config 的更多信息,請參閱在 Apple 平台上使用 Firebase Remote Config

  1. 確保您的Podfile中使用了遠端配置:

    pod 'Firebase/RemoteConfig'
    
  2. 將以下內容新增至應用程式的AppDelegate檔案的頂部:

    迅速

    注意:此 Firebase 產品不適用於 macOS、Mac Catalyst、watchOS 目標。
    import FirebaseRemoteConfig
    

    Objective-C

    注意:此 Firebase 產品不適用於 macOS、Mac Catalyst、watchOS 目標。
    @import FirebaseRemoteConfig;
    
  3. AppDelegate檔案中,將以下程式碼加入application:didFinishLaunchingWithOptions:實例方法中的launchOptions語句中:

    迅速

    注意:此產品不適用於 macOS、Mac Catalyst、watchOS 目標。
    remoteConfig = RemoteConfig.remoteConfig()
    // You can change the "false" below to "true" to permit more fetches when validating
    // your app, but you should change it back to "false" or remove this statement before
    // distributing your app in production.
    let remoteConfigSettings = RemoteConfigSettings(developerModeEnabled: false)
    remoteConfig.configSettings = remoteConfigSettings!
    // Load in-app defaults from a plist file that sets perf_disable to false until
    // you update values in the Firebase console.
    remoteConfig.setDefaultsFromPlistFileName("RemoteConfigDefaults")
    // Important! This needs to be applied before FirebaseApp.configure()
    if !remoteConfig["perf_disable"].boolValue {
        // The following line disables all automatic (out-of-the-box) monitoring
        Performance.sharedInstance().isInstrumentationEnabled = false
        // The following line disables all custom monitoring
        Performance.sharedInstance().isDataCollectionEnabled = false
    }
    else {
        Performance.sharedInstance().isInstrumentationEnabled = true
        Performance.sharedInstance().isDataCollectionEnabled = true
    }
    // Use Firebase library to configure APIs
    FirebaseApp.configure()
    

    Objective-C

    注意:此 Firebase 產品不適用於 macOS、Mac Catalyst、watchOS 目標。
    self.remoteConfig = [FIRRemoteConfig remoteConfig];
    // You can change the NO below to YES to permit more fetches when validating
    // your app, but you should change it back to NO or remove this statement before
    // distributing your app in production.
    FIRRemoteConfigSettings *remoteConfigSettings =
        [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:NO];
    self.remoteConfig.configSettings = remoteConfigSettings;
    // Load in-app defaults from a plist file that sets perf_disable to false until
    // you update values in the Firebase console.
    [self.remoteConfig setDefaultsFromPlistFileName:@"RemoteConfigDefaults"];
    // Important! This needs to be applied before [FIRApp configure]
    if (!self.remoteConfig[@"perf_disable"].numberValue.boolValue) {
        // The following line disables all automatic (out-of-the-box) monitoring
        [FIRPerformance sharedInstance].instrumentationEnabled = NO;
        // The following line disables all custom monitoring
        [FIRPerformance sharedInstance].dataCollectionEnabled = NO;
    }
    else {
        [FIRPerformance sharedInstance].instrumentationEnabled = YES;
        [FIRPerformance sharedInstance].dataCollectionEnabled = YES;
    }
    // Use Firebase library to configure APIs
    [FIRApp configure];
    
  4. ViewController.m或應用程式使用的其他實作檔案​​中,新增以下程式碼以取得並啟動遠端設定值:

    迅速

    注意:此 Firebase 產品不適用於 macOS、Mac Catalyst、watchOS 目標。
    //RemoteConfig fetch and activation in your app, shortly after startup
    remoteConfig.fetch(withExpirationDuration: TimeInterval(30.0)) { (status, error) -> Void in
      if status == .success {
        print("Config fetched!")
        self.remoteConfig.activateFetched()
      } else {
        print("Config not fetched")
        print("Error \(error!.localizedDescription)")
      }
    }
    

    Objective-C

    注意:此 Firebase 產品不適用於 macOS、Mac Catalyst、watchOS 目標。
    //RemoteConfig fetch and activation in your app, shortly after startup
    [self.remoteConfig fetchWithExpirationDuration:30.0 completionHandler:^(FIRRemoteConfigFetchStatus status, NSError *error) {
      if (status == FIRRemoteConfigFetchStatusSuccess) {
        NSLog(@"Config fetched!");
        [self.remoteConfig activateFetched];
      } else {
        NSLog(@"Config not fetched");
        NSLog(@"Error %@", error.localizedDescription);
      }
    }];
    
  5. 若要在 Firebase 控制台中停用效能監控,請在套用的專案中建立一個perf_disable參數,然後將其值設為true

    如果將perf_disable的值設為false ,效能監控將保持啟用狀態。

單獨停用自動或自訂資料收集

您可以對上面和 Firebase 控制台中顯示的程式碼進行一些更改,以便您可以獨立於自訂監控來停用所有自動(開箱即用)監控。

  1. 將以下程式碼加入application:didFinishLaunchingWithOptions:實例方法中的launchOptions語句中(而不是上面顯示的相同實例方法):

    迅速

    注意:此 Firebase 產品不適用於 macOS、Mac Catalyst、watchOS 目標。
    remoteConfig = FIRRemoteConfig.remoteConfig()
    let remoteConfigSettings = FIRRemoteConfigSettings(developerModeEnabled: true)
    remoteConfig.configSettings = remoteConfigSettings!
    // Important! This needs to be applied before FirebaseApp.configure()
    if remoteConfig["perf_disable_auto"].boolValue {
        // The following line disables all automatic (out-of-the-box) monitoring
        Performance.sharedInstance().isInstrumentationEnabled = false
    }
    else {
        Performance.sharedInstance().isInstrumentationEnabled = true
    }
    if remoteConfig["perf_disable_manual"].boolValue {
        // The following line disables all custom monitoring
        Performance.sharedInstance().isDataCollectionEnabled = false
    }
    else {
        Performance.sharedInstance().isDataCollectionEnabled = true
    }
    // Use Firebase library to configure APIs
    FirebaseApp.configure()
    

    Objective-C

    注意:此 Firebase 產品不適用於 macOS、Mac Catalyst、watchOS 目標。
    self.remoteConfig = [FIRRemoteConfig remoteConfig];
    FIRRemoteConfigSettings *remoteConfigSettings =
        [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:YES];
    self.remoteConfig.configSettings = remoteConfigSettings;
    // Important! This needs to be applied before [FirebaseApp configure]
    if (self.remoteConfig[@"perf_disable_auto"].numberValue.boolValue) {
        // The following line disables all automatic (out-of-the-box) monitoring
        [FIRPerformance sharedInstance].instrumentationEnabled = NO;
    }
    else {
        [FIRPerformance sharedInstance].instrumentationEnabled = YES;
    }
    if (self.remoteConfig[@"perf_disable_manual"].numberValue.boolValue) {
        // The following line disables all custom monitoring
        [FIRPerformance sharedInstance].dataCollectionEnabled = NO;
    }
    else {
        [FIRPerformance sharedInstance].dataCollectionEnabled = YES;
    }
    // Use Firebase library to configure APIs
    [FirebaseApp configure];
    
  2. 在 Firebase 控制台中完成以下操作:

    • 若要停用所有自動(開箱即用)監控,請在應用程式的專案中建立perf_disable_auto參數,然後將其值設為true
    • 若要停用所有自訂監控,請在應用程式的專案中建立perf_disable_manual參數,然後將其值設為true