Skip to content

(fix) tracker-js: apply error sampleRate the right way round - #615

Open
sarmah-rup wants to merge 1 commit into
Swetrix:mainfrom
sarmah-rup:fix/error-sample-rate
Open

sarmah-rup wants to merge 1 commit into
Swetrix:mainfrom
sarmah-rup:fix/error-sample-rate

Conversation

@sarmah-rup

@sarmah-rup sarmah-rup commented Oct 5, 2026 •

Copy link
Copy Markdown

Changes

Fixes #614.

captureError skipped an error when sampleRate >= Math.random(), so sampleRate: 1 (the default) dropped every automatically captured error and sampleRate: 0 sent all of them. I flipped the comparison so sampleRate is the share of errors that get sent, as the option's doc comment describes. No breaking changes, except that setups which relied on the inverted behaviour will now send the share they asked for.

Testing

Added packages/tracker-js/tests/errorSampling.test.ts. It uses the real Lib with Math.random stubbed and covers rates 1, 0 and 0.2. All 3 fail on main and pass with the fix. The full tracker-js suite passes (11 suites, 89 tests), and tsc --noEmit and prettier are clean.

AI assistance

Claude (Anthropic) helped me trace the bug and write the test. I reviewed the change and ran the tests myself.

Checklist

  • Database: no migrations are needed.
  • Edition coverage: the tracker script is shared, so this applies to both Cloud and Community Edition.
  • Documentation: no changes needed, the docs already describe the correct behaviour.

Summary by CodeRabbit

  • Bug Fixes
    • Error sampling now consistently honors the configured sample rate. Errors are sent when the random value is below the rate and dropped when it is equal to or above it. This clarifies the behavior at the sampling threshold, including when the rate is set to 0 or 1.

captureError skipped an error when sampleRate >= Math.random(), so
sampleRate: 1 dropped every error and sampleRate: 0 sent all of them.
Skip when Math.random() >= sampleRate instead, which matches the
documented meaning (share of errors to send).
@CLAassistant

CLAassistant commented Oct 5, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@coderabbitai

coderabbitai Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Review in Change Stack β†’

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. πŸŽ‰

ℹ️ Recent review info
βš™οΈ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e65bcb51-9ac8-4d71-84ba-41ef1ad352a5
πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 321a9ba and eee7091.

πŸ“’ Files selected for processing (2)
  • packages/tracker-js/src/Lib.ts
  • packages/tracker-js/tests/errorSampling.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.


πŸ“ Walkthrough

Walkthrough

captureError now drops errors when the random value is greater than or equal to sampleRate. New tests cover sample rates of 0, 0.2, and 1.

Changes

Error sampling

Layer / File(s) Summary
Sampling rule and tests
packages/tracker-js/src/Lib.ts, packages/tracker-js/tests/errorSampling.test.ts
captureError now drops an error when the random value is greater than or equal to sampleRate. Tests check that rates of 0 and 1 send no errors and all errors, respectively, and that a rate of 0.2 sends at 0.1 but not at 0.9.

Priority: βž– Normal

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix Β· Severity of issue fixed: Medium

Suggested reviewers: blaumaus

Merge Risk: βšͺ Minimal Β· up to eee70

Automatic error sampling now follows the configured rate. No merge-blocking issue is identified.

Security Architecture Review

Security architecture risk: πŸ”΅ Low Β· up to eee70

The fix restores the requested error sampling behavior without changing payload fields, destinations, or callback vetoes. Explicit high sampling rates can now transmit substantially more error data. No introduced vulnerability was established, but server-side handling was not verified.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred β€” The affected client-side scope is error telemetry for configured Lib instances using numeric sampling rates. Higher admitted shares increase transmission of existing error content to the configured endpoint, without adding payload categories or credentials. Receiver-side isolation and effective downstream exposure were not verified.

Trust Boundaries and Controls

  • observed β€” Automatic listener registration still checks disabled tracking, browser context, configured DNT respect, localhost restrictions, and automation detection. Direct captureError calls do not independently repeat that gate, but this predates the PR. Transport still selects the configured apiURL or default host.
πŸš₯ Pre-merge checks | βœ… 5
βœ… Passed checks (5 passed)
Check name Status Explanation
Title check βœ… Passed The title clearly identifies the tracker-js error sampleRate fix.
Description check βœ… Passed The description includes all required sections. It explains the change, reports test results, identifies AI assistance, and completes the checklist with relevant explanations.
Linked Issues check βœ… Passed Issue #614 requires sampleRate to be the share of automatically captured errors sent, with 1 sending all, 0 sending none, and intermediate rates sampling accordingly. The change makes captureError…
Out of Scope Changes check βœ… Passed The supplied whole-PR summary identifies a one-line change in Lib.captureError and tests for the sampling behavior in issue #614. Both changes support the linked issue. The summary shows no unrelate…
Docstring Coverage βœ… Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • Create a new PR
  • Autopilot Β· Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
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.

bug: trackErrors sampleRate is inverted, sampleRate: 1 sends no errors

2 participants