Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
fix(pgbouncer): fix setting db name in collectDatabases() (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyam8 authored Aug 7, 2022
1 parent 03f9133 commit 2c12f65
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
7 changes: 4 additions & 3 deletions modules/pgbouncer/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,13 @@ var (
}
)

func newDatabaseCharts(dbname string) *module.Charts {
func newDatabaseCharts(dbname, pgDBName string) *module.Charts {
charts := dbChartsTmpl.Copy()
for _, c := range *charts {
c.ID = fmt.Sprintf(c.ID, dbname)
c.Labels = []module.Label{
{Key: "database", Value: dbname},
{Key: "postgres_database", Value: pgDBName},
}
for _, d := range c.Dims {
d.ID = fmt.Sprintf(d.ID, dbname)
Expand All @@ -228,8 +229,8 @@ func newDatabaseCharts(dbname string) *module.Charts {
return charts
}

func (p *PgBouncer) addNewDatabaseCharts(dbname string) {
charts := newDatabaseCharts(dbname)
func (p *PgBouncer) addNewDatabaseCharts(dbname, pgDBName string) {
charts := newDatabaseCharts(dbname, pgDBName)
if err := p.Charts().Add(*charts...); err != nil {
p.Warning(err)
}
Expand Down
7 changes: 5 additions & 2 deletions modules/pgbouncer/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (p *PgBouncer) collectMetrics(mx map[string]int64) {
}
if !db.hasCharts {
db.hasCharts = true
p.addNewDatabaseCharts(name)
p.addNewDatabaseCharts(name, db.pgDBName)
}

mx["db_"+name+"_total_xact_count"] = db.totalXactCount
Expand Down Expand Up @@ -124,9 +124,11 @@ func (p *PgBouncer) collectDatabases() error {
var db string
return p.collectQuery(q, func(column, value string) {
switch column {
case "database":
case "name":
db = value
p.getDBMetrics(db).updated = true
case "database":
p.getDBMetrics(db).pgDBName = value
case "max_connections":
p.getDBMetrics(db).maxConnections = parseInt(value)
case "current_connections":
Expand Down Expand Up @@ -315,6 +317,7 @@ func (p *PgBouncer) resetMetrics() {
for name, db := range p.metrics.dbs {
p.metrics.dbs[name] = &dbMetrics{
name: db.name,
pgDBName: db.pgDBName,
hasCharts: db.hasCharts,
}
}
Expand Down
5 changes: 4 additions & 1 deletion modules/pgbouncer/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ type metrics struct {
dbs map[string]*dbMetrics
}

// dbMetrics represents PgBouncer database (not the PostgreSQL database of the outgoing connection).
type dbMetrics struct {
name string
name string
pgDBName string

updated bool
hasCharts bool

Expand Down

0 comments on commit 2c12f65

Please sign in to comment.