Firebase Summit is happening now. Tune in to learn how Firebase can help you accelerate app development, gain actionable insights and scale with ease. Join now

Add custom monitoring for specific network requests (iOS & 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 iOS 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.

The HTTP/S network requests that you specifically capture in this way appear in the Firebase console along with the network requests that Performance Monitoring captures automatically (in the Network tab).

Add custom network request traces

Use the Performance Monitoring HTTPMetric API (Swift | Obj-C) 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:

Swift

guard let metric = HTTPMetric(url: "https://www.google.com", httpMethod: .get) else { return }

metric.start()
guard let url = URL(string: "https://www.google.com") else { return }
let request: URLRequest = URLRequest(url:url)
let session = URLSession(configuration: .default)
let dataTask = session.dataTask(with: request) { (urlData, response, error) in
        if let httpResponse = response as? HTTPURLResponse {
         metric.responseCode = httpResponse.statusCode
        }
        metric.stop()
}
dataTask.resume()

Objective-C

@property (nonatomic) FIRHTTPMetric *metric;

- (void)beginManualNetworkInstrumentation {
  self.metric =
      [[FIRHttpMetric alloc] initWithURL:[NSURL URLWithString:@"https://www.google.com"]
                              HTTPMethod:FIRHTTPMethodGET];

  [self.metric start];

  NSURLRequest *request =
      [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.google.com"]];
  NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                                delegate:self];
  [connection resume];
}

- (void)connection:(NSURLConnection *)connection
    didReceiveResponse:(NSURLResponse *) response {
  NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response
  self.metric.responseCode = httpResponse.statusCode;
  [self.metric stop];
}

Custom network request traces also support adding custom attributes (Swift | Obj-C) but not custom metrics.