97e980cd8a
storagenodes have like 10 or more databases. without this tag they all get sent as the same value, stomping on each other. Change-Id: Ib12019684d6ea8f2a5b83df584056dfa79e3c4b3
44 lines
1.3 KiB
Go
44 lines
1.3 KiB
Go
// Copyright (C) 2019 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package dbutil
|
|
|
|
import (
|
|
"database/sql"
|
|
"flag"
|
|
"time"
|
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
|
)
|
|
|
|
var (
|
|
maxIdleConns = flag.Int("db.max_idle_conns", 20, "Maximum Amount of Idle Database connections, -1 means the stdlib default")
|
|
maxOpenConns = flag.Int("db.max_open_conns", 25, "Maximum Amount of Open Database connections, -1 means the stdlib default")
|
|
connMaxLifetime = flag.Duration("db.conn_max_lifetime", -1, "Maximum Database Connection Lifetime, -1ns means the stdlib default")
|
|
)
|
|
|
|
// ConfigurableDB contains methods for configuring a database.
|
|
type ConfigurableDB interface {
|
|
SetMaxIdleConns(int)
|
|
SetMaxOpenConns(int)
|
|
SetConnMaxLifetime(time.Duration)
|
|
Stats() sql.DBStats
|
|
}
|
|
|
|
// Configure Sets Connection Boundaries and adds db_stats monitoring to monkit
|
|
func Configure(db ConfigurableDB, dbName string, mon *monkit.Scope) {
|
|
if *maxIdleConns >= 0 {
|
|
db.SetMaxIdleConns(*maxIdleConns)
|
|
}
|
|
if *maxOpenConns >= 0 {
|
|
db.SetMaxOpenConns(*maxOpenConns)
|
|
}
|
|
if *connMaxLifetime >= 0 {
|
|
db.SetConnMaxLifetime(*connMaxLifetime)
|
|
}
|
|
mon.Chain(monkit.StatSourceFunc(
|
|
func(cb func(key monkit.SeriesKey, field string, val float64)) {
|
|
monkit.StatSourceFromStruct(monkit.NewSeriesKey("db_stats").WithTag("db_name", dbName), db.Stats()).Stats(cb)
|
|
}))
|
|
}
|