Skip to content

[DRAFT] ⚗️ Set view.in_foreground on all non-View RUM events#4266

Open
allspain wants to merge 1 commit intomainfrom
richard.klein/view-in-foreground-all-events
Open

[DRAFT] ⚗️ Set view.in_foreground on all non-View RUM events#4266
allspain wants to merge 1 commit intomainfrom
richard.klein/view-in-foreground-all-events

Conversation

@allspain
Copy link
Collaborator

@allspain allspain commented Mar 3, 2026

<!-- rfc-publish: richard.klein/view-in-foreground-all-events -->

RFC: Set view.in_foreground on All Non-View RUM Events

Status: Complete

Branch: richard.klein/view-in-foreground-all-events

Last updated: 2026-03-03

PR: PR #4266

Summary

This change extends the view.in_foreground flag from only Action and Error events to all non-View RUM event types: Resources, Long Tasks, Long Animation Frames, and Vitals. View events already have the more granular _dd.page_states / in_foreground_periods and are unaffected.

The change is minimal — a single condition flip in the Assemble hook, type additions, and schema updates — enabling teams to filter background vs foreground behavior across all event types without any new runtime cost.

Motivation

The view.in_foreground boolean was originally added for Action and Error events to support Session Replay use cases. Over time, other teams found it valuable for understanding background behavior — for example, filtering out resources fetched while the tab is hidden (background API polling), ignoring long tasks that fire in inactive tabs, or scoping vital measurements to foreground-only activity.

Currently, Resource, Long Task, and Vital events lack this field, forcing consumers to join event data with View page_states to determine foreground status — an expensive and error-prone operation at query time. Since the pageStateHistory module already tracks page visibility for every event's startTime, extending the flag to all non-View events is straightforward.

Jira: RUM-10673

Solution

Approach

Rather than adding each new event type to an allowlist (the previous pattern: eventType === ACTION || eventType === ERROR), the Assemble hook now uses a single negation: if the event is not a View, attach view.in_foreground. The View branch already handles its own _dd.page_states and returns early, so the else branch naturally covers all current and future non-View event types.

Architecture

The startPageStateHistory function in packages/rum-core/src/domain/contexts/pageStateHistory.ts registers an Assemble hook that enriches outgoing RUM events with page-state information. Previously, the hook had three code paths: View events got _dd.page_states, Action/Error events got view.in_foreground, and all other event types returned SKIPPED. After this change, the hook has two paths: View events get _dd.page_states, everything else gets view.in_foreground. The SKIPPED sentinel is no longer needed and its import has been removed.

The wasInPageStateDuringPeriod function — which checks whether the page was in PageState.ACTIVE at the event's startTime — is reused as-is. No new runtime logic was added.

Schema

The view.in_foreground field added to RawRumResourceEvent in packages/rum-core/src/rawRumEvent.types.ts:

view?: {
  in_foreground: boolean
}

The same view? block was added to RawRumLongTaskEvent, RawRumLongAnimationFrameEvent, and RawRumVitalEvent with the identical shape. This matches the existing pattern on RawRumActionEvent and RawRumErrorEvent.

The corresponding JSON schema property added to resource-schema.json, long_task-schema.json, and _vital-common-schema.json in rum-events-format:

"view": {
  "type": "object",
  "description": "View properties",
  "required": [],
  "properties": {
    "in_foreground": {
      "type": "boolean",
      "description": "Is the <event_type> starting in the foreground (focus in browser)",
      "readOnly": true
    }
  },
  "readOnly": true
}

This matches the existing view property in action-schema.json and error-schema.json.

Testing

Run yarn test:unit --spec packages/rum-core/src/domain/contexts/pageStateHistory.spec.ts to verify all 19 tests pass. The existing test loop at line 178 was expanded from [RumEventType.ACTION, RumEventType.ERROR] to include RumEventType.RESOURCE, RumEventType.LONG_TASK, and RumEventType.VITAL, so the same in_foreground: true / in_foreground: false assertions now cover all affected event types.

References

Extend view.in_foreground from Action/Error events to also cover
Resource, Long Task, and Vital events. This allows teams to filter
background vs foreground behavior across all event types.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@allspain allspain requested a review from a team as a code owner March 3, 2026 11:08
@github-actions
Copy link

github-actions bot commented Mar 3, 2026


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

@allspain allspain changed the title ⚗️ Set view.in_foreground on all non-View RUM events [DRAFT] ⚗️ Set view.in_foreground on all non-View RUM events Mar 3, 2026
@cit-pr-commenter-54b7da
Copy link

cit-pr-commenter-54b7da bot commented Mar 3, 2026

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 174.03 KiB 174.00 KiB -32 B -0.02%
Rum Profiler 4.71 KiB 4.71 KiB 0 B 0.00%
Rum Recorder 24.88 KiB 24.88 KiB 0 B 0.00%
Logs 56.54 KiB 56.54 KiB 0 B 0.00%
Flagging 944 B 944 B 0 B 0.00%
Rum Slim 129.73 KiB 129.71 KiB -18 B -0.01%
Worker 23.63 KiB 23.63 KiB 0 B 0.00%
🚀 CPU Performance
Action Name Base CPU Time (ms) Local CPU Time (ms) 𝚫%
addglobalcontext N/A 0.0039 N/A
addaction N/A 0.013 N/A
adderror N/A 0.0127 N/A
addtiming N/A 0.0028 N/A
startview N/A 0.0146 N/A
startstopsessionreplayrecording N/A 0.0008 N/A
logmessage N/A 0.0155 N/A
🧠 Memory Performance
Action Name Base Memory Consumption Local Memory Consumption 𝚫
addglobalcontext N/A 27.12 KiB N/A
addaction N/A 51.08 KiB N/A
addtiming N/A 26.27 KiB N/A
adderror N/A 55.23 KiB N/A
startstopsessionreplayrecording N/A 25.11 KiB N/A
startview N/A 450.15 KiB N/A
logmessage N/A 45.87 KiB N/A

🔗 RealWorld

@datadog-official
Copy link

datadog-official bot commented Mar 3, 2026

✅ Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage (details)
Patch Coverage: 100.00%
Overall Coverage: 77.20% (+0.02%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 12cd4a9 | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback!

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.

1 participant