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

Add x-datadog-origin header only when instrumenting with datadog tracing #1107

Merged
merged 1 commit into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class URLSessionInterceptor: URLSessionInterceptorType {
internal let injectTracingHeadersToFirstPartyRequests: Bool
/// Additional header injected to intercepted 1st party requests.
/// Set to `x-datadog-origin: rum` if both RUM and Tracing instrumentations are enabled and `nil` in all other cases.
internal let additionalHeadersForFirstPartyRequests: [String: String]?
internal let datadogOriginHeader: (field: String, value: String)?
/// Tracing sampler used to sample traces generated by the SDK or RUM BE.
internal let tracingSampler: Sampler

Expand Down Expand Up @@ -88,15 +88,13 @@ public class URLSessionInterceptor: URLSessionInterceptorType {
if configuration.instrumentRUM {
// If RUM instrumentation is enabled, additional `x-datadog-origin: rum` header is injected to the user request,
// so that user's backend instrumentation can further process it and count on RUM quota (w/o affecting APM Index Spans counts).
self.additionalHeadersForFirstPartyRequests = [
TracingHTTPHeaders.ddOrigin.field: TracingHTTPHeaders.ddOrigin.value
]
self.datadogOriginHeader = TracingHTTPHeaders.ddOrigin
} else {
self.additionalHeadersForFirstPartyRequests = nil
self.datadogOriginHeader = nil
}
} else {
self.injectTracingHeadersToFirstPartyRequests = false
self.additionalHeadersForFirstPartyRequests = nil
self.datadogOriginHeader = nil
}
}

Expand Down Expand Up @@ -250,6 +248,9 @@ public class URLSessionInterceptor: URLSessionInterceptorType {
switch $0 {
case .datadog:
writer = HTTPHeadersWriter(sampler: tracingSampler)
if let datadogOriginHeader = datadogOriginHeader {
newRequest.setValue(datadogOriginHeader.value, forHTTPHeaderField: datadogOriginHeader.field)
}
case .b3:
writer = OTelHTTPHeadersWriter(
sampler: tracingSampler,
Expand All @@ -268,10 +269,6 @@ public class URLSessionInterceptor: URLSessionInterceptorType {
writer.tracePropagationHTTPHeaders.forEach { field, value in
newRequest.setValue(value, forHTTPHeaderField: field)
}

additionalHeadersForFirstPartyRequests?.forEach { field, value in
newRequest.setValue(value, forHTTPHeaderField: field)
}
}
return newRequest
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class URLSessionInterceptorTests: XCTestCase {
"Tracing headers should be injected when only Tracing instrumentation is enabled."
)
XCTAssertNil(
interceptor.additionalHeadersForFirstPartyRequests,
interceptor.datadogOriginHeader,
"Just the tracing headers should be injected when only Tracing instrumentation is enabled."
)
}
Expand All @@ -79,7 +79,7 @@ class URLSessionInterceptorTests: XCTestCase {
"Tracing headers should not be injected when only RUM instrumentation is enabled."
)
XCTAssertNil(
interceptor.additionalHeadersForFirstPartyRequests,
interceptor.datadogOriginHeader,
"No additional headers should be injected when only RUM instrumentation is enabled."
)
}
Expand All @@ -103,9 +103,14 @@ class URLSessionInterceptorTests: XCTestCase {
"Tracing headers should be injected when both Tracing and RUM instrumentations are enabled."
)
XCTAssertEqual(
interceptor.additionalHeadersForFirstPartyRequests,
[TracingHTTPHeaders.ddOrigin.field: TracingHTTPHeaders.ddOrigin.value],
"Additional `x-datadog-origin: rum` header should be injected when both Tracing and RUM instrumentations are enabled."
interceptor.datadogOriginHeader?.field,
TracingHTTPHeaders.ddOrigin.field,
"Additional `x-datadog-origin` header field should be injected when both Tracing and RUM instrumentations are enabled."
)
XCTAssertEqual(
interceptor.datadogOriginHeader?.value,
TracingHTTPHeaders.ddOrigin.value,
"Additional `rum` header value should be injected when both Tracing and RUM instrumentations are enabled."
)
}

Expand Down