Wyłącz Monitorowanie wydajności Firebase


Aby umożliwić użytkownikom włączanie lub wyłączanie Firebase Performance Monitoring, możesz chcesz skonfigurować aplikację w taki sposób, aby móc włączać i wyłączać usługę Performance Monitoring. Ty Może się też okazać przydatne podczas tworzenia i testowania aplikacji.

Oto kilka opcji do rozważenia:

  • Podczas tworzenia aplikacji możesz wyłączyć pakiet SDK Performance Monitoring oraz opcję włącz ją ponownie w czasie działania aplikacji.

  • Aplikację możesz skompilować z włączonym pakietem SDK Performance Monitoring, ale masz też możliwość jej wyłączenia w czasie działania za pomocą pakietu Firebase Remote Config.

  • Możesz całkowicie dezaktywować pakiet SDK Performance Monitoring bez możliwości jego włączenia w czasie działania aplikacji.

Wyłączanie Performance Monitoring podczas kompilowania aplikacji

Jedną z sytuacji, w której wyłączenie Performance Monitoring podczas tworzenia aplikacji może być warto uniknąć raportowania danych o skuteczności z przedpremierowej wersji podczas tworzenia i testowania aplikacji.

Aby wyłączyć lub dezaktywować Performance Monitoring, możesz dodać jeden z 2 kluczy do plik z listą właściwości (Info.plist) dla aplikacji Apple:

  • Aby wyłączyć Performance Monitoring, ale zezwolić aplikacji na jej włączanie w czasie działania, ustaw Z firebase_performance_collection_enabled na false w aplikacji Info.plist.

  • Aby całkowicie dezaktywować usługę Performance Monitoring bez opcji włączania jej w czasie działania, ustaw firebase_performance_collection_deactivated na true w aplikacji Info.plist.

Wyłączaj aplikację w czasie działania za pomocą funkcji Remote Config

Firebase Remote Config umożliwia zmianę działania i wyglądu aplikacji, więc jest to idealny sposób na wyłączenie funkcji Performance Monitoring w instancji wdrożonych aplikacji.

Aby wyłączyć zbieranie danych przez funkcję Performance Monitoring przy następnym uruchomieniu aplikacji Apple: skorzystaj z przykładowego kodu widocznego poniżej. Więcej informacji o używaniu Remote Config w aplikacji Apple znajdziesz w artykule Korzystanie z funkcji Firebase Remote Config na platformach Apple.

  1. Upewnij się, że Podfile używa Remote Config:

    pod 'Firebase/RemoteConfig'
    
  2. Na początku pliku AppDelegate z informacjami o aplikacji dodaj:

    Swift

    Uwaga: ta usługa Firebase nie jest dostępna na systemy docelowe macOS, Mac Catalyst i watchOS.
    import FirebaseRemoteConfig
    

    Objective-C

    Uwaga: ta usługa Firebase nie jest dostępna na systemy docelowe macOS, Mac Catalyst i watchOS.
    @import FirebaseRemoteConfig;
    
  3. W pliku AppDelegate dodaj ten kod do instrukcji launchOptions w metodzie instancji application:didFinishLaunchingWithOptions::

    Swift

    Uwaga: ta usługa nie jest dostępna na urządzeniach z systemem macOS, Mac Catalyst ani 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

    Uwaga: ta usługa Firebase nie jest dostępna w przypadku docelowych systemów macOS, Mac Catalyst i 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. W pliku ViewController.m lub innym pliku implementacji używanym przez aplikację dodaj ten kod do pobrania i aktywowania wartości Remote Config:

    Swift

    Uwaga: ta usługa Firebase nie jest dostępna na systemy docelowe macOS, Mac Catalyst i 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

    Uwaga: ta usługa Firebase nie jest dostępna na systemy docelowe macOS, Mac Catalyst i 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. Aby wyłączyć Performance Monitoring w konsoli Firebase, utwórz parametr perf_disable w projekcie aplikacji, a potem ustaw jego wartość na true.

    Jeśli ustawisz wartość perf_disable na false, Performance Monitoring pozostanie bez zmian. .

Osobne wyłączanie automatycznego lub niestandardowego zbierania danych

Możesz wprowadzić pewne zmiany w powyższym kodzie i w konsoli Firebase, aby umożliwić wyłączenie całego automatycznego (domyślnego) monitorowania niezależnie od monitorowania niestandardowego.

  1. Dodaj ten kod do instrukcji launchOptions w tabeli application:didFinishLaunchingWithOptions: (zamiast co widać powyżej dla tej samej metody instancji):

    Swift

    Uwaga: ta usługa Firebase nie jest dostępna na systemy docelowe macOS, Mac Catalyst i 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

    Uwaga: ta usługa Firebase nie jest dostępna na systemy docelowe macOS, Mac Catalyst i 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. W konsoli Firebase wykonaj te czynności:

    • Aby wyłączyć całe automatyczne (gotowe) monitorowanie, utwórz perf_disable_auto w projekcie aplikacji, a następnie ustaw jego na true.
    • Aby wyłączyć wszystkie niestandardowe monitorowanie, utwórz w projekcie aplikacji parametr perf_disable_manual i ustaw jego wartość na true.