Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

fix(sentry): drop errors from third-party and injected scripts (#13304)

Vinicius Lourençof15570fadd7d
diff --git a/frontend/src/AppRoutes/__tests__/sentryFilters.test.ts b/frontend/src/AppRoutes/__tests__/sentryFilters.test.ts
new file mode 100644
index 0000000000..a95268f5df
--- /dev/null
+++ b/frontend/src/AppRoutes/__tests__/sentryFilters.test.ts
@@ -0,0 +1,140 @@
+import type { ErrorEvent, StackFrame } from '@sentry/react';
+
+import {
+ hasNoAppFrames,
+ isExternalIntegrationError,
+ SENTRY_IGNORE_ERRORS,
+} from '../sentryFilters';
+
+const APP_BUNDLE = 'https://example.us.signoz.cloud/assets/index-B3QqK3ie.js';
+
+function eventWithFrames(...filenames: string[]): ErrorEvent {
+ return {
+ type: undefined,
+ exception: {
+ values: [
+ {
+ type: 'TypeError',
+ value: 'Failed to fetch',
+ stacktrace: {
+ frames: filenames.map((filename) => ({ filename, lineno: 1, colno: 1 })),
+ },
+ },
+ ],
+ },
+ };
+}
+
+describe('isExternalIntegrationError', () => {
+ it.each([
+ [
+ 'PostHog recorder',
+ 'https://us-assets.i.posthog.com/static/posthog-recorder.js?v=1.298.0',
+ ],
+ [
+ 'Pylon widget',
+ 'https://widget.usepylon.com/widget/00000000-0000-0000-0000-000000000000',
+ ],
+ ['Appcues', 'https://fast.appcues.com/123456.js'],
+ ])('drops an error with a %s frame', (_, url) => {
+ expect(isExternalIntegrationError(eventWithFrames(APP_BUNDLE, url))).toBe(
+ true,
+ );
+ });
+
+ it('drops it when the top frame is our bundle', () => {
+ expect(
+ isExternalIntegrationError(
+ eventWithFrames(
+ 'https://us-assets.i.posthog.com/static/posthog-recorder.js?v=1.298.0',
+ '<anonymous>',
+ APP_BUNDLE,
+ ),
+ ),
+ ).toBe(true);
+ });
+
+ it('keeps an error from our bundle only', () => {
+ expect(
+ isExternalIntegrationError(eventWithFrames(APP_BUNDLE, '<anonymous>')),
+ ).toBe(false);
+ });
+
+ it('keeps an error from a host that only contains an integration name', () => {
+ expect(
+ isExternalIntegrationError(
+ eventWithFrames(
+ 'https://posthog.example.com/app.js',
+ 'https://notusepylon.com/widget.js',
+ ),
+ ),
+ ).toBe(false);
+ });
+
+ it('keeps an error without a stack trace', () => {
+ expect(isExternalIntegrationError({ type: undefined })).toBe(false);
+ });
+});
+
+describe('hasNoAppFrames', () => {
+ const PAGE_URL = 'https://example.us.signoz.cloud/dashboard/01a081e1';
+
+ function eventWithStackFrames(frames: StackFrame[]): ErrorEvent {
+ return {
+ type: undefined,
+ exception: {
+ values: [{ type: 'SyntaxError', stacktrace: { frames } }],
+ },
+ };
+ }
+
+ it('drops a script appended to the page', () => {
+ expect(
+ hasNoAppFrames(
+ eventWithStackFrames([
+ { filename: PAGE_URL, lineno: 1, colno: 30, function: 'global code' },
+ { filename: '[native code]', function: 'eval' },
+ { filename: '[native code]', function: 'append' },
+ ]),
+ ),
+ ).toBe(true);
+ });
+
+ it('drops a browser extension error', () => {
+ expect(
+ hasNoAppFrames(
+ eventWithStackFrames([
+ { filename: 'chrome-extension://abc/content.js', lineno: 4, colno: 12 },
+ ]),
+ ),
+ ).toBe(true);
+ });
+
+ it('keeps an error with a frame from our bundle', () => {
+ expect(hasNoAppFrames(eventWithFrames(PAGE_URL, APP_BUNDLE))).toBe(false);
+ });
+
+ it('keeps an error without a stack trace', () => {
+ expect(hasNoAppFrames({ type: undefined })).toBe(false);
+ });
+
+ it('keeps an error whose frames have no position', () => {
+ expect(
+ hasNoAppFrames(
+ eventWithStackFrames([{ filename: '[native code]', function: 'eval' }]),
+ ),
+ ).toBe(false);
+ });
+});
+
+describe('SENTRY_IGNORE_ERRORS', () => {
+ it('ignores the bundled PostHog recorder load failure', () => {
+ expect(
+ SENTRY_IGNORE_ERRORS.some((pattern) =>
+ pattern.test(
+ 'Error: Called on script loaded before session recording is available',
+ ),
+ ),
+ ).toBe(true);
+ });
+});
diff --git a/frontend/src/AppRoutes/index.tsx b/frontend/src/AppRoutes/index.tsx
index 706da4dc2b..be071978c9 100644
--- a/frontend/src/AppRoutes/index.tsx
+++ b/frontend/src/AppRoutes/index.tsx
@@ -22,6 +22,7 @@ import { ChatSupportState, useChatSupport } from 'hooks/useChatSupport';
import { useIsDarkMode } from 'hooks/useDarkMode';
import { useGetTenantLicense } from 'hooks/useGetTenantLicense';
import { StatusCodes } from 'http-status-codes';
+import { IS_PROD } from 'lib/env';
import history from 'lib/history';
import ErrorBoundaryFallback from 'pages/ErrorBoundaryFallback/ErrorBoundaryFallback';
import posthog from 'posthog-js';
@@ -37,6 +38,11 @@ import defaultRoutes, {
LIST_LICENSES,
SUPPORT_ROUTE,
} from './routes';
+import {
+ hasNoAppFrames,
+ isExternalIntegrationError,
+ SENTRY_IGNORE_ERRORS,
+} from './sentryFilters';
const appRouter = (children: ReactNode): ReactNode => (
<Router history={history}>
@@ -310,16 +316,7 @@ function App(): JSX.Element {
tunnel: window.signozBootData.settings.sentry.tunnel,
environment: process.env.ENVIRONMENT,
release: process.env.VERSION,
- // A tab that outlived a deploy requests hashed assets the new build no longer
- // has. `lazyRetry` recovers by reloading once, so this class is not worth
- // reporting. The stylesheet message is Vite's own; the module ones are the
- // same failure worded differently by Chromium, Firefox and Safari.
- ignoreErrors: [
- /Unable to preload CSS for/,
- /Failed to fetch dynamically imported module/,
- /error loading dynamically imported module/,
- /Importing a module script failed/,
- ],
+ ignoreErrors: SENTRY_IGNORE_ERRORS,
integrations: [
// Kept for the `transaction` tag used in routing, even though
// tracing is disabled. Ref: https://github.com/SigNoz/platform-pod/issues/2393#issuecomment-4603658055
@@ -354,6 +351,15 @@ function App(): JSX.Element {
return null;
}
+ if (isExternalIntegrationError(event)) {
+ return null;
+ }
+
+ // Dev serves source from /src/, so every frame would look foreign there.
+ if (IS_PROD && hasNoAppFrames(event)) {
+ return null;
+ }
+
const sessionReplayUrl = posthog.get_session_replay_url?.({
withTimestamp: true,
});
diff --git a/frontend/src/AppRoutes/sentryFilters.ts b/frontend/src/AppRoutes/sentryFilters.ts
new file mode 100644
index 0000000000..68e1cfdbd8
--- /dev/null
+++ b/frontend/src/AppRoutes/sentryFilters.ts
@@ -0,0 +1,49 @@
+import type { ErrorEvent } from '@sentry/react';
+
+export const SENTRY_IGNORE_ERRORS = [
+ // A tab that outlived a deploy requests hashed assets the new build no longer
+ // has. `lazyRetry` recovers by reloading once, so this class is not worth
+ // reporting. The stylesheet message is Vite's own; the module ones are the
+ // same failure worded differently by Chromium, Firefox and Safari.
+ /Unable to preload CSS for/,
+ /Failed to fetch dynamically imported module/,
+ /error loading dynamically imported module/,
+ /Importing a module script failed/,
+ // Thrown by the bundled posthog-js when its recorder script fails to load, so
+ // its frames point at our own bundle and the URL check below misses it.
+ /Called on script loaded before session recording is available/,
+];
+
+const APP_ASSETS_PATH = '/assets/';
+
+// Frames without a position are `[native code]`, which says nothing about the
+// script that threw.
+export function hasNoAppFrames(event: ErrorEvent): boolean {
+ const frames = (event.exception?.values ?? [])
+ .flatMap((exception) => exception.stacktrace?.frames ?? [])
+ .filter(
+ (frame) => frame.filename && (frame.lineno != null || frame.colno != null),
+ );
+
+ return (
+ frames.length > 0 &&
+ !frames.some((frame) => frame.filename?.includes(APP_ASSETS_PATH))
+ );
+}
+
+// PostHog's lazy recorder, the Pylon widget and Appcues.
+const EXTERNAL_SCRIPT_URLS = [
+ /^https?:\/\/[^/]+\.posthog\.com\//,
+ /^https?:\/\/(?:[^/]+\.)?usepylon\.com\//,
+ /^https?:\/\/(?:[^/]+\.)?appcues\.(?:com|net)\//,
+];
+
+// Checks every frame, not only the top one: a failed fetch from the recorder
+// tops out in Sentry's own fetch wrapper, which is bundled with the app.
+export function isExternalIntegrationError(event: ErrorEvent): boolean {
+ return (event.exception?.values ?? []).some((exception) =>
+ (exception.stacktrace?.frames ?? []).some((frame) =>
+ EXTERNAL_SCRIPT_URLS.some((url) => url.test(frame.filename ?? '')),
+ ),
+ );
+}