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 * `%s`: state
* `%m`: state duration * `%m`: state duration
* `%q`: query * `%q`: query
* `%a`: application name
# License # License
`pgterminate` is released under [The Unlicense](LICENSE) license. Code is under public domain. `pgterminate` is released under [The Unlicense](LICENSE) license. Code is under public domain.

View file

@ -3,6 +3,7 @@ package base
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"github.com/jouir/pgterminate/log" "github.com/jouir/pgterminate/log"
"github.com/lib/pq" "github.com/lib/pq"
) )
@ -48,7 +49,8 @@ func (db *Db) Sessions() (sessions []*Session) {
datname as db, datname as db,
coalesce(host(client_addr)::text || ':' || client_port::text, 'localhost') as client, coalesce(host(client_addr)::text || ':' || client_port::text, 'localhost') as client,
state as state, substring(query from 1 for %d) as query, 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 from pg_catalog.pg_stat_activity
where pid <> pg_backend_pid();`, maxQueryLength) where pid <> pg_backend_pid();`, maxQueryLength)
log.Debugf("query: %s\n", query) log.Debugf("query: %s\n", query)
@ -58,13 +60,13 @@ func (db *Db) Sessions() (sessions []*Session) {
for rows.Next() { for rows.Next() {
var pid sql.NullInt64 var pid sql.NullInt64
var user, db, client, state, query sql.NullString var user, db, client, state, query, applicationName sql.NullString
var stateDuration float64 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) Panic(err)
if pid.Valid && user.Valid && db.Valid && client.Valid && state.Valid && query.Valid { 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)) 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 // Session represents a PostgreSQL backend
type Session struct { type Session struct {
Pid int64 Pid int64
User string User string
Db string Db string
Client string Client string
State string State string
Query string Query string
StateDuration float64 StateDuration float64
ApplicationName string
} }
// NewSession instanciates a Session // 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{ return &Session{
Pid: pid, Pid: pid,
User: user, User: user,
Db: db, Db: db,
Client: client, Client: client,
State: state, State: state,
Query: query, Query: query,
StateDuration: stateDuration, StateDuration: stateDuration,
ApplicationName: applicationName,
} }
} }
@ -39,6 +41,7 @@ func (s *Session) Format(format string) string {
"%s": s.State, "%s": s.State,
"%m": fmt.Sprintf("%f", s.StateDuration), "%m": fmt.Sprintf("%f", s.StateDuration),
"%q": s.Query, "%q": s.Query,
"%a": s.ApplicationName,
} }
output := format output := format