-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
prom_reporter.go
211 lines (182 loc) · 6.14 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
package services
import (
"context"
"database/sql"
"time"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/store"
"github.com/smartcontractkit/chainlink/core/store/models"
"go.uber.org/multierr"
"gopkg.in/guregu/null.v4"
)
//go:generate mockery --name PrometheusBackend --output ../internal/mocks/ --case=underscore
type (
promReporter struct {
db *sql.DB
backend PrometheusBackend
}
PrometheusBackend interface {
SetUnconfirmedTransactions(int64)
SetMaxUnconfirmedAge(float64)
SetMaxUnconfirmedBlocks(int64)
SetPipelineRunsQueued(n int)
SetPipelineTaskRunsQueued(n int)
}
defaultBackend struct{}
)
var (
promUnconfirmedTransactions = promauto.NewGauge(prometheus.GaugeOpts{
Name: "unconfirmed_transactions",
Help: "Number of currently unconfirmed transactions",
})
promMaxUnconfirmedAge = promauto.NewGauge(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.",
})
promMaxUnconfirmedBlocks = promauto.NewGauge(prometheus.GaugeOpts{
Name: "max_unconfirmed_blocks",
Help: "The max number of blocks any currently unconfirmed transaction has been unconfirmed for",
})
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(n int64) {
promUnconfirmedTransactions.Set(float64(n))
}
func (defaultBackend) SetMaxUnconfirmedAge(s float64) {
promMaxUnconfirmedAge.Set(s)
}
func (defaultBackend) SetMaxUnconfirmedBlocks(n int64) {
promMaxUnconfirmedBlocks.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, opts ...PrometheusBackend) store.HeadTrackable {
var backend PrometheusBackend
if len(opts) > 0 {
backend = opts[0]
} else {
backend = defaultBackend{}
}
return &promReporter{db, backend}
}
// Do nothing on connect, simply wait for the next head
func (pr *promReporter) Connect(*models.Head) error {
return nil
}
func (pr *promReporter) Disconnect() {
// pass
}
func (pr *promReporter) OnNewLongestChain(ctx context.Context, head models.Head) {
err := multierr.Combine(
errors.Wrap(pr.reportPendingEthTxes(ctx), "reportPendingEthTxes failed"),
errors.Wrap(pr.reportMaxUnconfirmedAge(ctx), "reportMaxUnconfirmedAge failed"),
errors.Wrap(pr.reportMaxUnconfirmedBlocks(ctx, head), "reportMaxUnconfirmedBlocks failed"),
errors.Wrap(pr.reportPipelineRunStats(ctx), "reportPipelineRunStats failed"),
)
if err != nil {
logger.Errorw("Error reporting prometheus metrics", "err", err)
}
}
func (pr *promReporter) reportPendingEthTxes(ctx context.Context) (err error) {
rows, err := pr.db.QueryContext(ctx, `SELECT count(*) FROM eth_txes WHERE state = 'unconfirmed'`)
if err != nil {
return errors.Wrap(err, "failed to query for unconfirmed eth_tx count")
}
defer func() {
err = multierr.Combine(err, rows.Close())
}()
var unconfirmed int64
for rows.Next() {
if err := rows.Scan(&unconfirmed); err != nil {
return errors.Wrap(err, "unexpected error scanning row")
}
}
pr.backend.SetUnconfirmedTransactions(unconfirmed)
return nil
}
func (pr *promReporter) reportMaxUnconfirmedAge(ctx context.Context) (err error) {
now := time.Now()
rows, err := pr.db.QueryContext(ctx, `SELECT min(broadcast_at) FROM eth_txes WHERE state = 'unconfirmed'`)
if err != nil {
return errors.Wrap(err, "failed to query for unconfirmed eth_tx count")
}
defer func() {
err = multierr.Combine(err, rows.Close())
}()
var broadcastAt null.Time
for rows.Next() {
if err := rows.Scan(&broadcastAt); err != nil {
return errors.Wrap(err, "unexpected error scanning row")
}
}
var seconds float64
if broadcastAt.Valid {
nanos := now.Sub(broadcastAt.ValueOrZero())
seconds = float64(nanos) / 1000000000
}
pr.backend.SetMaxUnconfirmedAge(seconds)
return nil
}
func (pr *promReporter) reportMaxUnconfirmedBlocks(ctx context.Context, head models.Head) (err error) {
rows, err := pr.db.QueryContext(ctx, `
SELECT MIN(broadcast_before_block_num) FROM eth_tx_attempts
JOIN eth_txes ON eth_txes.id = eth_tx_attempts.eth_tx_id
AND eth_txes.state = 'unconfirmed'`)
if err != nil {
return errors.Wrap(err, "failed to query for min broadcast_before_block_num")
}
defer func() {
err = multierr.Combine(err, rows.Close())
}()
var earliestUnconfirmedTxBlock null.Int
for rows.Next() {
if err := rows.Scan(&earliestUnconfirmedTxBlock); err != nil {
return errors.Wrap(err, "unexpected error scanning row")
}
}
var blocksUnconfirmed int64
if !earliestUnconfirmedTxBlock.IsZero() {
blocksUnconfirmed = head.Number - earliestUnconfirmedTxBlock.ValueOrZero()
}
pr.backend.SetMaxUnconfirmedBlocks(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{}{}
}
pipelineRunsQueued := len(pipelineRunsQueuedSet)
pr.backend.SetPipelineTaskRunsQueued(pipelineTaskRunsQueued)
pr.backend.SetPipelineRunsQueued(pipelineRunsQueued)
return nil
}