diff --git a/README.md b/README.md index 2bf5550..a39bad6 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,7 @@ The following placeholders are available to format log messages using `log-forma * `%s`: state * `%m`: state duration * `%q`: query +* `%a`: application name # License `pgterminate` is released under [The Unlicense](LICENSE) license. Code is under public domain. diff --git a/base/db.go b/base/db.go index a628948..65ff15e 100644 --- a/base/db.go +++ b/base/db.go @@ -3,6 +3,7 @@ package base import ( "database/sql" "fmt" + "github.com/jouir/pgterminate/log" "github.com/lib/pq" ) @@ -48,7 +49,8 @@ func (db *Db) Sessions() (sessions []*Session) { datname as db, coalesce(host(client_addr)::text || ':' || client_port::text, 'localhost') as client, state as state, substring(query from 1 for %d) as query, - coalesce(extract(epoch from now() - state_change), 0) as "stateDuration" + coalesce(extract(epoch from now() - state_change), 0) as "stateDuration", + application_name as "applicationName" from pg_catalog.pg_stat_activity where pid <> pg_backend_pid();`, maxQueryLength) log.Debugf("query: %s\n", query) @@ -58,13 +60,13 @@ func (db *Db) Sessions() (sessions []*Session) { for rows.Next() { var pid sql.NullInt64 - var user, db, client, state, query sql.NullString + var user, db, client, state, query, applicationName sql.NullString var stateDuration float64 - err := rows.Scan(&pid, &user, &db, &client, &state, &query, &stateDuration) + err := rows.Scan(&pid, &user, &db, &client, &state, &query, &stateDuration, &applicationName) Panic(err) - if pid.Valid && user.Valid && db.Valid && client.Valid && state.Valid && query.Valid { - sessions = append(sessions, NewSession(pid.Int64, user.String, db.String, client.String, state.String, query.String, stateDuration)) + if pid.Valid && user.Valid && db.Valid && client.Valid && state.Valid && query.Valid && applicationName.Valid { + sessions = append(sessions, NewSession(pid.Int64, user.String, db.String, client.String, state.String, query.String, stateDuration, applicationName.String)) } } diff --git a/base/session.go b/base/session.go index 98150fc..29e3611 100644 --- a/base/session.go +++ b/base/session.go @@ -7,25 +7,27 @@ import ( // Session represents a PostgreSQL backend type Session struct { - Pid int64 - User string - Db string - Client string - State string - Query string - StateDuration float64 + Pid int64 + User string + Db string + Client string + State string + Query string + StateDuration float64 + ApplicationName string } // NewSession instanciates a Session -func NewSession(pid int64, user string, db string, client string, state string, query string, stateDuration float64) *Session { +func NewSession(pid int64, user string, db string, client string, state string, query string, stateDuration float64, applicationName string) *Session { return &Session{ - Pid: pid, - User: user, - Db: db, - Client: client, - State: state, - Query: query, - StateDuration: stateDuration, + Pid: pid, + User: user, + Db: db, + Client: client, + State: state, + Query: query, + StateDuration: stateDuration, + ApplicationName: applicationName, } } @@ -39,6 +41,7 @@ func (s *Session) Format(format string) string { "%s": s.State, "%m": fmt.Sprintf("%f", s.StateDuration), "%q": s.Query, + "%a": s.ApplicationName, } output := format