Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 667
beauXjames Avatar asked Nov 16 '22 21:11

beauXjames


1 Answers

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*'
    ]
  }
}
like image 150
Shayan Khan Avatar answered Nov 19 '22 10:11

Shayan Khan