我在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和内存。我的应用程序中是否需要配置任何地方?
答案 0 :(得分:3)
查看swagger-stats模块。它公开了Prometheus指标,包括node.js进程的内存和CPU使用情况:
它还公开了API指标,因此您可以使用Prometheus和Grafana监控API使用情况以及CPU /内存。
中的更多详情答案 1 :(得分:3)
您可以使用appmetrics-prometheus-client npm包,以简单的步骤检测代码。
它在/metrics
端点公开以下指标:
CPU:
内存:
事件循环:
垃圾收集:
HTTP请求:
答案 2 :(得分:1)
你必须自己修剪一些东西。因此,您可以按照设定的时间间隔自行进行这些测量,并使用类似https://nodejs.org/api/process.html#process_process_memoryusage的内容来获取内存使用情况,更新指标并收集它们。