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-3516 Provide the correct sampling priority for our Span events based on APM new rules #1913

Conversation

mariusc83
Copy link
Member

@mariusc83 mariusc83 commented Mar 13, 2024

What does this PR do?

Following the new rules in APM Java project we will provide the right sampling priority based on the APM tracer configuration.

Motivation

What inspired you to submit this pull request?

Additional Notes

Anything else we should know when reviewing?

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • Make sure you discussed the feature or bugfix with the maintaining team in an Issue
  • Make sure each commit and the PR mention the Issue number (cf the CONTRIBUTING doc)

@mariusc83 mariusc83 self-assigned this Mar 13, 2024
@mariusc83 mariusc83 force-pushed the mconstantin/rum-3516/correctly-provide-sampling-tags-for-otel-spans branch 2 times, most recently from 64cb324 to ef6b729 Compare March 13, 2024 12:54
Base automatically changed from mconstantin/rum-3294/bundle-dd-trace-core-into-dd-sdk-trace to feature/otel-support March 13, 2024 12:55
@mariusc83 mariusc83 force-pushed the mconstantin/rum-3516/correctly-provide-sampling-tags-for-otel-spans branch from ef6b729 to a9433a0 Compare March 13, 2024 12:57
@mariusc83 mariusc83 marked this pull request as ready for review March 13, 2024 12:57
@mariusc83 mariusc83 requested review from a team as code owners March 13, 2024 12:57
@mariusc83 mariusc83 force-pushed the mconstantin/rum-3516/correctly-provide-sampling-tags-for-otel-spans branch 8 times, most recently from 5992b52 to 8924351 Compare March 13, 2024 16:39
Comment on lines 526 to 524
val priority = ddSpanContext.samplingPriority
assertThat(priority).isIn(-1, 2)
Copy link
Member

Choose a reason for hiding this comment

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

Warning

This test is not testing whether we are using the sample rate passed in. We just test that when we set a sample rate, the span will have priority manual drop or manual keep.

A better test would be to create 100 spans with the tracer, and then count the number of manual keep, and make sure it's relatively close to the input sample rate.

Copy link
Member Author

Choose a reason for hiding this comment

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

I am not trying to test the sampling mechanism here, I am just trying to test that if you specify a sample rate you will either get -1 or 2 but the intention is not to test the sampling logic.

@mariusc83 mariusc83 force-pushed the mconstantin/rum-3516/correctly-provide-sampling-tags-for-otel-spans branch 2 times, most recently from 3669e9b to 8f02a06 Compare March 14, 2024 08:47
@@ -224,7 +237,7 @@ class OtelTracerProvider(
internal const val TRACER_ALREADY_EXISTS_WARNING_MESSAGE =
"Tracer for %s already exists. Returning existing instance."
internal const val DEFAULT_TRACER_NAME = "android"
internal const val DEFAULT_SAMPLE_RATE = 100.0
internal const val KEEP_ALL_SAMPLE_RATE = 100.0
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
internal const val KEEP_ALL_SAMPLE_RATE = 100.0
internal const val KEEP_ALL_SAMPLE_RATE_PERCENT = 100.0

try {
pendingTraceBufferWorker.join(10000)
} catch (e: InterruptedException) {
// ignore
Copy link
Member

Choose a reason for hiding this comment

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

should we not log that this has happened? Alternatively if we keep it, I have some memory there is a convention to change the variable name to ignored in the catch block

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah I am currently testing things here as I don't really know why my tests are failing in CI. This is currently WIP until I figure out what's the bottom of the issue.

@mariusc83 mariusc83 force-pushed the mconstantin/rum-3516/correctly-provide-sampling-tags-for-otel-spans branch 2 times, most recently from ff10d25 to 9a14bf3 Compare March 14, 2024 13:16
@codecov-commenter
Copy link

codecov-commenter commented Mar 14, 2024

Codecov Report

Merging #1913 (dcd9317) into feature/otel-support (f18d527) will decrease coverage by 25.95%.
Report is 2 commits behind head on feature/otel-support.
The diff coverage is 100.00%.

Additional details and impacted files
@@                    Coverage Diff                    @@
##           feature/otel-support    #1913       +/-   ##
=========================================================
- Coverage                 82.37%   56.43%   -25.95%     
=========================================================
  Files                       490      796      +306     
  Lines                     17411    29605    +12194     
  Branches                   2610     4854     +2244     
=========================================================
+ Hits                      14342    16705     +2363     
- Misses                     2348    11786     +9438     
- Partials                    721     1114      +393     
Files Coverage Δ
...main/java/com/datadog/trace/core/PendingTrace.java 47.85% <ø> (ø)
...in/com/datadog/android/trace/OtelTracerProvider.kt 94.12% <100.00%> (ø)
...ternal/domain/event/OtelDdSpanToSpanEventMapper.kt 92.42% <100.00%> (+1.38%) ⬆️

... and 335 files with indirect coverage changes

@mariusc83 mariusc83 force-pushed the mconstantin/rum-3516/correctly-provide-sampling-tags-for-otel-spans branch 2 times, most recently from cfeaed3 to 166d20b Compare March 14, 2024 14:41
@mariusc83 mariusc83 force-pushed the mconstantin/rum-3516/correctly-provide-sampling-tags-for-otel-spans branch from 166d20b to e546eed Compare March 15, 2024 08:08
Copy link
Member

@0xnm 0xnm left a comment

Choose a reason for hiding this comment

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

lgtm, I've added some questions for the better understanding

@@ -9,6 +9,8 @@
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;

import android.util.Log;
Copy link
Member

Choose a reason for hiding this comment

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

unused import?


// Then
val priority = delegateSpan.samplingPriority
assertThat(priority).isEqualTo(2)
Copy link
Member

Choose a reason for hiding this comment

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

It seems we can use here PrioritySampling.USER_KEEP, same for the tests below

Suggested change
assertThat(priority).isEqualTo(2)
assertThat(priority).isEqualTo(PrioritySampling.USER_KEEP)

}

@Test
fun `M use keep priority W buildSpan { not provided sample rate }`() {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
fun `M use keep priority W buildSpan { not provided sample rate }`() {
fun `M use auto-keep priority W buildSpan { not provided sample rate }`() {

@mariusc83 mariusc83 force-pushed the mconstantin/rum-3516/correctly-provide-sampling-tags-for-otel-spans branch from e546eed to 8e26066 Compare March 20, 2024 13:48

// Then
val priority = delegateSpan.samplingPriority
assertThat(priority).isEqualTo(PrioritySampling.USER_KEEP.toInt())
Copy link
Member

Choose a reason for hiding this comment

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

I believe it should be PrioritySampling.USER_DROP here by looking on the name of the test

}

@Test
fun `M use auto - keep priority W buildSpan { not provided sample rate }`() {
Copy link
Member

Choose a reason for hiding this comment

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

nit

Suggested change
fun `M use auto - keep priority W buildSpan { not provided sample rate }`() {
fun `M use auto-keep priority W buildSpan { not provided sample rate }`() {

@mariusc83 mariusc83 force-pushed the mconstantin/rum-3516/correctly-provide-sampling-tags-for-otel-spans branch from 8e26066 to 858200e Compare March 20, 2024 14:46
@mariusc83 mariusc83 requested a review from 0xnm March 20, 2024 15:10
0xnm
0xnm previously approved these changes Mar 20, 2024
Comment on lines 524 to 525
val priority = delegateSpan.samplingPriority
assertThat(priority).isIn(PrioritySampling.USER_DROP.toInt(), PrioritySampling.USER_KEEP.toInt())
Copy link
Member

Choose a reason for hiding this comment

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

Caution

This part still doesn't test that the appropriate sample rate is being used… the fact that a specific field is set on the core tracer (as tested line 445) doesn't mean it's the one that will be applied. Since we're testing the actual priority set on the spans, we should really try creating 100 spans and measure the effective sampling rate and compare that to the expected one (with some margin of error)

@mariusc83 mariusc83 force-pushed the mconstantin/rum-3516/correctly-provide-sampling-tags-for-otel-spans branch from 858200e to dcd9317 Compare March 21, 2024 09:59
@mariusc83 mariusc83 requested review from xgouchet and 0xnm March 21, 2024 10:01
Comment on lines +535 to +540
assertThat(droppedSpans.size + keptSpans.size).isEqualTo(numberOfSpans)
// The sampler does not guarantee the exact number of dropped/kept spans due to the random nature
// of the sampling so we use an offset to allow a small margin of error
val offset = 10
assertThat(droppedSpans.size).isCloseTo(expectedDroppedSpans, Offset.offset(offset))
assertThat(keptSpans.size).isCloseTo(expectedKeptSpans, Offset.offset(offset))
Copy link
Member

Choose a reason for hiding this comment

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

Great :)

@mariusc83 mariusc83 merged commit 50e49ba into feature/otel-support Mar 21, 2024
23 checks passed
@mariusc83 mariusc83 deleted the mconstantin/rum-3516/correctly-provide-sampling-tags-for-otel-spans branch March 21, 2024 11:09
@xgouchet xgouchet added this to the 2.11.x milestone Jul 31, 2024
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.

5 participants