Manually consume URLSessionTaskMetrics data
Discussed in https://github.com/getsentry/sentry-cocoa/discussions/3150
Originally posted by WFT July 14, 2023 Does Sentry support gathering URLSessionTaskTransactionMetrics with its automatic URLSession breadcrumbs/performance tracking? If so, how do I enable it? As far as I can tell the default options don't do it.
It would be helpful to track the performance of our API from the perspective of our end users to know at least:
- “secure connection time” AKA tls time
- fetchStartDate -> responseStartDate AKA “time to first byte”
URLSessionTaskMetrics provides excellent information for HTTP requests. You can retrieve the metrics via URLSessionTaskDelegate. Swizzling these delegates is a dangerous option we don't want to implement. Instead, we could offer a manual API do set some type of SentryURLSessionTaskDelegate to forward these metrics to the SDK. We still need to figure out how to map the data to our HTTP instrumentation. A proper SentryURLSessionTaskDelegate could fully replace the SentryNetworkTrackingIntegration with swizzling.
The URLSessionTaskMetrics works by using a delegate with something like the following
guard let imgUrl = URL(string: "https://sentry-brand.storage.googleapis.com/sentry-logo-black.png") else {
return
}
let session = URLSession(configuration: URLSessionConfiguration.default)
let dataTask = session.dataTask(with: imgUrl) { (_, _, _) in }
dataTask.delegate = Delegate()
dataTask.resume()
class Delegate: NSObject, URLSessionTaskDelegate {
func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
}
}