性能監控收集跟踪以幫助您監控應用程序的性能。跟踪是應用程序中兩個時間點之間捕獲的性能數據的報告。
Performance Monitoring 自動收集的網絡請求跟踪包括您的應用程序的大多數網絡請求。但是,某些請求可能不會被報告,或者您可能會使用不同的庫來發出網絡請求。在這些情況下,您可以使用 Performance Monitoring API 手動檢測自定義網絡請求跟踪。自定義網絡請求跟踪僅支持 Apple 和 Android 應用程序。
自定義網絡請求跟踪的默認指標與性能監控自動收集的網絡請求跟踪的指標相同,特別是響應時間、響應和請求負載大小以及成功率。自定義網絡請求跟踪不支持添加自定義指標。
在您的代碼中,您使用 Performance Monitoring SDK 提供的 API 定義自定義網絡請求跟踪的開始和結束。
自定義網絡請求跟踪與性能監控自動捕獲的網絡請求一起顯示在 Firebase 控制台中(在跟踪表的網絡請求子選項卡中)。
添加自定義網絡請求跟踪
使用性能監控 HTTPMetric API ( Swift | Obj-C ) 添加自定義網絡請求跟踪以監控特定網絡請求。
要在性能監控中手動檢測自定義網絡請求,請添加類似於以下的代碼:
迅速
注意:此 Firebase 產品不適用於 macOS、Mac Catalyst、watchOS 目標。
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()
目標-C
注意:此 Firebase 產品不適用於 macOS、Mac Catalyst、watchOS 目標。
@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];
}
自定義網絡請求跟踪還支持添加自定義屬性 ( Swift | Obj-C ),但不支持自定義指標。
下一步
- 為降低應用程序性能的網絡請求設置警報。例如,如果特定 URL 模式的響應時間超過您設置的閾值,您可以為您的團隊配置電子郵件警報。