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

时间:2017-07-01 01:44:16

标签: node.js prometheus

我在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路径:

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

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

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

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

当我转到localhost:3030/metrics链接时,我可以阅读以下信息:

# 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和内存。我的应用程序中是否需要配置任何地方?

3 个答案:

答案 0 :(得分:3)

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

  • 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 /内存。

Documentation

中的更多详情

答案 1 :(得分:3)

您可以使用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

答案 2 :(得分:1)

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