Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Limit Sentry sampling

Blue Mousefd7041999a7a
diff --git a/backend/apps/production/src/app.module.ts b/backend/apps/production/src/app.module.ts
index 89d7054ac..892998436 100644
--- a/backend/apps/production/src/app.module.ts
+++ b/backend/apps/production/src/app.module.ts
@@ -2,6 +2,8 @@ import _toNumber from 'lodash/toNumber'
import { Module } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'
import { ConfigModule } from '@nestjs/config'
+import { APP_FILTER } from '@nestjs/core'
+import { SentryModule, SentryGlobalFilter } from '@sentry/nestjs/setup'
import { ScheduleModule } from '@nestjs/schedule'
import { NestjsFormDataModule } from 'nestjs-form-data'
import { MailerModule as NodeMailerModule } from '@nestjs-modules/mailer'
@@ -30,6 +32,7 @@ import { AppController } from './app.controller'
import { isPrimaryNode, isPrimaryClusterNode } from './common/utils'
const modules = [
+ SentryModule.forRoot(),
ConfigModule.forRoot({
cache: true,
envFilePath: '.env',
@@ -97,5 +100,11 @@ const modules = [
...(isPrimaryNode() && isPrimaryClusterNode() ? [TaskManagerModule] : []),
],
controllers: [AppController],
+ providers: [
+ {
+ provide: APP_FILTER,
+ useClass: SentryGlobalFilter,
+ },
+ ],
})
export class AppModule {}
diff --git a/backend/apps/production/src/instrument.ts b/backend/apps/production/src/instrument.ts
index 9fc32b6f3..455875047 100644
--- a/backend/apps/production/src/instrument.ts
+++ b/backend/apps/production/src/instrument.ts
@@ -5,15 +5,70 @@ import { sentryIgnoreErrors } from './common/constants'
const isProduction = process.env.NODE_ENV === 'production'
-Sentry.init({
- dsn: process.env.SENTRY_DSN,
- integrations: [nodeProfilingIntegration()],
- tracesSampleRate: isProduction ? 0.2 : 1.0,
- profilesSampleRate: isProduction ? 0.2 : 1.0,
- ignoreErrors: sentryIgnoreErrors,
- enabled: process.env.SENTRY_ENABLED === 'true',
- // When we will have multiple slave nodes, their names should be set using the .env file
- serverName: process.env.IS_PRIMARY_NODE
- ? 'Primary node'
- : 'Secondary node #1',
-})
+if (process.env.SENTRY_ENABLED === 'true') {
+ Sentry.init({
+ dsn: process.env.SENTRY_DSN,
+ integrations: [nodeProfilingIntegration()],
+ tracesSampler: ({ name, attributes, parentSampled }) => {
+ // Do not sample Telegram webhook-related traces
+ if (name.includes('api.telegram.org')) {
+ return 0
+ }
+
+ // Do not sample CRON task for Telegram messages
+ if (name.includes('FROM `message`')) {
+ return 0
+ }
+
+ // Do not sample middlewares
+ if (attributes?.['express.type'] === 'middleware') {
+ return 0
+ }
+
+ // Redis events are not really important, don't sample too much
+ if (attributes?.['db.system'] === 'redis') {
+ return 0.01
+ }
+
+ // Don't sample mysql events
+ if (attributes?.['db.system'] === 'mysql') {
+ return 0
+ }
+
+ // Do not sample LOG / heartbeat events
+ if (
+ name.includes('log/error') ||
+ name.includes('log/hb') ||
+ name.includes('log/custom') ||
+ name.includes('getHeartBeatStats') ||
+ name.includes('AnalyticsController.log') ||
+ name.includes('/log')
+ ) {
+ return 0
+ }
+
+ // Do not sample health checks ever
+ if (name.includes('/ping') || name.includes('health')) {
+ return 0
+ }
+
+ // These are important - take a big sample
+ if (name.includes('auth')) {
+ return 1
+ }
+
+ // Continue trace decision, if there is any parentSampled information
+ if (typeof parentSampled === 'boolean') {
+ return parentSampled
+ }
+
+ // Else, use default sample rate
+ return isProduction ? 0.2 : 1
+ },
+ ignoreErrors: sentryIgnoreErrors,
+ // When we will have multiple slave nodes, their names should be set using the .env file
+ serverName: process.env.IS_PRIMARY_NODE
+ ? 'Primary node'
+ : 'Secondary node #1',
+ })
+}