微信登录

性能监控工具 - 第三方工具 - New Relic 等工具介绍

Node.js 性能监控工具 - 第三方工具 - New Relic 等工具介绍

在 Node.js 应用开发和部署过程中,性能监控至关重要。它能帮助开发者及时发现和解决应用中的性能瓶颈,保障应用的稳定运行。本文将详细介绍几个常用的 Node.js 性能监控第三方工具,重点介绍 New Relic,并提供相关演示代码。

常见 Node.js 性能监控第三方工具概述

工具名称 特点 适用场景
New Relic 功能强大,提供全面的性能监控和分析功能,支持多种编程语言和框架,有可视化界面,便于操作和查看数据。 中大型企业级应用,对性能监控要求较高,需要深入分析和可视化展示的场景。
Datadog 提供实时指标监控、日志管理和分布式追踪功能,与云服务集成良好。 基于云的应用,需要与云环境紧密结合进行监控的场景。
AppDynamics 专注于应用性能管理,提供详细的事务追踪和代码级分析。 需要深入了解应用内部事务和代码性能的场景。

New Relic 详细介绍

特点

  • 全面监控:可以监控 Node.js 应用的 CPU 使用率、内存占用、请求响应时间等关键指标。
  • 可视化界面:提供直观的仪表盘和图表,方便开发者查看应用的性能数据。
  • 分布式追踪:支持分布式系统的性能监控,帮助开发者定位和解决跨服务的性能问题。

安装和配置

1. 注册 New Relic 账号

首先,访问 New Relic 官网 注册一个账号。

2. 安装 New Relic 代理

在 Node.js 项目中,可以使用 npm 安装 New Relic 代理:

  1. npm install newrelic

3. 配置 New Relic

在项目根目录下创建一个 newrelic.js 文件,内容如下:

  1. 'use strict';
  2. /**
  3. * New Relic agent configuration.
  4. *
  5. * See lib/config.defaults.js in the agent distribution for a more complete
  6. * description of configuration variables and their potential values.
  7. */
  8. exports.config = {
  9. /**
  10. * Array of application names.
  11. */
  12. app_name: ['Your Node.js App Name'],
  13. /**
  14. * Your New Relic license key.
  15. */
  16. license_key: 'YOUR_LICENSE_KEY',
  17. logging: {
  18. /**
  19. * Level at which to log. 'trace' is most useful to New Relic when diagnosing
  20. * issues with the agent, 'info' and higher will impose the least overhead on
  21. * production applications.
  22. */
  23. level: 'info'
  24. },
  25. /**
  26. * When true, all request headers except for those listed in attributes.exclude
  27. * will be captured for all traces, unless otherwise specified in a destination's
  28. * attributes include/exclude lists.
  29. */
  30. allow_all_headers: true,
  31. attributes: {
  32. /**
  33. * Prefix of attributes to exclude from all destinations. Allows * as wildcard
  34. * at end.
  35. *
  36. * NOTE: If excluding headers, they must be in camelCase form to be filtered.
  37. *
  38. * @env NEW_RELIC_ATTRIBUTES_EXCLUDE
  39. */
  40. exclude: [
  41. 'request.headers.cookie',
  42. 'request.headers.authorization',
  43. 'request.headers.proxyAuthorization',
  44. 'request.headers.setCookie*',
  45. 'request.headers.x*',
  46. 'response.headers.cookie',
  47. 'response.headers.authorization',
  48. 'response.headers.proxyAuthorization',
  49. 'response.headers.setCookie*',
  50. 'response.headers.x*'
  51. ]
  52. }
  53. };

'Your Node.js App Name' 替换为你的应用名称,'YOUR_LICENSE_KEY' 替换为你在 New Relic 账号中获取的许可证密钥。

4. 在应用中引入 New Relic

在 Node.js 应用的入口文件(通常是 app.jsindex.js)中,在其他代码之前引入 New Relic:

  1. require('newrelic');
  2. const express = require('express');
  3. const app = express();
  4. app.get('/', (req, res) => {
  5. res.send('Hello, World!');
  6. });
  7. const port = process.env.PORT || 3000;
  8. app.listen(port, () => {
  9. console.log(`Server is running on port ${port}`);
  10. });

查看监控数据

启动 Node.js 应用后,New Relic 会开始收集应用的性能数据。登录 New Relic 控制台,你可以看到各种性能指标的可视化展示,如请求响应时间、吞吐量、错误率等。

总结

通过使用第三方性能监控工具,如 New Relic,开发者可以更方便地监控 Node.js 应用的性能,及时发现和解决潜在的问题。本文介绍了常见的 Node.js 性能监控工具,并详细介绍了 New Relic 的安装、配置和使用方法。希望这些内容能帮助你更好地管理和优化 Node.js 应用的性能。