From 7e9267da19cc3f1427104ce6130f77b038238ab4 Mon Sep 17 00:00:00 2001 From: GalvinGao Date: Sat, 4 Apr 2020 01:12:57 -0400 Subject: [PATCH] fix(sentry-spike): add rate limit to every client sending error report --- src/main.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/main.js b/src/main.js index 752c707f..db0a4449 100644 --- a/src/main.js +++ b/src/main.js @@ -18,13 +18,18 @@ import { Integrations as ApmIntegrations } from '@sentry/apm'; const production = process.env.NODE_ENV === 'production'; if (production) { + const sentEvents = {}; + + // set the limitation of a same client sending the same message event to Sentry for every session + const maxSameEventPerClient = 20; + Sentry.init({ dsn: 'https://9636aaa824a744f98a619df0aaabba00@sentry.io/1536764', integrations: [ new Integrations.Vue({Vue, attachProps: true}), new ApmIntegrations.Tracing(), ], - tracesSampleRate: 0.001, + tracesSampleRate: 0.005, release: 'frontend-v2@' + (config.version || 'unknown'), ignoreErrors: [ //// START: those errors are found at https://docs.sentry.io/platforms/javascript/#decluttering-sentry @@ -72,7 +77,25 @@ if (production) { /127\.0\.0\.1:4001\/isrunning/i, // Cacaoweb /webappstoolbarba\.texthelp\.com\//i, /metrics\.itunes\.apple\.com\.edgesuite\.net\//i - ] + ], + beforeSend(event) { + const {message} = event; + if (message in sentEvents) { + const counts = sentEvents[message]; + + // if there's still 'quota' for the client to send this event + if (counts < maxSameEventPerClient) { + // record that we have send the event this time + sentEvents[message] = counts + 1; + // report event + return event + } + } else { + // this has not yet been sent; init var and send it + sentEvents[message] = 1; + return event + } + } }); }