0.3.0 Removing internal parallelism: postgresql

This commit is contained in:
Cameron Sparr 2015-12-19 15:58:57 -07:00
parent 41374aabcb
commit e25ac0d587
1 changed files with 16 additions and 45 deletions

View File

@ -11,46 +11,32 @@ import (
_ "github.com/lib/pq" _ "github.com/lib/pq"
) )
type Server struct { type Postgresql struct {
Address string Address string
Databases []string Databases []string
OrderedColumns []string OrderedColumns []string
} }
type Postgresql struct {
Servers []*Server
}
var ignoredColumns = map[string]bool{"datid": true, "datname": true, "stats_reset": true} var ignoredColumns = map[string]bool{"datid": true, "datname": true, "stats_reset": true}
var sampleConfig = ` var sampleConfig = `
# specify servers via an array of tables
[[plugins.postgresql.servers]]
# specify address via a url matching: # specify address via a url matching:
# postgres://[pqgotest[:password]]@localhost[/dbname]?sslmode=[disable|verify-ca|verify-full] # postgres://[pqgotest[:password]]@localhost[/dbname]?sslmode=[disable|verify-ca|verify-full]
# or a simple string: # or a simple string:
# host=localhost user=pqotest password=... sslmode=... dbname=app_production # host=localhost user=pqotest password=... sslmode=... dbname=app_production
# #
# All connection parameters are optional. By default, the host is localhost # All connection parameters are optional.
# and the user is the currently running user. For localhost, we default
# to sslmode=disable as well.
# #
# Without the dbname parameter, the driver will default to a database # 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 # 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 # connection with the server and doesn't restrict the databases we are trying
# to grab metrics for. # to grab metrics for.
# #
address = "host=localhost user=postgres sslmode=disable" address = "host=localhost user=postgres sslmode=disable"
# A list of databases to pull metrics about. If not specified, metrics for all # A list of databases to pull metrics about. If not specified, metrics for all
# databases are gathered. # databases are gathered.
# databases = ["app_production", "testing"]
# databases = ["app_production", "blah_testing"]
# [[plugins.postgresql.servers]]
# address = "influx@remoteserver"
` `
func (p *Postgresql) SampleConfig() string { func (p *Postgresql) SampleConfig() string {
@ -65,42 +51,27 @@ func (p *Postgresql) IgnoredColumns() map[string]bool {
return ignoredColumns return ignoredColumns
} }
var localhost = &Server{Address: "sslmode=disable"} var localhost = "host=localhost sslmode=disable"
func (p *Postgresql) Gather(acc plugins.Accumulator) error { func (p *Postgresql) Gather(acc plugins.Accumulator) error {
if len(p.Servers) == 0 {
p.gatherServer(localhost, acc)
return nil
}
for _, serv := range p.Servers {
err := p.gatherServer(serv, acc)
if err != nil {
return err
}
}
return nil
}
func (p *Postgresql) gatherServer(serv *Server, acc plugins.Accumulator) error {
var query string var query string
if serv.Address == "" || serv.Address == "localhost" { if p.Address == "" || p.Address == "localhost" {
serv = localhost p.Address = localhost
} }
db, err := sql.Open("postgres", serv.Address) db, err := sql.Open("postgres", p.Address)
if err != nil { if err != nil {
return err return err
} }
defer db.Close() defer db.Close()
if len(serv.Databases) == 0 { if len(p.Databases) == 0 {
query = `SELECT * FROM pg_stat_database` query = `SELECT * FROM pg_stat_database`
} else { } else {
query = fmt.Sprintf(`SELECT * FROM pg_stat_database WHERE datname IN ('%s')`, strings.Join(serv.Databases, "','")) query = fmt.Sprintf(`SELECT * FROM pg_stat_database WHERE datname IN ('%s')`,
strings.Join(p.Databases, "','"))
} }
rows, err := db.Query(query) rows, err := db.Query(query)
@ -111,13 +82,13 @@ func (p *Postgresql) gatherServer(serv *Server, acc plugins.Accumulator) error {
defer rows.Close() defer rows.Close()
// grab the column information from the result // grab the column information from the result
serv.OrderedColumns, err = rows.Columns() p.OrderedColumns, err = rows.Columns()
if err != nil { if err != nil {
return err return err
} }
for rows.Next() { for rows.Next() {
err = p.accRow(rows, acc, serv) err = p.accRow(rows, acc)
if err != nil { if err != nil {
return err return err
} }
@ -130,20 +101,20 @@ type scanner interface {
Scan(dest ...interface{}) error Scan(dest ...interface{}) error
} }
func (p *Postgresql) accRow(row scanner, acc plugins.Accumulator, serv *Server) error { func (p *Postgresql) accRow(row scanner, acc plugins.Accumulator) error {
var columnVars []interface{} var columnVars []interface{}
var dbname bytes.Buffer var dbname bytes.Buffer
// this is where we'll store the column name with its *interface{} // this is where we'll store the column name with its *interface{}
columnMap := make(map[string]*interface{}) columnMap := make(map[string]*interface{})
for _, column := range serv.OrderedColumns { for _, column := range p.OrderedColumns {
columnMap[column] = new(interface{}) columnMap[column] = new(interface{})
} }
// populate the array of interface{} with the pointers in the right order // populate the array of interface{} with the pointers in the right order
for i := 0; i < len(columnMap); i++ { for i := 0; i < len(columnMap); i++ {
columnVars = append(columnVars, columnMap[serv.OrderedColumns[i]]) columnVars = append(columnVars, columnMap[p.OrderedColumns[i]])
} }
// deconstruct array of variables and send to Scan // deconstruct array of variables and send to Scan
@ -159,7 +130,7 @@ func (p *Postgresql) accRow(row scanner, acc plugins.Accumulator, serv *Server)
dbname.WriteString(string(dbnameChars[i])) dbname.WriteString(string(dbnameChars[i]))
} }
tags := map[string]string{"server": serv.Address, "db": dbname.String()} tags := map[string]string{"server": p.Address, "db": dbname.String()}
fields := make(map[string]interface{}) fields := make(map[string]interface{})
for col, val := range columnMap { for col, val := range columnMap {