Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit 5af73e8

Browse files
committed
auth: add Query method to audit and call it from server handler
This new call logs queries even when they have an error and show how much time they took. Signed-off-by: Javi Fontan <jfontan@gmail.com>
1 parent a8b0c56 commit 5af73e8

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

auth/audit.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ func (a *Audit) Allowed(ctx *sql.Context, permission Permission) error {
7171
return err
7272
}
7373

74+
// Query implements AuditQuery interface.
75+
func (a *Audit) Query(ctx *sql.Context, d time.Duration, err error) {
76+
if q, ok := a.auth.(*Audit); ok {
77+
q.Query(ctx, d, err)
78+
}
79+
80+
a.method.Query(ctx, d, err)
81+
}
82+
7483
// NewAuditLog creates a new AuditMethod that logs to a logrus.Logger.
7584
func NewAuditLog(l *logrus.Logger) AuditMethod {
7685
la := l.WithField("system", "audit")

server/handler.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import (
66
"strconv"
77
"strings"
88
"sync"
9+
"time"
910

1011
errors "gopkg.in/src-d/go-errors.v1"
1112
"gopkg.in/src-d/go-mysql-server.v0"
13+
"gopkg.in/src-d/go-mysql-server.v0/auth"
1214
"gopkg.in/src-d/go-mysql-server.v0/sql"
1315

1416
"github.com/sirupsen/logrus"
@@ -72,7 +74,7 @@ func (h *Handler) ComQuery(
7274
c *mysql.Conn,
7375
query string,
7476
callback func(*sqltypes.Result) error,
75-
) error {
77+
) (err error) {
7678
ctx := h.sm.NewContextWithQuery(c, query)
7779

7880
handled, err := h.handleKill(c, query)
@@ -84,7 +86,14 @@ func (h *Handler) ComQuery(
8486
return nil
8587
}
8688

89+
start := time.Now()
8790
schema, rows, err := h.e.Query(ctx, query)
91+
defer func() {
92+
if q, ok := h.e.Auth.(*auth.Audit); ok {
93+
q.Query(ctx, time.Since(start), err)
94+
}
95+
}()
96+
8897
if err != nil {
8998
return err
9099
}

0 commit comments

Comments
 (0)