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!

Start of integration with New Relic

Yevhenii Ka2f62062c792
diff --git a/.env.example b/.env.example
index b6a09aa1f..53dbf75f8 100644
--- a/.env.example
+++ b/.env.example
@@ -33,3 +33,5 @@ SMTP_MOCK=true
# Selfhosted
PASSWORD=12345678
+
+SELFHOSTED=true
\ No newline at end of file
diff --git a/package.json b/package.json
index bbaddaece..0ca34d5f1 100644
--- a/package.json
+++ b/package.json
@@ -51,6 +51,7 @@
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21",
"mysql2": "^2.3.3",
+ "newrelic": "^9.0.0",
"otplib": "^12.0.1",
"passport": "^0.6.0",
"passport-jwt": "^4.0.0",
@@ -72,6 +73,7 @@
"@types/express": "^4.17.13",
"@types/ioredis": "^4.28.10",
"@types/jest": "^27.4.1",
+ "@types/newrelic": "^7.0.3",
"@types/node": "^18.0.0",
"@types/passport-jwt": "^3.0.6",
"@types/supertest": "^2.0.12",
diff --git a/src/common/interceptors/newrelic.interceptor.ts b/src/common/interceptors/newrelic.interceptor.ts
new file mode 100644
index 000000000..1c237f037
--- /dev/null
+++ b/src/common/interceptors/newrelic.interceptor.ts
@@ -0,0 +1,19 @@
+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, function () {
+ const transaction = newrelic.getTransaction()
+ return next.handle().pipe(() => transaction.end())
+ })
+ }
+}
diff --git a/src/main.ts b/src/main.ts
index 9a4090440..19772f8c0 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -2,14 +2,20 @@ import { NestFactory } from '@nestjs/core'
import { ValidationPipe } from '@nestjs/common'
import * as cookieParser from 'cookie-parser'
import * as bodyParser from 'body-parser'
+import newrelic from 'newrelic';
import { AppModule } from './app.module'
+import { NewrelicInterceptor } from './common/interceptors/newrelic.interceptor';
async function bootstrap() {
const app = await NestFactory.create(AppModule)
app.use(cookieParser())
app.useGlobalPipes(new ValidationPipe())
+ if (process.env.SELFHOSTED) {
+ app.useGlobalInterceptors(new NewrelicInterceptor())
+ }
+
app.use(async (req, res, next) => {
res.header('Cross-Origin-Embedder-Policy', 'require-corp; report-to=\'default\'')
res.header('Cross-Origin-Opener-Policy', 'same-site; report-to=\'default\'')
diff --git a/src/newrelic.ts b/src/newrelic.ts
new file mode 100644
index 000000000..7ab9bc81b
--- /dev/null
+++ b/src/newrelic.ts
@@ -0,0 +1 @@
+// config here
\ No newline at end of file