Vô hiệu hóa giám sát hiệu suất Firebase

Để cho phép người dùng chọn tham gia hoặc không tham gia sử dụng Giám sát hiệu suất Firebase, bạn có thể muốn định cấu hình ứng dụng của mình để có thể bật và tắt Giám sát hiệu suất. Bạn cũng có thể thấy khả năng này hữu ích trong quá trình phát triển và thử nghiệm ứng dụng.

Sau đây là một số lựa chọn để xem xét:

  • Bạn có thể tắt SDK giám sát hiệu suất khi xây dựng ứng dụng của mình bằng tùy chọn bật lại SDK khi chạy.

  • Bạn có thể xây dựng ứng dụng của mình khi bật SDK giám sát hiệu suất nhưng có tùy chọn tắt ứng dụng này trong thời gian chạy bằng cách sử dụng Cấu hình từ xa Firebase.

  • Bạn có thể tắt hoàn toàn SDK giám sát hiệu suất mà không có tùy chọn bật SDK này khi chạy.

Tắt tính năng Giám sát hiệu suất trong quá trình xây dựng ứng dụng của bạn

Một tình huống mà việc tắt Giám sát hiệu suất trong quá trình xây dựng ứng dụng của bạn có thể hữu ích là tránh báo cáo dữ liệu hiệu suất từ ​​phiên bản tiền phát hành của ứng dụng trong quá trình phát triển và thử nghiệm ứng dụng.

Để tắt hoặc tắt Giám sát hiệu suất, bạn có thể thêm một trong hai khóa vào tệp danh sách thuộc tính ( Info.plist ) cho ứng dụng Apple của mình:

  • Để tắt Giám sát hiệu suất nhưng cho phép ứng dụng của bạn bật tính năng này trong thời gian chạy, hãy đặt firebase_performance_collection_enabled thành false trong tệp Info.plist của ứng dụng.

  • Để tắt hoàn toàn Giám sát hiệu suất mà không có tùy chọn bật tính năng này trong thời gian chạy, hãy đặt firebase_performance_collection_deactivated thành true trong tệp Info.plist của ứng dụng của bạn.

Tắt ứng dụng của bạn khi chạy bằng Cấu hình từ xa

Cấu hình từ xa Firebase cho phép bạn thực hiện các thay đổi đối với hành vi và giao diện của ứng dụng, do đó, nó cung cấp một cách lý tưởng để cho phép bạn tắt Giám sát hiệu suất trong các phiên bản đã triển khai của ứng dụng.

Để tắt tính năng thu thập dữ liệu Giám sát hiệu suất vào lần tiếp theo khi ứng dụng Apple của bạn khởi động, hãy sử dụng mã ví dụ hiển thị bên dưới. Để biết thêm thông tin về cách sử dụng Cấu hình từ xa trong ứng dụng Apple, hãy xem Sử dụng Cấu hình từ xa Firebase trên nền tảng Apple .

  1. Đảm bảo rằng Remote Config được sử dụng trong Podfile của bạn:

    pod 'Firebase/RemoteConfig'
    
  2. Thêm phần sau vào đầu tệp AppDelegate của ứng dụng của bạn:

    Nhanh

    Lưu ý: Sản phẩm Firebase này không khả dụng trên các mục tiêu macOS, Mac Catalyst, watchOS.
    import FirebaseRemoteConfig
    

    Mục tiêu-C

    Lưu ý: Sản phẩm Firebase này không khả dụng trên các mục tiêu macOS, Mac Catalyst, watchOS.
    @import FirebaseRemoteConfig;
    
  3. Trong tệp AppDelegate của bạn, hãy thêm mã sau vào câu lệnh launchOptions trong phương thức phiên bản application:didFinishLaunchingWithOptions: ::

    Nhanh

    Lưu ý: Sản phẩm này không khả dụng trên các mục tiêu 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()
    

    Mục tiêu-C

    Lưu ý: Sản phẩm Firebase này không khả dụng trên các mục tiêu 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. Trong ViewController.m hoặc một tệp triển khai khác được ứng dụng của bạn sử dụng, hãy thêm mã sau để tìm nạp và kích hoạt các giá trị Cấu hình từ xa:

    Nhanh

    Lưu ý: Sản phẩm Firebase này không khả dụng trên các mục tiêu 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)")
      }
    }
    

    Mục tiêu-C

    Lưu ý: Sản phẩm Firebase này không khả dụng trên các mục tiêu 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. Để tắt Giám sát hiệu suất trong bảng điều khiển Firebase, hãy tạo tham số perf_disable trong dự án ứng dụng của bạn, sau đó đặt giá trị của tham số đó thành true .

    Nếu bạn đặt giá trị perf_disable thành false thì Giám sát hiệu suất vẫn được bật.

Tắt riêng tính năng thu thập dữ liệu tự động hoặc tùy chỉnh

Bạn có thể thực hiện một số thay đổi đối với mã hiển thị ở trên và trong bảng điều khiển Firebase để cho phép bạn tắt tất cả tính năng giám sát tự động (có sẵn) riêng biệt với giám sát tùy chỉnh.

  1. Thêm mã sau vào câu lệnh launchOptions trong phương thức phiên application:didFinishLaunchingWithOptions: (thay vì mã được hiển thị ở trên cho cùng một phương thức phiên bản):

    Nhanh

    Lưu ý: Sản phẩm Firebase này không khả dụng trên các mục tiêu 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()
    

    Mục tiêu-C

    Lưu ý: Sản phẩm Firebase này không khả dụng trên các mục tiêu 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. Hoàn thành phần sau trong bảng điều khiển Firebase:

    • Để tắt tất cả tính năng giám sát tự động (có sẵn), hãy tạo tham số perf_disable_auto trong dự án ứng dụng của bạn, sau đó đặt giá trị của tham số đó thành true .
    • Để tắt tất cả giám sát tùy chỉnh, hãy tạo tham số perf_disable_manual trong dự án ứng dụng của bạn, sau đó đặt giá trị của tham số đó thành true .