השבתת מעקב אחר ביצועים ב-Firebase


במהלך הפיתוח והבדיקה של האפליקציה, יכול להיות שתרצו להשבית את Performance Monitoring.

לדוגמה, אם משביתים את Performance Monitoring במהלך תהליך ה-build של האפליקציה, אפשר:

  • להשבית פונקציות מסוימות של Performance Monitoring (כמו אלה שסופקו על ידי הפלאגין של Performance Monitoring ל-Gradle) בגרסאות ה-build לניפוי באגים, אבל להפעיל מחדש את הפונקציות בגרסת ה-build להפצה.

  • משביתים את Performance Monitoring כשמפתחים את האפליקציה, אבל מאפשרים לאפליקציה להפעיל אותה מחדש בזמן הריצה.

  • צריך להשבית את Performance Monitoring כשמפתחים את האפליקציה, ולא לאפשר לאפליקציה להפעיל אותו מחדש בזמן הריצה.

אפשר גם לפתח את האפליקציה כש-Performance Monitoring מופעל, אבל להשתמש ב-Firebase Remote Config כדי שתהיה לכם גמישות להשבית (ולהפעיל מחדש) את Performance Monitoring באפליקציה בסביבת הייצור. באפשרות הזו, אתם יכולים אפילו להגדיר את האפליקציה כך שהמשתמשים יוכלו להביע הסכמה או לבטל את ההסכמה לשימוש ב-Performance Monitoring.

השבתת Performance Monitoring במהלך תהליך פיתוח האפליקציה

אפשר להשבית את Performance Monitoring במהלך תהליך ה-build על ידי השבתת הפלאגין של Performance Monitoring Gradle ו/או על ידי השבתת ספריית Android של Performance Monitoring.

במהלך הפיתוח וניפוי הבאגים, כדאי להשבית את הפלאגין כי הכלים של הפלאגין יכולים להאריך את זמן ה-build. עם זאת, כדאי להשאיר את הספרייה מופעלת כדי שעדיין תוכלו לראות את נתוני הביצועים ממעקבי הפעילות של הפעלת האפליקציה, האפליקציה בחזית והאפליקציה ברקע וגם את כל מעקבי הפעילות של הקוד בהתאמה אישית באפליקציה.

השבתת הפלאגין של Performance Monitoring ל-Gradle

כדי להשבית את הפלאגין Performance Monitoring, אפשר להוסיף דגל instrumentationEnabled באמצעות האפשרויות הבאות:

  • Extension Property — משבית את הפלאגין של וריאנט build ספציפי בזמן הקימפול

  • מאפיין פרויקט – משבית את הפלאגין לכל וריאציות ה-build בזמן הקימפול

השבתת הפלאגין באמצעות דגל של מאפיין התוסף

באמצעות הדגל Extensions Property, אתם יכולים להשבית את הפלאגין של Performance Monitoring לווריאנט build ספציפי בזמן הקימפול.

  1. בקובץ Gradle ברמת הבסיס (ברמת הפרויקט) (<project>/build.gradle.kts או <project>/build.gradle), מוודאים שהתלות בפלאגין של Android Gradle מוגדרת כגרסה 3.4.0 ואילך.

    בגרסאות קודמות של הפלאגין של Android Gradle, עדיין אפשר להשבית את הפלאגין Performance Monitoring לווריאנט build ספציפי, אבל התרומה לזמן ה-build של הווריאנט הזה לא תבוטל לחלוטין.

  2. מוסיפים את הדגל הבא לקובץ Gradle של המודול (ברמת האפליקציה) (בדרך כלל <project>/<app-module>/build.gradle.kts או <project>/<app-module>/build.gradle), ואז מגדירים אותו כ-false כדי להשבית את הפלאגין של Performance Monitoring.

    Kotlin

    import com.google.firebase.perf.plugin.FirebasePerfExtension
    
    // ...
    
    android {
      // ...
      buildTypes {
        getByName("debug") {
          configure<FirebasePerfExtension> {
            // Set this flag to 'false' to disable @AddTrace annotation processing and
            // automatic monitoring of HTTP/S network requests
            // for a specific build variant at compile time.
            setInstrumentationEnabled(false)
          }
        }
      }
    }

    Groovy

    android {
      // ...
      buildTypes {
        debug {
          FirebasePerformance {
            // Set this flag to 'false' to disable @AddTrace annotation processing and
            // automatic monitoring of HTTP/S network requests
            // for a specific build variant at compile time.
            instrumentationEnabled false
          }
        }
      }
    }

השבתת הפלאגין באמצעות דגל של מאפיין הפרויקט

באמצעות הדגל Project Property, אתם יכולים להשבית את הפלאגין של Performance Monitoring לכל וריאנט ה-build בזמן הקימפול.

מוסיפים את הדגל הבא לקובץ gradle.properties, ואז מגדירים אותו ל-false כדי להשבית את הפלאגין Performance Monitoring.

// ...

// Set this flag to 'false' to disable @AddTrace annotation processing and
// automatic monitoring of HTTP/S network requests
// for all build variants at compile time.
firebasePerformanceInstrumentationEnabled=false

השבתת ספריית Performance Monitoring ל-Android

אם משביתים את הספרייה Performance Monitoring בזמן הקימפול, אפשר לבחור אם לאפשר לאפליקציה להפעיל את הספרייה בזמן הריצה.

להשבית את הספרייה בזמן הקימפול, אבל לאפשר לאפליקציה להפעיל אותה בזמן הריצה

מוסיפים את הרכיב <meta-data> הבא לקובץ AndroidManifest.xml של האפליקציה:

  <application>
    <meta-data
      android:name="firebase_performance_collection_enabled"
      android:value="false" />
  </application>

השבתת הספרייה בזמן הקימפול, אבל בלי לאפשר לאפליקציה להפעיל אותה בזמן הריצה

מוסיפים את הרכיב <meta-data> הבא לקובץ AndroidManifest.xml של האפליקציה:

  <application>
    <meta-data
      android:name="firebase_performance_collection_deactivated"
      android:value="true" />
  </application>

השבתת האפליקציה בזמן הריצה באמצעות Remote Config

Firebase Remote Config מאפשרת לכם לבצע שינויים בהתנהגות ובמראה של האפליקציה, כך שזו דרך אידיאלית להשבית את Performance Monitoring במופעים של האפליקציה שנפרסו.

כדי להשבית את איסוף הנתונים של Performance Monitoring בפעם הבאה שאפליקציית Android תופעל, אפשר להשתמש בקוד לדוגמה שמוצג בהמשך. מידע נוסף על השימוש ב-Remote Config באפליקציה ל-Android זמין במאמר שימוש ב-Firebase Remote Config ב-Android.

  1. מוודאים שהערך Remote Config נמצא בקטע dependencies בקובץ Gradle של המודול (ברמת האפליקציה) (בדרך כלל <project>/<app-module>/build.gradle.kts או <project>/<app-module>/build.gradle):

    Kotlin

      implementation("com.google.firebase:firebase-config-ktx:24.0.0")
    

    Java

      implementation("com.google.firebase:firebase-config:24.0.0")
    
  2. מגדירים את Remote Config ומשביתים את Performance Monitoring אם perf_disable מוגדר לערך true:

    Kotlin

    // Setup remote config
    val config = Firebase.remoteConfig
    
    // 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.
    // val configSettings = remoteConfigSettings {
    //     minimumFetchIntervalInSeconds = 3600
    // }
    // config.setConfigSettingsAsync(configSettings)
    // Load in-app defaults from an XML file that sets perf_disable to false until you update
    // values in the Firebase Console
    
    // Observe the remote config parameter "perf_disable" and disable Performance Monitoring if true
    config.setDefaultsAsync(R.xml.remote_config_defaults)
        .addOnCompleteListener { task ->
            if (task.isSuccessful) {
                Firebase.performance.isPerformanceCollectionEnabled = !config.getBoolean("perf_disable")
            } else {
                // An error occurred while setting default parameters
            }
        }

    Java

    // Setup remote config
    final FirebaseRemoteConfig config = 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()
    //       .setMinimumFetchIntervalInSeconds(3600)
    //       .build();
    // config.setConfigSettingsAsync(configSettings);
    // Load in-app defaults from an XML file that sets perf_disable to false until you update
    // values in the Firebase Console
    
    //Observe the remote config parameter "perf_disable" and disable Performance Monitoring if true
    config.setDefaultsAsync(R.xml.remote_config_defaults)
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful()) {
                        if (config.getBoolean("perf_disable")) {
                            FirebasePerformance.getInstance().setPerformanceCollectionEnabled(false);
                        } else {
                            FirebasePerformance.getInstance().setPerformanceCollectionEnabled(true);
                        }
                    } else {
                        // An error occurred while setting default parameters
                    }
                }
            });
  3. מוסיפים את הקוד הבא אל MainActivity.java כדי לאחזר ולהפעיל את הערכים של Remote Config:

    Kotlin

    // Remote Config fetches and activates parameter values from the service
    val config = Firebase.remoteConfig
    config.fetch(3600)
        .continueWithTask { task ->
            if (!task.isSuccessful) {
                task.exception?.let {
                    throw it
                }
            }
            config.activate()
        }
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                // Parameter values successfully activated
                // ...
            } else {
                // Handle errors
            }
        }

    Java

    //Remote Config fetches and activates parameter values from the service
    final FirebaseRemoteConfig config = FirebaseRemoteConfig.getInstance();
    config.fetch(3600)
            .continueWithTask(new Continuation<Void, Task<Boolean>>() {
                @Override
                public Task<Boolean> then(@NonNull Task<Void> task) throws Exception {
                    if (!task.isSuccessful()) {
                        throw task.getException();
                    }
                    return config.activate();
                }
            })
            .addOnCompleteListener(new OnCompleteListener<Boolean>() {
                @Override
                public void onComplete(@NonNull Task<Boolean> task) {
                    if (task.isSuccessful()) {
                        // Parameter values successfully activated
                        // ...
                    } else {
                        // Handle errors
                    }
                }
            });
  4. כדי להשבית את Performance Monitoring במסוף Firebase, יוצרים פרמטר perf_disable בפרויקט של האפליקציה ומגדירים את הערך שלו ל-true.

    השינוי הזה יהפוך את הקריאות ל-SDK של Performance Monitoring לקריאות "ללא פעולה" (NOOP), ויבטל את ההשפעות המשמעותיות של השימוש ב-SDK של Performance Monitoring על ביצועי האפליקציה.

    אם מגדירים את הערך של perf_disable ל-false, Performance Monitoring נשאר מופעל.