-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware_aggregatelogger.go
172 lines (148 loc) · 5.21 KB
/
middleware_aggregatelogger.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package work
import (
"fmt"
"time"
ginlogrus "github.com/Bose/go-gin-logrus/v2"
opentracing "github.com/opentracing/opentracing-go"
"github.com/sirupsen/logrus"
)
// ContextTraceIDField - used to find the trace id in the context - optional
var ContextTraceIDField string
// AggregateLogger defines the const string for getting the logger from a Job context
const AggregateLogger = "aggregateLogger"
const optionAggregateLoggingUseBanner = "optionAggregateLoggingUseBanner"
// WithBanner specifies the table name to use for an outbox
func WithBanner(useBanner bool) Option {
return func(o Options) {
o[optionAggregateLoggingUseBanner] = useBanner
}
}
const optionSilentNoResponse = "optionSilentNoResponse"
// WithSilentNoResponse specifies that StatusNoResponse requests should be silent (no logging)
func WithSilentNoResponse(silent bool) Option {
return func(o Options) {
o[optionSilentNoResponse] = silent
}
}
const optionSilentSuccess = "optionSilentSuccess"
// WithSilentSuccess specifies that StatusSuccess requests should be silent (no logging)
func WithSilentSuccess(silent bool) Option {
return func(o Options) {
o[optionSilentSuccess] = silent
}
}
// ReducedLoggingFunc defines a function type used for custom logic on when to print logs
type ReducedLoggingFunc func(workStatus Status, logBufferLength int) bool
var DefaultReducedLoggingFunc ReducedLoggingFunc = func(s Status, l int) bool {return false}
const optionReducedLoggingFunc = "optionReducedLoggingFunc"
// WithReducedLoggingFunc specifies the function used to set custom logic around when to print logs
func WithReducedLoggingFunc(a ReducedLoggingFunc) Option {
return func(o Options) {
o[optionReducedLoggingFunc] = a
}
}
const optionLogLevel = "optionLogLevel"
// WithLogLevel will set the logrus log level for the job handler
func WithLogLevel(level logrus.Level) Option {
return func(o Options) {
o[optionLogLevel] = level
}
}
// WithAggregateLogger is a middleware adapter for aggregated logging (see go-gin-logrus)
func WithAggregateLogger(
useBanner bool,
timeFormat string,
utc bool,
logrusFieldNameForTraceID string,
contextTraceIDField []byte,
opt ...Option) Adapter {
opts := GetOpts(opt...)
if contextTraceIDField != nil {
ContextTraceIDField = string(contextTraceIDField)
}
return func(h Handler) Handler {
return Handler(func(job *Job) error {
useBanner := false
if b, ok := opts[optionAggregateLoggingUseBanner].(bool); ok {
useBanner = b
}
silentNoResponse := false
if b, ok := opts[optionSilentNoResponse].(bool); ok {
silentNoResponse = b
}
silentSuccess := false
if b, ok := opts[optionSilentSuccess].(bool); ok {
silentSuccess = b
}
var reducedLoggingFunc ReducedLoggingFunc = DefaultReducedLoggingFunc
if f, ok := opts[optionReducedLoggingFunc].(ReducedLoggingFunc); ok {
reducedLoggingFunc = f
}
logLevel := logrus.DebugLevel
if l, ok := opts[optionLogLevel].(logrus.Level); ok {
logLevel = l
}
aggregateLoggingBuff := ginlogrus.NewLogBuffer(ginlogrus.WithBanner(false))
aggregateRequestLogger := &logrus.Logger{
Out: &aggregateLoggingBuff,
Formatter: new(logrus.JSONFormatter),
Hooks: make(logrus.LevelHooks),
Level: logLevel,
}
start := time.Now()
// you have to use this logger for every *logrus.Entry you create
job.Ctx.Set(AggregateLogger, aggregateRequestLogger)
// this will be deferred until after the chained handlers are executed
defer func() {
if (silentNoResponse && job.Ctx.Status() == StatusNoResponse) ||
(silentSuccess && job.Ctx.Status() == StatusSuccess) ||
reducedLoggingFunc(job.Ctx.Status(), aggregateLoggingBuff.Length()) {
return
}
end := time.Now()
latency := end.Sub(start)
if utc {
end = end.UTC()
}
var requestID string
// see if we're using github.com/Bose/go-gin-opentracing which will set a span in "tracing-context"
if s, ok := job.Ctx.Get("tracing-context"); ok {
span := s.(opentracing.Span)
requestID = fmt.Sprintf("%v", span)
}
// check a user defined context field
if len(requestID) == 0 && contextTraceIDField != nil {
if id, found := job.Ctx.Get(string(ContextTraceIDField)); found {
requestID = id.(string)
}
}
var comment interface{}
if c, found := job.Ctx.Get("comment"); found {
comment = c
}
fields := logrus.Fields{
logrusFieldNameForTraceID: requestID,
"status": job.Ctx.Status(),
"handler": job.Handler,
"latency-ms": float64(latency) / float64(time.Millisecond),
"time": end.Format(timeFormat),
}
if workerNumber, ok := job.Ctx.Value("workerNumber").(int64); ok {
fields["workerNumber"] = workerNumber
}
if comment != nil {
fields["comment"] = comment
}
aggregateLoggingBuff.StoreHeader("request-summary-info", fields)
if useBanner {
// need to upgrade go-gin-logrus to let clients define their own banner
// aggregateLoggingBuff.CustomBanner := "-------------------------------------------------------------"
aggregateLoggingBuff.AddBanner = true
}
fmt.Printf(aggregateLoggingBuff.String())
}()
err := h(job)
return err
})
}
}