Skip to content

beforeErrorSampling does not respect sampling based on event levelΒ #17873

@InterstellarStella

Description

@InterstellarStella

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/react

SDK Version

"@sentry/react": "^9.46.0"

Framework Version

"react": "^19.1.0"

Link to Sentry event

https://dragonfruit-us.sentry.io/issues/6928059664/?project=4504179109068800&query=is%3Aunresolved&referrer=issue-stream

Reproduction Example/SDK Setup

In instrument.js:

// ...
Sentry.replayIntegration({
      beforeErrorSampling: (event) => {
        console.log("beforeErrorSampling", event);
        if (event.level === "error") {
          console.log("Capturing replay for error");
          return event;
        }
        console.log("Not capturing replay because level is not error");
        return null;
      },
    }),
// ...
replaysSessionSampleRate: 0.0,
replaysOnErrorSampleRate: 1.0,

In App.jsx:

 <button
          type="button"
          onClick={() => {
            Sentry.withScope(function (scope) {
              scope.setLevel("info");
              // The exception has the event level set by the scope (info).
              Sentry.captureException(new Error("custom error"));
            });
          }}
        >
          Send info
        </button>

Steps to Reproduce

  1. Set up a beforeErrorSampling that decides when a replay should be recorded based on the event level, making sure that the replay session sampling rate is 0
  2. Using the scope, change the event level to one that should not send a replay to Sentry based on your logic
  3. Trigger an event that should not have a replay attached to it

Expected Result

The event arrives in Sentry without a replay.

Actual Result

The event arrives in Sentry with a replay.
The above is a reproduction from my test app and org. Original customer ticket here.

Additional Context

Tip: React with πŸ‘ to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions