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-3470 feat: Head-based sampling for manual distributed tracing #1793

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
18 changes: 18 additions & 0 deletions Datadog/Datadog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@
614CADD72510BAC000B93D2D /* Environment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 614CADD62510BAC000B93D2D /* Environment.swift */; };
614E9EB3244719FA007EE3E1 /* BundleType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 614E9EB2244719FA007EE3E1 /* BundleType.swift */; };
614ED36C260352DC00C8C519 /* CrashReporter.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = 614ED36B260352DC00C8C519 /* CrashReporter.xcframework */; };
615192CD2BD6948B0005A782 /* HTTPHeadersWriterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 615192CC2BD6948B0005A782 /* HTTPHeadersWriterTests.swift */; };
615192CE2BD6948B0005A782 /* HTTPHeadersWriterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 615192CC2BD6948B0005A782 /* HTTPHeadersWriterTests.swift */; };
615192D02BD6B7C90005A782 /* DatadogTracer+InjectAndExtract.swift in Sources */ = {isa = PBXBuildFile; fileRef = 615192CF2BD6B7C90005A782 /* DatadogTracer+InjectAndExtract.swift */; };
615192D12BD6B7C90005A782 /* DatadogTracer+InjectAndExtract.swift in Sources */ = {isa = PBXBuildFile; fileRef = 615192CF2BD6B7C90005A782 /* DatadogTracer+InjectAndExtract.swift */; };
61570005246AADFA00E96950 /* DatadogObjc.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 61133BF0242397DA00786299 /* DatadogObjc.framework */; };
615A4A8324A3431600233986 /* Trace+objc.swift in Sources */ = {isa = PBXBuildFile; fileRef = 615A4A8224A3431600233986 /* Trace+objc.swift */; };
615A4A8924A34FD700233986 /* DDTracerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 615A4A8824A34FD700233986 /* DDTracerTests.swift */; };
Expand Down Expand Up @@ -352,6 +356,8 @@
6167E72A2B84C11900C3CA2D /* DDCrashReportMocks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6167E7282B84C11900C3CA2D /* DDCrashReportMocks.swift */; };
6167E72C2B84C72B00C3CA2D /* UIKitHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6167E72B2B84C72B00C3CA2D /* UIKitHelpers.swift */; };
6167E72D2B84C72B00C3CA2D /* UIKitHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6167E72B2B84C72B00C3CA2D /* UIKitHelpers.swift */; };
616AAA6D2BDA674C00AB9DAD /* TraceSamplingStrategy+objc.swift in Sources */ = {isa = PBXBuildFile; fileRef = 616AAA6C2BDA674C00AB9DAD /* TraceSamplingStrategy+objc.swift */; };
616AAA6E2BDA674C00AB9DAD /* TraceSamplingStrategy+objc.swift in Sources */ = {isa = PBXBuildFile; fileRef = 616AAA6C2BDA674C00AB9DAD /* TraceSamplingStrategy+objc.swift */; };
616B668E259CC28E00968EE8 /* DDRUMMonitorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 616B668D259CC28E00968EE8 /* DDRUMMonitorTests.swift */; };
616F8C272BB1CD990061EA53 /* ProcessIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 616F8C262BB1CD990061EA53 /* ProcessIdentifier.swift */; };
616F8C282BB1CD990061EA53 /* ProcessIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 616F8C262BB1CD990061EA53 /* ProcessIdentifier.swift */; };
Expand Down Expand Up @@ -2244,6 +2250,8 @@
614CADD62510BAC000B93D2D /* Environment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Environment.swift; sourceTree = "<group>"; };
614E9EB2244719FA007EE3E1 /* BundleType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BundleType.swift; sourceTree = "<group>"; };
614ED36B260352DC00C8C519 /* CrashReporter.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = CrashReporter.xcframework; path = ../Carthage/Build/CrashReporter.xcframework; sourceTree = "<group>"; };
615192CC2BD6948B0005A782 /* HTTPHeadersWriterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPHeadersWriterTests.swift; sourceTree = "<group>"; };
615192CF2BD6B7C90005A782 /* DatadogTracer+InjectAndExtract.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DatadogTracer+InjectAndExtract.swift"; sourceTree = "<group>"; };
6152C84224BE2165006A1679 /* MockServerAddress.local.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = MockServerAddress.local.xcconfig; sourceTree = "<group>"; };
615519252461BCE7002A85CF /* Datadog.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Datadog.xcconfig; sourceTree = "<group>"; };
615519262461BCE7002A85CF /* Datadog.local.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Datadog.local.xcconfig; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2288,6 +2296,7 @@
6167E7222B837FF100C3CA2D /* BinaryImageMocks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BinaryImageMocks.swift; sourceTree = "<group>"; };
6167E7282B84C11900C3CA2D /* DDCrashReportMocks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DDCrashReportMocks.swift; sourceTree = "<group>"; };
6167E72B2B84C72B00C3CA2D /* UIKitHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIKitHelpers.swift; sourceTree = "<group>"; };
616AAA6C2BDA674C00AB9DAD /* TraceSamplingStrategy+objc.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TraceSamplingStrategy+objc.swift"; sourceTree = "<group>"; };
616B668D259CC28E00968EE8 /* DDRUMMonitorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DDRUMMonitorTests.swift; sourceTree = "<group>"; };
616C0A9D28573DFF00C13264 /* RUMOperatingSystemInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RUMOperatingSystemInfo.swift; sourceTree = "<group>"; };
616C0AA028573F6300C13264 /* RUMOperatingSystemInfoTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RUMOperatingSystemInfoTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4252,6 +4261,7 @@
6132BF4A24A49C7200D7BD17 /* Propagation */ = {
isa = PBXGroup;
children = (
616AAA6C2BDA674C00AB9DAD /* TraceSamplingStrategy+objc.swift */,
6132BF4B24A49C8F00D7BD17 /* HTTPHeadersWriter+objc.swift */,
A79B0F5E292BA435008742B3 /* B3HTTPHeadersWriter+objc.swift */,
A728ADAA2934EA2100397996 /* W3CHTTPHeadersWriter+objc.swift */,
Expand Down Expand Up @@ -5735,6 +5745,7 @@
619CE75D2A458CE1005588CB /* TraceConfigurationTests.swift */,
61AD4E172451C7FF006E34EA /* TracingFeatureMocks.swift */,
61F3E3622BC5556D00C7881E /* DatadogTracer+SamplingTests.swift */,
615192CF2BD6B7C90005A782 /* DatadogTracer+InjectAndExtract.swift */,
61C5A89824509C1100DA608C /* DDSpanTests.swift */,
61F1A620249A45E400075390 /* DDSpanContextTests.swift */,
D2F1B81426D8E5FF009F3293 /* DDNoopTracerTests.swift */,
Expand Down Expand Up @@ -6049,6 +6060,7 @@
61B558D32469CDD8001460D3 /* TraceIDGeneratorTests.swift */,
3CCECDAE2BC688120013C125 /* SpanIDGeneratorTests.swift */,
61F3E3652BC595F600C7881E /* HTTPHeadersReaderTests.swift */,
615192CC2BD6948B0005A782 /* HTTPHeadersWriterTests.swift */,
A79B0F5A292B7C06008742B3 /* B3HTTPHeadersWriterTests.swift */,
A79B0F60292BB071008742B3 /* B3HTTPHeadersReaderTests.swift */,
A728ADA22934DB5000397996 /* W3CHTTPHeadersWriterTests.swift */,
Expand Down Expand Up @@ -7856,6 +7868,7 @@
6132BF4C24A49C8F00D7BD17 /* HTTPHeadersWriter+objc.swift in Sources */,
6132BF4724A498D800D7BD17 /* DDSpan+objc.swift in Sources */,
615A4A8B24A3568900233986 /* OTSpan+objc.swift in Sources */,
616AAA6D2BDA674C00AB9DAD /* TraceSamplingStrategy+objc.swift in Sources */,
611720D52524D9FB00634D9E /* DDURLSessionDelegate+objc.swift in Sources */,
9E55407C25812D1C00F6E3AD /* RUM+objc.swift in Sources */,
D2A434AA2A8E40A20028E329 /* SessionReplay+objc.swift in Sources */,
Expand Down Expand Up @@ -8561,6 +8574,7 @@
61F3E3632BC5556D00C7881E /* DatadogTracer+SamplingTests.swift in Sources */,
D2C1A51C29C4C75700946C31 /* ContextMessageReceiverTests.swift in Sources */,
619CE7612A458D66005588CB /* TraceTests.swift in Sources */,
615192D02BD6B7C90005A782 /* DatadogTracer+InjectAndExtract.swift in Sources */,
D2C1A52029C4C75700946C31 /* DDSpanTests.swift in Sources */,
D2C1A51B29C4C75700946C31 /* DDSpanContextTests.swift in Sources */,
D2C1A52729C4C7D000946C31 /* TracingFeatureMocks.swift in Sources */,
Expand Down Expand Up @@ -8764,6 +8778,7 @@
61F3E3642BC5556D00C7881E /* DatadogTracer+SamplingTests.swift in Sources */,
D2C1A56529C4F2E800946C31 /* ContextMessageReceiverTests.swift in Sources */,
619CE7622A458D66005588CB /* TraceTests.swift in Sources */,
615192D12BD6B7C90005A782 /* DatadogTracer+InjectAndExtract.swift in Sources */,
D2C1A56629C4F2E800946C31 /* DDSpanTests.swift in Sources */,
D2C1A56729C4F2E800946C31 /* DDSpanContextTests.swift in Sources */,
D2C1A56829C4F2E800946C31 /* TracingFeatureMocks.swift in Sources */,
Expand Down Expand Up @@ -9019,6 +9034,7 @@
3CCCA5C52ABAF0F80029D7BD /* DDURLSessionInstrumentation+objc.swift in Sources */,
D2CB6FA027C5217A00A62B57 /* Trace+objc.swift in Sources */,
D2CB6FA127C5217A00A62B57 /* HTTPHeadersWriter+objc.swift in Sources */,
616AAA6E2BDA674C00AB9DAD /* TraceSamplingStrategy+objc.swift in Sources */,
D2CB6FA227C5217A00A62B57 /* DDSpan+objc.swift in Sources */,
D2CB6FA327C5217A00A62B57 /* OTSpan+objc.swift in Sources */,
D2CB6FA427C5217A00A62B57 /* DDURLSessionDelegate+objc.swift in Sources */,
Expand Down Expand Up @@ -9193,6 +9209,7 @@
D2160CE929C0E00200FAA9A5 /* MethodSwizzlerTests.swift in Sources */,
D2216EC32A96649500ADAEC8 /* FeatureBaggageTests.swift in Sources */,
D2160CDC29C0DF6700FAA9A5 /* HostsSanitizerTests.swift in Sources */,
615192CD2BD6948B0005A782 /* HTTPHeadersWriterTests.swift in Sources */,
D2F44FB8299AA1DA0074B0D9 /* DataCompressionTests.swift in Sources */,
D2160CE029C0DF6700FAA9A5 /* URLSessionDelegateAsSuperclassTests.swift in Sources */,
D2EBEE3B29BA163E00B15732 /* B3HTTPHeadersReaderTests.swift in Sources */,
Expand Down Expand Up @@ -9239,6 +9256,7 @@
D2EBEE4229BA163F00B15732 /* W3CHTTPHeadersReaderTests.swift in Sources */,
D2216EC42A96649700ADAEC8 /* FeatureBaggageTests.swift in Sources */,
D2160CDD29C0DF6700FAA9A5 /* HostsSanitizerTests.swift in Sources */,
615192CE2BD6948B0005A782 /* HTTPHeadersWriterTests.swift in Sources */,
D2F44FB9299AA1DB0074B0D9 /* DataCompressionTests.swift in Sources */,
D2160CE129C0DF6700FAA9A5 /* URLSessionDelegateAsSuperclassTests.swift in Sources */,
D2EBEE3F29BA163F00B15732 /* B3HTTPHeadersReaderTests.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,6 @@
<Test
Identifier = "HeadBasedSamplingTests/testSendingDroppedDistributedTraceWithParent_throughURLSessionInstrumentationAPI()">
</Test>
<Test
Identifier = "HeadBasedSamplingTests/testSendingSampledDistributedTraceWithNoParent_throughTracerAPI()">
</Test>
<Test
Identifier = "HeadBasedSamplingTests/testSendingSampledDistributedTraceWithParent_throughTracerAPI()">
</Test>
</SkippedTests>
</TestableReference>
<TestableReference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,19 @@ internal struct DebugManualTraceInjectionView: View {

switch selectedTraceHeaderType {
case .datadog:
let writer = HTTPHeadersWriter(sampleRate: sampleRate)
let writer = HTTPHeadersWriter(samplingStrategy: .custom(sampleRate: sampleRate))
Tracer.shared().inject(spanContext: span.context, writer: writer)
writer.traceHeaderFields.forEach { request.setValue($0.value, forHTTPHeaderField: $0.key) }
case .w3c:
let writer = W3CHTTPHeadersWriter(
sampleRate: sampleRate,
tracestate: [:]
)
let writer = W3CHTTPHeadersWriter(samplingStrategy: .custom(sampleRate: sampleRate), tracestate: [:])
Tracer.shared().inject(spanContext: span.context, writer: writer)
writer.traceHeaderFields.forEach { request.setValue($0.value, forHTTPHeaderField: $0.key) }
case .b3Single:
let writer = B3HTTPHeadersWriter(sampleRate: sampleRate, injectEncoding: .single)
let writer = B3HTTPHeadersWriter(samplingStrategy: .custom(sampleRate: sampleRate), injectEncoding: .single)
Tracer.shared().inject(spanContext: span.context, writer: writer)
writer.traceHeaderFields.forEach { request.setValue($0.value, forHTTPHeaderField: $0.key) }
case .b3Multiple:
let writer = B3HTTPHeadersWriter(sampleRate: sampleRate, injectEncoding: .multiple)
let writer = B3HTTPHeadersWriter(samplingStrategy: .custom(sampleRate: sampleRate), injectEncoding: .multiple)
Tracer.shared().inject(spanContext: span.context, writer: writer)
writer.traceHeaderFields.forEach { request.setValue($0.value, forHTTPHeaderField: $0.key) }
}
Expand Down
18 changes: 6 additions & 12 deletions Datadog/IntegrationUnitTests/Trace/HeadBasedSamplingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ class HeadBasedSamplingTests: XCTestCase {

// MARK: - Distributed Tracing (through Tracer API)

// TODO: RUM-3470 Enable this test when head-based sampling is supported for distributed tracing through Tracer API
func testSendingSampledDistributedTraceWithNoParent_throughTracerAPI() throws {
/*
This is the situation where distributed trace starts with the span created with Datadog tracer:
Expand All @@ -279,15 +278,14 @@ class HeadBasedSamplingTests: XCTestCase {
*/

let localTraceSampling: Float = 100 // keep all
let distributedTraceSampling: Float = 0 // drop all

// Given
traceConfig.sampleRate = localTraceSampling
Trace.enable(with: traceConfig, in: core)

// When
var request: URLRequest = .mockAny()
let writer = HTTPHeadersWriter(sampleRate: distributedTraceSampling)
let writer = HTTPHeadersWriter(samplingStrategy: .headBased)
let span = Tracer.shared(in: core).startSpan(operationName: "network.span")
Tracer.shared(in: core).inject(spanContext: span.context, writer: writer)
writer.traceHeaderFields.forEach { field, value in request.setValue(value, forHTTPHeaderField: field) }
Expand All @@ -309,7 +307,7 @@ class HeadBasedSamplingTests: XCTestCase {
XCTAssertEqual(request.value(forHTTPHeaderField: TracingHTTPHeaders.samplingPriorityField), "1")
}

// TODO: RUM-3470 Enable this test when head-based sampling is supported for distributed tracing through Tracer API
// TODO: RUM-3535 Enable this test when trace context injection control is implemented
func testSendingDroppedDistributedTraceWithNoParent_throughTracerAPI() throws {
/*
This is the situation where distributed trace starts with the span created with Datadog tracer:
Expand All @@ -319,15 +317,14 @@ class HeadBasedSamplingTests: XCTestCase {
*/

let localTraceSampling: Float = 0 // drop all
let distributedTraceSampling: Float = 100 // keep all

// Given
traceConfig.sampleRate = localTraceSampling
Trace.enable(with: traceConfig, in: core)

// When
var request: URLRequest = .mockAny()
let writer = HTTPHeadersWriter(sampleRate: distributedTraceSampling)
let writer = HTTPHeadersWriter(samplingStrategy: .headBased)
let span = Tracer.shared(in: core).startSpan(operationName: "network.span")
Tracer.shared(in: core).inject(spanContext: span.context, writer: writer)
writer.traceHeaderFields.forEach { field, value in request.setValue(value, forHTTPHeaderField: field) }
Expand All @@ -349,7 +346,6 @@ class HeadBasedSamplingTests: XCTestCase {
XCTAssertEqual(request.value(forHTTPHeaderField: TracingHTTPHeaders.samplingPriorityField), "0")
}

// TODO: RUM-3470 Enable this test when head-based sampling is supported for distributed tracing through Tracer API
func testSendingSampledDistributedTraceWithParent_throughTracerAPI() throws {
/*
This is the situation where distributed trace starts with an active local span and is continued with the span
Expand All @@ -361,15 +357,14 @@ class HeadBasedSamplingTests: XCTestCase {
*/

let localTraceSampling: Float = 100 // keep all
let distributedTraceSampling: Float = 0 // drop all

// Given
traceConfig.sampleRate = localTraceSampling
Trace.enable(with: traceConfig, in: core)

// When
var request: URLRequest = .mockAny()
let writer = HTTPHeadersWriter(sampleRate: distributedTraceSampling)
let writer = HTTPHeadersWriter(samplingStrategy: .headBased)
let parentSpan = Tracer.shared(in: core).startSpan(operationName: "active.span").setActive()
let span = Tracer.shared(in: core).startSpan(operationName: "network.span")
Tracer.shared(in: core).inject(spanContext: span.context, writer: writer)
Expand Down Expand Up @@ -399,7 +394,7 @@ class HeadBasedSamplingTests: XCTestCase {
XCTAssertEqual(request.value(forHTTPHeaderField: TracingHTTPHeaders.samplingPriorityField), "1")
}

// TODO: RUM-3470 Enable this test when head-based sampling is supported for distributed tracing through Tracer API
// TODO: RUM-3535 Enable this test when trace context injection control is implemented
func testSendingDroppedDistributedTraceWithParent_throughTracerAPI() throws {
/*
This is the situation where distributed trace starts with an active local span and is continued with the span
Expand All @@ -411,15 +406,14 @@ class HeadBasedSamplingTests: XCTestCase {
*/

let localTraceSampling: Float = 0 // drop all
let distributedTraceSampling: Float = 100 // keep all

// Given
traceConfig.sampleRate = localTraceSampling
Trace.enable(with: traceConfig, in: core)

// When
var request: URLRequest = .mockAny()
let writer = HTTPHeadersWriter(sampleRate: distributedTraceSampling)
let writer = HTTPHeadersWriter(samplingStrategy: .headBased)
let parentSpan = Tracer.shared(in: core).startSpan(operationName: "active.span").setActive()
let span = Tracer.shared(in: core).startSpan(operationName: "network.span")
Tracer.shared(in: core).inject(spanContext: span.context, writer: writer)
Expand Down
Loading