Skip to main content

Performance Monitoring

To start using Performance Monitoring for Firebase package within your project, import it at the top of your project files:

import 'package:firebase_performance/firebase_performance.dart';

Before using Performance Monitoring, you must first have ensured you have initialized FlutterFire.

To create a new Performance Monitoring for Firebase instance, call the instance getter on FirebasePerformance:

FirebasePerformance performance = FirebasePerformance.instance;

By default, this allows you to interact with Performance Monitoring using the default Firebase App.

Automatic Tracing

Automatic screen rendering performance monitoring is not possible for individual Flutter screens. A single view controller encapsulates your entire Flutter application natively so the underlying native Firebase SDK is not aware of screen transitions.

Automatic Tracing#

When installed, Android & iOS will automatically report metrics such as application start time, network requests and other useful data.

Custom tracing#

You can create your own traces to monitor performance data associated with specific code in your app. With a custom code trace, you can measure how long it takes your app to complete a specific task or a set of tasks, for example loading a set of images or querying your database.

To setup a custom trace, create a new Trace instance by calling the newTrace() method:

FirebasePerformance performance = FirebasePerformance.instance;
Trace trace = performance.newTrace('custom-trace');

The name provided will appear within the Firebase Console, allowing you to provide unique names for different trace metrics.

When it makes sense to start a trace, call the start() method on the Trace instance. Once started, you can apply custom metric data to the trace by calling setMetric():

Trace trace = performance.newTrace('custom-trace');
await trace.start();
// Set metrics you wish to track
trace.setMetric('sum', 200);
trace.setMetric('time', 342340435);

The API also allows you to increment metric data:

trace.setMetric('sum', 200);
// `sum` will be incremented to 201
trace.incrementMetric('sum', 1);

You can also set non-metric related data, such as a user id on the trace by calling the putAttribute() method. Note, each trace supports up to 5 attributes.

trace.putAttribute('userId', '1234');

Once your trace has completed, call the stop() method. The data will then be sent to the Firebase Console:

await trace.stop();

HTTP Request Tracing#

The network request traces automatically collected by Performance Monitoring include most network requests for your app.

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 platforms. To setup a custom network request trace, see below:

// Using http package to make a network request
import 'package:http/http.dart' as http;
FirebasePerformance performance = FirebasePerformance.instance;
// Create a `HttpMetric` instance using the URL you're requesting as well as the type of request
String url = 'https://firebase.flutter.dev';
HttpMetric metric = performance
.newHttpMetric(url, HttpMethod.Get);
// You may also assign up to 5 attributes for each trace
metric.putAttribute('foo', 'bar');
// Start the trace
await metric.start();
// Make the request
Uri url = Uri.parse(url);
var response = await http.get(url);
// Set specific headers to be collated
metric.responseContentType = response.headers['Content-Type'];
metric.httpResponseCode = response.statusCode;
metric.responsePayloadSize = response.contentLength;
// Stops the trace. This is when the data is sent to the Firebase server and it will appear in your Firebase console
await metric.stop();

Stop Automatic Data Collection#

To stop automatic data collection, you can call setPerformanceCollectionEnabled like in the example:

FirebasePerformance performance = FirebasePerformance.instance;
// Custom data collection is, by default, enabled
bool isEnabled = await performance.isPerformanceCollectionEnabled();
// Set data collection to `false`
await performance.setPerformanceCollectionEnabled(false);