Postgres Exporter

grafana-agent内置了postgres_exporter,来采集Postgres Server的metrics采集。

我们强烈推荐您分配独立的账号,供grafana-agent来连接到Postgres server,以避免过度授权带来的安全性问题,具体可以餐你考postgres exporter官方文档.

配置并启用cadvisor_exporter

  1. server:
  2. log_level: info
  3. http_listen_port: 12345
  4. metrics:
  5. global:
  6. scrape_interval: 15s
  7. remote_write:
  8. - url: 'https://n9e-server:19000/prometheus/v1/write'
  9. basic_auth:
  10. username: ${FC_USERNAME}
  11. password: ${FC_PASSWORD}
  12. integrations:
  13. postgres_exporter:
  14. enabled: true
  15. EOF

采集的关键指标列表

  1. pg_locks_count : pg_locks_count{datname=~"$datname", instance=~"$instance", mode=~"$mode"} != 0
  2. pg_postmaster_start_time_seconds : pg_postmaster_start_time_seconds{release="$release", instance="$instance"} * 1000
  3. pg_settings_effective_cache_size_bytes : pg_settings_effective_cache_size_bytes{instance="$instance"}
  4. pg_settings_maintenance_work_mem_bytes : pg_settings_maintenance_work_mem_bytes{instance="$instance"}
  5. pg_settings_max_connections : pg_settings_max_connections{release="$release", instance="$instance"}
  6. pg_settings_max_parallel_workers : pg_settings_max_parallel_workers{instance="$instance"}
  7. pg_settings_max_wal_size_bytes : pg_settings_max_wal_size_bytes{instance="$instance"}
  8. pg_settings_max_worker_processes : pg_settings_max_worker_processes{instance="$instance"}
  9. pg_settings_random_page_cost : pg_settings_random_page_cost{instance="$instance"}
  10. pg_settings_seq_page_cost : pg_settings_seq_page_cost
  11. pg_settings_shared_buffers_bytes : pg_settings_shared_buffers_bytes{instance="$instance"}
  12. pg_settings_work_mem_bytes : pg_settings_work_mem_bytes{instance="$instance"}
  13. pg_stat_activity_count : pg_stat_activity_count{datname=~"$datname", instance=~"$instance", state="active"} !=0
  14. pg_stat_activity_count : pg_stat_activity_count{datname=~"$datname", instance=~"$instance", state=~"idle|idle in transaction|idle in transaction (aborted)"}
  15. pg_stat_bgwriter_buffers_alloc : irate(pg_stat_bgwriter_buffers_alloc{instance="$instance"}[5m])
  16. pg_stat_bgwriter_buffers_backend : irate(pg_stat_bgwriter_buffers_backend{instance="$instance"}[5m])
  17. pg_stat_bgwriter_buffers_backend_fsync : irate(pg_stat_bgwriter_buffers_backend_fsync{instance="$instance"}[5m])
  18. pg_stat_bgwriter_buffers_checkpoint : irate(pg_stat_bgwriter_buffers_checkpoint{instance="$instance"}[5m])
  19. pg_stat_bgwriter_buffers_clean : irate(pg_stat_bgwriter_buffers_clean{instance="$instance"}[5m])
  20. pg_stat_bgwriter_checkpoint_sync_time : irate(pg_stat_bgwriter_checkpoint_sync_time{instance="$instance"}[5m])
  21. pg_stat_bgwriter_checkpoint_write_time : irate(pg_stat_bgwriter_checkpoint_write_time{instance="$instance"}[5m])
  22. pg_stat_database_blks_hit : pg_stat_database_blks_hit{instance="$instance", datname=~"$datname"} / (pg_stat_database_blks_read{instance="$instance", datname=~"$datname"} + pg_stat_database_blks_hit{instance="$instance", datname=~"$datname"})
  23. pg_stat_database_conflicts : irate(pg_stat_database_conflicts{instance="$instance", datname=~"$datname"}[5m])
  24. pg_stat_database_deadlocks : irate(pg_stat_database_deadlocks{instance="$instance", datname=~"$datname"}[5m])
  25. pg_stat_database_temp_bytes : irate(pg_stat_database_temp_bytes{instance="$instance", datname=~"$datname"}[5m])
  26. pg_stat_database_tup_deleted : pg_stat_database_tup_deleted{datname=~"$datname", instance=~"$instance"} != 0
  27. pg_stat_database_tup_fetched : SUM(pg_stat_database_tup_fetched{datname=~"$datname", instance=~"$instance"})
  28. pg_stat_database_tup_fetched : pg_stat_database_tup_fetched{datname=~"$datname", instance=~"$instance"} != 0
  29. pg_stat_database_tup_inserted : SUM(pg_stat_database_tup_inserted{release="$release", datname=~"$datname", instance=~"$instance"})
  30. pg_stat_database_tup_inserted : pg_stat_database_tup_inserted{datname=~"$datname", instance=~"$instance"} != 0
  31. pg_stat_database_tup_returned : pg_stat_database_tup_returned{datname=~"$datname", instance=~"$instance"} != 0
  32. pg_stat_database_tup_updated : SUM(pg_stat_database_tup_updated{datname=~"$datname", instance=~"$instance"})
  33. pg_stat_database_tup_updated : pg_stat_database_tup_updated{datname=~"$datname", instance=~"$instance"} != 0
  34. pg_stat_database_xact_commit : irate(pg_stat_database_xact_commit{instance="$instance", datname=~"$datname"}[5m])
  35. pg_stat_database_xact_rollback : irate(pg_stat_database_xact_rollback{instance="$instance", datname=~"$datname"}[5m])
  36. pg_static : pg_static{release="$release", instance="$instance"}
  37. process_cpu_seconds_total : avg(rate(process_cpu_seconds_total{release="$release", instance="$instance"}[5m]) * 1000)
  38. process_open_fds : process_open_fds{release="$release", instance="$instance"}
  39. process_resident_memory_bytes : avg(rate(process_resident_memory_bytes{release="$release", instance="$instance"}[5m]))
  40. process_virtual_memory_bytes : avg(rate(process_virtual_memory_bytes{release="$release", instance="$instance"}[5m]))

完整地配置项说明

  1. # Enables the postgres_exporter integration, allowing the Agent to automatically
  2. # collect system metrics from the configured postgres server address
  3. [enabled: <boolean> | default = false]
  4. # Sets an explicit value for the instance label when the integration is
  5. # self-scraped. Overrides inferred values.
  6. #
  7. # The default value for this integration is inferred from a truncated version of
  8. # the first DSN in data_source_names. The truncated DSN includes the hostname
  9. # and database name (if used) of the server, but does not include any user
  10. # information.
  11. #
  12. # If data_source_names contains more than one entry, the integration will fail to
  13. # load and a value for instance must be manually provided.
  14. [instance: <string>]
  15. # Automatically collect metrics from this integration. If disabled,
  16. # the postgres_exporter integration will be run but not scraped and thus not
  17. # remote-written. Metrics for the integration will be exposed at
  18. # /integrations/postgres_exporter/metrics and can be scraped by an external
  19. # process.
  20. [scrape_integration: <boolean> | default = <integrations_config.scrape_integrations>]
  21. # How often should the metrics be collected? Defaults to
  22. # prometheus.global.scrape_interval.
  23. [scrape_interval: <duration> | default = <global_config.scrape_interval>]
  24. # The timeout before considering the scrape a failure. Defaults to
  25. # prometheus.global.scrape_timeout.
  26. [scrape_timeout: <duration> | default = <global_config.scrape_timeout>]
  27. # Allows for relabeling labels on the target.
  28. relabel_configs:
  29. [- <relabel_config> ... ]
  30. # Relabel metrics coming from the integration, allowing to drop series
  31. # from the integration that you don't care about.
  32. metric_relabel_configs:
  33. [ - <relabel_config> ... ]
  34. # How frequent to truncate the WAL for this integration.
  35. [wal_truncate_frequency: <duration> | default = "60m"]
  36. # Monitor the exporter itself and include those metrics in the results.
  37. [include_exporter_metrics: <bool> | default = false]
  38. #
  39. # Exporter-specific configuration options
  40. #
  41. # Data Source Names specifies the Postgres server(s) to connect to. This is
  42. # REQUIRED but may also be specified by the POSTGRES_EXPORTER_DATA_SOURCE_NAME
  43. # environment variable, where DSNs the environment variable are separated by
  44. # commas. If neither are set, the integration will fail to start.
  45. #
  46. # The format of this is specified here: https://pkg.go.dev/github.com/lib/pq#ParseURL
  47. #
  48. # A working example value for a server with a password is:
  49. # "postgresql://username:passwword@localhost:5432/database?sslmode=disable"
  50. #
  51. # Multiple DSNs may be provided here, allowing for scraping from multiple
  52. # servers.
  53. data_source_names:
  54. - <string>
  55. # Disables collection of metrics from pg_settings.
  56. [disable_settings_metrics: <boolean> | default = false]
  57. # Autodiscover databases to collect metrics from. If false, only collects
  58. # metrics from databases collected from data_source_names.
  59. [autodiscover_databases: <boolean> | default = false]
  60. # Excludes specific databases from being collected when autodiscover_databases
  61. # is true.
  62. exclude_databases:
  63. [ - <string> ]
  64. # Includes only specific databases (excluding all others) when autodiscover_databases
  65. # is true.
  66. include_databases:
  67. [ - <string> ]
  68. # Path to a YAML file containing custom queries to run. Check out
  69. # postgres_exporter's queries.yaml for examples of the format:
  70. # https://github.com/prometheus-community/postgres_exporter/blob/master/queries.yaml
  71. [query_path: <string> | default = ""]
  72. # When true, only exposes metrics supplied from query_path.
  73. [disable_default_metrics: <boolean> | default = false]