这是indexloc提供的服务,不要输入任何密码
Skip to content

pgsty/pg_exporter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PG Exporter

Prometheus exporter for PostgreSQL metrics

You can download latest binaries on release page

Features

  • Support both Postgres & Pgbouncer
  • Flexible, completely customizable queries & metrics
  • More control on execution policy (version, primay/standby, cluster/database level), cache, timeout

Quick Start

export PG_EXPORTER_URL='postgres://postgres:password@localhost:5432/postgres'
export PG_EXPORTER_CONFIG='/path/to/conf/file/or/dir'
pg_exporter

Note that pg_export does not built in default metrics. All metrics are defined in customizable config files. By default, it will use ./pg_exporter.yaml as config path. or you can specify a conf dir like /etc/pg_exporter/conf

Or the docker way:

docker run \
  --env=PG_EXPORTER_URL='postgres://postgres:password@docker.for.mac.host.internal:5432/postgres' \
  --env=PGB_EXPORTER_WEB_LISTEN_ADDRESS=':8848' \
  --env=PGB_EXPORTER_WEB_TELEMETRY_PATH='/metrics' \
  -p 8848:8848 \
  pg_exporter

The default listen address is localhost:8848 and the default telemetry path is /metrics.

curl localhost:8848/metrics

And the default data source name is:

postgresql:///?sslmode=disable

If the target database name is pgbouncer, The exporter will turn into pgbouncer mode automatically. Provide you are not really going to create a real world "pgbouncer" database.

Run

usage: pg_exporter [<flags>]

Flags:
  --help                         Show context-sensitive help 
  --url="postgresql:///postgres?sslmode=disable"
                                 postgres connect url
  --config="./pg_exporter.yaml"  Path to config files
  --label=""                     Comma separated list label=value pair
  --disable-cache                force not using cache
  --web.listen-address=":8848"   prometheus web server listen address
  --web.telemetry-path="/metrics"
                                 Path under which to expose metrics.
  --explain                      dry run and explain queries
  --version                      Show application version.
  --log.level="info"             Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal]
  --log.format="logger:stderr"   Set the log target and format. 

Config

Config files are using YAML format, there are lots of examples in the conf dir

here is a sample config that scrapes pg_database metrics:

pg_database:
  query: |
    SELECT datname,
           pg_database_size(oid)      AS size,
           age(datfrozenxid)          AS age,
           datistemplate              AS is_template,
           datallowconn               AS allow_conn,
           datconnlimit               AS conn_limit,
           datfrozenxid::TEXT::BIGINT as frozen_xid
    FROM pg_database;
  ttl: 100
  tags: [cluster]
  min_version: 100000
  max_version: 130000
  metrics:
    - datname:
        usage: LABEL
        description: database name
    - size:
        usage: GAUGE
        description: database size in bytes
    - age:
        usage: GAUGE
        description: database age calculated by age(datfrozenxid)
    - is_template:
        usage: GAUGE
        description: 1 for template db , 0 for normal db
    - allow_conn:
        usage: GAUGE
        description: 1 allow connection, 0 does not allow
    - conn_limit:
        usage: GAUGE
        description: connection limit, -1 for no limit
    - frozen_xid:
        usage: GAUGE
        description: tuple with xmin below this will always be visable (until wrap around)

If a directory is given, pg_exporter will iterate the directory and load config in alphabetic order.

Build

go build

To build a static stand alone binary for docker scratch

CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' -o pg_exporter

To build a docker image, using:

docker build -t pg_exporter .

Or just download latest prebuilt binaries on release page

About

Author:Vonng (fengruohang@outlook.com)

License:BSD

Regards: I used to be a faithful user of PostgresExporter, until I felt some functionailities are missing. Some ideas are blatantly copy from it.