-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
prom_reporter.go
250 lines (220 loc) · 7.78 KB
/
prom_reporter.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package promreporter
import (
"context"
"database/sql"
"math/big"
"sync"
"time"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"go.uber.org/multierr"
"gopkg.in/guregu/null.v4"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
//go:generate mockery --quiet --name PrometheusBackend --output ../../internal/mocks/ --case=underscore
type (
promReporter struct {
db *sql.DB
lggr logger.Logger
backend PrometheusBackend
newHeads *utils.Mailbox[*evmtypes.Head]
chStop utils.StopChan
wgDone sync.WaitGroup
reportPeriod time.Duration
utils.StartStopOnce
}
PrometheusBackend interface {
SetUnconfirmedTransactions(*big.Int, int64)
SetMaxUnconfirmedAge(*big.Int, float64)
SetMaxUnconfirmedBlocks(*big.Int, int64)
SetPipelineRunsQueued(n int)
SetPipelineTaskRunsQueued(n int)
}
defaultBackend struct{}
)
var (
promUnconfirmedTransactions = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "unconfirmed_transactions",
Help: "Number of currently unconfirmed transactions",
}, []string{"evmChainID"})
promMaxUnconfirmedAge = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "max_unconfirmed_tx_age",
Help: "The length of time the oldest unconfirmed transaction has been in that state (in seconds). Will be 0 if there are no unconfirmed transactions.",
}, []string{"evmChainID"})
promMaxUnconfirmedBlocks = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "max_unconfirmed_blocks",
Help: "The max number of blocks any currently unconfirmed transaction has been unconfirmed for",
}, []string{"evmChainID"})
promPipelineRunsQueued = promauto.NewGauge(prometheus.GaugeOpts{
Name: "pipeline_runs_queued",
Help: "The total number of pipeline runs that are awaiting execution",
})
promPipelineTaskRunsQueued = promauto.NewGauge(prometheus.GaugeOpts{
Name: "pipeline_task_runs_queued",
Help: "The total number of pipeline task runs that are awaiting execution",
})
)
func (defaultBackend) SetUnconfirmedTransactions(evmChainID *big.Int, n int64) {
promUnconfirmedTransactions.WithLabelValues(evmChainID.String()).Set(float64(n))
}
func (defaultBackend) SetMaxUnconfirmedAge(evmChainID *big.Int, s float64) {
promMaxUnconfirmedAge.WithLabelValues(evmChainID.String()).Set(s)
}
func (defaultBackend) SetMaxUnconfirmedBlocks(evmChainID *big.Int, n int64) {
promMaxUnconfirmedBlocks.WithLabelValues(evmChainID.String()).Set(float64(n))
}
func (defaultBackend) SetPipelineRunsQueued(n int) {
promPipelineTaskRunsQueued.Set(float64(n))
}
func (defaultBackend) SetPipelineTaskRunsQueued(n int) {
promPipelineRunsQueued.Set(float64(n))
}
func NewPromReporter(db *sql.DB, lggr logger.Logger, opts ...interface{}) *promReporter {
var backend PrometheusBackend = defaultBackend{}
period := 15 * time.Second
for _, opt := range opts {
switch v := opt.(type) {
case time.Duration:
period = v
case PrometheusBackend:
backend = v
}
}
chStop := make(chan struct{})
return &promReporter{
db: db,
lggr: lggr.Named("PromReporter"),
backend: backend,
newHeads: utils.NewSingleMailbox[*evmtypes.Head](),
chStop: chStop,
reportPeriod: period,
}
}
// Start starts PromReporter.
func (pr *promReporter) Start(context.Context) error {
return pr.StartOnce("PromReporter", func() error {
pr.wgDone.Add(1)
go pr.eventLoop()
return nil
})
}
func (pr *promReporter) Close() error {
return pr.StopOnce("PromReporter", func() error {
close(pr.chStop)
pr.wgDone.Wait()
return nil
})
}
func (pr *promReporter) Name() string {
return pr.lggr.Name()
}
func (pr *promReporter) HealthReport() map[string]error {
return map[string]error{pr.Name(): pr.StartStopOnce.Healthy()}
}
func (pr *promReporter) OnNewLongestChain(ctx context.Context, head *evmtypes.Head) {
pr.newHeads.Deliver(head)
}
func (pr *promReporter) eventLoop() {
pr.lggr.Debug("Starting event loop")
defer pr.wgDone.Done()
ctx, cancel := pr.chStop.NewCtx()
defer cancel()
for {
select {
case <-pr.newHeads.Notify():
head, exists := pr.newHeads.Retrieve()
if !exists {
continue
}
pr.reportHeadMetrics(ctx, head)
case <-time.After(pr.reportPeriod):
if err := errors.Wrap(pr.reportPipelineRunStats(ctx), "reportPipelineRunStats failed"); err != nil {
pr.lggr.Errorw("Error reporting prometheus metrics", "err", err)
}
case <-pr.chStop:
return
}
}
}
func (pr *promReporter) reportHeadMetrics(ctx context.Context, head *evmtypes.Head) {
evmChainID := head.EVMChainID.ToInt()
err := multierr.Combine(
errors.Wrap(pr.reportPendingEthTxes(ctx, evmChainID), "reportPendingEthTxes failed"),
errors.Wrap(pr.reportMaxUnconfirmedAge(ctx, evmChainID), "reportMaxUnconfirmedAge failed"),
errors.Wrap(pr.reportMaxUnconfirmedBlocks(ctx, head), "reportMaxUnconfirmedBlocks failed"),
)
if err != nil && ctx.Err() == nil {
pr.lggr.Errorw("Error reporting prometheus metrics", "err", err)
}
}
func (pr *promReporter) reportPendingEthTxes(ctx context.Context, evmChainID *big.Int) (err error) {
var unconfirmed int64
if err := pr.db.QueryRowContext(ctx, `SELECT count(*) FROM evm.txes WHERE state = 'unconfirmed' AND evm_chain_id = $1`, evmChainID.String()).Scan(&unconfirmed); err != nil {
return errors.Wrap(err, "failed to query for unconfirmed eth_tx count")
}
pr.backend.SetUnconfirmedTransactions(evmChainID, unconfirmed)
return nil
}
func (pr *promReporter) reportMaxUnconfirmedAge(ctx context.Context, evmChainID *big.Int) (err error) {
var broadcastAt null.Time
now := time.Now()
if err := pr.db.QueryRowContext(ctx, `SELECT min(initial_broadcast_at) FROM evm.txes WHERE state = 'unconfirmed' AND evm_chain_id = $1`, evmChainID.String()).Scan(&broadcastAt); err != nil {
return errors.Wrap(err, "failed to query for unconfirmed eth_tx count")
}
var seconds float64
if broadcastAt.Valid {
nanos := now.Sub(broadcastAt.ValueOrZero())
seconds = float64(nanos) / 1000000000
}
pr.backend.SetMaxUnconfirmedAge(evmChainID, seconds)
return nil
}
func (pr *promReporter) reportMaxUnconfirmedBlocks(ctx context.Context, head *evmtypes.Head) (err error) {
var earliestUnconfirmedTxBlock null.Int
err = pr.db.QueryRowContext(ctx, `
SELECT MIN(broadcast_before_block_num) FROM evm.tx_attempts
JOIN evm.txes ON evm.txes.id = evm.tx_attempts.eth_tx_id
WHERE evm.txes.state = 'unconfirmed'
AND evm_chain_id = $1
AND evm.txes.state = 'unconfirmed'`, head.EVMChainID.String()).Scan(&earliestUnconfirmedTxBlock)
if err != nil {
return errors.Wrap(err, "failed to query for min broadcast_before_block_num")
}
var blocksUnconfirmed int64
if !earliestUnconfirmedTxBlock.IsZero() {
blocksUnconfirmed = head.Number - earliestUnconfirmedTxBlock.ValueOrZero()
}
pr.backend.SetMaxUnconfirmedBlocks(head.EVMChainID.ToInt(), blocksUnconfirmed)
return nil
}
func (pr *promReporter) reportPipelineRunStats(ctx context.Context) (err error) {
rows, err := pr.db.QueryContext(ctx, `
SELECT pipeline_run_id FROM pipeline_task_runs WHERE finished_at IS NULL
`)
if err != nil {
return errors.Wrap(err, "failed to query for pipeline_run_id")
}
defer func() {
err = multierr.Combine(err, rows.Close())
}()
pipelineTaskRunsQueued := 0
pipelineRunsQueuedSet := make(map[int32]struct{})
for rows.Next() {
var pipelineRunID int32
if err = rows.Scan(&pipelineRunID); err != nil {
return errors.Wrap(err, "unexpected error scanning row")
}
pipelineTaskRunsQueued++
pipelineRunsQueuedSet[pipelineRunID] = struct{}{}
}
if err = rows.Err(); err != nil {
return err
}
pipelineRunsQueued := len(pipelineRunsQueuedSet)
pr.backend.SetPipelineTaskRunsQueued(pipelineTaskRunsQueued)
pr.backend.SetPipelineRunsQueued(pipelineRunsQueued)
return nil
}