feat: add New Relic APM instrumentation to backend
Add Node.js New Relic agent with an on/off switch via NEW_RELIC_ENABLED in .env. Uses a preload script (-r newrelic-preload.js) so the agent loads before all other modules. Configured entirely through environment variables — no newrelic.js config file needed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -17,10 +17,16 @@ WORKDIR /app
|
||||
COPY package*.json ./
|
||||
RUN npm ci --omit=dev && npm cache clean --force
|
||||
|
||||
# Copy compiled output from builder
|
||||
# Copy compiled output and New Relic preload from builder
|
||||
COPY --from=builder /app/dist ./dist
|
||||
COPY --from=builder /app/newrelic-preload.js ./newrelic-preload.js
|
||||
|
||||
# New Relic agent — configured entirely via environment variables
|
||||
ENV NEW_RELIC_NO_CONFIG_FILE=true
|
||||
ENV NEW_RELIC_DISTRIBUTED_TRACING_ENABLED=true
|
||||
ENV NEW_RELIC_LOG=stdout
|
||||
|
||||
EXPOSE 3000
|
||||
|
||||
# Run the compiled JS directly — no ts-node, no watch, no devDeps
|
||||
CMD ["node", "dist/main"]
|
||||
# Preload the New Relic agent (activates only when NEW_RELIC_ENABLED=true)
|
||||
CMD ["node", "-r", "./newrelic-preload.js", "dist/main"]
|
||||
|
||||
@@ -7,6 +7,11 @@ RUN npm install
|
||||
|
||||
COPY . .
|
||||
|
||||
# New Relic agent — configured entirely via environment variables
|
||||
ENV NEW_RELIC_NO_CONFIG_FILE=true
|
||||
ENV NEW_RELIC_DISTRIBUTED_TRACING_ENABLED=true
|
||||
ENV NEW_RELIC_LOG=stdout
|
||||
|
||||
EXPOSE 3000
|
||||
|
||||
CMD ["npm", "run", "start:dev"]
|
||||
|
||||
7
backend/newrelic-preload.js
Normal file
7
backend/newrelic-preload.js
Normal file
@@ -0,0 +1,7 @@
|
||||
// Conditionally load the New Relic agent before any other modules.
|
||||
// Controlled by the NEW_RELIC_ENABLED environment variable (.env).
|
||||
'use strict';
|
||||
|
||||
if (process.env.NEW_RELIC_ENABLED === 'true') {
|
||||
require('newrelic');
|
||||
}
|
||||
@@ -8,7 +8,7 @@
|
||||
"start": "nest start",
|
||||
"start:dev": "nest start --watch",
|
||||
"start:debug": "nest start --debug --watch",
|
||||
"start:prod": "node dist/main",
|
||||
"start:prod": "node -r ./newrelic-preload.js dist/main",
|
||||
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\"",
|
||||
"test": "jest",
|
||||
"test:watch": "jest --watch",
|
||||
@@ -37,6 +37,7 @@
|
||||
"reflect-metadata": "^0.2.2",
|
||||
"rxjs": "^7.8.1",
|
||||
"typeorm": "^0.3.20",
|
||||
"newrelic": "latest",
|
||||
"uuid": "^9.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
Reference in New Issue
Block a user