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!

(improvement) Sentry NestJS integration (#240)

Andrii Romasiun6378571fa5ea
diff --git a/.gitignore b/.gitignore
index 25b826461..668e777cc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -41,3 +41,6 @@ yarn.lock
*.mmdb.gz
.env.prod
+
+# Sentry Config File
+.sentryclirc
diff --git a/apps/production/src/instrument.ts b/apps/production/src/instrument.ts
new file mode 100644
index 000000000..0a1105ca6
--- /dev/null
+++ b/apps/production/src/instrument.ts
@@ -0,0 +1,17 @@
+import * as Sentry from '@sentry/nestjs'
+import { nodeProfilingIntegration } from '@sentry/profiling-node'
+
+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_MASTER_NODE ? 'Master node' : 'Slave node #1',
+})
diff --git a/apps/production/src/main.ts b/apps/production/src/main.ts
index 5a2ab1341..f7ce264f4 100644
--- a/apps/production/src/main.ts
+++ b/apps/production/src/main.ts
@@ -1,36 +1,18 @@
-import { NestFactory, BaseExceptionFilter, HttpAdapterHost } from '@nestjs/core'
+import './instrument'
+
+import { NestFactory } from '@nestjs/core'
import { ValidationPipe, VersioningType } from '@nestjs/common'
import * as cookieParser from 'cookie-parser'
import * as bodyParser from 'body-parser'
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'
import { getBotToken } from 'nestjs-telegraf'
-import { ConfigService } from '@nestjs/config'
-import * as Sentry from '@sentry/node'
-import { isDevelopment, sentryIgnoreErrors } from './common/constants'
+import { isDevelopment } from './common/constants'
import { AppModule } from './app.module'
async function bootstrap() {
const app = await NestFactory.create(AppModule)
- const configService = app.get(ConfigService)
-
- const isSentryEnabled = configService.get<boolean>('SENTRY_ENABLED')
-
- if (isSentryEnabled) {
- const isProduction = configService.get<string>('NODE_ENV') === 'production'
-
- Sentry.init({
- dsn: configService.get<string>('SENTRY_DSN'),
- tracesSampleRate: isProduction ? 0.2 : 1.0,
- ignoreErrors: sentryIgnoreErrors,
- })
-
- const { httpAdapter } = app.get(HttpAdapterHost)
-
- Sentry.setupNestErrorHandler(app, new BaseExceptionFilter(httpAdapter))
- }
-
app.use(cookieParser())
app.useGlobalPipes(new ValidationPipe())
diff --git a/apps/production/tsconfig.app.json b/apps/production/tsconfig.app.json
index d8e88dc36..2997fae6f 100644
--- a/apps/production/tsconfig.app.json
+++ b/apps/production/tsconfig.app.json
@@ -11,6 +11,9 @@
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
- "skipLibCheck": true
+ "skipLibCheck": true,
+ "inlineSources": true,
+
+ "sourceRoot": "/"
}
}
diff --git a/package.json b/package.json
index a48c7577d..0105643d5 100644
--- a/package.json
+++ b/package.json
@@ -11,7 +11,7 @@
"refresh-submodules": "git submodule foreach git pull",
"init-submodules": "git submodule update --init --recursive",
"prebuild": "rimraf dist",
- "build": "npm run refresh-submodules && nest build production && mv apps/production/dist dist",
+ "build": "npm run refresh-submodules && nest build production && mv apps/production/dist dist && npm run sentry:sourcemaps",
"build:selfhosted": "npm run prebuild && nest build selfhosted && mv apps/selfhosted/dist dist && npm run postbuild",
"postbuild": "cp .env dist/.env",
"format": "prettier --write \"apps/selfhosted/src/**/*.ts\" \"apps/production/src/**/*.ts\"",
@@ -23,7 +23,8 @@
"start:prod": "npm run clickhouse:initialise && cross-env NODE_ENV=production node dist/main",
"lint": "eslint \"{apps/production/src,apps/selfhosted/src}/**/*.ts\"",
"lint:fix": "eslint \"{apps/production/src,apps/selfhosted/src}/**/*.ts\" --fix",
- "find-deadcode": "ts-prune"
+ "find-deadcode": "ts-prune",
+ "sentry:sourcemaps": "sentry-cli sourcemaps inject --org my-org-f6 --project swetrix-api ./dist && sentry-cli sourcemaps upload --org my-org-f6 --project swetrix-api ./dist"
},
"dependencies": {
"@faker-js/faker": "^8.4.1",
@@ -41,7 +42,9 @@
"@nestjs/terminus": "^10.2.3",
"@nestjs/typeorm": "^10.0.1",
"@paypal/payouts-sdk": "^1.1.1",
- "@sentry/node": "^8.0.0-rc.3",
+ "@sentry/cli": "^2.32.1",
+ "@sentry/nestjs": "^8.13.0",
+ "@sentry/profiling-node": "^8.13.0",
"axios": "^1.6.8",
"bcrypt": "^5.1.1",
"blake3": "^2.1.7",
@@ -141,5 +144,8 @@
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
+ },
+ "resolutions": {
+ "string-width": "4.2.3"
}
}
diff --git a/tsconfig.json b/tsconfig.json
index d8e88dc36..a58ad45b9 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -11,6 +11,12 @@
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
- "skipLibCheck": true
+ "skipLibCheck": true,
+ "inlineSources": true,
+
+ // Set `sourceRoot` to "/" to strip the build path prefix
+ // from generated source code references.
+ // This improves issue grouping in Sentry.
+ "sourceRoot": "/"
}
}