How do I integrate NewRelic into a Node Typescript Express server bundled with Webpack?
Asked Answered
A

1

11

Frankly, I've tried it all. I'm not a total whiz with Webpack, however I seem to be getting along pretty well over the years with configuring new projects.

What I cannot seem to do now is set up the NewRelic service into an existing Node/Typescript/Express/Webpack application.

As it stands, my app gets nicely bundled to a single file in my /dist folder and runs quick and nimble. Seems like this 'node agent' put out by New Relic doesn't play well with Typescript imports.

Webpack Config

const path = require('path');
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
const NodemonPlugin = require ('nodemon-webpack-plugin');

module.exports = (env = {}) => {
const config = {
    entry: ['./src/app.ts'],
    mode: env.development ? 'development' : 'production',
    target: 'node',
    devtool: env.development ? 'inline-source-map' : false,
    resolve: {
        extensions: ['.ts', '.js'],
        modules: ['node_modules', 'src', 'package.json'],
    },
    module: {
        rules: [
            {
                test: /\.ts$/,
                use: ['ts-loader', 'eslint-loader'],
                // exclude: /node_modules/,
            },
        ],
    },
    plugins: [],
    externals: [ 'newrelic', nodeExternals() ]
};

if (env.nodemon) {
    config.watch = true;
    config.plugins.push(new NodemonPlugin())
}
return config;
};
  • there exists a standard /project_root/.newrelic file
  • CircleCi picks up this project up and runs "build:ci" script from package.json ==> "webpack"
  • output is /dist/main.js

references
https://docs.newrelic.com/docs/agents/nodejs-agent/installation-configuration/install-nodejs-agent https://docs.newrelic.com/docs/agents/nodejs-agent/installation-configuration/nodejs-agent-configuration https://discuss.newrelic.com/t/node-agent-fails-with-webpack/24874

Anklebone answered 7/11, 2019 at 17:38 Comment(0)
S
6

Your first line of the starting point of the app should be

import newrelic from 'newrelic';

Of course, run npm install newrelic --save first

Then, create a newrelic.js file on the root of the repo (outside of src).

Then you put in the details like:

'use strict'
exports.config = {
  app_name: ['appName'],
  license_key: '1234567890',
  allow_all_headers: true,
  attributes: {
    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*'
    ]
  }
}
Sitra answered 16/12, 2021 at 6:27 Comment(2)
Thank you. you can also do import 'newrelic'; and that way linting won't complain about unused variable.Procurance
This here is a great comment from @ATilara, but most importantly, I needed this so that my nest build script (aka webpack) does not remove import statement from the generated js fileEncroachment

© 2022 - 2024 — McMap. All rights reserved.