-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprom.go
More file actions
72 lines (55 loc) · 1.75 KB
/
prom.go
File metadata and controls
72 lines (55 loc) · 1.75 KB
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
package scorumgo
import (
"context"
"encoding/json"
"errors"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/scorum/scorum-go/caller"
"github.com/scorum/scorum-go/rpc/protocol"
)
var (
callsProcessed = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "call_pending",
Help: "The number of pending calls at the moment",
}, []string{"api", "method"})
callDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "call_duration_millisecond",
Help: "",
}, []string{"status", "api", "method"})
)
type PrometheusInterceptor struct {
caller caller.CallCloser
}
func NewPrometheusInterceptor(caller caller.CallCloser) *PrometheusInterceptor {
return &PrometheusInterceptor{caller: caller}
}
func (c *PrometheusInterceptor) Call(ctx context.Context, api string, method string, args []interface{}, reply interface{}) error {
start := time.Now()
callsProcessed.WithLabelValues(api, method).Inc()
defer callsProcessed.WithLabelValues(api, method).Dec()
err := c.caller.Call(ctx, api, method, args, reply)
if err != nil {
var status = "error"
if errors.Is(err, protocol.ErrWaitResponseTimeout) {
status = "timeout"
}
callDuration.WithLabelValues(status, api, method).
Observe(float64(time.Since(start).Nanoseconds()) / 1000000)
return err
}
callDuration.WithLabelValues("ok", api, method).
Observe(float64(time.Since(start).Nanoseconds()) / 1000000)
return nil
}
func (c *PrometheusInterceptor) SetCallback(api string, method string, callback func(raw json.RawMessage)) error {
err := c.caller.SetCallback(api, method, callback)
if err != nil {
return err
}
return nil
}
func (c *PrometheusInterceptor) Close() error {
return c.caller.Close()
}