RUM-15702 Add trace sampling decision to DatadogEventBridge#3495
Conversation
ca5e54f to
4ebe836
Compare
This comment has been minimized.
This comment has been minimized.
Codecov Report❌ Patch coverage is
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
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
💡 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".
| val rumContext = sdkCore.getFeatureContext(Feature.RUM_FEATURE_NAME, useContextThread = false) | ||
| val tracingContext = sdkCore.getFeatureContext(Feature.TRACING_FEATURE_NAME, useContextThread = false) |
There was a problem hiding this comment.
useContextThread = false, getFeatureContext acquires a read lock (
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?
There was a problem hiding this comment.
I agree, I don't think it is a best approach.
WDYT about implementing FeatureContextUpdateReceiver maybe?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Addressed this by using WebViewRumFeature as the receiver.
44a85fb to
3990aeb
Compare
| val tracingContext = webViewRumFeature?.cachedTracingContext.orEmpty() | ||
|
|
||
| val sessionId = (rumContext[SESSION_ID_KEY] as? String) | ||
| ?.takeIf { rumContext[SESSION_STATE_KEY] == TRACKED_STATE } |
There was a problem hiding this comment.
❓ 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) |
There was a problem hiding this comment.
❓ Do you think we need 100ms? Looks like too much but just checking if there is a reason.
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)