Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download Issues: URLSession Task Metrics logging #1494

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions podcasts/DownloadManager+URLSessionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ extension DownloadManager: URLSessionDelegate, URLSessionDownloadDelegate {
return
}

FileLog.shared.addMessage("Download failed \(error.domain) \(error.code): \(error.localizedDescription)")

// check for ones we cancelled
guard let episode = episodeForTask(task, forceReload: true) else { return } // we no longer have this episode
removeEpisodeFromCache(episode)
Expand Down Expand Up @@ -130,6 +132,15 @@ extension DownloadManager: URLSessionDelegate, URLSessionDownloadDelegate {
}
}

func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
let message = log(metrics: metrics)

if let error = task.error {
let url = task.currentRequest?.url?.absoluteString ?? "unknown"
FileLog.shared.addMessage("Failed Download for URL: \(url) \(message)")
}
}

private func episodeForTask(_ task: URLSessionDownloadTask, forceReload: Bool) -> BaseEpisode? {
guard let downloadId = task.taskDescription else { return nil }

Expand All @@ -153,4 +164,14 @@ extension DownloadManager: URLSessionDelegate, URLSessionDownloadDelegate {
DataManager.sharedManager.saveEpisode(downloadStatus: .downloadFailed, downloadError: message, downloadTaskId: nil, episode: episode)
NotificationCenter.postOnMainThread(notification: Constants.Notifications.episodeDownloadStatusChanged, object: episode.uuid)
}

private func log(metrics: URLSessionTaskMetrics) -> String {
let duration = metrics.taskInterval.duration
let redirectCount = metrics.redirectCount
let isProxy = metrics.transactionMetrics.last?.isProxyConnection
let isCellular = metrics.transactionMetrics.last?.isCellular
let isMultipath = metrics.transactionMetrics.last?.isMultipath
let tlsCipherSuite = metrics.transactionMetrics.last?.negotiatedTLSCipherSuite
return "Duration: \(duration) Redirects: \(redirectCount) isProxy: \(String(describing: isProxy)) isCellular: \(String(describing: isCellular)) isMultipath: \(String(describing: isMultipath)) tlsCipherSuite: \(String(describing: tlsCipherSuite))"
}
}