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

(Feature)|Surface request metrics to response middleware #150

Merged
merged 2 commits into from
Apr 6, 2020

Conversation

eneko
Copy link
Contributor

@eneko eneko commented Apr 3, 2020

This pull request includes (pick all that apply):

  • Bugfixes
  • New features
  • Breaking changes
  • Documentation updates
  • Unit tests
  • Other

Summary

In order to track request duration and other metrics, we want to expose this information via response middleware.

Requests metrics are provided by URLSessionDataTask via URLSessionDataDelegate (see changes in URLSessionClient.swift, line 319):

/// Stores request metrics in task response, for later consumption.
/// This delegate method is always called after `didReceive response`, and before `didCompleteWithError`.
@available(iOS 10.0, *)
func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
    var taskResponse = taskResponseFor(taskIdentifier: task.taskIdentifier)
    taskResponse.metrics = metrics
    update(taskResponse: taskResponse, for: task.taskIdentifier)
}

In the delegate method above, we store the collected metrics inside the task response structure. This structure is then passed to all response middleware in synchronouslyProcessResponseMiddleware().

Breaking changes

  • Applications and/or frameworks implementing ResponsePipelineMiddleware will have to be updated.

Implementation

  • SessionDelegate has been updated to capture request metrics in TaskResponse.
  • URLSessionClient has been updated to pass TaskResponse to any response middleware.
  • ResponsePipelineMiddleware has been refactored to pass a TaskResponse structure, which contains response data, HTTP response, error, and metrics, where available.
  • Existing unit tests have been updated accordingly, and new tests have been added.
  • Private URL extension has been moved to test targets.
  • @testable imports have been removed where possible.

Test Plan

  • Run all tests

Copy link
Contributor

@tiltem tiltem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool, URLSessionTaskMetrics FTW! Nice work

let responseMiddleware = TransformingResponseMiddleware { taskResponse in
var taskResponse = taskResponse
XCTAssertGreaterThanOrEqual(taskResponse.metrics?.taskInterval.duration ?? 0, 2)
expectation.fulfill()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

@johnhammerlund johnhammerlund left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Just one comment regarding accessibility modifiers.

R~

Comment on lines 13 to 18
var data: Data?
var response: HTTPURLResponse?
var expectedContentLength: Int64?
var error: Error?
@available(iOS 10, *)
lazy var metrics: URLSessionTaskMetrics? = nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be public, minus the expectedContentLength

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, good catch! Saved me from doing a patch release :)

@eneko eneko merged commit 66454a1 into master Apr 6, 2020
@eneko eneko deleted the feature/timed-requests branch April 6, 2020 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants