-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
52 lines (43 loc) · 1.18 KB
/
middleware.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
package main
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/metrics"
)
type loggingMiddleware struct {
logger log.Logger
next Router
}
func (mw loggingMiddleware) route(ctx context.Context, request jsonrpcRequest) (response jsonrpcResponse, err error) {
defer func(begin time.Time) {
paramsString, _ := json.Marshal(request.Params)
mw.logger.Log(
"id", request.ID,
"method", request.Method,
"params", string(paramsString),
"err", err,
"took", time.Since(begin),
)
}(time.Now())
response, err = mw.next.route(ctx, request)
return
}
//instru
type instrumentingMiddleware struct {
requestCount metrics.Counter
requestLatency metrics.Histogram
countResult metrics.Histogram
next Router
}
func (mw instrumentingMiddleware) route(ctx context.Context, request jsonrpcRequest) (response jsonrpcResponse, err error) {
defer func(begin time.Time) {
lvs := []string{"method", request.Method, "error", fmt.Sprint(err != nil)}
mw.requestCount.With(lvs...).Add(1)
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
}(time.Now())
response, err = mw.next.route(ctx, request)
return
}