Skip to content

RUM-15702 Add trace sampling decision to DatadogEventBridge#3495

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 9 commits into
developfrom
marroz/RUM-15702-add-trace-sampling-decision-to-bridge
Jun 8, 2026
Merged

RUM-15702 Add trace sampling decision to DatadogEventBridge#3495
gh-worker-dd-mergequeue-cf854d[bot] merged 9 commits into
developfrom
marroz/RUM-15702-add-trace-sampling-decision-to-bridge

Conversation

@arroz

@arroz arroz commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

With deterministic sampling, the decision of sampling requests on native clients became consistent, based on the sampling rate and session ID. However, when a native client loads an instrumented web site on a WebView, there is a disconnect between the sampling decision used by the browser SDK of the page being viewed in the WebView, and the native client's decision. This cannot be fixed by post-processing on the native client when the data is funneled through it, since the browser SDK needs to know if headers should be injected on requests, or not. More details here (internal).

This is the Android counterpart of DataDog/browser-sdk#4516 and DataDog/dd-sdk-ios#2859.

How?

To fix this problem, this patch introduces a way for Android to obtain the sampling decision from the Browser SDK running on the WebView through the JS bridge. Unlike iOS, this is a simple sync call that obtains the decision from the bridge.

On the JS side, depending on the decision being injected, tracing sample rate will be set to 100 (when the decision is true) or 0 (when it's false). Because of that, the Android SDK re-writes the _dd.rule_psr value with the actual sampling rate configured in the iOS SDK.

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)

@arroz
arroz force-pushed the marroz/RUM-15702-add-trace-sampling-decision-to-bridge branch from ca5e54f to 4ebe836 Compare June 2, 2026 12:08
@datadog-official

This comment has been minimized.

@arroz arroz changed the title Marroz/rum 15702 add trace sampling decision to bridge RUM-15702 Add trace sampling decision to DatadogEventBridge Jun 2, 2026
@codecov-commenter

codecov-commenter commented Jun 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.12195% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.27%. Comparing base (6c72554) to head (3990aeb).

Files with missing lines Patch % Lines
...lin/com/datadog/android/webview/WebViewTracking.kt 50.00% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #3495      +/-   ##
===========================================
+ Coverage    72.21%   72.27%   +0.06%     
===========================================
  Files          965      965              
  Lines        35607    35642      +35     
  Branches      5932     5947      +15     
===========================================
+ Hits         25711    25757      +46     
+ Misses        8279     8274       -5     
+ Partials      1617     1611       -6     
Files with missing lines Coverage Δ
...dog/android/webview/internal/DatadogEventBridge.kt 100.00% <100.00%> (ø)
...id/webview/internal/rum/WebViewRumEventConsumer.kt 92.96% <100.00%> (+0.89%) ⬆️
...roid/webview/internal/rum/WebViewRumEventMapper.kt 97.37% <100.00%> (+0.23%) ⬆️
.../android/webview/internal/rum/WebViewRumFeature.kt 100.00% <100.00%> (ø)
...lin/com/datadog/android/webview/WebViewTracking.kt 84.43% <50.00%> (-1.29%) ⬇️

... and 37 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@arroz
arroz marked this pull request as ready for review June 2, 2026 14:57
@arroz
arroz requested review from a team as code owners June 2, 2026 14:57

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e326da0c2a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +88 to +89
val rumContext = sdkCore.getFeatureContext(Feature.RUM_FEATURE_NAME, useContextThread = false)
val tracingContext = sdkCore.getFeatureContext(Feature.TRACING_FEATURE_NAME, useContextThread = false)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ I'm a bit worried about these two invocations. Even with useContextThread = false, getFeatureContext acquires a read lock (

return@Callable feature.featureContextLock.readLock().safeWithLock {
) on the calling thread.

IIUC getIsTraceSampled() is a @JavascriptInterface method called synchronously from the WebView JS engine, the JS thread will block for the duration of that lock if a concurrent context write is holding the write.

We ran into the same issue in PR #3342 (#3342 (comment)). I would say that we have a similar case here.

Anyway, @0xnm - @satween, can you confirm that what I'm saying is correct?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree, I don't think it is a best approach.

WDYT about implementing FeatureContextUpdateReceiver maybe?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

WDYT about implementing FeatureContextUpdateReceiver maybe?

Agree, sounds better. We'll need to deal with removeContextUpdateReceiver because DatadogEventBridge doesn't have a lifecycle so we probably need to handle it in a different place but I think that's a better approach.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed this by using WebViewRumFeature as the receiver.

@arroz
arroz force-pushed the marroz/RUM-15702-add-trace-sampling-decision-to-bridge branch from 44a85fb to 3990aeb Compare June 5, 2026 12:55
@arroz
arroz requested review from 0xnm, hamorillo and satween June 8, 2026 07:33

@hamorillo hamorillo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Left some minor comments. LGTM overall. I would also wait for @satween's approval, though.

Thanks for the changes!

val tracingContext = webViewRumFeature?.cachedTracingContext.orEmpty()

val sessionId = (rumContext[SESSION_ID_KEY] as? String)
?.takeIf { rumContext[SESSION_STATE_KEY] == TRACKED_STATE }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

❓ I understand that we are adding this guard here to filter the EXPIRED state. However, I don't see any tests covering this. Should we add at least one? Is that the reason for the guard?

latch.await(5, TimeUnit.SECONDS)
lastActual = actual
if (actual == expectedResult) return
Thread.sleep(100)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

❓ Do you think we need 100ms? Looks like too much but just checking if there is a reason.

@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot deleted the marroz/RUM-15702-add-trace-sampling-decision-to-bridge branch June 8, 2026 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants