Skip to content

RUM-386: Process feature context on the context thread#2704

Merged
0xnm merged 1 commit into
nogorodnikov/rum-8170/feature-context-syncfrom
nogorodnikov/rum-386/process-feature-context-on-context-thread
Jun 11, 2025
Merged

RUM-386: Process feature context on the context thread#2704
0xnm merged 1 commit into
nogorodnikov/rum-8170/feature-context-syncfrom
nogorodnikov/rum-386/process-feature-context-on-context-thread

Conversation

@0xnm

@0xnm 0xnm commented Jun 5, 2025

Copy link
Copy Markdown
Member

What does this PR do?

This PR changes the way feature context is read and updated. Now, by default, all calls to update feature context or to read it will delegate to the context processing thread, this will guarantee that sequence of the updates is corresponding to the sequence of the SDK API calls causing it.

There is also a way to update/read feature context from the caller thread, because sometimes calls are already done from the context thread (and sometimes we are ok doing such calls not from the context thread).

Since feature context is becoming a shared resource, it is guarded by the locks (write lock and read lock, read more about ReentrantReadWriteLock).

To avoid deadlock, write lock has a timeout during which it will try to acquire lock. If it is not possible to do during the timeout, update operation will be aborted.

This change now guarantees that, for example, there is the following API sequence:

--> RUM Context A
--> call to RUM updating RUM Context to Context B
--> logs/traces call

Then there won't be a situation when Logs/Traces read Context A instead of Context B just because they are processed faster than RUM.

This invariant is covered by the integration test.

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)

@codecov-commenter

codecov-commenter commented Jun 5, 2025

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 68.29268% with 39 lines in your changes missing coverage. Please review.

Project coverage is 69.90%. Comparing base (d4c9984) to head (bb9a9a8).

Files with missing lines Patch % Lines
...n/com/datadog/android/core/internal/DatadogCore.kt 59.76% 27 Missing and 6 partials ⚠️
.../android/rum/internal/monitor/DatadogRumMonitor.kt 90.00% 1 Missing and 1 partial ⚠️
.../com/datadog/android/api/feature/FeatureSdkCore.kt 50.00% 1 Missing ⚠️
...og/android/core/internal/DatadogContextProvider.kt 85.71% 0 Missing and 1 partial ⚠️
...tadog/android/core/internal/NoOpInternalSdkCore.kt 0.00% 1 Missing ⚠️
...in/com/datadog/android/core/internal/SdkFeature.kt 83.33% 1 Missing ⚠️
Additional details and impacted files
@@                              Coverage Diff                               @@
##           nogorodnikov/rum-8170/feature-context-sync    #2704      +/-   ##
==============================================================================
- Coverage                                       69.98%   69.90%   -0.09%     
==============================================================================
  Files                                             816      817       +1     
  Lines                                           30662    30721      +59     
  Branches                                         5146     5149       +3     
==============================================================================
+ Hits                                            21458    21473      +15     
- Misses                                           7768     7808      +40     
- Partials                                         1436     1440       +4     
Files with missing lines Coverage Δ
...n/com/datadog/android/core/internal/CoreFeature.kt 87.64% <ø> (-0.18%) ⬇️
...tadog/android/core/internal/NoOpContextProvider.kt 2.56% <ø> (+0.06%) ⬆️
...adog/android/core/internal/utils/ConcurrencyExt.kt 86.21% <ø> (ø)
...oid/sessionreplay/internal/SessionReplayFeature.kt 98.26% <100.00%> (-0.58%) ⬇️
...sionreplay/internal/SessionReplayRecordCallback.kt 100.00% <100.00%> (ø)
.../android/trace/opentelemetry/OtelTracerProvider.kt 95.49% <100.00%> (ø)
...lin/com/datadog/android/webview/WebViewTracking.kt 85.09% <100.00%> (ø)
...datadog/android/okhttp/trace/TracingInterceptor.kt 75.48% <100.00%> (+0.28%) ⬆️
.../com/datadog/android/api/feature/FeatureSdkCore.kt 50.00% <50.00%> (ø)
...og/android/core/internal/DatadogContextProvider.kt 97.73% <85.71%> (-2.27%) ⬇️
... and 4 more

... and 43 files with indirect coverage changes

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

@0xnm
0xnm force-pushed the nogorodnikov/rum-386/process-feature-context-on-context-thread branch 2 times, most recently from 52bda93 to 7536927 Compare June 6, 2025 06:34
@0xnm
0xnm marked this pull request as ready for review June 6, 2025 07:18
@0xnm
0xnm requested review from a team as code owners June 6, 2025 07:18
contextProvider = DatadogContextProvider(coreFeature) {
features.keys.map {
it to {
// already on the context thread

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.

Just a suggestion. No change needed.

For these things it might be worth having some sort of assertIsContextThread() functions that crash if it is not the right thread. And make them crash only in our sample app.

We could put these in many places.

I see that we currently have isDeveloperModeEnabled. But this could be used by the customer setUseDeveloperModeWhenDebuggable.

You might also want to crash in debug mode here instead of logging. But if we have some kind of monitors on these logs in our sample, it is fine.

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.

instead of logging

in addition to logging

@0xnm
0xnm force-pushed the nogorodnikov/rum-386/process-feature-context-on-context-thread branch from 7536927 to 6d10fd8 Compare June 6, 2025 09:26
@0xnm
0xnm force-pushed the nogorodnikov/rum-386/process-feature-context-on-context-thread branch from 6d10fd8 to bb9a9a8 Compare June 6, 2025 09:49

@mariusc83 mariusc83 left a comment

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.

LGTM

@0xnm
0xnm merged commit 28116bd into nogorodnikov/rum-8170/feature-context-sync Jun 11, 2025
@0xnm
0xnm deleted the nogorodnikov/rum-386/process-feature-context-on-context-thread branch June 11, 2025 09:29
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