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!

(feature) Sentry error handling service (#173)

Yevhenii Kulisidiec606e60412b
diff --git a/.env.example b/.env.example
index be94e84c8..e39e8d74d 100644
--- a/.env.example
+++ b/.env.example
@@ -40,10 +40,6 @@ SMTP_MOCK=true
PASSWORD=12345678
-# Use New Relic monitoring, leave empty if not using it
-USE_NEW_RELIC=
-NEW_RELIC_LICENSE_KEY=
-
# Swetrix CDN for storing files
CDN_URL=http://localhost:5006
CDN_ACCESS_TOKEN=SOME_SECRET_TOKEN
@@ -76,3 +72,7 @@ TELEGRAM_WEBHOOK_SECRET_TOKEN='' # random string; Only characters A-Z, a-z, 0-9,
# Paddle
PADDLE_VENDOR_ID=
PADDLE_API_KEY=
+
+# Sentry
+SENTRY_ENABLED=false
+SENTRY_DSN=''
\ No newline at end of file
diff --git a/Dockerfile.prod b/Dockerfile.prod
index 75627af3e..37e9de8c8 100644
--- a/Dockerfile.prod
+++ b/Dockerfile.prod
@@ -31,8 +31,6 @@ ENV TZ=UTC \
SMTP_USER=smtp_user_name \
SMTP_PASSWORD=smtp_password \
- USE_NEW_RELIC= \
- NEW_RELIC_LICENSE_KEY= \
CDN_URL=http://localhost:5006 \
CDN_ACCESS_TOKEN=SOME_SECRET_TOKEN \
CAPTCHA_SALT= \
@@ -47,7 +45,9 @@ ENV TZ=UTC \
TELEGRAM_WEBHOOK_DOMAIN= \
TELEGRAM_WEBHOOK_PATH='/v1/telegram/webhook' \
TELEGRAM_WEBHOOK_IP_ADDRESS= \
- TELEGRAM_WEBHOOK_SECRET_TOKEN=
+ TELEGRAM_WEBHOOK_SECRET_TOKEN= \
+ SENTRY_ENABLED=true \
+ SENTRY_DSN=
RUN apk add --no-cache tzdata && cp /usr/share/zoneinfo/$TZ /etc/localtime
WORKDIR /app
COPY --from=build /build/package*.json ./
diff --git a/apps/production/src/common/constants.ts b/apps/production/src/common/constants.ts
index 1605a8059..81708410d 100644
--- a/apps/production/src/common/constants.ts
+++ b/apps/production/src/common/constants.ts
@@ -43,7 +43,6 @@ const clickhouse = new ClickHouse({
})
const { JWT_ACCESS_TOKEN_SECRET } = process.env
-const isNewRelicEnabled = Boolean(process.env.USE_NEW_RELIC)
const isDevelopment = process.env.NODE_ENV === 'development'
const PRODUCTION_ORIGIN = process.env.CLIENT_URL || 'https://swetrix.com'
@@ -143,7 +142,6 @@ export {
PROJECT_INVITE_EXPIRE,
TWO_FACTOR_AUTHENTICATION_APP_NAME,
IP_REGEX,
- isNewRelicEnabled,
ORIGINS_REGEX,
REDIS_LOG_PERF_CACHE_KEY,
CAPTCHA_SALT,
diff --git a/apps/production/src/common/interceptors/newrelic.interceptor.ts b/apps/production/src/common/interceptors/newrelic.interceptor.ts
deleted file mode 100644
index 9691788c9..000000000
--- a/apps/production/src/common/interceptors/newrelic.interceptor.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-import {
- Injectable,
- NestInterceptor,
- ExecutionContext,
- CallHandler,
-} from '@nestjs/common'
-import { Observable } from 'rxjs'
-// eslint-disable-next-line @typescript-eslint/no-var-requires
-const newrelic = require('newrelic')
-
-@Injectable()
-export class NewrelicInterceptor implements NestInterceptor {
- intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
- return newrelic.startWebTransaction(context.getHandler().name, () => {
- const transaction = newrelic.getTransaction()
- return next.handle().pipe(() => transaction.end())
- })
- }
-}
diff --git a/apps/production/src/common/interceptors/sentry.interceptor.ts b/apps/production/src/common/interceptors/sentry.interceptor.ts
new file mode 100644
index 000000000..df6c5fb0a
--- /dev/null
+++ b/apps/production/src/common/interceptors/sentry.interceptor.ts
@@ -0,0 +1,21 @@
+import {
+ CallHandler,
+ ExecutionContext,
+ Injectable,
+ NestInterceptor,
+} from '@nestjs/common'
+import { tap } from 'rxjs'
+import * as Sentry from '@sentry/node'
+
+@Injectable()
+export class SentryInterceptor implements NestInterceptor {
+ intercept(_context: ExecutionContext, next: CallHandler) {
+ return next.handle().pipe(
+ tap({
+ error: exception => {
+ Sentry.captureException(exception)
+ },
+ }),
+ )
+ }
+}
diff --git a/apps/production/src/main.ts b/apps/production/src/main.ts
index 7d9659c25..3486a7260 100644
--- a/apps/production/src/main.ts
+++ b/apps/production/src/main.ts
@@ -5,12 +5,31 @@ import * as bodyParser from 'body-parser'
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'
import { getBotToken } from 'nestjs-telegraf'
-import { isNewRelicEnabled, isDevelopment } from './common/constants'
+import { ConfigService } from '@nestjs/config'
+import * as Sentry from '@sentry/node'
+import { isDevelopment } from './common/constants'
import { AppModule } from './app.module'
-import { NewrelicInterceptor } from './common/interceptors/newrelic.interceptor'
+import { SentryInterceptor } from './common/interceptors/sentry.interceptor'
async function bootstrap() {
const app = await NestFactory.create(AppModule)
+
+ const configService = app.get(ConfigService)
+
+ const isSentryEnabled = configService.getOrThrow<boolean>('SENTRY_ENABLED')
+
+ if (isSentryEnabled) {
+ const isProduction =
+ configService.getOrThrow<string>('NODE_ENV') === 'production'
+
+ Sentry.init({
+ dsn: configService.get<string>('SENTRY_DSN'),
+ tracesSampleRate: isProduction ? 0.1 : 1.0,
+ })
+
+ app.useGlobalInterceptors(new SentryInterceptor())
+ }
+
app.use(cookieParser())
app.useGlobalPipes(new ValidationPipe())
@@ -28,10 +47,6 @@ async function bootstrap() {
SwaggerModule.setup('api', app, document)
}
- if (isNewRelicEnabled && process.env.NODE_ENV !== 'development') {
- app.useGlobalInterceptors(new NewrelicInterceptor())
- }
-
// eslint-disable-next-line consistent-return
app.use(async (req, res, next) => {
res.header(
diff --git a/apps/production/src/newrelic.ts b/apps/production/src/newrelic.ts
deleted file mode 100644
index 6fed0ca71..000000000
--- a/apps/production/src/newrelic.ts
+++ /dev/null
@@ -1,60 +0,0 @@
-/**
- * New Relic agent configuration.
- *
- * See lib/config/default.js in the agent distribution for a more complete
- * description of configuration variables and their potential values.
- */
-exports.config = {
- /**
- * Array of application names.
- */
- app_name: ['SwetrixAPI'],
- /**
- * Your New Relic license key.
- */
- license_key: process.env.NEW_RELIC_LICENSE_KEY,
- logging: {
- /**
- * Level at which to log. 'trace' is most useful to New Relic when diagnosing
- * issues with the agent, 'info' and higher will impose the least overhead on
- * production applications.
- */
- level: 'info',
- },
- /**
- * When true, all request headers except for those listed in attributes.exclude
- * will be captured for all traces, unless otherwise specified in a destination's
- * attributes include/exclude lists.
- */
- allow_all_headers: true,
- application_logging: {
- forwarding: {
- /**
- * Toggles whether the agent gathers log records for sending to New Relic.
- */
- enabled: true,
- },
- },
- attributes: {
- /**
- * Prefix of attributes to exclude from all destinations. Allows * as wildcard
- * at end.
- *
- * NOTE: If excluding headers, they must be in camelCase form to be filtered.
- *
- * @env NEW_RELIC_ATTRIBUTES_EXCLUDE
- */
- exclude: [
- 'request.headers.cookie',
- 'request.headers.authorization',
- 'request.headers.proxyAuthorization',
- 'request.headers.setCookie*',
- 'request.headers.x*',
- 'response.headers.cookie',
- 'response.headers.authorization',
- 'response.headers.proxyAuthorization',
- 'response.headers.setCookie*',
- 'response.headers.x*',
- ],
- },
-}
diff --git a/docker-compose-prod.yml b/docker-compose-prod.yml
index 884399ec3..64877bc7b 100644
--- a/docker-compose-prod.yml
+++ b/docker-compose-prod.yml
@@ -34,8 +34,6 @@ services:
- SMTP_USER=smtp_user_name
- SMTP_PASSWORD=smtp_password
- - USE_NEW_RELIC=
- - NEW_RELIC_LICENSE_KEY=
- CDN_URL=http://localhost:5006
- CDN_ACCESS_TOKEN=SOME_SECRET_TOKEN
- CAPTCHA_SALT=
@@ -51,6 +49,8 @@ services:
- TELEGRAM_WEBHOOK_PATH='/v1/telegram/webhook'
- TELEGRAM_WEBHOOK_IP_ADDRESS=
- TELEGRAM_WEBHOOK_SECRET_TOKEN=
+ - SENTRY_ENABLED=true
+ - SENTRY_DSN=
links:
- 'redis'
- 'mariadb'
@@ -77,7 +77,7 @@ services:
- MYSQL_USER=swetrix
- MYSQL_PASSWORD=password
volumes:
- - swetrix-users-data:/var/lib/mysql
+ - swetrix-users-data:/var/lib/mysql
clickhouse:
image: clickhouse/clickhouse-server:23.4.2.11-alpine
container_name: clickhouse
@@ -98,4 +98,3 @@ volumes:
driver: local
swetrix-users-data:
driver: local
-
diff --git a/migrations/mysql/2023_07_06.sql b/migrations/mysql/2023_07_06.sql
index e2965b27c..f0a46b24e 100644
--- a/migrations/mysql/2023_07_06.sql
+++ b/migrations/mysql/2023_07_06.sql
@@ -5,4 +5,4 @@ CREATE TABLE IF NOT EXISTS `message` (
`extra` json DEFAULT NULL,
`createdAt` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
PRIMARY KEY (`id`)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
\ No newline at end of file
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
diff --git a/package.json b/package.json
index a26b5745b..ef73538c9 100644
--- a/package.json
+++ b/package.json
@@ -36,6 +36,7 @@
"@nestjs/schedule": "^3.0.1",
"@nestjs/swagger": "^7.0.1",
"@nestjs/typeorm": "^10.0.0",
+ "@sentry/node": "^7.59.2",
"axios": "^1.4.0",
"bcrypt": "^5.1.0",
"blake3": "^2.1.7",
@@ -66,7 +67,6 @@
"nestjs-form-data": "^1.8.7",
"nestjs-i18n": "^10.2.6",
"nestjs-telegraf": "^2.6.3",
- "newrelic": "^10.3.0",
"otplib": "^12.0.1",
"passport": "^0.6.0",
"passport-headerapikey": "^1.2.2",
@@ -93,7 +93,6 @@
"@types/ioredis": "^4.28.10",
"@types/jest": "^29.5.2",
"@types/multer": "^1.4.7",
- "@types/newrelic": "^9.14.0",
"@types/node": "^20.3.1",
"@types/passport-jwt": "^3.0.8",
"@types/supertest": "^2.0.12",