-
Notifications
You must be signed in to change notification settings - Fork 133
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
RUMM-1322 Allow initializing DDURLSessionDelegate
before starting the SDK
#483
RUMM-1322 Allow initializing DDURLSessionDelegate
before starting the SDK
#483
Conversation
…ion tests This will randomly initialize `URLSession` before or after `Datadog.initialize()` in all session-based integration tests.
} | ||
|
||
/// An object performing interception of requests sent with `URLSession`. | ||
public class URLSessionInterceptor: URLSessionInterceptorType { | ||
public static var shared: URLSessionInterceptor? { | ||
URLSessionAutoInstrumentation.instance?.interceptor | ||
URLSessionAutoInstrumentation.instance?.interceptor as? URLSessionInterceptor |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 This casting is now required and is tested in existing URLSessionAutoInstrumentationTests
tests.
internal protocol URLSessionInterceptorType: class { | ||
func modify(request: URLRequest, session: URLSession?) -> URLRequest | ||
func taskCreated(task: URLSessionTask, session: URLSession?) | ||
func taskMetricsCollected(task: URLSessionTask, metrics: URLSessionTaskMetrics) | ||
func taskReceivedData(task: URLSessionTask, data: Data) | ||
func taskCompleted(task: URLSessionTask, error: Error?) | ||
|
||
var handler: URLSessionInterceptionHandler { get } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 This adds a bit noise to the URLSessionInterceptorType
interface and could be additionally mitigated by decoupling the handler
creation to upstream (to URLSessionAutoInstrumentation
class). This however seems over engineering if done in the scope of this PR - we may want to refactor this on a stronger reason with more constraints for changing the abstraction.
What and why?
🚚 This PR removes the constraint of initializing
DDURLSessionDelegate
afterDatadog.initialize()
. From now on, users can create our delegate freely, no matter ofDatadog.initialize()
. The SDK will only track requests issued after starting the SDK.This is to provide more freedom for the user setup and cut off error-prone edge cases (like race conditions).
How?
I changed the
DDURLSessionDelegate
to obtain the interceptor's reference through lazily computed variable, instead of injecting its value oninit()
. The obsolete misconfiguration warnings were also removed.To test the new behaviour, I randomized the
URLSession
-based integration tests setup to randomly create delegate before or after theDatadog.initialize()
. This includes 4 integration tests usingURLSession
andNSURLSession
: 2 for RUM Resources and 2 for Tracing auto instrumentation.Review checklist