Skip to content

[RUM-15525] Add sessionSampleRate configuration option#139

Merged
cdn34dd merged 5 commits into
mainfrom
carlosnogueira/RUM-15525/sample-sessions
Jun 23, 2026
Merged

[RUM-15525] Add sessionSampleRate configuration option#139
cdn34dd merged 5 commits into
mainfrom
carlosnogueira/RUM-15525/sample-sessions

Conversation

@cdn34dd

@cdn34dd cdn34dd commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

Motivation

Adds support for client-side session sampling in the Electron SDK. Without this, every session is always collected. Implements sessionSampleRate (0–100) analogous to the other SDKs.

Tracked in RUM-15525.

Changes

New sessionSampleRate configuration option (src/config.ts)

  • Optional in InitConfiguration, required (defaults to 100) in the resolved Configuration.
  • Validated the same way as telemetrySampleRate — invalid values log an error and the validator returns undefined, which aborts init() (no configuration is built, init() returns false).

Deterministic sampler (src/tools/Sampler.ts)

  • isSessionSampled uses the Knuth-factor consistent-sampling method, aligned with the Datadog cross-language spec (and the browser-core implementation): (uuid_node * knuthFactor) % 2^64 compared against the rate, using BigInt to avoid 64-bit overflow.
  • Same session ID always produces the same decision — intentional, to enable future replay correlation when sessionReplaySampleRate is added.
  • Fractional rates (e.g. 0.1) are supported.
  • If the session ID doesn't match the expected UUID format, the session is dropped (not sampled).
  • Also adds correctedChildSampleRate(parentRate, childRate) ported from browser-core, in preparation for the upcoming Session Replay PR. It is currently unused (no caller yet) but lands here since the SR PR follows immediately.

Session sampling propagation (src/domain/session/SessionManager.ts)

  • isSampled is computed once at session creation via isSessionSampled(id, sessionSampleRate).
  • A sampled session is recorded in the session history; a non-sampled session is simply never added. No on-disk format change is required, so existing session-history files remain fully compatible — no migration needed.

Hook behavior for non-sampled sessions (src/domain/session/SessionContext.ts)

  • Because a non-sampled session is absent from the history, all three hooks miss on it: RUM and span events return DISCARDED, and telemetry returns SKIPPED (telemetry still sends, without session attribution). SessionContext also exposes getActiveSessionId() (see below); remaining changes are a cosmetic local rename.

Internal context (src/index.ts, src/domain/session/SessionManager.ts, src/domain/session/SessionContext.ts)

  • getInternalContext() now returns undefined for any session that isn't currently tracked — including non-sampled (and expired) sessions — instead of always returning the active session's id. This prevents leaking a session_id (used for cross-product correlation) for a session that produces no RUM.
  • Backed by a single source of truth: SessionContext.getActiveSessionId() resolves the session from the same history the RUM/span/telemetry hooks consult, so internal context stays consistent with what those hooks attribute (a non-sampled session is absent; an expired one is closed).

Test instructions

Checklist

  • Tested locally (playground)
  • Added unit tests for this change.
  • Added e2e/integration tests for this change.
  • Updated related documentation.

@cdn34dd cdn34dd requested a review from a team as a code owner June 17, 2026 15:33

@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: 7346c3fd45

ℹ️ 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 thread src/config.ts Outdated
Comment thread src/tools/Sampler.ts Outdated
Comment thread src/config.ts Outdated
Comment thread src/tools/Sampler.ts Outdated
Comment thread src/domain/session/SessionContext.ts Outdated
Comment thread src/domain/session/SessionManager.ts Outdated
@cdn34dd cdn34dd force-pushed the carlosnogueira/RUM-15525/sample-sessions branch from 7346c3f to 292d50b Compare June 18, 2026 14:54
bcaudan added a commit that referenced this pull request Jun 19, 2026
…Rate

Cherry-picked from #139.
Drop this commit once PR#139 lands on main.
Comment thread src/tools/DiskValueHistory.ts Outdated
Comment thread src/domain/session/SessionManager.spec.ts
Comment thread src/domain/session/SessionManager.ts
Comment thread src/tools/Sampler.ts
Copilot AI review requested due to automatic review settings June 19, 2026 15:12
@cdn34dd cdn34dd force-pushed the carlosnogueira/RUM-15525/sample-sessions branch from 292d50b to bc009dc Compare June 19, 2026 15:12

Copilot AI 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.

Pull request overview

Adds client-side session sampling to the Electron SDK via a new sessionSampleRate config option and deterministic sampling based on the session UUID, with propagation into session lifecycle handling and unit tests.

Changes:

  • Added sessionSampleRate to configuration and validation logic.
  • Introduced a deterministic sampler utility (isSessionSampled) and unit tests for sampling behavior.
  • Wired session sampling into session creation so non-sampled sessions are discarded from RUM attribution.

Architectural Flow

buildConfiguration() resolves sessionSampleRateinit() passes resolved config into SessionManager.start()SessionManager.createNewSession() generates a UUID and makes a deterministic sampling decision → session history (SessionContext/DiskValueHistory) is updated to drive hook enrichment (RUM/telemetry/spans) based on history lookups.

Score

2.6 / 5.0

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
src/tools/Sampler.ts Adds deterministic session sampling + helper rate math.
src/tools/Sampler.spec.ts Adds unit tests for sampler determinism and boundary conditions.
src/tools/DiskValueHistory.ts Minor typing/cleanup in disk history restore logic.
src/mocks.specUtil.ts Updates test configuration helper with sessionSampleRate.
src/index.ts Passes resolved configuration into SessionManager.start().
src/domain/session/SessionManager.ts Computes sampling decision on session creation and gates session history updates.
src/domain/session/SessionManager.spec.ts Updates SessionManager tests and adds sampling-related scenarios.
src/domain/session/SessionContext.ts Minor refactor of hook code (variable naming).
src/config.ts Adds sessionSampleRate and updates sample-rate validation/build logic.
src/config.spec.ts Adds config validation tests for sessionSampleRate and updates telemetry sample-rate invalid cases.

Comment thread src/tools/Sampler.ts
Comment thread src/config.ts
Comment thread src/config.ts
Comment thread src/config.spec.ts
Comment thread src/config.spec.ts
Comment thread src/domain/session/SessionManager.ts
Comment thread src/domain/session/SessionManager.ts
Comment thread src/domain/session/SessionManager.ts
Comment thread src/domain/session/SessionContext.ts
@cdn34dd cdn34dd requested a review from bcaudan June 19, 2026 15:41
@cdn34dd cdn34dd force-pushed the carlosnogueira/RUM-15525/sample-sessions branch from bc009dc to 8f3aa3b Compare June 19, 2026 16:25
bcaudan
bcaudan previously approved these changes Jun 22, 2026
@bcaudan

bcaudan commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

It could be nice to document the new option in the README.md

cdn34dd added 5 commits June 23, 2026 08:00
- getInternalContext() returned the active session's id regardless of
sampling, so a non-sampled session (which produces no RUM) could still
leak a session_id into cross-product correlation — pointing consumers at
a RUM session that was never ingested.

Back getInternalContext() with a single source of truth: SessionContext
now exposes getActiveSessionId(), which looks the session up in the same
history the RUM/span/telemetry hooks consult. A non-sampled session is
absent (never added) and an expired one is closed, so both resolve to
undefined,  keeping internal context consistent with what the hooks
actually attribute. Removes the parallel status/isSampled check on the
Session object.
Copilot AI review requested due to automatic review settings June 23, 2026 07:07
@cdn34dd cdn34dd force-pushed the carlosnogueira/RUM-15525/sample-sessions branch from 8f3aa3b to ebddc93 Compare June 23, 2026 07:07
@datadog-datadog-prod-us1

This comment has been minimized.

Copilot AI 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.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.

Comment thread src/tools/Sampler.ts
Comment thread src/tools/Sampler.ts
Comment thread src/tools/Sampler.ts
@cdn34dd cdn34dd changed the title Add sessionSampleRate configuration option [RUM-15525] Add sessionSampleRate configuration option Jun 23, 2026
@cdn34dd cdn34dd merged commit 3bef7e9 into main Jun 23, 2026
16 checks passed
@cdn34dd cdn34dd deleted the carlosnogueira/RUM-15525/sample-sessions branch June 23, 2026 07:36
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.

3 participants