Catch up on everthing we announced at this year's Firebase Summit. Learn more

Add custom monitoring for specific network requests (Apple & Android apps)

Performance Monitoring collects traces to help you monitor the performance of your app. A trace is a report of performance data captured between two points in time in your app.

The network request traces automatically collected by Performance Monitoring include most network requests for your app. However, some requests might not be reported or you might use a different library to make network requests. In these cases, you can use the Performance Monitoring API to manually instrument custom network request traces. Custom network request traces are only supported for Apple and Android apps.

The default metrics for a custom network request trace are the same as those for the network request traces automatically collected by Performance Monitoring, specifically response time, response and request payload size, and success rate. Custom network request traces do not support adding custom metrics.

In your code, you define the beginning and the end of a custom network request trace using the APIs provided by the Performance Monitoring SDK.

Custom network request traces appear in the Firebase console alongside the network requests that Performance Monitoring captures automatically (in the Network requests subtab of the traces table).

Add custom network request traces

Use the Performance Monitoring HttpMetric API to add custom network request traces to monitor specific network requests.

To manually instrument custom network requests in Performance Monitoring, add code similar to the following:

Java

HttpMetric metric =
        FirebasePerformance.getInstance().newHttpMetric("https://www.google.com",
                FirebasePerformance.HttpMethod.GET);
final URL url = new URL("https://www.google.com");
metric.start();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json");
try {
    DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream());
    outputStream.write(data);
} catch (IOException ignored) {
}
metric.setRequestPayloadSize(data.length);
metric.setHttpResponseCode(conn.getResponseCode());
printStreamContent(conn.getInputStream());

conn.disconnect();
metric.stop();

Kotlin+KTX

val url = URL("https://www.google.com")
val metric = Firebase.performance.newHttpMetric("https://www.google.com",
        FirebasePerformance.HttpMethod.GET)
metric.trace {
    val conn = url.openConnection() as HttpURLConnection
    conn.doOutput = true
    conn.setRequestProperty("Content-Type", "application/json")
    try {
        val outputStream = DataOutputStream(conn.outputStream)
        outputStream.write(data)
    } catch (ignored: IOException) {
    }

    // Set HttpMetric attributes
    setRequestPayloadSize(data.size.toLong())
    setHttpResponseCode(conn.responseCode)

    printStreamContent(conn.inputStream)

    conn.disconnect()
}

Custom network request traces also support adding custom attributes but not custom metrics.