Join us for Firebase Summit on November 10, 2021. Tune in to learn how Firebase can help you accelerate app development, release with confidence, and scale with ease. Register

禁用 Firebase 性能監控

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

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

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

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

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

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

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

要禁用或停用性能監控,您可以添加兩個關鍵屬性列表文件(一個Info.plist為您的iOS應用):

  • 要禁用性能監控,但讓你的應用程序在運行時啟用它,集firebase_performance_collection_enabledfalse在應用程序的Info.plist文件。

  • 要完全停用性能監控,沒有選擇在運行時啟用它,集firebase_performance_collection_deactivatedtrue在應用程序的Info.plist文件。

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

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

要在您的 iOS 應用程序下次啟動時禁用性能監控數據收集,請使用下面顯示的示例代碼。有關在iOS應用使用遠程配置的詳細信息,請參閱使用火力地堡遠程配置iOS上

  1. 確保遠程配置,那麼在您使用Podfile

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

    迅速

    import FirebaseRemoteConfig
    

    目標-C

    @import FirebaseRemoteConfig;
    
  3. 在你AppDelegate文件,下面的代碼添加到launchOptions在報表application:didFinishLaunchingWithOptions:實例方法:

    迅速

    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

    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 ,或者您的應用程序使用的另一種實現文件,添加以下代碼來獲取和激活遠程配置值:

    迅速

    //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

    //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. 要禁用表現在火力地堡控制台監測,建立在你的應用程序的項目perf_disable參數,然後將其值設置為true

    如果您的perf_disable值設置為false ,性能監控功能處於啟用狀態。

單獨禁用自動或自定義數據收集

您可以對上面顯示的代碼和 Firebase 控制台中的代碼進行一些更改,以便將所有自動(開箱即用)監控與自定義監控分開禁用。

  1. 下面的代碼添加到launchOptions在報表application:didFinishLaunchingWithOptions:實例方法(而不是上面什麼顯示了相同的實例方法'S):

    迅速

    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

    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