
Node.js 导出器
本页内容
引言
以下快速入门指南提供了 prom-client for Node.js Prometheus 指标导出器的设置说明以及预配置的仪表盘、告警规则和记录规则。完成本快速入门中的步骤后,您将
设置并配置 prom-client for Node.js 以收集 Node.js 应用程序指标,如事件循环延迟、活动句柄和 GC 指标。prom-client for Node.js 将这些指标以 Prometheus 格式公开。
配置 Prometheus 来抓取 prom-client for Node.js 指标,并可选地将其发送到 Grafana Cloud。
设置一组预配置和精选的记录规则,以缓存频繁的 Prometheus 查询。
导入 Grafana 仪表盘以可视化您的指标数据。
设置 Prometheus 告警规则,以根据您的指标数据触发告警。
指标用量
此导出器默认发布约 78 个 Prometheus 时间序列。要查看此导出器默认发送的指标列表,请在此处下载示例指标抓取。
请注意,根据配置,prom-client for Node.js 可能会收集和发布比默认集多得多的指标。要了解有关配置 prom-client for Node.js 和切换其收集器的更多信息,请参阅 prom-client for Node.js 的 GitHub 仓库。
除了切换 prom-client for Node.js 的设置外,您还可以通过丢弃不需要存储在 Prometheus 或 Grafana Cloud 中的时间序列来减少指标使用量。要了解如何执行此操作,请参阅 Grafana Cloud 文档中的使用 relabeling 减少 Prometheus 指标使用量。
Grafana Cloud 的 Node.js 集成
如果您正在使用 Grafana Cloud,则可以通过安装 Node.js 集成来跳过本指南中的所有步骤,该集成旨在帮助您通过几个命令和点击快速启动并运行。免费注册。
要了解如何使用 Node.js 集成收集 Node.js 指标,请参阅 Grafana Cloud 文档中的Node.js 集成。
本页内容
prom-client for Node.js 快速入门
在本指南中,您将学习如何设置和配置 prom-client for Node.js 以收集 Node.js 指标(如事件循环延迟和活动句柄),并将其以 Prometheus 格式公开。然后,您将配置 Prometheus 来抓取 Node.js 指标,并可选地将其发送到 Grafana Cloud。最后,您将设置一组预配置和精选的 Grafana 仪表盘和 告警规则。完成本指南后,您将拥有可用于可视化 Node.js 指标的仪表盘以及一组预配置的告警。
如果您正在使用 Grafana Cloud,Node.js 集成可以帮助您快速启动并运行。Node.js 集成将 prom-client for Node.js 嵌入到 Grafana Cloud Agent 中,并自动配置告警规则和仪表盘,这样您就不必手动完成本指南中的步骤。要了解如何使用 Node.js 集成设置 prom-client for Node.js,请参阅 Grafana Cloud 文档中的Node.js 集成。
先决条件
在开始之前,您应该具备以下条件
- 您的机器上安装了 Node.js。要了解更多信息,请参阅 Node.js 网站上的下载。
- Prometheus 在您的环境中或直接在机器上运行。要了解如何安装 Prometheus,请参阅 Prometheus 文档中的安装。
- Grafana 在您的环境中或直接在机器上运行。要了解如何安装 Grafana,请参阅 Grafana 文档中的安装 Grafana。
- (可选)Grafana Cloud 帐户。Grafana Cloud 托管 Grafana 和基于 Mimir 的 Prometheus 指标端点。您仍然需要使用在您的环境中安装的 Prometheus 或 Grafana Cloud Agent 来抓取指标。要了解有关 Grafana Cloud 的更多信息,请参阅Grafana Cloud。
步骤 1:设置 prom-client for Node.js
在此步骤中,您将设置 prom-client for Node.js 以收集 Node.js 指标并以 Prometheus 格式公开它们。本指南使用带有 Node.js 版本 15.11.0
的 Ubuntu 20.04 系统。步骤可能因您的操作系统和 Node.js 版本而略有不同。
本指南将演示新 Node.js 应用程序的设置步骤。要为现有 Node.js 应用程序进行仪表,请参阅 prom-client
的 GitHub 仓库。
首先,登录您的机器并创建一个名为 app
的新应用程序目录。进入此目录
mkdir app
cd
使用 npm init
初始化 Node 项目
npm init
除以下提示外,其他都使用默认值
entry point: (index.js)
对于此提示,输入 app.js
,然后按 ENTER
。
接下来,使用 npm
安装 express
和 prom-client
npm install express prom-client --save
最后,使用您喜欢的文本编辑器创建最小的应用程序。打开一个名为 app.js
的文件,并粘贴以下内容
import express from 'express';
import { collectDefaultMetrics, register } from 'prom-client';
collectDefaultMetrics();
const app = express();
app.get('/metrics', async (_req, res) => {
try {
res.set('Content-Type', register.contentType);
res.end(await register.metrics());
} catch (err) {
res.status(500).end(err);
}
});
app.listen(4001, '0.0.0.0');
这个精简的应用程序收集一套标准的 Node.js 指标,并在 /metrics
端点以 Prometheus 格式公开它们。它在 localhost
的端口 4001
上监听请求。
完成后,保存并关闭文件。
最后,使用 Node 运行应用程序
node app.js
您将看不到任何输出,但您的 shell 应该会挂起。在另一个会话中,使用 curl
模拟 Prometheus 抓取
curl localhost:4001/metrics
. . .
# HELP nodejs_gc_duration_seconds Garbage collection duration by kind, one of major, minor, incremental or weakcb.
# TYPE nodejs_gc_duration_seconds histogram
nodejs_gc_duration_seconds_bucket{le="0.001",kind="incremental"} 2
nodejs_gc_duration_seconds_bucket{le="0.01",kind="incremental"} 4
nodejs_gc_duration_seconds_bucket{le="0.1",kind="incremental"} 4
nodejs_gc_duration_seconds_bucket{le="1",kind="incremental"} 4
nodejs_gc_duration_seconds_bucket{le="2",kind="incremental"} 4
nodejs_gc_duration_seconds_bucket{le="5",kind="incremental"} 4
nodejs_gc_duration_seconds_bucket{le="+Inf",kind="incremental"} 4
nodejs_gc_duration_seconds_sum{kind="incremental"} 0.007554849
nodejs_gc_duration_seconds_count{kind="incremental"} 4
nodejs_gc_duration_seconds_bucket{le="0.001",kind="major"} 0
nodejs_gc_duration_seconds_bucket{le="0.01",kind="major"} 2
nodejs_gc_duration_seconds_bucket{le="0.1",kind="major"} 2
nodejs_gc_duration_seconds_bucket{le="1",kind="major"} 2
nodejs_gc_duration_seconds_bucket{le="2",kind="major"} 2
nodejs_gc_duration_seconds_bucket{le="5",kind="major"} 2
nodejs_gc_duration_seconds_bucket{le="+Inf",kind="major"} 2
nodejs_gc_duration_seconds_sum{kind="major"} 0.007220236999999999
nodejs_gc_duration_seconds_count{kind="major"} 2
如果您看到上述输出,则表示您已成功将 prom-client 安装到您的 Node.js 应用程序中。您已准备好开始使用 Prometheus 抓取 Node.js 指标。
除了 collectDefaultMetrics
,prom-client
还可以收集自定义 Node.js 指标,并允许您配置指标标签。要了解有关这些功能的更多信息,请参阅 prom-client for Node.js GitHub 仓库。
步骤 2:使用 Prometheus 抓取 prom-client 指标
现在 prom-client 已在您的机器上运行,您可以配置一个 Prometheus 抓取任务来收集和存储 Node.js 指标。
将以下抓取任务配置添加到您的 prometheus.yml
配置文件中的 scrape_configs
部分
- job_name: nodejs
static_configs:
- targets: ['localhost:4001']
将 localhost
替换为运行 Node.js 的机器的 IP 地址。如果您在同一台机器上运行 Prometheus,则这将是 localhost
。要了解有关配置 Prometheus 的更多信息,请参阅 Prometheus 文档中的配置。
如果您没有 prometheus.yml
配置文件,请使用您喜欢的文本编辑器创建一个简单的文件。打开您首选的文本编辑器,并粘贴以下 Prometheus 配置
global:
scrape_interval: 15s
scrape_configs:
- job_name: nodejs
static_configs:
- targets: ['localhost:4001']
此配置告诉 Prometheus 每 15 秒抓取所有任务。唯一配置的抓取任务名为 nodejs
,并定义了一个 localhost:4001
目标。默认情况下,Prometheus 将使用 HTTP 抓取 /metrics
端点。
保存并关闭文件。然后,您可以使用以下命令运行 Prometheus 并指定该文件
./prometheus --config.file=./prometheus.yml
将指标发送到 Grafana Cloud
要将 Node.js 指标从 Prometheus 发送到 Grafana Cloud,请在您的 prometheus.yml
配置文件中配置 remote_write
参数。要了解更多信息,请参阅 Grafana Cloud 文档中的Metrics — Prometheus。要了解有关 remote_write
参数的更多信息,请参阅 Prometheus 文档中的remote_write
。
步骤 3:配置仪表盘
本快速入门包括一个仪表盘
- Node.js 概述
要了解如何将这些仪表盘导入 Grafana,请参阅 Grafana 文档中的导入仪表盘。
您可以在此处获取仪表盘。
步骤 4:配置告警
使用 Prometheus 告警规则,您可以定义当 PromQL 表达式在一段时间内超过某个阈值或满足指定条件时触发的告警。例如,您可以定义一个 HighRequestLatency
告警,当请求延迟指标在一段时间内大于某个阈值时触发。一旦触发告警条件,告警将进入 Pending
状态。在满足由 for
参数定义的时间段后,告警将进入 Firing
状态。您可以使用 Alertmanager 等工具配置触发告警的路由和通知。Alertmanager 也内置于 Grafana Cloud 中。
您可以在此处获取告警规则 YAML 文件。
将告警规则加载到 Prometheus 中
要将告警规则加载到 Prometheus 中,请在您的 prometheus.yml
配置文件中添加以下内容
rule_files:
- "nodejs-rules.yml"
请务必将 nodejs-rules.yml
替换为您的 Node.js 应用程序告警规则 YAML 文件的路径。
将告警规则加载到 Grafana Cloud 中
要了解如何将告警规则加载到 Grafana Cloud 中,请参阅使用 cortextool 配置 Prometheus 和 Loki 规则。
结论
在本快速入门中,您在 Linux 机器上安装并运行了 prom-client for Node.js。然后,您配置了 Prometheus 来抓取 prom-client for Node.js 公开的数据库和 Node.js 应用程序指标。您将记录规则和告警规则加载到 Prometheus 中,最后导入 Grafana 仪表盘以可视化您的 Node.js 应用程序指标。
如果您正在使用 Grafana Cloud,则可以通过使用 Grafana Cloud Agent 安装 Node.js 应用程序集成来跳过本指南中的所有步骤。此集成将预配置的 prom-client for Node.js 嵌入到 Agent 中,并自动配置 Grafana 仪表盘以及 Prometheus 告警和记录规则,因此您不必手动导入它们。要了解如何设置 Node.js 应用程序集成,请参阅Grafana Cloud 集成。
仪表盘、记录规则和告警规则是使用 prom-client for Node.js Mixin 生成的。Mixin 是由主题专家精心策划和设计的可重用仪表盘、记录规则和告警模板。要了解更多信息,请参阅 Node.js 应用程序 Mixin 仓库。
本页内容
本快速入门包含以下告警规则
NodejsDown
instance
上的 Node.js job
未运行。
groups:
- name: NodejsAlerts
rules:
- alert: NodejsDown
expr: process_start_time_seconds != 1
for: 5m
labels:
severity: critical
annotations:
description: 'Node.js {{$labels.job}} on {{$labels.instance}} is not up.'
summary: Node.js not up
此告警规则 YAML 文件是使用 prom-client for Node.js mixin 生成的。
本页内容
Grafana Cloud 带有不断扩展的集成集,可在几分钟内快速启动并运行可观察性堆栈。Node.js 应用程序集成内置于 Grafana Cloud Agent 中,它公开并抓取重要的 Node.js 应用程序指标,并将其推送到 Grafana Cloud。Agent 将使用嵌入的 prom-client for Node.js 抓取指标,Grafana Cloud 将自动配置定制的 Grafana 仪表盘和告警,用于可视化和处理这些数据。
要了解更多信息,请查看Grafana Cloud 文档。
工作原理
配置、安装、连接和维护 Prometheus 监控组件通常需要重要的领域知识。从设置到仪表盘和告警可能需要相当长的时间。作为 Grafana 的创建者以及 Prometheus 和 Cortex 的核心贡献者,我们构建了简单的集成来抽象掉一些此类工作,以便快速入门。工作原理
- 注册(或登录)免费 Grafana Cloud 帐户。
- 选择您要观察的目标(不断扩展的目录)。
- 运行一行命令来安装 Grafana Agent。Agent 嵌入并预配置了 Exporter 来公开默认指标,并将其推送到 Grafana Cloud 指标后端。
- 瞧!您将看到定制的 Grafana 仪表盘,并将受益于合理的默认告警。
正在寻找不同的 Exporter 或集成?请查看我们不断增长的流行组件集成库,例如 MySQL、Postgres、Redis、Memcached 等等。