Skip to content

✨ [RUM-10144] apply context defined just after init to View event generated during init - #3597

Merged
BenoitZugmeyer merged 10 commits into
mainfrom
benoit/delay-initial-view
Aug 13, 2026
Merged

✨ [RUM-10144] apply context defined just after init to View event generated during init#3597
BenoitZugmeyer merged 10 commits into
mainfrom
benoit/delay-initial-view

Conversation

@BenoitZugmeyer

@BenoitZugmeyer BenoitZugmeyer commented Jun 4, 2025

Copy link
Copy Markdown
Member

Motivation

Usually, context (global/user/account/feature flags etc.) is defined just after calling RUM.init(), which is an issue because events that are generated during init() will not have this context.

This is only problematic for View events: RUM.init() generates a first View event that is missing the context defined right after. It is usually fine, as we can expect a RUM View Update to be generated at some point later. But sometimes no View Update happen for some reason (connectivity error, browser exiting abruptly...). In those cases, it is unexpected that the View isn't including the context.

Fixes #3935

Changes

This PR fixes this issue by slightly delaying the first View update. It uses Promise.resolve() to schedule a view update in the next micro task. It should not impact reliability as using Promise.resolve().then() to schedule a micro task is not impacted by timers throttling.

Please review commit by commit.

Test instructions

In the sandbox, define a user just after init():

      DD_RUM.init({
        clientToken: 'xxx',
        applicationId: 'xxx',
        ...
      })
      DD_RUM.setUser({ id: 'foo' })

Using the devtools extension, look at individual View events by unchecking Show only the latest View event in the "Events" tab.

When loading the sandbox URL, all View events should contain the @usr.id, even the very first View.

Checklist

  • Tested locally
  • Tested on staging
  • Added unit tests for this change.
  • Added e2e/integration tests for this change.

@BenoitZugmeyer
BenoitZugmeyer force-pushed the benoit/delay-initial-view branch 3 times, most recently from e28ba32 to 2b6121e Compare June 4, 2025 15:45
@codecov-commenter

codecov-commenter commented Jun 4, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.39%. Comparing base (b3ee2b8) to head (2b6121e).
⚠️ Report is 587 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3597      +/-   ##
==========================================
+ Coverage   92.37%   92.39%   +0.01%     
==========================================
  Files         323      323              
  Lines        8135     8148      +13     
  Branches     1841     1842       +1     
==========================================
+ Hits         7515     7528      +13     
  Misses        620      620              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@cit-pr-commenter

cit-pr-commenter Bot commented Jun 4, 2025

Copy link
Copy Markdown

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 180.58 KiB 180.68 KiB +98 B +0.05%
Rum Profiler 8.43 KiB 8.43 KiB 0 B 0.00%
Rum Recorder 21.12 KiB 21.12 KiB 0 B 0.00%
Logs 57.03 KiB 57.03 KiB 0 B 0.00%
Rum Salesforce N/A 138.49 KiB N/A N/A N/A
Rum Slim 138.46 KiB 138.48 KiB +18 B +0.01%
Worker 22.96 KiB 22.96 KiB 0 B 0.00%
Rum Shopify N/A 200.25 KiB N/A N/A N/A
Rum-shopify Profiler N/A 8.43 KiB N/A N/A N/A
Rum-shopify Recorder N/A 3.72 KiB N/A N/A N/A

@BenoitZugmeyer

Copy link
Copy Markdown
Member Author

/to-staging

@dd-devflow

dd-devflow Bot commented Jun 4, 2025

Copy link
Copy Markdown
Contributor

View all feedbacks in Devflow UI.

2025-06-04 16:03:38 UTC ℹ️ Start processing command /to-staging


2025-06-04 16:03:48 UTC ℹ️ Branch Integration: starting soon, merge expected in approximately 17m43s (p90)

Commit 2b6121e26e will soon be integrated into staging-23.


2025-06-04 16:15:46 UTC ℹ️ Branch Integration: This commit was successfully integrated

Commit 2b6121e26e has been merged into staging-23 in merge commit 6826f9d5e7.

Check out the triggered pipeline on Gitlab 🦊

If you need to revert this integration, you can use the following command: /code revert-integration -b staging-23

dd-mergequeue Bot added a commit that referenced this pull request Jun 4, 2025
Integrated commit sha: 2b6121e

Co-authored-by: BenoitZugmeyer <benoit.zugmeyer@datadoghq.com>
@dd-devflow dd-devflow Bot added the staging-23 label Jun 4, 2025
@BenoitZugmeyer
BenoitZugmeyer force-pushed the benoit/delay-initial-view branch from 2b6121e to bbf879f Compare August 10, 2026 13:59
@datadog-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Aug 10, 2026

Copy link
Copy Markdown

Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
Patch Coverage: 90.91%
Overall Coverage: 77.21% (+0.01%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 1f18c98 | Docs | Datadog PR Page | Give us feedback!

Safari processes buffered PerformanceObserver resource entries when the mock clock ticks, so assert only on view events.
@BenoitZugmeyer

Copy link
Copy Markdown
Member Author

@codex review

@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: 303932c962

ℹ️ 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 +291 to +293
if (documentVersion === 0) {
triggerViewUpdate()
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve the deferred refresh after synchronous view updates

issue: When another API emits the active view synchronously before this microtask—most notably setViewName()—the documentVersion === 0 guard suppresses the deferred update. For example, init(); setViewName('checkout'); setUser({ id: '123' }) sends the first view before the user is set and then may never send a corrected view because changing user/global/feature-flag context does not itself schedule one. The deferred callback should only be skipped when the view has ended, or otherwise still emit an update that incorporates the rest of the same-task context calls.

Useful? React with 👍 / 👎.

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.

It's probably fine, setViewName usage is a bit niche, and fixing makes things a bit more complex: it means introducing yet another lifecycle event to signal that a view is about to be updated, so we can the new view name without waiting for the deferred promise.

@BenoitZugmeyer
BenoitZugmeyer marked this pull request as ready for review August 11, 2026 08:53
@BenoitZugmeyer
BenoitZugmeyer requested a review from a team as a code owner August 11, 2026 08:53
@sbarrio
sbarrio requested a review from thomas-lebeau August 12, 2026 07:06
Comment thread packages/browser-rum-core/src/domain/view/trackViews.ts
Comment thread packages/browser-core/test/emulate/mockClock.ts
@BenoitZugmeyer
BenoitZugmeyer force-pushed the benoit/delay-initial-view branch from 85688ca to 1f18c98 Compare August 13, 2026 15:44
@BenoitZugmeyer
BenoitZugmeyer merged commit 0b06411 into main Aug 13, 2026
31 checks passed
@BenoitZugmeyer
BenoitZugmeyer deleted the benoit/delay-initial-view branch August 13, 2026 16:43
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Initial version of RUM view event is missing global context

4 participants