如何使用 Prometheus 监控 nodejs cpu 和 Mac 上的内存使用情况?

How to use Prometheus to monitor nodejs cpu and memory usage on Mac?

我在 Mac OS 上有一个 nodejs 应用程序,我想监控它的 cpu 和内存使用情况。我设置了 Prometheus 服务器,配置如下:

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
      monitor: 'codelab-monitor'

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first.rules"
  # - "second.rules"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'controller'
    scrape_interval: 1s
    static_configs:
      - targets: ['172.16.123.1:3030']
        labels:
          group: 'demo'

  - job_name: "node"
    scrape_interval: "15s"
    static_configs:
       - targets: ['localhost:9100']

在我的 nodejs 应用程序中,我导入了 prom-client 依赖项。并创建了一个 /metrics 路径,其中 returns 指标数据:

const express = require('express');
const app = express();

app.get('/metrics', (req, res) => {
  const m = prom.register.metrics();
  console.log(m);
  res.end(m);
});

在我的服务class中,我使用prom.Counter来记录请求数

const counter = new prom.Counter('create_connection', 'The number of requests')
counter.inc()

当我去 localhost:3030/metrics link 时,我可以阅读以下信息:

# HELP nodejs_gc_runs_total Count of total garbage collections.
# TYPE nodejs_gc_runs_total counter

# HELP nodejs_gc_pause_seconds_total Time spent in GC Pause in seconds.
# TYPE nodejs_gc_pause_seconds_total counter

# HELP nodejs_gc_reclaimed_bytes_total Total number of bytes reclaimed by GC.
# TYPE nodejs_gc_reclaimed_bytes_total counter

# HELP create_connection The number of requests
# TYPE create_connection counter
create_connection 18

我可以看到 create_connection 被调用了 18 次。现在我转到 Prometheus 图表页面,我可以看到 create_connection 的图表。但我的问题是如何查看我的 nodejs 应用程序在一段时间内消耗了多少 CPU 和内存。我的应用程序中有什么地方需要配置吗?

你必须自己测试一些东西。因此,您可以按设定的时间间隔自行进行这些测量,并使用 https://nodejs.org/api/process.html#process_process_memoryusage 之类的方法来获取内存使用情况,更新指标,然后收集这些指标。

看看 swagger-stats 模块。它公开了 Prometheus 指标,包括内存和 CPU node.js 进程的使用情况:

  • nodejs_process_memory_rss_bytes
  • nodejs_process_memory_heap_total_bytes
  • nodejs_process_memory_heap_used_bytes
  • nodejs_process_memory_external_bytes
  • nodejs_process_cpu_usage_percentage

它还公开了 API 指标,因此您可以使用 Prometheus 和 Grafana 监控 API 使用情况以及 CPU/Memory。

Documentation中有更多详细信息

您可以使用 appmetrics-prometheus-client npm 包,它通过简单的步骤检测代码。

它在 /metrics 端点公开以下指标:

  1. CPU:

    • cpu.process
    • cpu.system
  2. 内存:

    • memory.process.private
    • memory.process.physical
    • memory.process.virtual
    • memory.system.used
    • memory.system.total
  3. 事件循环:

    • eventloop.latency.min
    • eventloop.latency.max
    • eventloop.latency.avg
  4. 垃圾回收:

    • gc.size
    • gc.used
    • gc.duration
  5. HTTP 请求:

    • http