菜单
文档breadcrumb arrow Grafana Alloybreadcrumb arrow 参考breadcrumb arrow 组件breadcrumb arrow prometheusbreadcrumb arrow prometheus.exporter.postgres
开源

prometheus.exporter.postgres

prometheus.exporter.postgres 组件嵌入了 postgres_exporter,用于从 PostgreSQL 数据库收集指标。

可以指定多个 prometheus.exporter.postgres 组件,只需为它们指定不同的标签即可。

用法

alloy
prometheus.exporter.postgres "LABEL" {
    data_source_names = DATA_SOURCE_NAMES_LIST
}

参数

支持以下参数

名称类型描述默认值必需
data_source_nameslist(secret)指定要连接的 PostgreSQL 服务器。
disable_settings_metricsbool禁用从 pg_settings 收集指标。false
disable_default_metricsbool当为 true 时,仅公开从 custom_queries_config_path 提供的指标。false
custom_queries_config_pathstring包含要公开为指标的自定义查询的 YAML 文件的路径。""
enabled_collectorslist(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 参数控制的收集器将仅应用于列表中的第一个数据源。

支持以下块

层次结构描述必需
autodiscoveryautodiscovery数据库发现设置。

autodiscovery 块

autodiscovery 块配置数据库的发现,在 data_source_names 中指定的数据库之外。

支持以下参数

名称类型描述默认值必需
enabledbool是否自动发现其他数据库。false
database_allowlistlist(string)要筛选的数据库列表,表示仅抓取这些数据库。
database_denylistlist(string)要筛选掉的数据库列表,表示将抓取所有其他数据库。

如果将 enabled 设置为 true 且未指定允许列表或拒绝列表,则导出器将从所有数据库中抓取。

如果禁用 autodiscovery,则 database_allowlistdatabase_denylist 都将不起作用。

导出的字段

导出以下字段,并且可以被其他组件引用。

名称类型描述
targetslist(map(string))可用于收集导出器指标的目标。

例如,targets 可以传递给 discovery.relabel 组件以重写目标的标签集,也可以传递给收集公开指标的 prometheus.scrape 组件。

导出的目标使用 内存流量 地址,该地址由 run 命令 指定的配置。

组件运行状况

仅当给定无效配置时,prometheus.exporter.postgres 才报告为不健康。

调试信息

prometheus.exporter.postgres 不公开任何组件特定的调试信息。

调试指标

prometheus.exporter.postgres 不公开任何组件特定的调试指标。

示例

从 PostgreSQL 服务器收集指标

此示例使用 prometheus.exporter.postgres 组件从本地运行且具有所有默认设置的 PostgreSQL 服务器收集指标

alloy
// 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 中的查询派生的自定义指标替换默认指标

alloy
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 数据库之外的所有数据库收集自定义指标。

alloy
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 具有可供以下组件使用的导出

注意

连接某些组件可能不合理,或者组件可能需要进一步配置才能使连接正常工作。有关更多详细信息,请参阅链接的文档。