URLInstrumentation: only set a Task Delegate if there is no Session Delegate #747
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #744
Majority of the changes are in test/example code. The actual fix is quite small, but relies on calling
task.value(forKey: "session") as? URLSession
to get the associated session on a task.Alternatives
Intercept Task Creation
One other approach we discussed in the sig was to intercept the task creation and call
objc_setAssociatedObject
on the task, if the session had an active delegate. We already do this here:opentelemetry-swift/Sources/Instrumentation/URLSession/URLSessionInstrumentation.swift
Lines 192 to 195 in 6a2c29d
However, that blog of code isn't swizzling the
URLSession.data(for:)
orURLSession.data(from:)
functions, which are used to create asynchronous tasks. We could start swizzling those functions as well, if we wanted to, but that felt like a riskier change.Swizzle UISession
Honeycomb has an alternate URL instrumentation implementation that swizzles the
URLSession
andURLSessionTask
constructors directly. This lets us get a much stronger handle on the sessions and tasks in a wider variety of cases. Switching to that approach would be more costly and time consuming, but may serve us in the long run since the implementation is simpler.