Disable the Firebase Performance Monitoring SDK についてのメモです。
Firebase Performance Monitoring は、有効または無効に設定することができる。
Firebase Performance Monitoring を有効にした状態でビルドすれば、Firebase Remote Configを使用することで、Firebase Performance Monitoringを無効に切り替えることもできる。
もちろん、Performance Monitoringを完全に無効にすることもできます。実行時に有効にするオプションはありません。
パフォーマンスモニタリングは現在ベータ版です。
アプリのビルドプロセス中にパフォーマンス監視を無効にする
アプリの作成プロセス中にパフォーマンスモニタリングを無効にすると便利な場合があるのは、アプリの開発やテスト中にアプリのプレリリース版のパフォーマンスデータを報告しないようにすることです。
Android
アプリケーションのgradle.propertiesファイルに次のプロパティを追加して、ビルド時に自動トレースとHTTP/Sネットワーク要求の監視(カスタムトレースではない)を無効にすることができます。
firebasePerformanceInstrumentationEnabled=false
このプロパティをtrueに変更すると、自動トレースとHTTP/Sネットワーク要求の監視が再度有効になります。
ビルド時にPerformance Monitoringを無効にすることもできますが、アプリケーションのAndroidManifest.xmlファイルの要素に要素を追加することで、実行時にアプリケーションを有効にすることができます。
<application>
...
<meta-data android:name="firebase_performance_collection_enabled" android:value="false" />
...
</application>
実行時にPerformance Monitoringを有効にするオプションなしでPerformance Monitoringを完全に無効にするには、次のように、アプリケーションのAndroidManifest.xmlファイルの要素に要素を追加します。
<application>
...
<meta-data android:name="firebase_performance_collection_deactivated" android:value="true" />
...
</application>
注:この設定はfirebase_performance_collection_enabled設定を無効にし、パフォーマンスモニタリングを再び有効にするには、アプリのAndroidManifest.xmlファイルから削除する必要があります。
Remote Configを使用して実行時にアプリを無効にする
Remote Configを使用すると、アプリの動作や外観を変更できるため、アプリのデプロイされたインスタンスでパフォーマンスモニタリングを無効にすることができます。
Android
次に表示されるサンプルコードを使用して、Androidアプリの起動時にパフォーマンスモニタリングデータ収集を無効にすることができます。
-
Remote ConfigがモジュールのGradleファイル(通常はapp / build.gradle)の依存関係セクションにあることを確認します。
compile 'com.google.firebase:firebase-config:10.2.5'
-
次のクラスをインポートします
import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.firebase.remoteconfig.FirebaseRemoteConfig; import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings;
-
次に、perf_disableがtrueに設定されている場合は、Remote Configを設定し、Performance Monitoringを無効にします。
//Setup remote config final FirebaseRemoteConfig mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance(); // You can uncomment the following two statements to permit more fetches when // validating your app, but you should comment out or delete these lines before // distributing your app in production. // FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder() // .setDeveloperModeEnabled(BuildConfig.DEBUG) // .build(); // mFirebaseRemoteConfig.setConfigSettings(configSettings); // Load in-app defaults from an XML file that sets perf_disable to false until you update // values in the Firebase Console mFirebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults); //Observe the remote config parameter "perf_disable" and disable Performance Monitoring if true if (mFirebaseRemoteConfig.getBoolean("perf_disable")) { FirebasePerformance.getInstance().setPerformanceCollectionEnabled(false); } else { FirebasePerformance.getInstance().setPerformanceCollectionEnabled(true); }
-
最後に、MainActivity.javaに次のコードを追加して、Remote Config値をフェッチしてアクティブにする。
//Remote Config fetches and activates parameter values from the service mFirebaseRemoteConfig.fetch(3600) .addOnCompleteListener(this, new OnCompleteListener() { @Override public void onComplete(@NonNull Task task) { if (task.isSuccessful()) { mFirebaseRemoteConfig.activateFetched(); } else { } } });
Firebaseコンソールでパフォーマンス監視を無効にするには、アプリケーションのプロジェクトにperf_disableパラメータを作成し、その値をtrueに設定します。
この変更により、Performance Monitoring SDKの「操作なし」呼び出し(NOOP)が呼び出され、アプリでPerformance Monitoring SDKを使用することによるアプリのパフォーマンスへの重大な影響が排除されます。
perf_disableの値をfalseに設定すると、パフォーマンス監視は有効のままです。