diff --git a/.env.example b/.env.example index 63aa1b0..0592894 100644 --- a/.env.example +++ b/.env.example @@ -12,3 +12,8 @@ AI_API_KEY=your_nvidia_api_key_here AI_MODEL=qwen/qwen3.5-397b-a17b # Set to 'true' to enable detailed AI prompt/response logging AI_DEBUG=false + +# New Relic APM — set ENABLED=true and provide your license key to activate +NEW_RELIC_ENABLED=false +NEW_RELIC_LICENSE_KEY=your_new_relic_license_key_here +NEW_RELIC_APP_NAME=HOALedgerIQ_App diff --git a/backend/Dockerfile b/backend/Dockerfile index c8fac84..52774f6 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -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"] diff --git a/backend/Dockerfile.dev b/backend/Dockerfile.dev index eb830df..032b2ca 100644 --- a/backend/Dockerfile.dev +++ b/backend/Dockerfile.dev @@ -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"] diff --git a/backend/newrelic-preload.js b/backend/newrelic-preload.js new file mode 100644 index 0000000..2767be1 --- /dev/null +++ b/backend/newrelic-preload.js @@ -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'); +} diff --git a/backend/package.json b/backend/package.json index fb86991..5f9b694 100644 --- a/backend/package.json +++ b/backend/package.json @@ -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": { diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 37891d9..1550a01 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -37,6 +37,9 @@ services: - AI_API_KEY=${AI_API_KEY} - AI_MODEL=${AI_MODEL} - AI_DEBUG=${AI_DEBUG:-false} + - NEW_RELIC_ENABLED=${NEW_RELIC_ENABLED:-false} + - NEW_RELIC_LICENSE_KEY=${NEW_RELIC_LICENSE_KEY:-} + - NEW_RELIC_APP_NAME=${NEW_RELIC_APP_NAME:-HOALedgerIQ_App} deploy: resources: limits: diff --git a/docker-compose.yml b/docker-compose.yml index 922339e..553e54d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -26,6 +26,9 @@ services: - AI_API_KEY=${AI_API_KEY} - AI_MODEL=${AI_MODEL} - AI_DEBUG=${AI_DEBUG:-false} + - NEW_RELIC_ENABLED=${NEW_RELIC_ENABLED:-false} + - NEW_RELIC_LICENSE_KEY=${NEW_RELIC_LICENSE_KEY:-} + - NEW_RELIC_APP_NAME=${NEW_RELIC_APP_NAME:-HOALedgerIQ_App} volumes: - ./backend/src:/app/src - ./backend/nest-cli.json:/app/nest-cli.json