diff --git a/plugins/inputs/postgresql/README.md b/plugins/inputs/postgresql/README.md index d6771ade6..6ab44e2b5 100644 --- a/plugins/inputs/postgresql/README.md +++ b/plugins/inputs/postgresql/README.md @@ -62,6 +62,39 @@ host=localhost user=pgotest dbname=app_production sslmode=require sslkey=/etc/te ```toml [[inputs.postgresql]] - address = "postgres://telegraf@localhost/someDB" - ignored_databases = ["template0", "template1"] + ## specify address via a url matching: + ## postgres://[pqgotest[:password]]@localhost[/dbname]?sslmode=[disable|verify-ca|verify-full] + ## or a simple string: + ## host=localhost user=pqgotest password=... sslmode=... dbname=app_production + ## + ## All connection parameters are optional. + ## + ## Without the dbname parameter, the driver will default to a database + ## with the same name as the user. This dbname is just for instantiating a + ## connection with the server and doesn't restrict the databases we are trying + ## to grab metrics for. + ## + address = "host=localhost user=postgres sslmode=disable" + ## A custom name for the database that will be used as the "server" tag in the + ## measurement output. If not specified, a default one generated from + ## the connection address is used. + # outputaddress = "db01" + + ## connection configuration. + ## maxlifetime - specify the maximum lifetime of a connection. + ## default is forever (0s) + # max_lifetime = "0s" + + ## A list of databases to explicitly ignore. If not specified, metrics for all + ## databases are gathered. Do NOT use with the 'databases' option. + # ignored_databases = ["postgres", "template0", "template1"] + + ## A list of databases to pull metrics about. If not specified, metrics for all + ## databases are gathered. Do NOT use with the 'ignored_databases' option. + # databases = ["app_production", "testing"] + + ## Whether to use prepared statements when connecting to the database. + ## This should be set to false when connecting through a PgBouncer instance + ## with pool_mode set to transaction. + prepared_statements = true ``` diff --git a/plugins/inputs/postgresql/postgresql.go b/plugins/inputs/postgresql/postgresql.go index a90f571b7..b131642ca 100644 --- a/plugins/inputs/postgresql/postgresql.go +++ b/plugins/inputs/postgresql/postgresql.go @@ -15,8 +15,9 @@ import ( type Postgresql struct { Service - Databases []string - IgnoredDatabases []string + Databases []string `toml:"databases"` + IgnoredDatabases []string `toml:"ignored_databases"` + PreparedStatements bool `toml:"prepared_statements"` } var ignoredColumns = map[string]bool{"stats_reset": true} @@ -53,6 +54,11 @@ var sampleConfig = ` ## A list of databases to pull metrics about. If not specified, metrics for all ## databases are gathered. Do NOT use with the 'ignored_databases' option. # databases = ["app_production", "testing"] + + ## Whether to use prepared statements when connecting to the database. + ## This should be set to false when connecting through a PgBouncer instance + ## with pool_mode set to transaction. + # prepared_statements = true ` func (p *Postgresql) SampleConfig() string { @@ -67,6 +73,11 @@ func (p *Postgresql) IgnoredColumns() map[string]bool { return ignoredColumns } +func (p *Postgresql) Init() error { + p.Service.IsPgBouncer = !p.PreparedStatements + return nil +} + func (p *Postgresql) Gather(acc telegraf.Accumulator) error { var ( err error @@ -198,8 +209,8 @@ func init() { MaxIdle: 1, MaxOpen: 1, MaxLifetime: config.Duration(0), - IsPgBouncer: false, }, + PreparedStatements: true, } }) } diff --git a/plugins/inputs/postgresql/service.go b/plugins/inputs/postgresql/service.go index e765316b0..2ef65617d 100644 --- a/plugins/inputs/postgresql/service.go +++ b/plugins/inputs/postgresql/service.go @@ -94,7 +94,7 @@ type Service struct { MaxOpen int MaxLifetime config.Duration DB *sql.DB - IsPgBouncer bool + IsPgBouncer bool `toml:"-"` } var socketRegexp = regexp.MustCompile(`/\.s\.PGSQL\.\d+$`) diff --git a/plugins/inputs/postgresql_extensible/README.md b/plugins/inputs/postgresql_extensible/README.md index 7afddbfde..bfba3e5c8 100644 --- a/plugins/inputs/postgresql_extensible/README.md +++ b/plugins/inputs/postgresql_extensible/README.md @@ -28,7 +28,12 @@ The example below has two queries are specified, with the following parameters: # A list of databases to pull metrics about. If not specified, metrics for all # databases are gathered. # databases = ["app_production", "testing"] - # + + ## Whether to use prepared statements when connecting to the database. + ## This should be set to false when connecting through a PgBouncer instance + ## with pool_mode set to transaction. + prepared_statements = true + # Define the toml config where the sql queries are stored # New queries can be added, if the withdbname is set to true and there is no # databases defined in the 'databases field', the sql query is ended by a 'is diff --git a/plugins/inputs/postgresql_extensible/postgresql_extensible.go b/plugins/inputs/postgresql_extensible/postgresql_extensible.go index bb776abdc..61af57e66 100644 --- a/plugins/inputs/postgresql_extensible/postgresql_extensible.go +++ b/plugins/inputs/postgresql_extensible/postgresql_extensible.go @@ -18,11 +18,12 @@ import ( type Postgresql struct { postgresql.Service - Databases []string - AdditionalTags []string - Timestamp string - Query query - Debug bool + Databases []string + AdditionalTags []string + Timestamp string + Query query + Debug bool + PreparedStatements bool `toml:"prepared_statements"` Log telegraf.Logger } @@ -59,6 +60,11 @@ var sampleConfig = ` ## default is forever (0s) max_lifetime = "0s" + ## Whether to use prepared statements when connecting to the database. + ## This should be set to false when connecting through a PgBouncer instance + ## with pool_mode set to transaction. + # prepared_statements = true + ## A list of databases to pull metrics about. If not specified, metrics for all ## databases are gathered. ## databases = ["app_production", "testing"] @@ -125,6 +131,7 @@ func (p *Postgresql) Init() error { } } } + p.Service.IsPgBouncer = !p.PreparedStatements return nil } @@ -348,6 +355,7 @@ func init() { MaxLifetime: config.Duration(0), IsPgBouncer: false, }, + PreparedStatements: true, } }) }