Skip to content

Commit

Permalink
Add command-line option for lock timeout.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevan committed Jul 26, 2020
1 parent fb12947 commit 145c4c5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
7 changes: 7 additions & 0 deletions cmd/pgmetrics/main.go
Expand Up @@ -40,6 +40,7 @@ Usage:
General options:
-t, --timeout=SECS individual query timeout in seconds (default: 5)
--lock-timeout=MILLIS lock timeout in milliseconds (default: 50)
-i, --input=FILE don't connect to db, instead read and display
this previously saved JSON file
-V, --version output version information, then exit
Expand Down Expand Up @@ -193,6 +194,7 @@ func (o *options) parse() (args []string) {
s.SetProgram("pgmetrics")
// general
s.UintVarLong(&o.CollectConfig.TimeoutSec, "timeout", 't', "")
s.UintVarLong(&o.CollectConfig.LockTimeoutMillisec, "lock-timeout", 0, "")
s.BoolVarLong(&o.CollectConfig.NoSizes, "no-sizes", 'S', "")
s.StringVarLong(&o.input, "input", 'i', "")
help := s.StringVarLong(&o.help, "help", '?', "").SetOptional()
Expand Down Expand Up @@ -249,6 +251,11 @@ func (o *options) parse() (args []string) {
printTry()
os.Exit(2)
}
if o.CollectConfig.LockTimeoutMillisec == 0 {
fmt.Fprintln(os.Stderr, "lock-timeout must be greater than 0")
printTry()
os.Exit(2)
}
if err := getRegexp(o.CollectConfig.Schema); err != nil {
fmt.Fprintf(os.Stderr, "bad POSIX regular expression for -c/--schema: %v\n", err)
printTry()
Expand Down
10 changes: 6 additions & 4 deletions collector/collect.go
Expand Up @@ -61,8 +61,9 @@ func isValidIdent(s string) bool {
// specify which metrics to collect and how.
type CollectConfig struct {
// general
TimeoutSec uint
NoSizes bool
TimeoutSec uint
LockTimeoutMillisec uint
NoSizes bool

// collection
Schema string
Expand Down Expand Up @@ -92,7 +93,8 @@ type CollectConfig struct {
func DefaultCollectConfig() CollectConfig {
cc := CollectConfig{
// ------------------ general
TimeoutSec: 5,
TimeoutSec: 5,
LockTimeoutMillisec: 50,
//NoSizes: false,

// ------------------ collection
Expand Down Expand Up @@ -176,7 +178,7 @@ func Collect(o CollectConfig, dbnames []string) *pgmetrics.Model {

// set timeouts (but not for pgbouncer, it does not like them)
if !(len(dbnames) == 1 && dbnames[0] == "pgbouncer") {
connstr += makeKV("lock_timeout", "50") // 50 msec. Just fail fast on locks.
connstr += makeKV("lock_timeout", strconv.Itoa(int(o.LockTimeoutMillisec)))
connstr += makeKV("statement_timeout", strconv.Itoa(int(o.TimeoutSec)*1000))
}

Expand Down

0 comments on commit 145c4c5

Please sign in to comment.