Skip to content

Conditional SDK Module Loading Based on Sample Rates #14260

@smeubank

Description

@smeubank

Problem Statement

Currently, the Sentry SDK loads all configured modules (error tracking, performance monitoring, session replay, etc.) regardless of their sample rates. This means users who will never generate certain types of events due to sampling still have to download the associated code, increasing bundle size unnecessarily.

For high-traffic applications where sampling rates are very low (e.g., 1% for errors, 0.01% for replays), this creates unnecessary overhead for users who will not have these events sent to Sentry.

Current Behavior

  • All SDK modules are loaded upfront
  • Bundle size impact affects all users regardless of whether they'll send events
  • Sample rates only determine if events are sent, not if code is loaded
    • this is the currently the intent of those sample rate configs, so i wouldn't interpret that as negative
  • Sampling decisions can be made dynamically during the session based on event attributes

Desired Outcome

A stripped down "SDK" that:

  1. Loads only a minimal core module initially
  2. Determines if a user/session falls within "some" sampling rates
  3. Dynamically imports additional modules (tracing, replay, etc.) only when needed
  4. Reduces bundle size impact for users outside sampling thresholds

Proposed Solution

Architecture Changes

  1. Create a lightweight core module that:

    • Handles initialization and sampling decisions
    • Contains minimal error tracking capabilities
    • Manages dynamic imports of additional features
  2. Split current functionality into separate loadable chunks:

    • Performance monitoring
    • Session replay
    • User feedback
    • etc.
  3. Implement dynamic loading logic

Potential Tradeoffs and Limitations

  1. Loss of Dynamic/Functional Sampling Capabilities:

  2. Timing Considerations:

    • Initial sampling decision must be made before knowing full context
  3. Implementation Complexity:

    • Need to carefully manage module loading states
    • Must handle race conditions between dynamic imports and event generation
    • Additional complexity in managing module dependencies (should be handled already since tree shaking requires that the bundles can be stripped down already)

Discussion Points

  • would there be a preload sample rate config, additionally?

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions