prometheus.exporter.postgres
prometheus.exporter.postgres 组件嵌入了 postgres_exporter,用于从 PostgreSQL 数据库收集指标。
您可以通过为 prometheus.exporter.postgres 组件指定不同的标签来使用多个该组件。
用法
prometheus.exporter.postgres "<LABEL>" {
data_source_names = "<DATA_SOURCE_NAMES_LIST>"
}参数
您可以在 prometheus.exporter.postgres 中使用以下参数
| 名称 | 类型 | 描述 | 默认值 | 必需 |
|---|---|---|---|---|
data_source_names | list(secret) | 指定要连接的 PostgreSQL 服务器。 | 是 | |
custom_queries_config_path | string | 包含用于暴露为指标的自定义查询的 YAML 文件路径。 | "" | 否 |
disable_default_metrics | bool | 当为 true 时,仅暴露来自 custom_queries_config_path 中提供的指标。 | 否 | 否 |
disable_settings_metrics | bool | 禁用从 pg_settings 收集指标。 | 否 | 否 |
enabled_collectors | list(string) | 要启用的采集器列表。有关更多详细信息,请参阅下方信息。 | [] | 否 |
有关 data_source_names 中连接字符串格式的更多信息,请参阅 PostgreSQL 文档。
有关 postgres_exporter 仓库 中 custom_queries_config_path 文件示例,请参阅此处。
注意
不建议使用某些环境变量,因为它们会影响 所有
prometheus.exporter.postgres组件。有关环境变量的完整列表,请参阅postgres_exporter仓库。
默认情况下,启用的指标集与上游 postgres_exporter 相同。如果设置了 custom_queries_config_path,则会暴露给定配置文件中定义的额外指标。如果 disable_default_metrics 设置为 true,则只会暴露 custom_queries_config_path 文件中定义的指标。
可以通过设置 enabled_collectors 参数来控制一部分指标采集器。以下采集器可供选择
databasedatabase_wraparoundlockslong_running_transactionspostmasterprocess_idlereplicationreplication_slotstat_activity_autovacuumstat_bgwriterstat_checkpointer- 仅在 PostgreSQL 17 及更高版本中支持stat_databasestat_statementsstat_user_tablesstat_wal_receiverstatio_user_indexesstatio_user_tableswalxlog_location
默认情况下,以下采集器是启用的
databaselocksreplicationreplication_slotstat_bgwriterstat_databasestat_user_tablesstatio_user_tableswal
注意
由于上游 exporter 的限制,当使用多个
data_source_names时,通过enabled_collectors参数控制的采集器仅适用于列表中的第一个数据源。
块
您可以在 prometheus.exporter.postgres 中使用以下块
| 名称 | 描述 | 必需 |
|---|---|---|
autodiscovery | 数据库发现设置。 | 否 |
autodiscovery
autodiscovery 块配置了数据库发现,这些数据库不包括在 data_source_names 中指定的任何数据库之外。
支持以下参数
| 名称 | 类型 | 描述 | 默认值 | 必需 |
|---|---|---|---|---|
database_allowlist | list(string) | 要过滤的数据库列表,意味着只有这些数据库会被抓取。 | 否 | |
database_denylist | list(string) | 要过滤掉的数据库列表,意味着所有其他数据库都会被抓取。 | 否 | |
enabled | bool | 是否自动发现其他数据库。 | 否 | 否 |
如果 enabled 设置为 true 且未指定允许列表或拒绝列表,exporter 将从所有数据库抓取。
如果 autodiscovery 被禁用,database_allowlist 和 database_denylist 均无效。
导出的字段
导出以下字段,这些字段可被其他组件引用。
| 名称 | 类型 | 描述 |
|---|---|---|
targets | list(map(string)) | 可用于收集 exporter 指标的目标。 |
例如,targets 可以传递给 discovery.relabel 组件来重写目标的标签集,或传递给 prometheus.scrape 组件来收集暴露的指标。
导出的目标使用 [run 命令](../../../cli/run/) 指定的已配置的[内存流量地址](../../../../get-started/component_controller/#in-memory-traffic)。
组件健康状况
仅当配置无效时,prometheus.exporter.postgres 会报告不健康。
调试信息
prometheus.exporter.postgres 不暴露任何组件特定的调试信息。
调试指标
prometheus.exporter.postgres 不暴露任何组件特定的调试指标。
示例
从 PostgreSQL 服务器收集指标
以下示例使用 prometheus.exporter.postgres 组件,通过所有默认设置从本地运行的 PostgreSQL 服务器收集指标
// Because no autodiscovery is defined, this will only scrape the 'database_name' database, as defined
// in the DSN below.
prometheus.exporter.postgres "example" {
data_source_names = ["postgresql://username:password@localhost:5432/database_name?sslmode=disable"]
}
prometheus.scrape "default" {
targets = prometheus.exporter.postgres.example.targets
forward_to = [prometheus.remote_write.demo.receiver]
}
prometheus.remote_write "demo" {
endpoint {
url = "<PROMETHEUS_REMOTE_WRITE_URL>"
basic_auth {
username = "<USERNAME>"
password = "<PASSWORD>"
}
}
}替换以下内容
<PROMETHEUS_REMOTE_WRITE_URL>: 用于发送指标的与 Prometheusremote_write兼容的服务器 URL。<USERNAME>: 用于向remote_writeAPI 进行身份验证的用户名。<PASSWORD>: 用于向remote_writeAPI 进行身份验证的密码。
从允许列表中的数据库集收集自定义指标
以下示例使用 prometheus.exporter.postgres 组件,从一组特定数据库收集自定义指标,用 /etc/alloy/custom-postgres-metrics.yaml 中查询派生的自定义指标替换默认指标
prometheus.exporter.postgres "example" {
data_source_names = ["postgresql://username:password@localhost:5432/database_name?sslmode=disable"]
// This block configures autodiscovery to check for databases outside of the 'database_name' db
// specified in the DSN above. The database_allowlist field means that only the 'frontend_app' and 'backend_app'
// databases will be scraped.
autodiscovery {
enabled = true
database_allowlist = ["frontend_app", "backend_app"]
}
disable_default_metrics = true
custom_queries_config_path = "/etc/alloy/custom-postgres-metrics.yaml"
}
prometheus.scrape "default" {
targets = prometheus.exporter.postgres.example.targets
forward_to = [prometheus.remote_write.demo.receiver]
}
prometheus.remote_write "demo" {
endpoint {
url = "<PROMETHEUS_REMOTE_WRITE_URL>"
basic_auth {
username = "<USERNAME>"
password = "<PASSWORD>"
}
}
}替换以下内容
<PROMETHEUS_REMOTE_WRITE_URL>: 用于发送指标的与 Prometheusremote_write兼容的服务器 URL。<USERNAME>: 用于向remote_writeAPI 进行身份验证的用户名。<PASSWORD>: 用于向remote_writeAPI 进行身份验证的密码。
收集除拒绝列表中的数据库外的所有数据库的指标
此示例使用 prometheus.exporter.postgres 组件收集除 secrets 数据库外的所有数据库的自定义指标。
prometheus.exporter.postgres "example" {
data_source_names = ["postgresql://username:password@localhost:5432/database_name?sslmode=disable"]
// The database_denylist field will filter out those databases from the list of databases to scrape,
// meaning that all databases *except* these will be scraped.
//
// In this example it will scrape all databases except for the one named 'secrets'.
autodiscovery {
enabled = true
database_denylist = ["secrets"]
}
}
prometheus.scrape "default" {
targets = prometheus.exporter.postgres.example.targets
forward_to = [prometheus.remote_write.demo.receiver]
}
prometheus.remote_write "demo" {
endpoint {
url = "<PROMETHEUS_REMOTE_WRITE_URL>"
basic_auth {
username = "<USERNAME>"
password = "<PASSWORD>"
}
}
}替换以下内容
<PROMETHEUS_REMOTE_WRITE_URL>: 用于发送指标的与 Prometheusremote_write兼容的服务器 URL。<USERNAME>: 用于向remote_writeAPI 进行身份验证的用户名。<PASSWORD>: 用于向remote_writeAPI 进行身份验证的密码。
兼容组件
prometheus.exporter.postgres 导出的内容可被以下组件使用
- 使用 Targets 的组件
注意
连接某些组件可能不合理,或者组件可能需要进一步配置才能正确连接。有关详细信息,请参阅链接的文档。



