-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathmiddleware_logging.go
81 lines (68 loc) · 2.44 KB
/
middleware_logging.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package session
import (
"context"
"time"
"github.com/influxdata/influxdb/v2"
"go.uber.org/zap"
)
// SessionLogger is a logger service middleware for sessions
type SessionLogger struct {
logger *zap.Logger
sessionService influxdb.SessionService
}
var _ influxdb.SessionService = (*SessionLogger)(nil)
// NewSessionLogger returns a logging service middleware for the User Service.
func NewSessionLogger(log *zap.Logger, s influxdb.SessionService) *SessionLogger {
return &SessionLogger{
logger: log,
sessionService: s,
}
}
// FindSession calls the underlying session service and logs the results of the request
func (l *SessionLogger) FindSession(ctx context.Context, key string) (session *influxdb.Session, err error) {
defer func(start time.Time) {
dur := zap.Duration("took", time.Since(start))
if err != nil {
l.logger.Debug("failed to session find", zap.Error(err), dur)
return
}
l.logger.Debug("session find", dur)
}(time.Now())
return l.sessionService.FindSession(ctx, key)
}
// ExpireSession calls the underlying session service and logs the results of the request
func (l *SessionLogger) ExpireSession(ctx context.Context, key string) (err error) {
defer func(start time.Time) {
dur := zap.Duration("took", time.Since(start))
if err != nil {
l.logger.Debug("failed to session expire", zap.Error(err), dur)
return
}
l.logger.Debug("session expire", dur)
}(time.Now())
return l.sessionService.ExpireSession(ctx, key)
}
// CreateSession calls the underlying session service and logs the results of the request
func (l *SessionLogger) CreateSession(ctx context.Context, user string) (s *influxdb.Session, err error) {
defer func(start time.Time) {
dur := zap.Duration("took", time.Since(start))
if err != nil {
l.logger.Debug("failed to session create", zap.Error(err), dur)
return
}
l.logger.Debug("session create", dur)
}(time.Now())
return l.sessionService.CreateSession(ctx, user)
}
// RenewSession calls the underlying session service and logs the results of the request
func (l *SessionLogger) RenewSession(ctx context.Context, session *influxdb.Session, newExpiration time.Time) (err error) {
defer func(start time.Time) {
dur := zap.Duration("took", time.Since(start))
if err != nil {
l.logger.Debug("failed to session renew", zap.Error(err), dur)
return
}
l.logger.Debug("session renew", dur)
}(time.Now())
return l.sessionService.RenewSession(ctx, session, newExpiration)
}