gpt4 book ai didi

node.js - 如何在Mac上使用Prometheus监控nodejs cpu和内存使用情况?

转载 作者:太空宇宙 更新时间:2023-11-03 22:42:09 27 4
gpt4 key购买 nike

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

最佳答案

看看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

关于node.js - 如何在Mac上使用Prometheus监控nodejs cpu和内存使用情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44856659/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com