Add application name placeholder to log format

This commit is contained in:
Julien Riou 2019-02-16 15:01:48 +01:00
parent 24eb4fe203
commit 3f2b1d86f3
No known key found for this signature in database
GPG key ID: BA3E15176E45E85D
3 changed files with 26 additions and 20 deletions

View file

@ -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.

View file

@ -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))
}
}

View file

@ -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