Catch up on highlights from Firebase at Google I/O 2023. Learn more

禁用 Firebase 性能監控

要讓您的用戶選擇加入或退出使用 Firebase 性能監控,您可能需要配置您的應用,以便您可以啟用和禁用性能監控。您可能還會發現此功能在應用程序開發和測試期間很有用。

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

  • 您可以在構建應用程序時禁用性能監控 SDK,並可以選擇在運行時重新啟用它。

  • 您可以在啟用性能監控 SDK 的情況下構建您的應用程序,但可以選擇在運行時使用 Firebase 遠程配置將其禁用。

  • 您可以完全停用 Performance Monitoring SDK,沒有在運行時啟用它的選項。

在您的應用構建過程中禁用性能監控

在應用構建過程中禁用性能監控可能有用的一種情況是,避免在應用開發和測試期間報告應用預發布版本的性能數據。

要禁用或停用性能監控,您可以將兩個鍵之一添加到您的 Apple 應用程序的屬性列表文件 ( Info.plist ):

  • 要禁用性能監控,但允許您的應用在運行時啟用它,請在應用的Info.plist文件中將firebase_performance_collection_enabled設置為false

  • 要完全停用性能監控,並且沒有在運行時啟用它的選項,請在應用程序的Info.plist文件中將firebase_performance_collection_deactivated設置為true

使用遠程配置在運行時禁用您的應用

Firebase Remote Config 允許您更改應用程序的行為和外觀,因此它提供了一種理想的方式讓您在已部署的應用程序實例中禁用性能監控。

要在您的 Apple 應用程序下次啟動時禁用性能監控數據收集,請使用下面顯示的示例代碼。有關在 Apple 應用程序中使用 Remote Config 的更多信息,請參閱在 Apple 平台上使用 Firebase Remote Config

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

    pod 'Firebase/RemoteConfig'
    
  2. 將以下內容添加到應用的AppDelegate文件的頂部:

    迅速

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

    目標-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()
    

    目標-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)")
      }
    }
    

    目標-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()
    

    目標-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