-
Notifications
You must be signed in to change notification settings - Fork 180
/
monitor.go
57 lines (45 loc) · 1.01 KB
/
monitor.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
package monitor
import (
"fmt"
"time"
"github.com/go-kit/kit/metrics"
"github.com/tendermint/tendermint/libs/log"
)
// RpcMetrics ...
type RpcMetrics struct {
Counter metrics.Counter
Histogram metrics.Histogram
}
type Monitor struct {
method string
logger log.Logger
lastTime time.Time
metrics map[string]*RpcMetrics
}
func GetMonitor(method string, logger log.Logger, metrics map[string]*RpcMetrics) *Monitor {
return &Monitor{
method: method,
logger: logger,
metrics: metrics,
}
}
func (m *Monitor) OnBegin() *Monitor {
m.lastTime = time.Now()
if m.metrics == nil {
return m
}
if _, ok := m.metrics[m.method]; ok {
m.metrics[m.method].Counter.Add(1)
}
return m
}
func (m *Monitor) OnEnd(args ...interface{}) {
elapsed := time.Since(m.lastTime).Seconds()
m.logger.Debug(fmt.Sprintf("RPC: Method<%s>, Elapsed<%fms>, Params<%v>", m.method, elapsed*1e3, args))
if m.metrics == nil {
return
}
if _, ok := m.metrics[m.method]; ok {
m.metrics[m.method].Histogram.Observe(elapsed)
}
}