0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

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
This commit is contained in:
Kevin Ansfield 2024-10-30 11:47:15 +00:00
parent cca6a38e53
commit 98c06f8126
2 changed files with 45 additions and 8 deletions

View file

@ -2,6 +2,8 @@ import {Debug} from '@sentry/integrations';
import {Replay} from '@sentry/replay'; import {Replay} from '@sentry/replay';
import {isAjaxError} from 'ember-ajax/errors'; import {isAjaxError} from 'ember-ajax/errors';
const FILTERED_URL_REGEX = /\/e\.ghost\.org|plausible\.io/;
export function getSentryConfig(dsn, environment, appVersion, transport) { export function getSentryConfig(dsn, environment, appVersion, transport) {
const extraIntegrations = []; const extraIntegrations = [];
@ -50,7 +52,7 @@ export function getSentryConfig(dsn, environment, appVersion, transport) {
}, },
beforeBreadcrumb(breadcrumb) { beforeBreadcrumb(breadcrumb) {
// ignore breadcrumbs for event tracking to reduce noise in error reports // 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 null;
} }
return breadcrumb; return breadcrumb;
@ -97,7 +99,8 @@ export function getSentryTestConfig(transport) {
export function beforeSend(event, hint) { export function beforeSend(event, hint) {
try { try {
const exception = hint.originalException; const originalException = hint?.originalException;
event.contexts = event.contexts || {};
event.tags = event.tags || {}; event.tags = event.tags || {};
event.tags.shown_to_user = event.tags.shown_to_user || false; event.tags.shown_to_user = event.tags.shown_to_user || false;
event.tags.grammarly = !!document.querySelector('[data-gr-ext-installed]'); event.tags.grammarly = !!document.querySelector('[data-gr-ext-installed]');
@ -107,6 +110,11 @@ export function beforeSend(event, hint) {
return null; 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 the error value includes a model id then overwrite it to improve grouping
if (event.exception && event.exception.values && event.exception.values.length > 0) { if (event.exception && event.exception.values && event.exception.values.length > 0) {
const pattern = /<(post|page):[a-f0-9]+>/; 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 // ajax errors — improve logging and add context for debugging
if (isAjaxError(exception) && exception.payload && exception.payload.errors && exception.payload.errors.length > 0) { if (isAjaxError(originalException) && originalException.payload && originalException.payload.errors && originalException.payload.errors.length > 0) {
const error = exception.payload.errors[0]; const error = originalException.payload.errors[0];
event.exception.values[0].type = `${error.type}: ${error.context}`; event.exception.values[0].type = `${error.type}: ${error.context}`;
event.exception.values[0].value = error.message; event.exception.values[0].value = error.message;
event.exception.values[0].context = error.context; event.exception.values[0].context = error.context;
@ -128,14 +136,13 @@ export function beforeSend(event, hint) {
} }
// Do not report posthog-js errors to Sentry // Do not report posthog-js errors to Sentry
if (hint && hint.originalException && hint.originalException.stack) { if (originalException?.stack?.includes('/posthog-js/')) {
if (hint.originalException.stack.includes('/posthog-js/')) { return null;
return null;
}
} }
return event; return event;
} catch (error) { } 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 // If any errors occur in beforeSend, send the original event to Sentry
// Better to have some information than no information // Better to have some information than no information
return event; return event;

View file

@ -113,5 +113,35 @@ describe('Unit: Util: sentry', function () {
expect(result.tags.ajax_url).to.equal(undefined); expect(result.tags.ajax_url).to.equal(undefined);
expect(result.contexts.ajax).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);
});
}); });
}); });