-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.go
147 lines (116 loc) · 4.81 KB
/
wrapper.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
package prometheus
import (
"context"
"fmt"
"github.com/asim/go-micro/v3/client"
"github.com/asim/go-micro/v3/registry"
"github.com/asim/go-micro/v3/server"
"github.com/prometheus/client_golang/prometheus"
)
type wrapper struct {
client.Client
prom *plugin
callFunc client.CallFunc
}
func (p *plugin) NewClientWrapper() client.Wrapper {
return func(c client.Client) client.Client {
handler := &wrapper{Client: c, prom: p}
return handler
}
}
func (p *plugin) NewCallWrapper() client.CallWrapper {
return func(fn client.CallFunc) client.CallFunc {
handler := &wrapper{callFunc: fn, prom: p}
return handler.CallFunc
}
}
func (p *plugin) NewHandlerWrapper() server.HandlerWrapper {
handler := &wrapper{prom: p}
return handler.HandlerFunc
}
func (p *plugin) NewSubscriberWrapper() server.SubscriberWrapper {
handler := &wrapper{prom: p}
return handler.SubscriberFunc
}
func (w *wrapper) CallFunc(ctx context.Context, node *registry.Node, req client.Request, rsp interface{}, opts client.CallOptions) error {
status, endpoint := "success", fmt.Sprintf("%s.%s", req.Service(), req.Endpoint())
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(s float64) {
w.prom.callDur.WithLabelValues(w.prom.opts.ServiceID, w.prom.opts.ServiceName, w.prom.opts.ServiceVersion, endpoint).Observe(s)
}))
defer timer.ObserveDuration()
err := w.callFunc(ctx, node, req, rsp, opts)
if err != nil {
status = "failure"
}
w.prom.callCnt.WithLabelValues(w.prom.opts.ServiceID, w.prom.opts.ServiceName, w.prom.opts.ServiceVersion, endpoint, status).Inc()
return err
}
func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
status, endpoint := "success", fmt.Sprintf("%s.%s", req.Service(), req.Endpoint())
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(s float64) {
w.prom.callDur.WithLabelValues(w.prom.opts.ServiceID, w.prom.opts.ServiceName, w.prom.opts.ServiceVersion, endpoint).Observe(s)
}))
defer timer.ObserveDuration()
err := w.Client.Call(ctx, req, rsp, opts...)
if err != nil {
status = "failure"
}
w.prom.callCnt.WithLabelValues(w.prom.opts.ServiceID, w.prom.opts.ServiceName, w.prom.opts.ServiceVersion, endpoint, status).Inc()
return err
}
func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
status, endpoint := "success", fmt.Sprintf("%s.%s", req.Service(), req.Endpoint())
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(s float64) {
w.prom.streamDur.WithLabelValues(w.prom.opts.ServiceID, w.prom.opts.ServiceName, w.prom.opts.ServiceVersion, endpoint).Observe(s)
}))
defer timer.ObserveDuration()
stream, err := w.Client.Stream(ctx, req, opts...)
if err != nil {
status = "failure"
}
w.prom.streamCnt.WithLabelValues(w.prom.opts.ServiceID, w.prom.opts.ServiceName, w.prom.opts.ServiceVersion, endpoint, status).Inc()
return stream, err
}
func (w *wrapper) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
status, endpoint := "success", p.Topic()
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(s float64) {
w.prom.publishDur.WithLabelValues(w.prom.opts.ServiceID, w.prom.opts.ServiceName, w.prom.opts.ServiceVersion, endpoint).Observe(s)
}))
defer timer.ObserveDuration()
err := w.Client.Publish(ctx, p, opts...)
if err != nil {
status = "failure"
}
w.prom.publishCnt.WithLabelValues(w.prom.opts.ServiceName, w.prom.opts.ServiceVersion, w.prom.opts.ServiceID, endpoint, status).Inc()
return err
}
func (w *wrapper) HandlerFunc(fn server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
status, endpoint := "success", req.Endpoint()
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(s float64) {
w.prom.handleDur.WithLabelValues(w.prom.opts.ServiceID, w.prom.opts.ServiceName, w.prom.opts.ServiceVersion, endpoint).Observe(s)
}))
defer timer.ObserveDuration()
err := fn(ctx, req, rsp)
if err != nil {
status = "failure"
}
w.prom.handleCnt.WithLabelValues(w.prom.opts.ServiceID, w.prom.opts.ServiceName, w.prom.opts.ServiceVersion, endpoint, status).Inc()
return err
}
}
func (w *wrapper) SubscriberFunc(fn server.SubscriberFunc) server.SubscriberFunc {
return func(ctx context.Context, msg server.Message) error {
status, endpoint := "success", msg.Topic()
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(s float64) {
w.prom.subscribeDur.WithLabelValues(w.prom.opts.ServiceID, w.prom.opts.ServiceName, w.prom.opts.ServiceVersion, endpoint).Observe(s)
}))
defer timer.ObserveDuration()
err := fn(ctx, msg)
if err != nil {
status = "failure"
}
w.prom.subscribeCnt.WithLabelValues(w.prom.opts.ServiceID, w.prom.opts.ServiceName, w.prom.opts.ServiceVersion, endpoint, status).Inc()
return err
}
}