117.info
人生若只如初见

如何在 Ubuntu 上监控 Nodejs 应用

在 Ubuntu 上监控 Node.js 应用可以通过多种工具和方法实现,以下是一些常用的方法和工具:

使用 Prometheus 和 Grafana 进行监控

  • 安装 Node.js 的 Prometheus 客户端

    npm install prom-client
    
  • 设置 Prometheus 指标: 在 Node.js 应用程序中定义请求持续时间、活动请求等指标:

    const client = require('prom-client');
    const requestDuration = new client.histogram({
      name: 'http_request_duration_seconds',
      help: 'duration of http requests in seconds',
      labelnames: ['method', 'status']
    });
    const activeRequests = new client.gauge({
      name: 'active_requests',
      help: 'number of active requests'
    });
    
    app.use((req, res, next) => {
      const end = requestDuration.startTimer();
      activeRequests.inc();
      res.on('finish', () => {
        end({ method: req.method, status: res.statusCode });
        activeRequests.dec();
      });
      next();
    });
    
  • 公开指标端点: 创建端点以向 Prometheus 公开指标:

    app.get('/metrics', async (req, res) => {
      res.set('content-type', client.register.contentType);
      res.send(await client.register.metrics());
    });
    
  • 使用 Grafana 进行可视化: 将 Prometheus 连接到 Grafana 并创建自定义仪表板以可视化请求率、延迟和内存使用等指标。

使用 Winston 进行日志记录

  • 安装 Winston

    npm install winston
    
  • 配置 Winston

    const winston = require('winston');
    const logger = winston.createLogger({
      level: 'info',
      format: winston.format.json(),
      transports: [
        new winston.transports.console(),
        new winston.transports.file({ filename: 'error.log', level: 'error' }),
        new winston.transports.file({ filename: 'combined.log' })
      ]
    });
    
    logger.info('Application started');
    logger.error('An error occurred');
    

使用 Tinymonit 进行系统监控

  • 安装 Tinymonit

    npm install tinymonit -g
    
  • 收集系统监控信息

    const tm = require('tinymonit');
    const osstat = tm.osstat;
    Promise.resolve().then(() => osstat([8122])).then((stat) => console.log(stat));
    

实施健康检查

  • 健康检查端点
    app.get('/health', (req, res) => {
      const healthstatus = { uptime: process.uptime(), message: 'ok', timestamp: Date.now() };
      res.status(200).send(healthstatus);
    });
    

通过上述方法,您可以在 Ubuntu 上有效地监控 Node.js 应用的性能和健康状况。结合使用 Prometheus、Grafana、Winston 和 Tinymonit 等工具,您可以获得全面的监控数据,从而更好地理解和优化您的应用。

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe7acAzsNAQdfBFw.html

推荐文章

  • 如何在Ubuntu上配置PHP时区

    在 Ubuntu 上配置 PHP 时区,可以按照以下步骤进行操作:
    方法一:通过 php.ini 文件配置 找到 php.ini 文件: 通常位于 /etc/php/{version}/cli/php.ini 或...

  • ubuntu securecrt日志查看与管理

    在Ubuntu系统中使用SecureCRT进行日志查看与管理,可以按照以下步骤进行:
    查看日志 打开SecureCRT并连接到远程服务器。
    在菜单栏中,点击“Options”...

  • ubuntu compton未来发展趋势如何

    Compton 是一个在 Linux 下广泛使用的窗口管理器合成器,主要用于提供窗口的合成和阴影效果,从而增强桌面环境的视觉效果。关于 Compton 的未来发展趋势,可以从...

  • Ubuntu中PHP如何进行内存管理

    在Ubuntu中,PHP的内存管理主要通过配置文件和代码实现。以下是一些建议和方法来管理PHP内存: 修改php.ini配置文件:
    在Ubuntu系统中,PHP的内存限制通常在...

  • ubuntu域名能做什么用

    在Ubuntu系统中,域名可以用于多种用途,主要包括以下几点:
    网站托管 搭建个人博客或网站: 使用WordPress、Jekyll等静态或动态网站生成器。
    配置Ngi...

  • Debian下GitLab的安全设置有哪些

    在Debian系统下配置GitLab时,可以采取以下安全措施: 更新系统和软件包: 确保系统和所有软件包都是最新版本,以修补已知的安全漏洞。sudo apt update && sudo ...

  • 如何在Debian SFTP中启用日志记录

    在Debian系统中启用SFTP日志记录,可以通过编辑SSH配置文件来实现。以下是详细步骤:
    1. 编辑SSH配置文件
    首先,你需要编辑SSH服务器的配置文件 /etc/...

  • MongoDB在Debian上的监控方法

    在Debian上监控MongoDB有多种方法,以下是一些常用的监控方法:
    使用MongoDB Exporter和Prometheus进行监控 MongoDB Exporter:将MongoDB的指标转换为Prome...