Add monitoring for specific network requests
Performance Monitoring collects network requests automatically. Although this includes most network requests for your app, some might not be reported. To include specific network requests in Performance Monitoring, add the following code to your app:
class _MetricHttpClient extends BaseClient {
_MetricHttpClient(this._inner);
final Client _inner;
@override
Future<StreamedResponse> send(BaseRequest request) async {
final HttpMetric metric = FirebasePerformance.instance
.newHttpMetric(request.url.toString(), HttpMethod.Get);
await metric.start();
StreamedResponse response;
try {
response = await _inner.send(request);
metric
..responsePayloadSize = response.contentLength
..responseContentType = response.headers['Content-Type']
..requestPayloadSize = request.contentLength
..httpResponseCode = response.statusCode;
} finally {
await metric.stop();
}
return response;
}
}
class _MyAppState extends State<MyApp> {
.
.
.
Future<void> testHttpMetric() async {
final _MetricHttpClient metricHttpClient = _MetricHttpClient(Client());
final Request request =
Request("SEND", Uri.parse("https://www.google.com"));
metricHttpClient.send(request);
}
.
.
.
}