DEV Community

myougaTheAxo
myougaTheAxo

Posted on

Firebase Performance Monitoring on Android: Custom Traces & HTTP Metrics

Firebase Performance Monitoring on Android: Custom Traces & HTTP Metrics

Firebase Performance Monitoring reveals bottlenecks in your app. Custom traces let you measure specific operations while automatic HTTP monitoring tracks network performance.

Basic Performance Monitoring Setup

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()

        val firebasePerformance = FirebasePerformance.getInstance()
        firebasePerformance.isPerformanceCollectionEnabled = true
    }
}
Enter fullscreen mode Exit fullscreen mode

Creating Custom Traces

class DataRepository {
    fun fetchUserData() {
        val trace = FirebasePerformance.getInstance().newTrace("fetch_user_data")
        trace.start()

        try {
            val response = apiService.getUser()
            trace.putAttribute("response_size", response.data.size.toString())
        } finally {
            trace.stop()
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Custom Metrics in Traces

val trace = FirebasePerformance.getInstance().newTrace("database_operation")
trace.start()

// Your operation here
val duration = System.currentTimeMillis()

trace.putMetric("operation_time_ms", duration)
trace.putAttribute("table", "users")

trace.stop()
Enter fullscreen mode Exit fullscreen mode

HTTP Network Monitoring

// Automatic with Compose networking
val client = OkHttpClient.Builder()
    .addNetworkInterceptor(HttpTraceInterceptor())
    .build()

// Firebase automatically tracks HTTP calls
val response = client.newCall(request).execute()
Enter fullscreen mode Exit fullscreen mode

Analyzing Performance Data

Firebase console shows:

  • Screen rendering time
  • App startup time
  • Custom trace durations
  • HTTP request latency

Monitor regularly to identify degradation and optimize critical paths.


8 Android app templates available on Gumroad

Top comments (0)