Skip to main content

[JavaScript] How do I filter events based on the entire stack trace?

Issue

I set denyUrls (or allowUrls) to drop events by script URL, but matching frames deeper in the stack still reach Sentry. Those options only check the top stack frame, so the event is sent anyway.

Applies To

  • All SaaS Customers and Self-Hosted Users

  • JavaScript browser SDKs

  • denyUrls, allowUrls, and beforeSend

  • Error Monitoring

  • Filtering Events

Resolution

denyUrls and allowUrls only match the top stack frame’s file URL. To drop an event when any frame matches a pattern, use beforeSend and inspect every frame in the stack trace. Return null to drop the event.

  1. Add a beforeSend callback in Sentry.init.

  2. Read event.exception.values[0].stacktrace.frames (guard for missing data).

  3. If any frame’s filename matches your pattern, return null. Otherwise return the event.

Sentry.init({
  dsn: "___PUBLIC_DSN___",
  beforeSend(event) {
    const frames = event.exception?.values?.[0]?.stacktrace?.frames;
    if (!frames) {
      return event;
    }    for (const frame of frames) {
      if (frame.filename && /chrome-extension/i.test(frame.filename)) {
        return null;
      }
    }    return event;
  },
});

Did this answer your question?