-
Notifications
You must be signed in to change notification settings - Fork 672
/
metrics.go
97 lines (78 loc) · 2.53 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package metrics
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/ava-labs/avalanchego/utils/metric"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/vms/avm/block"
"github.com/ava-labs/avalanchego/vms/avm/txs"
)
var _ Metrics = (*metrics)(nil)
type Metrics interface {
metric.APIInterceptor
IncTxRefreshes()
IncTxRefreshHits()
IncTxRefreshMisses()
// MarkBlockAccepted updates all metrics relating to the acceptance of a
// block, including the underlying acceptance of the contained transactions.
MarkBlockAccepted(b block.Block) error
// MarkTxAccepted updates all metrics relating to the acceptance of a
// transaction.
//
// Note: This is not intended to be called during the acceptance of a block,
// as MarkBlockAccepted already handles updating transaction related
// metrics.
MarkTxAccepted(tx *txs.Tx) error
}
type metrics struct {
txMetrics *txMetrics
numTxRefreshes, numTxRefreshHits, numTxRefreshMisses prometheus.Counter
metric.APIInterceptor
}
func (m *metrics) IncTxRefreshes() {
m.numTxRefreshes.Inc()
}
func (m *metrics) IncTxRefreshHits() {
m.numTxRefreshHits.Inc()
}
func (m *metrics) IncTxRefreshMisses() {
m.numTxRefreshMisses.Inc()
}
func (m *metrics) MarkBlockAccepted(b block.Block) error {
for _, tx := range b.Txs() {
if err := tx.Unsigned.Visit(m.txMetrics); err != nil {
return err
}
}
return nil
}
func (m *metrics) MarkTxAccepted(tx *txs.Tx) error {
return tx.Unsigned.Visit(m.txMetrics)
}
func New(registerer prometheus.Registerer) (Metrics, error) {
txMetrics, err := newTxMetrics(registerer)
errs := wrappers.Errs{Err: err}
m := &metrics{txMetrics: txMetrics}
m.numTxRefreshes = prometheus.NewCounter(prometheus.CounterOpts{
Name: "tx_refreshes",
Help: "Number of times unique txs have been refreshed",
})
m.numTxRefreshHits = prometheus.NewCounter(prometheus.CounterOpts{
Name: "tx_refresh_hits",
Help: "Number of times unique txs have not been unique, but were cached",
})
m.numTxRefreshMisses = prometheus.NewCounter(prometheus.CounterOpts{
Name: "tx_refresh_misses",
Help: "Number of times unique txs have not been unique and weren't cached",
})
apiRequestMetric, err := metric.NewAPIInterceptor(registerer)
m.APIInterceptor = apiRequestMetric
errs.Add(
err,
registerer.Register(m.numTxRefreshes),
registerer.Register(m.numTxRefreshHits),
registerer.Register(m.numTxRefreshMisses),
)
return m, errs.Err
}