From 98c06f8126ee0798d9e9e6b335c40b6b5bf2f9eb Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Wed, 30 Oct 2024 11:47:15 +0000 Subject: [PATCH] Fixed removal of event tracker requests in Sentry no issue - filtering was previously added to breadcrumbs but that wasn't enough to clean up Sentry reports - added filtering to the `beforeSend` hook too so reports don't get cluttered with unhelpful XHR noise --- ghost/admin/app/utils/sentry.js | 23 ++++++++++------ ghost/admin/tests/unit/utils/sentry-test.js | 30 +++++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/ghost/admin/app/utils/sentry.js b/ghost/admin/app/utils/sentry.js index 11a60d1ad5..236daf85bf 100644 --- a/ghost/admin/app/utils/sentry.js +++ b/ghost/admin/app/utils/sentry.js @@ -2,6 +2,8 @@ import {Debug} from '@sentry/integrations'; import {Replay} from '@sentry/replay'; import {isAjaxError} from 'ember-ajax/errors'; +const FILTERED_URL_REGEX = /\/e\.ghost\.org|plausible\.io/; + export function getSentryConfig(dsn, environment, appVersion, transport) { const extraIntegrations = []; @@ -50,7 +52,7 @@ export function getSentryConfig(dsn, environment, appVersion, transport) { }, beforeBreadcrumb(breadcrumb) { // ignore breadcrumbs for event tracking to reduce noise in error reports - if (breadcrumb.category === 'http' && breadcrumb.data?.url?.match(/\/e\.ghost\.org|plausible\.io/)) { + if (breadcrumb.category === 'http' && breadcrumb.data?.url?.match(FILTERED_URL_REGEX)) { return null; } return breadcrumb; @@ -97,7 +99,8 @@ export function getSentryTestConfig(transport) { export function beforeSend(event, hint) { try { - const exception = hint.originalException; + const originalException = hint?.originalException; + event.contexts = event.contexts || {}; event.tags = event.tags || {}; event.tags.shown_to_user = event.tags.shown_to_user || false; event.tags.grammarly = !!document.querySelector('[data-gr-ext-installed]'); @@ -107,6 +110,11 @@ export function beforeSend(event, hint) { return null; } + // Do not report requests to our event tracking endpoints to reduce noise + if (event.request?.url?.match(FILTERED_URL_REGEX)) { + return null; + } + // if the error value includes a model id then overwrite it to improve grouping if (event.exception && event.exception.values && event.exception.values.length > 0) { const pattern = /<(post|page):[a-f0-9]+>/; @@ -115,8 +123,8 @@ export function beforeSend(event, hint) { } // ajax errors — improve logging and add context for debugging - if (isAjaxError(exception) && exception.payload && exception.payload.errors && exception.payload.errors.length > 0) { - const error = exception.payload.errors[0]; + if (isAjaxError(originalException) && originalException.payload && originalException.payload.errors && originalException.payload.errors.length > 0) { + const error = originalException.payload.errors[0]; event.exception.values[0].type = `${error.type}: ${error.context}`; event.exception.values[0].value = error.message; event.exception.values[0].context = error.context; @@ -128,14 +136,13 @@ export function beforeSend(event, hint) { } // Do not report posthog-js errors to Sentry - if (hint && hint.originalException && hint.originalException.stack) { - if (hint.originalException.stack.includes('/posthog-js/')) { - return null; - } + if (originalException?.stack?.includes('/posthog-js/')) { + return null; } return event; } catch (error) { + console.error('Error in beforeSend:', error); // eslint-disable-line no-console // If any errors occur in beforeSend, send the original event to Sentry // Better to have some information than no information return event; diff --git a/ghost/admin/tests/unit/utils/sentry-test.js b/ghost/admin/tests/unit/utils/sentry-test.js index 709d4be872..183244c80f 100644 --- a/ghost/admin/tests/unit/utils/sentry-test.js +++ b/ghost/admin/tests/unit/utils/sentry-test.js @@ -113,5 +113,35 @@ describe('Unit: Util: sentry', function () { expect(result.tags.ajax_url).to.equal(undefined); expect(result.contexts.ajax).to.equal(undefined); }); + + it('skips reporting e.ghost.org requests', () => { + const event = { + request: { + url: 'https://e.ghost.org/pg/injest/i/v0/e/' + } + }; + const exception = { + payload: { + errors: [] + } + }; + const hint = { + originalException: exception + }; + + const result = beforeSend(event, hint); + expect(result).to.equal(null); + }); + + it('skips reporting plausible requests', () => { + const event = { + request: { + url: 'https://plausible.io/api/event' + } + }; + + const result = beforeSend(event); + expect(result).to.equal(null); + }); }); });