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

RUM-1923 feat: add tracestate headers when using W3C tracecontext #1536

Merged
merged 3 commits into from
Nov 3, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- [BUGFIX] Optimize Session Replay diffing algorithm. See [#1524][]
- [FEATURE] Add network instrumentation for async/await URLSession APIs. See [#1394][]
- [FEATURE] Change default tracing headers for first party hosts to use both Datadog headers and W3C `tracecontext` headers. See [#1529][]
- [FEATURE] Add tracestate headers when using W3C tracecontext. See [#1536][]

# 2.4.0 / 18-10-2023

Expand Down Expand Up @@ -550,6 +551,7 @@ Release `2.0` introduces breaking changes. Follow the [Migration Guide](MIGRATIO
[#1394]: https://github.com/DataDog/dd-sdk-ios/pull/1394
[#1524]: https://github.com/DataDog/dd-sdk-ios/pull/1524
[#1529]: https://github.com/DataDog/dd-sdk-ios/pull/1529
[#1536]: https://github.com/DataDog/dd-sdk-ios/pull/1536
[@00fa9a]: https://github.com/00FA9A
[@britton-earnin]: https://github.com/Britton-Earnin
[@hengyu]: https://github.com/Hengyu
Expand Down
4 changes: 4 additions & 0 deletions DatadogCore/Tests/Datadog/TracerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ class TracerTests: XCTestCase {

// Then
let expectedHTTPHeaders1 = [
"tracestate": "dd=s:1;o:rum",
"traceparent": "00-00000000000000000000000000000001-0000000000000002-01"
]
XCTAssertEqual(httpHeadersWriter.traceHeaderFields, expectedHTTPHeaders1)
Expand All @@ -888,6 +889,7 @@ class TracerTests: XCTestCase {

// Then
let expectedHTTPHeaders2 = [
"tracestate": "dd=s:1;o:rum",
"traceparent": "00-00000000000000000000000000000004-0000000000000005-01"
]
XCTAssertEqual(httpHeadersWriter.traceHeaderFields, expectedHTTPHeaders2)
Expand All @@ -897,6 +899,7 @@ class TracerTests: XCTestCase {

// Then
let expectedHTTPHeaders3 = [
"tracestate": "dd=s:1;o:rum",
"traceparent": "00-0000000000000000000000000000004d-0000000000000058-01"
]
XCTAssertEqual(httpHeadersWriter.traceHeaderFields, expectedHTTPHeaders3)
Expand All @@ -915,6 +918,7 @@ class TracerTests: XCTestCase {

// Then
let expectedHTTPHeaders = [
"tracestate": "dd=s:0;o:rum",
"traceparent": "00-00000000000000000000000000000001-0000000000000002-00"
]
XCTAssertEqual(httpHeadersWriter.traceHeaderFields, expectedHTTPHeaders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ class TracingURLSessionHandlerTests: XCTestCase {
"X-B3-TraceId": "00000000000000000000000000000001",
"b3": "00000000000000000000000000000001-0000000000000001-1",
"x-datadog-trace-id": "1",
"tracestate": "dd=s:1;o:rum",
"x-datadog-parent-id": "1",
"x-datadog-sampling-priority": "1"
]
Expand Down
6 changes: 4 additions & 2 deletions DatadogCore/Tests/DatadogObjc/DDTracerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ class DDTracerTests: XCTestCase {
try objcTracer.inject(objcSpanContext, format: OT.formatTextMap, carrier: objcWriter)

let expectedHTTPHeaders = [
"traceparent": "00-00000000000000000000000000000001-0000000000000002-01"
"traceparent": "00-00000000000000000000000000000001-0000000000000002-01",
"tracestate": "dd=s:1;o:rum"
]
XCTAssertEqual(objcWriter.traceHeaderFields, expectedHTTPHeaders)
}
Expand All @@ -325,7 +326,8 @@ class DDTracerTests: XCTestCase {
try objcTracer.inject(objcSpanContext, format: OT.formatTextMap, carrier: objcWriter)

let expectedHTTPHeaders = [
"traceparent": "00-00000000000000000000000000000001-0000000000000002-00"
"traceparent": "00-00000000000000000000000000000001-0000000000000002-00",
"tracestate": "dd=s:0;o:rum"
]
XCTAssertEqual(objcWriter.traceHeaderFields, expectedHTTPHeaders)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,22 @@ public enum W3CHTTPHeaders {
/// can troubleshoot the behavior of every recorded request.
public static let traceparent = "traceparent"

/// The main purpose of the tracestate HTTP header is to provide additional vendor-specific trace identification
/// information across different distributed tracing systems and is a companion header for the traceparent field. It
/// also conveys information about the request’s position in multiple distributed tracing graphs.
public static let tracestate = "tracestate"

public enum Constants {
public static let version = "00"
public static let sampledValue = "01"
public static let unsampledValue = "00"
public static let separator = "-"

// MARK: - Datadog specific tracestate keys
public static let dd = "dd"
public static let sampling = "s"
public static let origin = "o"
public static let originRUM = "rum"
public static let tracestateSeparator = ";"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,20 @@ public class W3CHTTPHeadersWriter: TracePropagationHeadersWriter {
public func write(traceID: TraceID, spanID: SpanID, parentSpanID: SpanID?) {
typealias Constants = W3CHTTPHeaders.Constants

let sampled = sampler.sample()

traceHeaderFields[W3CHTTPHeaders.traceparent] = [
Constants.version,
String(traceID, representation: .hexadecimal32Chars),
String(spanID, representation: .hexadecimal16Chars),
sampler.sample() ? Constants.sampledValue : Constants.unsampledValue
sampled ? Constants.sampledValue : Constants.unsampledValue
]
.joined(separator: Constants.separator)

let ddtracestate = [
"\(Constants.sampling):\(sampled ? 1 : 0)",
"\(Constants.origin):\(Constants.originRUM)"
].joined(separator: Constants.tracestateSeparator)
traceHeaderFields[W3CHTTPHeaders.tracestate] = "\(Constants.dd)=\(ddtracestate)"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class W3CHTTPHeadersWriterTests: XCTestCase {

let headers = w3cHTTPHeadersWriter.traceHeaderFields
XCTAssertEqual(headers[W3CHTTPHeaders.traceparent], "00-000000000000000000000000000004d2-0000000000000929-01")
XCTAssertEqual(headers[W3CHTTPHeaders.tracestate], "dd=s:1;o:rum")
}

func testW3CHTTPHeadersWriterwritesSingleHeaderWithSampling() {
Expand All @@ -28,5 +29,6 @@ class W3CHTTPHeadersWriterTests: XCTestCase {

let headers = w3cHTTPHeadersWriter.traceHeaderFields
XCTAssertEqual(headers[W3CHTTPHeaders.traceparent], "00-000000000000000000000000000004d2-0000000000000929-00")
XCTAssertEqual(headers[W3CHTTPHeaders.tracestate], "dd=s:0;o:rum")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ class URLSessionRUMResourcesHandlerTests: XCTestCase {
XCTAssertEqual(
modifiedRequest.allHTTPHeaderFields,
[
"tracestate": "dd=s:1;o:rum",
"traceparent": "00-00000000000000000000000000000001-0000000000000001-01",
"X-B3-SpanId": "0000000000000001",
"X-B3-Sampled": "1",
Expand Down