This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
exporter.go
379 lines (345 loc) · 10.8 KB
/
exporter.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright 2017 Monax Industries Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package metrics
import (
"fmt"
"math"
"sort"
"github.com/tendermint/tendermint/types"
"github.com/hyperledger/burrow/acm/acmstate"
"github.com/hyperledger/burrow/logging"
"github.com/hyperledger/burrow/logging/structure"
"github.com/hyperledger/burrow/rpc"
"github.com/prometheus/client_golang/prometheus"
core_types "github.com/tendermint/tendermint/rpc/core/types"
)
const maxUnconfirmedTxsToFetch = 10000000000
const significantFiguresForSeconds = 3
type HistogramBuilder func(values []float64) (buckets map[float64]uint64, sum float64)
// Exporter is used to store Metrics data and embeds the config struct.
// This is done so that the relevant functions have easy access to the
// user defined runtime configuration when the Collect method is called.
type Exporter struct {
service InfoService
datum *Datum
chainID string
validatorMoniker string
blockSampleSize uint64
txPerBlockHistogramBuilder HistogramBuilder
timePerBlockHistogramBuilder HistogramBuilder
logger *logging.Logger
}
// Subset of rpc.Service
type InfoService interface {
Status() (*rpc.ResultStatus, error)
UnconfirmedTxs(maxTxs int64) (*rpc.ResultUnconfirmedTxs, error)
Peers() []core_types.Peer
Blocks(minHeight, maxHeight int64) (*rpc.ResultBlocks, error)
Stats() acmstate.AccountStatsGetter
}
// Datum is used to store data from all the relevant endpoints
type Datum struct {
LatestBlockHeight float64
UnconfirmedTxs float64
TotalPeers float64
InboundPeers float64
OutboundPeers float64
BlockSampleSize uint64
TotalTxs float64
TxPerBlockBuckets map[float64]uint64
TotalTime float64
TimePerBlockBuckets map[float64]uint64
AccountsWithCode float64
AccountsWithoutCode float64
}
// Exporter uses the InfoService to provide pre-aggregated metrics of various types that are then passed to prometheus
// as Const metrics rather than being accumulated by individual operations throughout the rest of the Burrow code.
func NewExporter(service InfoService, blockSampleSize int, logger *logging.Logger) (*Exporter, error) {
chainStatus, err := service.Status()
if err != nil {
return nil, fmt.Errorf("NewExporter(): %v", err)
}
return &Exporter{
datum: &Datum{},
service: service,
chainID: chainStatus.NodeInfo.Network,
validatorMoniker: chainStatus.NodeInfo.Moniker,
blockSampleSize: uint64(blockSampleSize),
txPerBlockHistogramBuilder: makeHistogramBuilder(identity),
timePerBlockHistogramBuilder: makeHistogramBuilder(significantFiguresRounder(significantFiguresForSeconds)),
logger: logger.With(structure.ComponentKey, "Metrics_Exporter"),
}, nil
}
// Describe - loops through the API metrics and passes them to prometheus.Describe
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
for _, m := range MetricDescriptions {
ch <- m
}
}
// Collect function, called on by Prometheus Client library
// This function is called when a scrape is performed by requesting /metrics
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
// Scrape the Data
var err = e.gatherData()
if err != nil {
return
}
// Set prometheus gauge metrics using the data gathered
ch <- prometheus.MustNewConstMetric(
Height,
prometheus.CounterValue,
e.datum.LatestBlockHeight,
e.chainID,
e.validatorMoniker,
)
ch <- prometheus.MustNewConstMetric(
UnconfirmedTransactions,
prometheus.GaugeValue,
e.datum.UnconfirmedTxs,
e.chainID,
e.validatorMoniker,
)
ch <- prometheus.MustNewConstMetric(
TotalPeers,
prometheus.GaugeValue,
e.datum.TotalPeers,
e.chainID,
e.validatorMoniker,
)
ch <- prometheus.MustNewConstMetric(
InboundPeers,
prometheus.GaugeValue,
e.datum.InboundPeers,
e.chainID,
e.validatorMoniker,
)
ch <- prometheus.MustNewConstMetric(
OutboundPeers,
prometheus.GaugeValue,
e.datum.OutboundPeers,
e.chainID,
e.validatorMoniker,
)
ch <- prometheus.MustNewConstHistogram(
TxPerBlock,
e.datum.BlockSampleSize,
e.datum.TotalTxs,
e.datum.TxPerBlockBuckets,
e.chainID,
e.validatorMoniker,
)
ch <- prometheus.MustNewConstHistogram(
TimePerBlock,
// Duration between each block in sample
e.datum.BlockSampleSize-1,
e.datum.TotalTime,
e.datum.TimePerBlockBuckets,
e.chainID,
e.validatorMoniker,
)
ch <- prometheus.MustNewConstMetric(
Contracts,
prometheus.GaugeValue,
e.datum.AccountsWithCode,
e.chainID,
e.validatorMoniker,
)
ch <- prometheus.MustNewConstMetric(
Users,
prometheus.GaugeValue,
e.datum.AccountsWithoutCode,
e.chainID,
e.validatorMoniker,
)
e.logger.InfoMsg("All Metrics successfully collected")
}
// gatherData - Collects the data from the API and stores into struct
func (e *Exporter) gatherData() error {
var err error
err = e.getStatus()
if err != nil {
return err
}
err = e.getMemPoolDepth()
if err != nil {
return err
}
err = e.getPeers()
if err != nil {
return err
}
blocks, err := e.getBlocks()
if err != nil {
return err
}
err = e.getTxBuckets(blocks.BlockMetas)
if err != nil {
return err
}
err = e.getBlockTimeBuckets(blocks.BlockMetas)
if err != nil {
return err
}
e.getAccountStats()
return nil
}
// Get status
func (e *Exporter) getStatus() error {
res, err := e.service.Status()
if err != nil {
return err
}
e.datum.LatestBlockHeight = float64(res.SyncInfo.LatestBlockHeight)
return nil
}
// Get unconfirmed_transactions
func (e *Exporter) getMemPoolDepth() error {
res, err := e.service.UnconfirmedTxs(maxUnconfirmedTxsToFetch)
if err != nil {
return err
}
e.datum.UnconfirmedTxs = float64(res.NumTxs)
return nil
}
// Get total peers
func (e *Exporter) getPeers() error {
peers := e.service.Peers()
e.datum.TotalPeers = float64(len(peers))
e.datum.InboundPeers = 0
e.datum.OutboundPeers = 0
for _, peer := range peers {
if peer.IsOutbound {
e.datum.OutboundPeers += 1
} else {
e.datum.InboundPeers += 1
}
}
return nil
}
func (e *Exporter) getBlocks() (*rpc.ResultBlocks, error) {
var minHeight uint64
var maxHeight uint64
maxHeight = uint64(e.datum.LatestBlockHeight)
if maxHeight >= e.blockSampleSize {
minHeight = maxHeight - (e.blockSampleSize - 1)
e.datum.BlockSampleSize = e.blockSampleSize
} else {
minHeight = 1
e.datum.BlockSampleSize = maxHeight
}
res, err := e.service.Blocks(int64(minHeight), int64(maxHeight))
if err != nil {
return nil, err
}
if len(res.BlockMetas) == 0 {
return nil, fmt.Errorf("no blocks returned")
}
return res, nil
}
// Get transaction buckets
func (e *Exporter) getTxBuckets(blockMetas []*types.BlockMeta) error {
e.datum.TotalTxs = 0
e.datum.TxPerBlockBuckets = map[float64]uint64{}
if len(blockMetas) == 0 {
return nil
}
// Collect number of txs per block as an array of floats
txsPerBlock := make([]float64, len(blockMetas))
for i, block := range blockMetas {
txsPerBlock[i] = float64(block.Header.NumTxs)
}
e.datum.TxPerBlockBuckets, e.datum.TotalTxs = e.txPerBlockHistogramBuilder(txsPerBlock)
return nil
}
func (e *Exporter) getBlockTimeBuckets(blockMetas []*types.BlockMeta) error {
e.datum.TotalTime = 0
e.datum.TimePerBlockBuckets = map[float64]uint64{}
if len(blockMetas) < 2 {
return nil
}
if blockMetas[0].Header.Height == 1 {
// The LastBlockTime on the first block is the GenesisDoc time! We don't want this
// in the block duration statistics
return e.getBlockTimeBuckets(blockMetas[1:])
}
blockDurations := make([]float64, len(blockMetas)-1)
for i := 0; i < len(blockMetas)-1; i++ {
timeBegan := blockMetas[i].Header.Time
timeEnded := blockMetas[i+1].Header.Time
blockDurations[i] = timeEnded.Sub(timeBegan).Seconds()
}
e.datum.TimePerBlockBuckets, e.datum.TotalTime = e.timePerBlockHistogramBuilder(blockDurations)
return nil
}
func (e *Exporter) getAccountStats() {
stats := e.service.Stats().GetAccountStats()
e.datum.AccountsWithCode = float64(stats.AccountsWithCode)
e.datum.AccountsWithoutCode = float64(stats.AccountsWithoutCode)
}
// Returns a function that builds a histogram.
//
// The builder takes a slice of values one for each entity in a sample, sorts it, and computes histogram buckets as
// a map from upper bounds (of values) to cumulative counts (of entities)
// such that count-many entities have value less than or equal to each upper bound.
// Returns this map and the sum of all values.
//
// The smoothing function can be used to round up the upper bounds of buckets so that generated buckets fall on rounder
// more predictable values. This can make querying for a specific bucket easier but not strictly necessary since we can
// use histogram_quantile function in promql to aggregate buckets without needing to know upper bounds ahead of time.
//
// For example if we have the collection (people are entities, numbers of oranges are values)
//
// Fred: 12 oranges
// Annie: 4 oranges
// Paul: 1 orange
//
// We return:
//
// upper bound number of oranges => cumulative number of people (with less than or equal to upper bound number of oranges)
// 1 => 1
// 4 => 2
// 12 => 3
//
// and 17 for the sum
func makeHistogramBuilder(smooth func(float64) float64) HistogramBuilder {
return func(vals []float64) (buckets map[float64]uint64, sum float64) {
buckets = make(map[float64]uint64)
sort.Slice(vals, func(i, j int) bool { return vals[i] < vals[j] })
var count uint64
for _, upper := range vals {
count++
// Use unsmoothed value for sum so we get true value
sum += upper
// Use smoothed upper bound for buckets. Collisions here are fine since the count is cumulative so any
// earlier upper/count pairs that are overwritten will have their count captured in the overwriting count
buckets[smooth(upper)] = count
}
return buckets, sum
}
}
func identity(x float64) float64 {
return x
}
func significantFiguresRounder(n int) func(float64) float64 {
// exponent base 10 for this many sig figs
sfPow := float64(n - 1)
return func(x float64) float64 {
// Floor of exponent to take us to 1 sig fig
log10x := math.Floor(math.Log10(x))
// Power of 10 to scale us to n sig figs
fac := math.Pow(10, sfPow-log10x)
// Scale to n sig figs, drop digits to right of decimal point, then scale back
return math.Round(x*fac) / fac
}
}