-
Notifications
You must be signed in to change notification settings - Fork 40
/
middleware.go
73 lines (60 loc) · 2.03 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package zenrpc
import (
"context"
"encoding/json"
"github.com/prometheus/client_golang/prometheus"
"log"
"strconv"
"time"
)
// Logger is middleware for JSON-RPC 2.0 Server.
// It's just an example for middleware, will be refactored later.
func Logger(l *log.Logger) MiddlewareFunc {
return func(h InvokeFunc) InvokeFunc {
return func(ctx context.Context, method string, params json.RawMessage) Response {
start, ip := time.Now(), "<nil>"
if req, ok := RequestFromContext(ctx); ok && req != nil {
ip = req.RemoteAddr
}
r := h(ctx, method, params)
l.Printf("ip=%s method=%s.%s duration=%v params=%s err=%s", ip, NamespaceFromContext(ctx), method, time.Since(start), params, r.Error)
return r
}
}
}
// Metrics is a middleware for logging duration of RPC requests via Prometheus. Default AppName is zenrpc.
// It exposes two metrics: appName_rpc_error_requests_count and appName_rpc_responses_duration_seconds.
func Metrics(appName string) MiddlewareFunc {
if appName == "" {
appName = "zenrpc"
}
rpcErrors := prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: appName,
Subsystem: "rpc",
Name: "error_requests_count",
Help: "Error requests count by method and error code.",
}, []string{"method", "code"})
rpcDurations := prometheus.NewSummaryVec(prometheus.SummaryOpts{
Namespace: appName,
Subsystem: "rpc",
Name: "responses_duration_seconds",
Help: "Response time by method and error code.",
}, []string{"method", "code"})
prometheus.MustRegister(rpcErrors, rpcDurations)
return func(h InvokeFunc) InvokeFunc {
return func(ctx context.Context, method string, params json.RawMessage) Response {
start, code := time.Now(), ""
r := h(ctx, method, params)
// log metrics
if n := NamespaceFromContext(ctx); n != "" {
method = n + "." + method
}
if r.Error != nil {
code = strconv.Itoa(r.Error.Code)
rpcErrors.WithLabelValues(method, code).Inc()
}
rpcDurations.WithLabelValues(method, code).Observe(time.Since(start).Seconds())
return r
}
}
}