prometheus.exporter.postgres
prometheus.exporter.postgres
组件嵌入了 postgres_exporter
,用于从 PostgreSQL 数据库收集指标。
可以指定多个 prometheus.exporter.postgres
组件,只需为它们指定不同的标签即可。
用法
prometheus.exporter.postgres "LABEL" {
data_source_names = DATA_SOURCE_NAMES_LIST
}
参数
支持以下参数
名称 | 类型 | 描述 | 默认值 | 必需 |
---|---|---|---|---|
data_source_names | list(secret) | 指定要连接的 PostgreSQL 服务器。 | 是 | |
disable_settings_metrics | bool | 禁用从 pg_settings 收集指标。 | false | 否 |
disable_default_metrics | bool | 当为 true 时,仅公开从 custom_queries_config_path 提供的指标。 | false | 否 |
custom_queries_config_path | string | 包含要公开为指标的自定义查询的 YAML 文件的路径。 | "" | 否 |
enabled_collectors | list(string) | 要启用的收集器列表。有关更多详细信息,请参阅以下信息。 | [] | 否 |
有关 data_source_names
中连接字符串格式的更多信息,请参阅 PostgreSQL 文档。
有关 custom_queries_config_path
文件示例,请参阅 postgres_exporter
存储库。
注意
有许多不建议使用的环境变量,因为它们会影响所有
prometheus.exporter.postgres
组件。有关环境变量的完整列表,请参阅postgres_exporter
存储库。
默认情况下,启用的指标集与上游 postgres_exporter
中的相同。如果设置了 custom_queries_config_path
,则会公开给定配置文件中定义的其他指标。如果将 disable_default_metrics
设置为 true
,则仅公开 custom_queries_config_path
文件中定义的指标。
可以通过设置 enabled_collectors
参数来控制指标收集器的子集。以下收集器可供选择
database
database_wraparound
locks
long_running_transactions
postmaster
process_idle
replication
replication_slot
stat_activity_autovacuum
stat_bgwriter
stat_database
stat_statements
stat_user_tables
stat_wal_receiver
statio_user_indexes
statio_user_tables
wal
xlog_location
默认情况下,启用以下收集器
database
locks
replication
replication_slot
stat_bgwriter
stat_database
stat_user_tables
statio_user_tables
wal
注意
由于上游导出器的限制,当使用多个
data_source_names
时,通过enabled_collectors
参数控制的收集器将仅应用于列表中的第一个数据源。
块
支持以下块
层次结构 | 块 | 描述 | 必需 |
---|---|---|---|
autodiscovery | autodiscovery | 数据库发现设置。 | 否 |
autodiscovery 块
autodiscovery
块配置数据库的发现,在 data_source_names
中指定的数据库之外。
支持以下参数
名称 | 类型 | 描述 | 默认值 | 必需 |
---|---|---|---|---|
enabled | bool | 是否自动发现其他数据库。 | false | 否 |
database_allowlist | list(string) | 要筛选的数据库列表,表示仅抓取这些数据库。 | 否 | |
database_denylist | list(string) | 要筛选掉的数据库列表,表示将抓取所有其他数据库。 | 否 |
如果将 enabled
设置为 true
且未指定允许列表或拒绝列表,则导出器将从所有数据库中抓取。
如果禁用 autodiscovery
,则 database_allowlist
和 database_denylist
都将不起作用。
导出的字段
导出以下字段,并且可以被其他组件引用。
名称 | 类型 | 描述 |
---|---|---|
targets | list(map(string)) | 可用于收集导出器指标的目标。 |
例如,targets
可以传递给 discovery.relabel
组件以重写目标的标签集,也可以传递给收集公开指标的 prometheus.scrape
组件。
导出的目标使用 内存流量 地址,该地址由 run 命令 指定的配置。
组件运行状况
仅当给定无效配置时,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
:Prometheus remote_write 兼容服务器的 URL,用于将指标发送到该服务器。USERNAME
:用于向remote_write
API 进行身份验证的用户名。PASSWORD
:用于向remote_write
API 进行身份验证的密码。
从允许列表中的数据库集合收集自定义指标
此示例使用 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
:Prometheus remote_write 兼容服务器的 URL,用于将指标发送到该服务器。USERNAME
:用于向remote_write
API 进行身份验证的用户名。PASSWORD
:用于向remote_write
API 进行身份验证的密码。
从除拒绝列表中的数据库之外的所有数据库收集指标
此示例使用 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
:Prometheus remote_write 兼容服务器的 URL,用于将指标发送到该服务器。USERNAME
:用于向remote_write
API 进行身份验证的用户名。PASSWORD
:用于向remote_write
API 进行身份验证的密码。
兼容的组件
prometheus.exporter.postgres
具有可供以下组件使用的导出
- 使用 Targets 的组件
注意
连接某些组件可能不合理,或者组件可能需要进一步配置才能使连接正常工作。有关更多详细信息,请参阅链接的文档。