Skip to content

Commit 8d3654a

Browse files
authored
Merge pull request #949 from onflow/mpeter/add-total-supply-metric
Add metric to track the Flow total supply on each block
2 parents 73b01eb + 603c1b6 commit 8d3654a

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

metrics/collector.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package metrics
22

33
import (
44
"fmt"
5+
"math/big"
56
"time"
67

78
"github.com/prometheus/client_golang/prometheus"
@@ -86,6 +87,11 @@ var rateLimitedTransactionsCounter = prometheus.NewCounter(prometheus.CounterOpt
8687
Help: "Total number of rate-limited transactions",
8788
})
8889

90+
var flowTotalSupply = prometheus.NewGauge(prometheus.GaugeOpts{
91+
Name: prefixedName("flow_total_supply"),
92+
Help: "Total supply of FLOW tokens in EVM at a given time (in smallest unit, wei)",
93+
})
94+
8995
var metrics = []prometheus.Collector{
9096
apiErrors,
9197
serverPanicsCounters,
@@ -102,6 +108,7 @@ var metrics = []prometheus.Collector{
102108
requestRateLimitedCounters,
103109
transactionsDroppedCounter,
104110
rateLimitedTransactionsCounter,
111+
flowTotalSupply,
105112
}
106113

107114
type Collector interface {
@@ -119,11 +126,14 @@ type Collector interface {
119126
RequestRateLimited(method string)
120127
TransactionsDropped(count int)
121128
TransactionRateLimited()
129+
FlowTotalSupply(totalSupply *big.Int)
122130
}
123131

124132
var _ Collector = &DefaultCollector{}
125133

126134
type DefaultCollector struct {
135+
logger zerolog.Logger
136+
127137
// TODO: for now we cannot differentiate which api request failed number of times
128138
apiErrorsCounter prometheus.Counter
129139
serverPanicsCounters *prometheus.CounterVec
@@ -140,6 +150,7 @@ type DefaultCollector struct {
140150
requestRateLimitedCounters *prometheus.CounterVec
141151
transactionsDroppedCounter prometheus.Counter
142152
rateLimitedTransactionsCounter prometheus.Counter
153+
flowTotalSupply prometheus.Gauge
143154
}
144155

145156
func NewCollector(logger zerolog.Logger) Collector {
@@ -149,6 +160,7 @@ func NewCollector(logger zerolog.Logger) Collector {
149160
}
150161

151162
return &DefaultCollector{
163+
logger: logger,
152164
apiErrorsCounter: apiErrors,
153165
serverPanicsCounters: serverPanicsCounters,
154166
cadenceBlockHeight: cadenceBlockHeight,
@@ -164,6 +176,7 @@ func NewCollector(logger zerolog.Logger) Collector {
164176
requestRateLimitedCounters: requestRateLimitedCounters,
165177
transactionsDroppedCounter: transactionsDroppedCounter,
166178
rateLimitedTransactionsCounter: rateLimitedTransactionsCounter,
179+
flowTotalSupply: flowTotalSupply,
167180
}
168181
}
169182

@@ -246,6 +259,19 @@ func (c *DefaultCollector) TransactionRateLimited() {
246259
c.rateLimitedTransactionsCounter.Inc()
247260
}
248261

262+
func (c *DefaultCollector) FlowTotalSupply(totalSupply *big.Int) {
263+
if totalSupply == nil {
264+
return
265+
}
266+
267+
floatTotalSupply, accuracy := totalSupply.Float64()
268+
if accuracy != big.Exact {
269+
c.logger.Warn().Msg("precision loss when casting total supply to float")
270+
}
271+
272+
c.flowTotalSupply.Set(floatTotalSupply)
273+
}
274+
249275
func prefixedName(name string) string {
250276
return fmt.Sprintf("evm_gateway_%s", name)
251277
}

metrics/nop.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package metrics
22

33
import (
4+
"math/big"
45
"time"
56
)
67

@@ -24,3 +25,4 @@ func (c *nopCollector) BlockIngestionTime(blockCreation time.Time) {}
2425
func (c *nopCollector) RequestRateLimited(method string) {}
2526
func (c *nopCollector) TransactionsDropped(count int) {}
2627
func (c *nopCollector) TransactionRateLimited() {}
28+
func (c *nopCollector) FlowTotalSupply(totalSupply *big.Int) {}

services/ingestion/engine.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ func (e *Engine) processEvents(events *models.CadenceEvents) error {
209209

210210
e.collector.EVMTransactionIndexed(len(events.Transactions()))
211211
e.collector.EVMHeightIndexed(events.Block().Height)
212+
e.collector.FlowTotalSupply(events.Block().TotalSupply)
212213
return nil
213214
}
214215

0 commit comments

Comments
 (0)