Table of contents
Ensuring that your Node.js application runs smoothly and efficiently is crucial for providing a great user experience. One powerful tool for monitoring the performance of your application is New Relic. In this blog post, we will guide you through the process of integrating New Relic with a Node.js application, allowing you to gain valuable insights into your application's performance.
What is New Relic?
New Relic is a comprehensive monitoring and observability platform that helps developers and operations teams understand and improve the performance of their applications. With New Relic, you can monitor various aspects of your application, such as response times, error rates, throughput, and more, enabling you to quickly identify and resolve performance issues.
Why Node.js?
Node.js is a popular JavaScript runtime that is known for its non-blocking, event-driven architecture, making it ideal for building scalable network applications. Monitoring Node.js applications is essential to ensure they perform well under various loads and to identify bottlenecks or issues before they impact users.
Setting Up New Relic with Node.js
To get started with New Relic and Node.js, follow these steps:
Step 1: Sign Up for New Relic
If you don't already have a New Relic account, you'll need to sign up. Visit the New Relic website and create an account.
Step 2: Install the New Relic Agent
Once you have an account, you can install the New Relic Node.js agent in your application. First, navigate to your Node.js application directory and install the New Relic agent using npm:
npm install newrelic --save
Step 3: Configure the New Relic Agent
After installing the New Relic agent, you need to configure it. New Relic requires a configuration file named newrelic.js
in the root directory of your application. You can generate this file by running:
npx newrelic install
Alternatively, you can manually create the newrelic.js
file. Here's an example configuration file:
'use strict';
/**
* New Relic agent configuration.
*
* See lib/config/default.js in the agent distribution for a more complete
* description of configuration variables and their potential values.
*/
exports.config = {
/**
* Array of application names.
*/
app_name: ['Your Application Name'],
/**
* Your New Relic license key.
*/
license_key: 'YOUR_NEW_RELIC_LICENSE_KEY',
/**
* This setting controls distributed tracing.
* Distributed tracing lets you see the path that a request takes through your
* distributed system. Enabling distributed tracing changes the behavior of some
* New Relic features, so carefully consult the transition guide before you enable
* this feature: https://docs.newrelic.com/docs/transition-guide-distributed-tracing
* Default is true.
*/
distributed_tracing: {
enabled: true,
},
logging: {
/**
* Level at which to log. 'trace' is most useful to New Relic when diagnosing
* issues with the agent, 'info' and higher will impose the least overhead on
* production applications.
*/
level: 'info',
},
/**
* When true, all request headers except for those listed in attributes.exclude
* will be captured for all traces, unless otherwise specified in a destination's
* attributes include/exclude lists.
*/
allow_all_headers: true,
attributes: {
/**
* Prefix of attributes to exclude from all destinations. Allows * as wildcard
* at end.
*
* NOTE: If excluding headers, they must be in camelCase form to be filtered.
*
* @env NEW_RELIC_ATTRIBUTES_EXCLUDE
*/
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*',
],
},
};
Replace 'YOUR_NEW_RELIC_LICENSE_KEY'
with your actual New Relic license key, which you can find in your New Relic account settings.
Step 4: Integrate the New Relic Agent
To integrate the New Relic agent into your application, you need to require it at the very top of your main application file (before any other require statements). For example, if your main application file is app.js
or server.js
, add the following line at the top:
require('newrelic');
Step 5: Start Your Application
Start your Node.js application as you normally would. The New Relic agent will automatically begin monitoring your application and sending data to New Relic.
node app.js
Step 6: View Your Application's Performance in New Relic
Log in to your New Relic account and navigate to the APM (Application Performance Monitoring) section. You should see your application listed there. Click on your application to view detailed performance metrics, including response times, throughput, error rates, and more.
Conclusion
Integrating New Relic with your Node.js application provides you with powerful tools to monitor and optimize your application's performance. By following the steps outlined in this guide, you can quickly set up New Relic and start gaining valuable insights into how your application is performing in real-time. This allows you to proactively identify and address potential issues, ensuring a smooth and responsive user experience.
New Relic offers a wealth of features and capabilities beyond basic monitoring, such as distributed tracing, alerting, and custom dashboards. As you become more familiar with the platform, you can leverage these advanced features to further enhance your application's performance and reliability. Happy monitoring!