Skip to content

RUM-3470 chore: Add tests for head-based trace sampling#1772

Merged
ncreated merged 8 commits into
developfrom
ncreated/RUM-3470/add-head-based-sampling-tests
Apr 12, 2024
Merged

RUM-3470 chore: Add tests for head-based trace sampling#1772
ncreated merged 8 commits into
developfrom
ncreated/RUM-3470/add-head-based-sampling-tests

Conversation

@ncreated

@ncreated ncreated commented Apr 11, 2024

Copy link
Copy Markdown
Member

What and why?

📦 🧰 This PR prepares DatadogTrace for the implementation of head-based sampling. It only brings refactoring and fills gaps in tests coverage. There are no changes to existing behaviours.

Notably, it adds HeadBasedSamplingTests suite that covers main scenarios we want to achieve with head-based sampling. That includes:

Head-based sampling of local trace, where all spans must be either "kept" or "dropped":

client-ios-app:     [---- custom.span.1 -----]
client-ios-app:       [-- custom.span.2 ---]
client-ios-app:         [- custom.span.3 -]

Head-based sampling of distributed trace, where all spans must be either "kept" or "dropped":

dd-sdk-ios:         [--- urlsession.request ---]
client backend:        [--- backend span ---]

Head-based sampling of distributed trace with local parent, where all spans must be either "kept" or "dropped":

client-ios-app:     [-------- active.span ---------]
dd-sdk-ios:           [-- urlsession.request --]
client backend:        [--- backend span ---]

The work has to be split between RUM-3470 (next PR) and RUM-3535 ("trace context injection"). To embrace common understanding of what we want to do, I propose to go red → green refactoring, hence I'm adding multiple tests that are yet disabled.

How?

  • Added HeadBasedSamplingTests to add missing coverage and set common goals for RUM-3470 and RUM-3535;
  • Renamed samplers in DatadogTrace for more clarity:
    • samplerlocalTraceSampler in DatadogTracer;
    • tracingSamplerdistributedTraceSampler in TracingURLSessionHandler;
  • To facilitate further work and better fit tests pyramid, moved first tracer tests from DatadogCoreTests (integration testing) to DatadogTraceTests (unit testing).

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
  • Add CHANGELOG entry for user facing changes

Custom CI job configuration (optional)

  • Run unit tests for Core, RUM, Trace, Logs, CR and WVT
  • Run unit tests for Session Replay
  • Run integration tests
  • Run smoke tests
  • Run tests for tools/

@ncreated ncreated self-assigned this Apr 11, 2024
@datadog-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Apr 11, 2024

Copy link
Copy Markdown

Datadog Report

Branch report: ncreated/RUM-3470/add-head-based-sampling-tests
Commit report: e7ac165
Test service: dd-sdk-ios

✅ 0 Failed, 3000 Passed, 0 Skipped, 11m 13.69s Wall Time
🔻 Test Sessions change in coverage: 8 decreased, 6 increased

🔻 Code Coverage Decreases vs Default Branch (8)

This report shows up to 5 code coverage decreases.

  • test DatadogCrashReportingTests tvOS 27.91% (-0.69%) - Details
  • test DatadogCrashReportingTests iOS 27.87% (-0.69%) - Details
  • test DatadogWebViewTrackingTests iOS 17.13% (-0.68%) - Details
  • test DatadogCoreTests tvOS 79.64% (-0.62%) - Details
  • test DatadogCoreTests iOS 73.56% (-0.4%) - Details

XCTAssertEqual(try spanMatcher.meta.custom(keyPath: "meta.globaltag2"), "overwrittenValue")
}

// MARK: - Tracer with sampling rate

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

💡 Moved to DatadogTracer+SamplingTests.swif - now part of DatadogTraceTests instead of DatadogCoreTests.

func interceptionDidComplete(interception: URLSessionTaskInterception)
}

internal struct NOPDatadogURLSessionInterceptor: DatadogURLSessionHandler {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

💡 🧹 not used anywhere

@available(*, deprecated, message: "Use `URLSessionInstrumentation.enable(with:)` instead.")
open class DatadogURLSessionDelegate: NSObject, URLSessionDataDelegate {
var interceptor: URLSessionInterceptor? {
var feature: NetworkInstrumentationFeature? {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

💡 🧹 to further drop dependency on URLSessionInterceptor - now it is only required for DatadogAlamofireExtension (soon dropped in RUM-3060).

Comment on lines -11 to +12
internal weak var core: DatadogCoreProtocol?
/// Trace feature scope.
private let featureScope: FeatureScope

@ncreated ncreated Apr 11, 2024

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

💡 to achieve mid-term goals ("no core dependency") from #1744. With this change we facilitate testing DatadogTracer in DatadogTraceTests (vs testing with core mock in DatadogCoreTests).

Comment on lines 59 to 60
internal let origin: String?
/// The sampling rate for the span (between 0 and 100)
/// The sampling rate for the span (between 0 and 1)
internal let samplingRate: Float

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

💡 🧹 this wasn't correct - when encoded, we normalize sample rate to 0...1 range

Comment on lines +199 to +202
<SkippedTests>
<Test
Identifier = "HeadBasedSamplingTests/testSamplingLocalTrace()">
</Test>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

💡 These tests are disabled on purpose. I propose we go red → green through RUM-3470 (next PR) and later RUM-3535 ("trace context injection control").

@ncreated
ncreated marked this pull request as ready for review April 11, 2024 12:19
@ncreated
ncreated requested review from a team as code owners April 11, 2024 12:19

@ganeshnj ganeshnj left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Very neatly written test cases. a couple of question, everything looks great.

Comment on lines +52 to +53
let allKept = spans.filter({ $0.isKept }).count == 3
let allDropped = spans.filter({ !$0.isKept }).count == 3

Copy link
Copy Markdown

Choose a reason for hiding this comment

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


do we have inconsistent isKept flag for local spans in a trace?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes 😞, this test is red if enabled. Although the original issue touches only on head-based sampling for distributed traces I'll be fixing it for local traces as well. It might be one fix that works for both.

// MARK: - Distributed Tracing

// TODO: RUM-3470 Enable this test when head-based sampling is supported
func testSendingSampledDistributedTraceWithNoParent() throws {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💯

}

// TODO: RUM-3470 Enable this test when head-based sampling is supported
func testSendingSampledDistributedTraceWithParent() throws {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💯

@ncreated
ncreated merged commit 891e2d3 into develop Apr 12, 2024
@ncreated
ncreated deleted the ncreated/RUM-3470/add-head-based-sampling-tests branch April 12, 2024 15:22
@ncreated ncreated mentioned this pull request Jun 25, 2025
4 tasks
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