停用 Firebase 性能监控

要允许用户可以选择启用或选择停用 Firebase 性能监控,您需要对应用进行配置,以便能启用和停用性能监控。这一功能在应用开发和测试期间可能也有用。

请考虑以下备选方案:

  • 您可以在构建应用时停用 Performance Monitoring SDK 并提供在运行时重新启用该 SDK 的选项。

  • 您可以在构建应用时启用 Performance Monitoring SDK 并提供在运行时停用该 SDK 的选项(使用 Firebase 远程配置)。

  • 您可以完全停用 Performance Monitoring SDK,不提供在运行时启用它的选项。

在应用构建过程中停用性能监控

需要在应用构建过程中停用性能监控的一种情况是,为了避免在应用开发和测试期间报告预发布版应用的性能数据。

若要停用或禁用性能监控,您可以将两个密钥之一添加到 iOS 应用的属性列表文件 (Info.plist):

  • 若要停用性能监控但允许您的应用在运行时将其启用,请在您应用的 Info.plist 文件中将 firebase_performance_collection_enabled 设为 false

  • 若要完全禁用性能监控且不提供在运行时将其启用的选项,请在您应用的 Info.plist 文件中将 firebase_performance_collection_deactivated 设为 true

使用远程配置在运行时停用您的应用

您可以使用 Firebase 远程配置更改应用的行为和外观,因此,要为您的应用的已部署实例停用性能监控,利用远程配置是一种理想的方式。

若要在 iOS 应用下次启动时停用性能监控数据收集,请使用下面的示例代码。如需了解关于在 iOS 应用中使用远程配置的详情,请参阅使用 Firebase 远程配置(iOS 版)

  1. 确保在 Podfile 中使用远程配置:

    pod 'Firebase/RemoteConfig'
    
  2. 在应用的 AppDelegate 文件的顶部添加以下内容:

    Swift

    import FirebaseRemoteConfig
    

    Objective-C

    @import FirebaseRemoteConfig;
    
  3. AppDelegate 文件中,将以下代码添加到 application:didFinishLaunchingWithOptions: 实例方法中的 launchOptions 语句:

    Swift

    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 automatic traces and HTTP/S network monitoring
        Performance.sharedInstance().isInstrumentationEnabled = false
        // The following line disables custom traces
        Performance.sharedInstance().isDataCollectionEnabled = false
    }
    else {
        Performance.sharedInstance().isInstrumentationEnabled = true
        Performance.sharedInstance().isDataCollectionEnabled = true
    }
    // Use Firebase library to configure APIs
    FirebaseApp.configure()
    

    Objective-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 automatic traces and HTTP/S network monitoring
        [FIRPerformance sharedInstance].instrumentationEnabled = NO;
        // The following line disables custom traces
        [FIRPerformance sharedInstance].dataCollectionEnabled = NO;
    }
    else {
        [FIRPerformance sharedInstance].instrumentationEnabled = YES;
        [FIRPerformance sharedInstance].dataCollectionEnabled = YES;
    }
    // Use Firebase library to configure APIs
    [FIRApp configure];
    
  4. ViewController.m 或应用使用的其他实现文件中添加以下代码,以提取并激活远程配置值:

    Swift

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

    //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 控制台进行一些更改,以允许分别停用自动数据收集(应用启动跟踪记录和 HTTP/HTTPS 网络请求)和手动数据收集(自定义跟踪记录)。

  1. 将以下代码添加到 application:didFinishLaunchingWithOptions: 实例方法(不是上述同一个实例方法)中的 launchOptions 语句:

    Swift

    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 automatic traces and HTTP/S network monitoring
        Performance.sharedInstance().isInstrumentationEnabled = false
    }
    else {
        Performance.sharedInstance().isInstrumentationEnabled = true
    }
    if remoteConfig["perf_disable_manual"].boolValue {
        // The following line disables custom traces
        Performance.sharedInstance().isDataCollectionEnabled = false
    }
    else {
        Performance.sharedInstance().isDataCollectionEnabled = true
    }
    // Use Firebase library to configure APIs
    FirebaseApp.configure()
    

    Objective-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 automatic traces and HTTP/S network monitoring
        [FIRPerformance sharedInstance].instrumentationEnabled = NO;
    }
    else {
        [FIRPerformance sharedInstance].instrumentationEnabled = YES;
    }
    if (self.remoteConfig[@"perf_disable_manual"].numberValue.boolValue) {
        // The following line disables custom traces
        [FIRPerformance sharedInstance].dataCollectionEnabled = NO;
    }
    else {
        [FIRPerformance sharedInstance].dataCollectionEnabled = YES;
    }
    // Use Firebase library to configure APIs
    [FirebaseApp configure];
    
  2. 在 Firebase 控制台中完成以下操作:

    • 若要停用自动跟踪记录和 HTTP/HTTPS 网络监控,请在应用的项目中创建一个 perf_disable_auto 参数,然后将其值设置为 true
    • 若要停用自定义跟踪记录,请在应用的项目中创建一个 perf_disable_manual 参数,然后将其值设置为 true