feat: add option to disable prepared statements for PostgreSQL (#9710)

This commit is contained in:
Alexander Krantz 2021-12-22 12:29:53 -08:00 committed by GitHub
parent 697855c98b
commit e6cd83f1e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 12 deletions

View File

@ -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
```

View File

@ -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,
}
})
}

View File

@ -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+$`)

View File

@ -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

View File

@ -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,
}
})
}