Use state_change for measuring elapsed time

This commit is contained in:
Julien Riou 2018-06-11 21:02:24 +02:00
commit 3a89201a94
No known key found for this signature in database
GPG key ID: BA3E15176E45E85D
3 changed files with 35 additions and 40 deletions

View file

@ -7,29 +7,25 @@ import (
// Session represents a PostgreSQL backend
type Session struct {
Pid int64
User string
Db string
Client string
State string
Query string
BackendDuration float64
XactDuration float64
QueryDuration float64
Pid int64
User string
Db string
Client string
State string
Query string
StateDuration float64
}
// NewSession instanciates a Session
func NewSession(pid int64, user string, db string, client string, state string, query string, backendDuration float64, xactDuration float64, queryDuration float64) Session {
func NewSession(pid int64, user string, db string, client string, state string, query string, stateDuration float64) Session {
return Session{
Pid: pid,
User: user,
Db: db,
Client: client,
State: state,
Query: query,
BackendDuration: backendDuration,
XactDuration: xactDuration,
QueryDuration: queryDuration,
Pid: pid,
User: user,
Db: db,
Client: client,
State: state,
Query: query,
StateDuration: stateDuration,
}
}
@ -51,17 +47,19 @@ func (s Session) String() string {
if s.State != "" {
output = append(output, fmt.Sprintf("state=%s", s.State))
}
if s.BackendDuration != 0 {
output = append(output, fmt.Sprintf("backend_duration=%f", s.BackendDuration))
if s.StateDuration != 0 {
output = append(output, fmt.Sprintf("state_duration=%f", s.StateDuration))
}
if s.XactDuration != 0 {
output = append(output, fmt.Sprintf("xact_duration=%f", s.XactDuration))
}
if s.QueryDuration != 0 {
output = append(output, fmt.Sprintf("query_duration=%f", s.QueryDuration))
}
if s.Query != "" {
if s.Query != "" && !s.IsIdle() {
output = append(output, fmt.Sprintf("query=%s", s.Query))
}
return strings.Join(output, " ")
}
// IsIdle returns true when a session is doing nothing
func (s Session) IsIdle() bool {
if s.State == "idle" || s.State == "idle in transaction" || s.State == "idle in transaction (aborted)" {
return true
}
return false
}