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

RUMM-1322 Allow initializing DDURLSessionDelegate before starting the SDK #483

Conversation

ncreated
Copy link
Member

@ncreated ncreated commented May 5, 2021

What and why?

🚚 This PR removes the constraint of initializing DDURLSessionDelegate after Datadog.initialize(). From now on, users can create our delegate freely, no matter of Datadog.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 on init(). 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 the Datadog.initialize(). This includes 4 integration tests using URLSession and NSURLSession: 2 for RUM Resources and 2 for Tracing auto instrumentation.

Review checklist

  • Feature or bugfix MUST have appropriate tests (unit, integration)
  • Make sure each commit and the PR mention the Issue number or JIRA reference

ncreated added 2 commits May 5, 2021 12:17
…ion tests

This will randomly initialize `URLSession` before or after `Datadog.initialize()` in all
session-based integration tests.
@ncreated ncreated self-assigned this May 5, 2021
@ncreated ncreated requested a review from a team as a code owner May 5, 2021 10:29
}

/// 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
Copy link
Member Author

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 }
Copy link
Member Author

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.

@ncreated ncreated merged commit dba5db3 into master May 6, 2021
@ncreated ncreated deleted the ncreated/RUMM-1322-allow-DDURLSessionDelegate-init-before-SDK-init branch May 6, 2021 12:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants