
Postgres Exporter
本页内容
简介
以下快速入门提供了 Postgres Exporter 的设置说明以及预配置的仪表盘和告警规则。按照本快速入门中的步骤操作后,您将具备
设置并配置 Postgres Exporter,以收集诸如每秒查询次数 (QPS) 以及每秒获取/返回/插入/更新/删除的行数等 Postgres 指标。Postgres Exporter 会将这些指标以 Prometheus 风格暴露出来。
配置 Prometheus 抓取 Postgres Exporter 指标,并可选地将其发送到 Grafana Cloud。
导入 Grafana 仪表盘以可视化您的指标数据。
设置 Prometheus 告警规则,以便对您的指标数据进行告警。
指标用量
此导出器默认发布约 450 个 Prometheus 时序数据。要查看此导出器默认发布的指标列表,请下载示例指标抓取数据此处。
请注意,根据配置,Postgres Exporter 可能会收集和发布比此默认集合多得多的指标。要了解有关配置 Postgres Exporter 和切换其收集器的更多信息,请参阅 Postgres Exporter GitHub 仓库。
除了切换 Postgres Exporter 的设置外,您还可以通过丢弃不需要存储在 Prometheus 或 Grafana Cloud 中的时序数据来减少指标用量。要了解如何做到这一点,请参阅 Grafana Cloud 文档中的使用 relabeling 减少 Prometheus 指标用量。
Grafana Cloud 的 Postgres Exporter 集成
如果您正在使用 Grafana Cloud,则可以通过安装 Postgres Exporter 集成跳过本指南中的所有步骤,该集成旨在帮助您通过几个命令和点击快速启动和运行。免费注册。
要了解如何使用 Postgres Exporter 集成设置 Postgres Exporter,请参阅 Grafana Cloud 文档中的Postgres Exporter 集成。
本页内容
Postgres Exporter 快速入门
在本指南中,您将学习如何设置和配置 Postgres Exporter,以收集诸如每秒查询次数 (QPS)、数据库死锁和冲突等 Postgres 指标,并将它们以 Prometheus 风格暴露出来。然后,您将配置 Prometheus 以抓取 Postgres 指标,并可选地将其发送到 Grafana Cloud。最后,您将设置一套预配置和精心挑选的 Grafana 仪表盘和 告警规则。本指南结束时,您将拥有可用于可视化 Postgres 指标的仪表盘,以及一套预配置的告警。
如果您正在使用Grafana Cloud,则 Postgres 集成可以帮助您快速启动和运行。Postgres 集成将 Postgres Exporter 嵌入到 Grafana Cloud Agent 中,并自动配置告警规则和仪表盘,因此您无需按照本指南的步骤操作。要了解如何使用 Postgres 集成设置 Postgres Exporter,请参阅 Grafana Cloud 文档中的Postgres 集成。
先决条件
在开始之前,您应该具备以下条件
- 一台与 Postgres Exporter 版本兼容的机器。要查看可用版本列表,请参阅版本。
- 您的机器上运行着 Postgres。要了解有关安装和运行 Postgres 的更多信息,请参阅Postgres 安装指南。
- 您的环境中或直接在机器上运行着 Prometheus。要了解如何安装 Prometheus,请参阅 Prometheus 文档中的安装。
- 您的环境中或直接在机器上运行着 Grafana。要了解如何安装 Grafana,请参阅 Grafana 文档中的安装 Grafana。
- (可选) 一个 Grafana Cloud 帐户。Grafana Cloud 托管 Grafana 和基于 Mimir 的 Prometheus 指标端点。您仍然需要使用环境中安装的 Prometheus 或 Grafana Cloud Agent 来抓取指标。要了解有关 Grafana Cloud 的更多信息,请参阅Grafana Cloud。
步骤 1:设置 Postgres Exporter
在此步骤中,您将在机器上设置 Postgres Exporter,以收集和暴露 Prometheus 格式的 Postgres 指标。本指南使用带有 Postgres 13.2 的 Ubuntu 20.04 系统。具体步骤可能因您的操作系统和 Postgres 版本而略有不同。
首先,登录到您的机器并下载相应的 Postgres Exporter 二进制文件。本指南使用 linux-amd64
二进制文件,但您应选择与您系统操作系统和架构对应的文件
wget https://github.com/prometheus-community/postgres_exporter/releases/download/v0.9.0/postgres_exporter-0.9.0.linux-amd64.tar.gz
请将 v0.9.0
替换为您想要安装的版本。本指南可能会过时,因此最好查看 Postgres Exporter 发布页面以获取最新的稳定版本。
解压 tarball 并 cd
进入目录
tar xvfz postgres_exporter-*.linux-amd64.tar.gz
cd postgres_exporter-*.linux-amd64
我们将以 postgres
用户身份运行 Postgres 导出器来获取数据库指标。要以非超级用户身份获取指标,您可以按照Postgres Exporter Github 仓库中的说明进行操作。
我们需要告知导出器如何连接到 Postgres 服务器。我们通过设置以下环境变量来完成此操作
export DATA_SOURCE_NAME='postgresql://postgres:enter_password_here@postgres_hostname:5432/postgres?sslmode=disable'
请务必将 postgres
替换为您将用于从 Postgres 获取指标的用户,将 enter_password_here
替换为您用户的密码,将 postgres_hostname
替换为 Postgres 实例的主机名(如果与 Postgres 在同一台机器上,则为 localhost)。
最后,运行导出器
./postgres_exporter
INFO[0000] Starting Server: :9187 source="postgres_exporter.go:1850"
如果您看到以上输出,则表示您已成功运行 Postgres Exporter。
Postgres Exporter 在端口 9187
上以 Prometheus 格式发布 Postgres 指标。您可以使用 curl
进行测试。您需要打开新的 SSH 会话或将 Postgres Exporter 进程置于后台才能使用 curl
。
curl https://:9187/metrics
# TYPE pg_exporter_scrapes_total counter
pg_exporter_scrapes_total 1
# HELP pg_locks_count Number of locks
# TYPE pg_locks_count gauge
pg_locks_count{datname="postgres",mode="accessexclusivelock",server="localhost:5432"} 0
pg_locks_count{datname="postgres",mode="accesssharelock",server="localhost:5432"} 1
pg_locks_count{datname="postgres",mode="exclusivelock",server="localhost:5432"} 0
pg_locks_count{datname="postgres",mode="rowexclusivelock",server="localhost:5432"} 0
pg_locks_count{datname="postgres",mode="rowsharelock",server="localhost:5432"} 0
pg_locks_count{datname="postgres",mode="sharelock",server="localhost:5432"} 0
pg_locks_count{datname="postgres",mode="sharerowexclusivelock",server="localhost:5432"} 0
pg_locks_count{datname="postgres",mode="shareupdateexclusivelock",server="localhost:5432"} 0
pg_locks_count{datname="postgres",mode="sireadlock",server="localhost:5432"} 0
如果您看到以上输出,则表示您已准备好开始使用 Prometheus 抓取 Postgres 指标。
为了避免从命令行运行和管理 Postgres Exporter,您可以创建一个 systemd
服务。要了解如何操作,请参阅创建 systemd 服务来管理 Agent。请将 Agent 二进制文件的路径替换为 Postgres Exporter 的路径。
您可以将 --extend.query-path
标志指定为您 queries.yaml
文件的路径,您可以在其中指定自定义查询。要了解有关这些功能的更多信息,请参阅Postgres Exporter GitHub 仓库。
步骤 2:使用 Prometheus 抓取 Postgres Exporter
现在 Postgres Exporter 已在您的机器上启动并运行,您可以配置一个 Prometheus 抓取任务来收集和存储 Postgres Exporter 指标。
将以下抓取任务配置添加到您的 prometheus.yml
配置文件的 scrape_configs
部分
- job_name: postgres
static_configs:
- targets: ['postgres_exporter_machine_IP_address:9187']
请将 postgres_exporter_machine_IP_address
替换为运行 Postgres Exporter 的机器的 IP 地址。如果您在同一台机器上运行 Prometheus,则此地址为 localhost
。要了解有关配置 Prometheus 的更多信息,请参阅 Prometheus 文档中的配置。
如果您没有 prometheus.yml
配置文件,请使用您喜欢的文本编辑器创建一个简单的文件。打开您首选的文本编辑器,粘贴以下 Prometheus 配置
global:
scrape_interval: 15s
scrape_configs:
- job_name: postgres
static_configs:
- targets: ['postgres_exporter_machine_IP_address:9187']
此配置指示 Prometheus 每隔 15 秒抓取所有任务。唯一配置的抓取任务名为 postgres
,并定义了 postgres_exporter_machine_IP_address:9187
目标。默认情况下,Prometheus 将使用 HTTP 抓取 /metrics
端点。
保存并关闭文件。然后,您可以使用以下命令运行 Prometheus 并指定该文件
./prometheus --config.file=./prometheus.yml
将指标发送到 Grafana Cloud
要将 Prometheus 中的 Postgres Exporter 指标发送到 Grafana Cloud,请在您的 prometheus.yml
配置文件中配置 remote_write
参数。要了解更多信息,请参阅 Grafana Cloud 文档中的指标 — Prometheus。要了解有关 remote_write
参数的更多信息,请参阅 Prometheus 文档中的remote_write
。
步骤 3:配置仪表盘
此快速入门包含一个仪表盘
- Postgres 概览
要了解如何将这些仪表盘导入 Grafana,请参阅 Grafana 文档中的导入仪表盘。
您可以在此处获取仪表盘。
步骤 4:配置告警
借助 Prometheus 告警规则,您可以定义在 PromQL 表达式超出某个阈值或在一段时间内满足指定条件时触发的告警。例如,您可以定义一个 HighRequestLatency
告警,当请求延迟指标在一段时间内大于某个阈值时触发。一旦触发告警条件,告警就会进入 Pending
(待处理)状态。在满足条件达到 for
参数定义的时间段后,告警将进入 Firing
(触发)状态。您可以使用诸如Alertmanager之类的工具配置触发告警的路由和通知。Alertmanager 也内置在 Grafana Cloud 中。
您可以在此处获取告警规则 YAML 文件。
将告警规则加载到 Prometheus 中
要将告警规则加载到 Prometheus 中,请将以下内容添加到您的 prometheus.yml
配置文件中
rule_files:
- "postgres_exporter_alerting_rules.yml"
请务必将 postgres_exporter_alerting_rules.yml
替换为您的 Postgres 告警规则 YAML 文件的路径。
将告警规则加载到 Grafana Cloud 中
要了解如何将告警规则加载到 Grafana Cloud 中,请参阅使用 cortextool 的 Prometheus 和 Loki 规则。
总结
在此快速入门中,您在 Linux 机器上安装并运行了 Postgres Exporter。然后,您配置了 Prometheus 以抓取由 Postgres Exporter 暴露的数据库和 Postgres 指标。您将告警规则加载到 Prometheus 中,最后导入了 Grafana 仪表盘以可视化您的 Postgres 指标。
如果您正在使用 Grafana Cloud,则可以通过安装带有 Grafana Cloud Agent 的 Postgres 集成来跳过本指南中的所有步骤。此集成将预配置的 Postgres Exporter 嵌入到 Agent 中,并自动配置 Grafana 仪表盘以及 Prometheus 告警和记录规则,因此您无需手动导入它们。要了解如何设置 Postgres 集成,请参阅Grafana Cloud 集成。
仪表盘、记录规则和告警规则是使用 Postgres Exporter Mixin 生成的。Mixin 是由主题专家策划和设计的、可重复使用的仪表盘、记录规则和告警模板。要了解更多信息,请参阅Postgres Mixin仓库。
本页内容
Postgres 概览仪表盘
此仪表盘包含以下指标的面板
- 当前 QPS
- 获取、返回、插入、更新、删除的行数
- 数据库死锁和冲突
- 缓存命中率
- 活跃连接数
- 缓冲区
您可以使用以下仪表盘 ID 直接导入此仪表盘:14114
。
您可以在此处获取仪表盘 JSON 文件。
此仪表盘是使用 Postgres Exporter mixin生成的。
本页内容
此快速入门包含以下告警规则
PostgreSQLMaxConnectionsReached
Postgres 可用连接已用尽
PostgreSQLHighConnections
Postgres 连接数超过当前配置的最大连接数限制的 80%
PostgreSQLDown
Postgres 未处理查询
PostgreSQLSlowQueries
Postgres 慢查询数量较高
PostgreSQLQPS
Postgres 每秒查询次数较高
PostgreSQLCacheHitRatio
Postgres 缓存命中率较低
下载以下告警规则 YAML 文件groups:
- name: PostgreSQL
rules:
- alert: PostgreSQLMaxConnectionsReached
expr: sum(pg_stat_activity_count) by (instance) >= sum(pg_settings_max_connections) by (instance) - sum(pg_settings_superuser_reserved_connections) by (instance)
for: 1m
labels:
severity: email
annotations:
summary: "{{ $labels.instance }} has maxed out Postgres connections."
description: "{{ $labels.instance }} is exceeding the currently configured maximum Postgres connection limit (current value: {{ $value }}s). Services may be degraded - please take immediate action (you probably need to increase max_connections in the Docker image and re-deploy."
- alert: PostgreSQLHighConnections
expr: sum(pg_stat_activity_count) by (instance) > (sum(pg_settings_max_connections) by (instance) - sum(pg_settings_superuser_reserved_connections) by (instance)) * 0.8
for: 10m
labels:
severity: email
annotations:
summary: "{{ $labels.instance }} is over 80% of max Postgres connections."
description: "{{ $labels.instance }} is exceeding 80% of the currently configured maximum Postgres connection limit (current value: {{ $value }}s). Please check utilization graphs and confirm if this is normal service growth, abuse or an otherwise temporary condition or if new resources need to be provisioned (or the limits increased, which is mostly likely)."
- alert: PostgreSQLDown
expr: pg_up != 1
for: 1m
labels:
severity: email
annotations:
summary: "PostgreSQL is not processing queries: {{ $labels.instance }}"
description: "{{ $labels.instance }} is rejecting query requests from the exporter, and thus probably not allowing DNS requests to work either. User services should not be effected provided at least 1 node is still alive."
- alert: PostgreSQLSlowQueries
expr: avg(rate(pg_stat_activity_max_tx_duration{datname!~"template.*"}[2m])) by (datname) > 2 * 60
for: 2m
labels:
severity: email
annotations:
summary: "PostgreSQL high number of slow on {{ $labels.cluster }} for database {{ $labels.datname }} "
description: "PostgreSQL high number of slow queries {{ $labels.cluster }} for database {{ $labels.datname }} with a value of {{ $value }} "
- alert: PostgreSQLQPS
expr: avg(irate(pg_stat_database_xact_commit{datname!~"template.*"}[5m]) + irate(pg_stat_database_xact_rollback{datname!~"template.*"}[5m])) by (datname) > 10000
for: 5m
labels:
severity: email
annotations:
summary: "PostgreSQL high number of queries per second {{ $labels.cluster }} for database {{ $labels.datname }}"
description: "PostgreSQL high number of queries per second on {{ $labels.cluster }} for database {{ $labels.datname }} with a value of {{ $value }}"
- alert: PostgreSQLCacheHitRatio
expr: avg(rate(pg_stat_database_blks_hit{datname!~"template.*"}[5m]) / (rate(pg_stat_database_blks_hit{datname!~"template.*"}[5m]) + rate(pg_stat_database_blks_read{datname!~"template.*"}[5m]))) by (datname) < 0.98
for: 5m
labels:
severity: email
annotations:
summary: "PostgreSQL low cache hit rate on {{ $labels.cluster }} for database {{ $labels.datname }}"
description: "PostgreSQL low on cache hit rate on {{ $labels.cluster }} for database {{ $labels.datname }} with a value of {{ $value }}"
此告警规则 YAML 文件是使用 Postgres Exporter mixin生成的。
本页内容
Grafana Cloud 提供了一套不断扩展的集成,可在几分钟内快速启动和运行可观测性堆栈。Postgres 集成内置于 Grafana Cloud Agent 中,可暴露和抓取重要的 Postgres 指标,并将其推送到 Grafana Cloud。Agent 将使用嵌入式 Postgres Exporter 抓取指标,而 Grafana Cloud 将自动提供定制的 Grafana 仪表盘和告警,用于可视化和处理这些数据。
要了解更多信息,请查看Grafana Cloud 文档。
工作原理
配置、安装、连接和维护 Prometheus 监控组件通常需要大量的领域知识。从设置到拥有仪表盘和告警可能需要相当长的时间。作为 Grafana 的创建者以及 Prometheus 和 Cortex 的核心贡献者,我们构建了简单的集成,以抽象掉一部分工作,从而帮助您快速入门。工作原理
- 注册(或登录)免费 Grafana Cloud 帐户。
- 选择您要观测的目标(一个不断扩展的目录)。
- 运行一条命令安装 Grafana Agent。该 Agent 嵌入并预配置 Exporter 以暴露默认指标,并将其推送到 Grafana Cloud 指标后端。
- 瞧!您将看到定制的 Grafana 仪表盘,并可受益于合理的默认告警设置。
正在寻找其他 Exporter 或集成?请查看我们不断增长的流行组件集成库,例如 MySQL、Postgres、Redis、Memcached 等。