停用 Firebase Performance Monitoring

如要讓使用者選擇啟用或停用 Firebase Performance Monitoring,您可以 ,以便啟用及停用 Performance Monitoring。個人中心 也可能會在開發及測試應用程式時利用這項功能非常實用。

以下提供幾個您可以考慮的選項:

  • 您可以在建構應用程式時停用 Performance Monitoring SDK,以及 在執行階段重新啟用。

  • 您可以在啟用 Performance Monitoring SDK 的情況下建構應用程式,但也可以選擇 並透過 Firebase 遠端設定在執行階段停用該功能

  • 您可以完全停用 Performance Monitoring SDK,而且無法啟用 執行程式碼

在應用程式建構程序中停用 Performance Monitoring

在應用程式建構程序中停用 Performance Monitoring 的情況可能是 可避免從應用程式的預先發布版記錄成效資料 期間。

如要停用或停用 Performance Monitoring,您可以在 Apple 應用程式的屬性清單檔案 (Info.plist):

  • 如要停用 Performance Monitoring,但允許應用程式在執行階段啟用這項功能,請進行設定 應用程式中的 firebase_performance_collection_enabledfalse Info.plist 檔案。

  • 如要完全停用 Performance Monitoring,而且無法在執行階段選擇啟用, 將應用程式的 firebase_performance_collection_deactivated 設為 true Info.plist 檔案。

使用遠端設定在執行階段停用應用程式

Firebase 遠端設定可讓您變更行為和外觀 以便在應用程式層級停用 Performance Monitoring 所部署的應用程式執行個體

如要在 Apple 應用程式下次啟動時停用 Performance Monitoring 資料收集功能, 請參考下方的範例程式碼。如要進一步瞭解如何 Apple 應用程式中的遠端設定功能,請參閱 在 Apple 平台上使用 Firebase 遠端設定

  1. 確保遠端設定在您的 Podfile 中使用:

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

    Swift

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

    Objective-C

    注意:這項 Firebase 產品不適用於 macOS、Mac Catalyst 和 watchOS 目標。
    @import FirebaseRemoteConfig;
    
  3. AppDelegate 檔案中,將下列程式碼新增至 launchOptions application:didFinishLaunchingWithOptions: 執行個體中的陳述式 方法:

    Swift

    注意:這項產品不適用於 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 或其他應用程式使用的實作檔案中,新增 使用下列程式碼擷取並啟用遠端設定值:

    Swift

    注意:這項 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 控制台中停用 Performance Monitoring,請建立 perf_disable 參數,然後將值設為 true

    如果將 perf_disable 的值設為 false,Performance Monitoring 將維持不變

分別停用自動或自訂資料收集功能

您可以對上方程式碼和 Firebase 控制台進行變更 可讓您分別停用 以及自訂監控方式

  1. 將下列程式碼加入launchOptions application:didFinishLaunchingWithOptions: 執行個體方法 (而非 如上所示的同一個執行個體方法,所示:

    Swift

    注意:這項 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