getsentry / getsentry/sentry-cocoa

Manually consume URLSessionTaskMetrics data

Open
#3,180 1 comment 0 reactions 0 assignees View on GitHub
Cocoa Improvement Spans
Dominant language
Swift
Stars
1.1k
Forks
418
Avg merge
2d 3h
Merged PRs (30d)
106

Description

### 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](https://developer.apple.com/documentation/foundation/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
```Swift
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) {

}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.