Skip to content

Commit

Permalink
Add application name placeholder to log format
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Riou committed Feb 16, 2019
1 parent 24eb4fe commit 3f2b1d8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
12 changes: 7 additions & 5 deletions base/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package base
import (
"database/sql"
"fmt"

"github.com/jouir/pgterminate/log"
"github.com/lib/pq"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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))
}
}

Expand Down
33 changes: 18 additions & 15 deletions base/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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
Expand Down

0 comments on commit 3f2b1d8

Please sign in to comment.