/
metrics.go
83 lines (67 loc) · 1.78 KB
/
metrics.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
package roothash
import (
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/oasislabs/oasis-core/go/common"
"github.com/oasislabs/oasis-core/go/common/pubsub"
"github.com/oasislabs/oasis-core/go/roothash/api"
)
var (
rootHashFinalizedRounds = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "oasis_finalized_rounds",
Help: "Number of finalized rounds.",
},
)
rootHashBlockInterval = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Name: "oasis_roothash_block_interval",
Help: "Time between roothash blocks (seconds).",
},
[]string{"runtime"},
)
rootHashCollectors = []prometheus.Collector{
rootHashFinalizedRounds,
rootHashBlockInterval,
}
_ api.Backend = (*metricsWrapper)(nil)
metricsOnce sync.Once
)
type metricsWrapper struct {
api.Backend
}
func (w *metricsWrapper) WatchBlocks(id common.Namespace) (<-chan *api.AnnotatedBlock, *pubsub.Subscription, error) {
return w.Backend.WatchBlocks(id)
}
func (w *metricsWrapper) worker() {
backend, ok := w.Backend.(api.MetricsMonitorable)
if !ok {
return
}
ch, sub := backend.WatchAllBlocks()
defer sub.Close()
lastBlockTime := make(map[common.Namespace]time.Time)
for {
blk, ok := <-ch
if !ok {
break
}
if ts, ok := lastBlockTime[blk.Header.Namespace]; ok {
rootHashBlockInterval.With(prometheus.Labels{
"runtime": blk.Header.Namespace.String(),
}).Observe(time.Since(ts).Seconds())
}
lastBlockTime[blk.Header.Namespace] = time.Now()
rootHashFinalizedRounds.Inc()
}
}
// NewMetricsWrapper wraps a roothash backend implementation with instrumentation.
func NewMetricsWrapper(base api.Backend) api.Backend {
metricsOnce.Do(func() {
prometheus.MustRegister(rootHashCollectors...)
})
w := &metricsWrapper{Backend: base}
go w.worker()
return w
}