Node.js: Filtering with "beforeSend" not having any effect

We’re attempting to use the beforeSend option to filter a cacophony of bogus errors coming from various scripts (ads, trackers, beacons) injected by our business partners.

However, I am not seeing any effect on our event load; all the matching events end up on the Sentry dashboard.

Unfortunately, while I’ve been able to verify that filtering in general works, I cannot consistently reproduce the errors we want to filter in the filter place. Many of them are AMP errors that I cannot manually trigger.

This is our logic:

import * as Sentry from '@sentry/browser';

Sentry.init({
  ...,
  beforeSend: (event, hint) => {
    let ignore = false;

    const err = hint && hint.originalException;
    if (err) {
      ignore = shouldIgnoreException(err.message) || shouldIgnoreException(err.stack);
    }
    if (!ignore) {
      const parsedStack = hint && hint.stack && hint.stack.stack;
      if (parsedStack) {
        ignore = parsedStack.find(s => shouldIgnoreException(s.url));
      }
    }

    if (ignore) {
      return null;
    }
    return event;
  }
});

const IGNORE = [
  /\/xsca\.[0-9.]+\.js\b/,
  /https:\/\/raw\.githubusercontent\.com\/ampproject\/amphtml\//,
  /\/instream\/video\/client\.js/,
  /\/ads\/scripts\/iiq_ev1\.js/,
  /\/vpaid\/units\/.*cmTagEXPANDABLE\.js/,
  /amp4ads-v0\.js/,
  /cmTagEXPANDABLE\.js/,
  /\/moatvideo\.js/,
  /\/load_tags\.js/,
  /Bootstrap's JavaScript requires jQuery/,
  /\/pageFold\/ftpagefold_/,
  /\/gtm\.js\b/,
  /\/metrics\.js\b/,
  /\/pagead\/expansion_embed\.js\b/,
  /\/js\/sdkloader\/ima3\.js\b/,
  /\/rq\/iv\/inside\.js\b/,
  /\/hearst\/news-3p\//,
  /should_do_lastpass_here/,
  /\/jquery\.min\.js\b/,
  /tags\.crwdcntrl\.net/,

  // Innocuous browser errors
  /ResizeObserver loop limit exceeded/,
  /ResizeObserver loop completed with undelivered notifications/,
  /Can't execute code from a freed script/,
  /\bmoz-extension:\/\//,

  // Caused by reCAPTCHA library
  /reCAPTCHA placeholder element must be an element or id/,

  // This one happens frequently, unknown source
  /UnhandledRejection: "Timeout"/
];

function shouldIgnoreException(s) {
  return s && IGNORE.find(pattern => pattern.test(s));
}

I’m guessing you’re using transactions for this?

Looks like they’ve specially disabled checking the beforeSend function if it’s a transition for some reason.

More specifically https://github.com/getsentry/sentry-javascript/blob/8eb72865dcd62ff719441105c7eda28007a07e9d/packages/core/src/baseclient.ts#L436

Thanks for explanation, does anybody know the reasons why the Transactions are deliberately filtered (ignored) for beforeSend() event? And is there any other way to intercept sending all transactions, to filter / debug them?

I have created the PR that enables filtering transactions via beforeSend function: Allow filtering Transactions in beforeSend callback function via separate option by MurzNN · Pull Request #3371 · getsentry/sentry-javascript · GitHub