Add pgbouncer input plugin (#3918)

This commit is contained in:
Loïc Blot
2018-08-02 00:44:10 +02:00
committed by Greg
parent e1160c26bc
commit 429d14101a
11 changed files with 311 additions and 1 deletions

View File

@@ -189,6 +189,7 @@ func init() {
MaxLifetime: internal.Duration{
Duration: 0,
},
IsPgBouncer: false,
},
}
})

View File

@@ -20,6 +20,7 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
"host=%s user=postgres sslmode=disable",
testutil.GetLocalHost(),
),
IsPgBouncer: false,
},
Databases: []string{"postgres"},
}

View File

@@ -3,6 +3,9 @@ package postgresql
import (
"database/sql"
"fmt"
"github.com/jackc/pgx"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/stdlib"
"net"
"net/url"
"regexp"
@@ -90,6 +93,7 @@ type Service struct {
MaxOpen int
MaxLifetime internal.Duration
DB *sql.DB
IsPgBouncer bool
}
// Start starts the ServiceInput's service, whatever that may be
@@ -100,7 +104,34 @@ func (p *Service) Start(telegraf.Accumulator) (err error) {
p.Address = localhost
}
if p.DB, err = sql.Open("pgx", p.Address); err != nil {
connectionString := p.Address
// Specific support to make it work with PgBouncer too
// See https://github.com/influxdata/telegraf/issues/3253#issuecomment-357505343
if p.IsPgBouncer {
d := &stdlib.DriverConfig{
ConnConfig: pgx.ConnConfig{
PreferSimpleProtocol: true,
RuntimeParams: map[string]string{
"client_encoding": "UTF8",
},
CustomConnInfo: func(c *pgx.Conn) (*pgtype.ConnInfo, error) {
info := c.ConnInfo.DeepCopy()
info.RegisterDataType(pgtype.DataType{
Value: &pgtype.OIDValue{},
Name: "int8OID",
OID: pgtype.Int8OID,
})
return info, nil
},
},
}
stdlib.RegisterDriverConfig(d)
connectionString = d.ConnectionString(p.Address)
}
if p.DB, err = sql.Open("pgx", connectionString); err != nil {
return err
}