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


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

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

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

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

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

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

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

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

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

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

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

  • Extension Property – משבית את הפלאגין עבור וריאציה ספציפית של build בזמן ההידור

  • Project Property – משבית את הפלאגין עבור כל וריאציות הבנייה בזמן ההידור

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

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

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

    בגרסאות קודמות של Android Gradle Plugin, עדיין אפשר להשבית את הפלאגין 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 עבור כל וריאציות הבנייה בזמן ההידור.

מוסיפים את הדגל הבא לקובץ 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

השבתת ספריית Android‏ Performance Monitoring

אם משביתים את ספריית 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:23.0.0")
    

    Java

      implementation("com.google.firebase:firebase-config:23.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.

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

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