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-696 Stop sending tracing Span for RUM Resources #314

Merged

Conversation

ncreated
Copy link
Member

What and why?

🚚 This PR changes the logic of auto-instrumented, network Span generation depending of which feature (RUM or Tracing) is enabled. This is because recently our RUM backend started generating spans for RUM Resources on behalf of the SDK.

New behaviour, assuming that set(firstPartyHosts:) is set and DDURLSessionDelegate() is installed:

Tracing feature RUM feature Global.sharedTracer Global.rum 1st party request 3rd party request
registered - send Span + inject tracing headers -
- registered send RUM Resource send RUM Resource
registered registered send RUM Resource with trace_id and span_id + inject tracing headers + inject x-datadog-origin: rum header send RUM Resource

Also, appropriate warnings are printed on different misconfiguration, e.g. when Tracing auto-instrumentation is enabled, and a first party request is sent while Global.sharedTracer is not registered.

How?

Continuing what was started in #262, the logic of interceptor was simplified. Now, instead of two distinct handlers (for RUM and Tracing), it takes only one interface:

internal protocol URLSessionInterceptionHandler {
    func notify_taskInterceptionStarted(interception: TaskInterception)
    func notify_taskInterceptionCompleted(interception: TaskInterception)
}

implemented by URLSessionRUMResourceHandler and URLSessionTracingHandler.

Interceptor:

  • injects / extracts SpanContext and custom x-datadog-origin: rum header into/from 1st party requests,
  • notifies handler on interception start and completion.

Handler:

  • URLSessionRUMResourceHandler - send RUM Resources and associate RUMSpanContext if set for the interception,
  • URLSessionTracingHandler - send Span only for interceptions flagged as interception.isFirstPartyRequest.

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 ncreated added this to the next-version milestone Nov 20, 2020
@ncreated ncreated requested a review from a team as a code owner November 20, 2020 08:21
@ncreated ncreated self-assigned this Nov 20, 2020
Copy link
Contributor

@buranmert buranmert left a comment

Choose a reason for hiding this comment

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

looks good, we can add one more test case though (i couldn't find if the case was already there)

Comment on lines 141 to 146
XCTAssertNil(interceptedThirdPartyRequest.allHTTPHeaderFields?[TracingHTTPHeaders.traceIDField])
XCTAssertNil(interceptedThirdPartyRequest.allHTTPHeaderFields?[TracingHTTPHeaders.parentSpanIDField])
XCTAssertNil(interceptedThirdPartyRequest.allHTTPHeaderFields?[TracingHTTPHeaders.originField])
XCTAssertNil(interceptedInternalRequest.allHTTPHeaderFields?[TracingHTTPHeaders.traceIDField])
XCTAssertNil(interceptedInternalRequest.allHTTPHeaderFields?[TracingHTTPHeaders.parentSpanIDField])
XCTAssertNil(interceptedInternalRequest.allHTTPHeaderFields?[TracingHTTPHeaders.originField])
Copy link
Contributor

Choose a reason for hiding this comment

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

i guess we expect request.allHTTPHeaderFields == nil/unchanged in those 2 cases:

XCTAssertNil(interceptedThirdPartyRequest.allHTTPHeaderFields?)
XCTAssertNil(interceptedInternalRequest.allHTTPHeaderFields?)

?
and the next lines might already cover them:

XCTAssertEqual(thirdPartyRequest, interceptedThirdPartyRequest)
XCTAssertEqual(internalRequest, interceptedInternalRequest)

if HTTPHeaderFields are changed, those assertions fail; don't they?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, inspecting .allHTTPHeaderFields is redundant if we assert the full request equality later 👍. However, due to request equality bug in iOS 14 SDK: https://openradar.appspot.com/radar?id=4988276943355904 it seemed safer to do it before. To be more verbose on this, I added extra helper: assertRequestsEqual(_:_:) which implements the workaround. Redundant checks are now removed 👌.

interceptor.additionalHeadersForFirstPartyRequests,
[TracingHTTPHeaders.originField: TracingHTTPHeaders.rumOriginValue],
"Additional `x-datadog-origin: rum` header should be injected when both Tracing and RUM instrumentations are enabled."
)
}

// MARK: - URLRequest Interception
Copy link
Contributor

Choose a reason for hiding this comment

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

we can add already traced request (original request with tracing headers) test case here
i guess we expect to have the original headers after the interception, correct?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, I don't understand 🙂. In quoted lines we do not test requests, but only internal state of the interceptor (interceptor.additionalHeadersForFirstPartyRequests).

There's no such case, where interceptor.modify(request:) is called for a request, which was previously intercepted. This contract is guaranteed in swizzler tests.

@ncreated ncreated requested a review from xgouchet November 23, 2020 09:15
Copy link
Member

@xgouchet xgouchet left a comment

Choose a reason for hiding this comment

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

LGTM

@ncreated ncreated merged commit 51cd4a3 into master Nov 23, 2020
@ncreated ncreated deleted the ncreated/RUMM-696-stop-sending-tracing-span-for-rum-resources branch November 23, 2020 14:37
@ncreated ncreated removed this from the next-version milestone Dec 16, 2020
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.

3 participants