Implement generic timing of blockchain rpc calls

indexv1
Martin Boehm 2018-03-21 01:23:17 +01:00
parent fbfb33cb5d
commit f1b1f9fe55
2 changed files with 96 additions and 2 deletions

View File

@ -9,6 +9,7 @@ import (
"fmt"
"io/ioutil"
"reflect"
"time"
"github.com/juju/errors"
)
@ -38,5 +39,98 @@ func NewBlockChain(coin string, configfile string, pushHandler func(*bchain.MQMe
if err != nil {
return nil, errors.Annotatef(err, "Error parsing file %v", configfile)
}
return bcf(config, pushHandler, metrics)
bc, err := bcf(config, pushHandler, metrics)
if err != nil {
return nil, err
}
return &blockChainWithMetrics{b: bc, m: metrics}, nil
}
type blockChainWithMetrics struct {
b bchain.BlockChain
m *common.Metrics
}
func (c *blockChainWithMetrics) observeRPCLatency(method string, start time.Time, err error) {
c.m.RPCLatency.With(common.Labels{"method": method, "error": err.Error()}).Observe(float64(time.Since(start)) / 1e6) // in milliseconds
}
func (c *blockChainWithMetrics) Shutdown() error {
return c.b.Shutdown()
}
func (c *blockChainWithMetrics) IsTestnet() bool {
return c.b.IsTestnet()
}
func (c *blockChainWithMetrics) GetNetworkName() string {
return c.b.GetNetworkName()
}
func (c *blockChainWithMetrics) GetBestBlockHash() (v string, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetBestBlockHash", s, err) }(time.Now())
return c.b.GetBestBlockHash()
}
func (c *blockChainWithMetrics) GetBestBlockHeight() (v uint32, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetBestBlockHeight", s, err) }(time.Now())
return c.b.GetBestBlockHeight()
}
func (c *blockChainWithMetrics) GetBlockHash(height uint32) (v string, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetBlockHash", s, err) }(time.Now())
return c.b.GetBlockHash(height)
}
func (c *blockChainWithMetrics) GetBlockHeader(hash string) (v *bchain.BlockHeader, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetBlockHeader", s, err) }(time.Now())
return c.b.GetBlockHeader(hash)
}
func (c *blockChainWithMetrics) GetBlock(hash string, height uint32) (v *bchain.Block, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetBlock", s, err) }(time.Now())
return c.b.GetBlock(hash, height)
}
func (c *blockChainWithMetrics) GetMempool() (v []string, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetMempool", s, err) }(time.Now())
return c.b.GetMempool()
}
func (c *blockChainWithMetrics) GetTransaction(txid string) (v *bchain.Tx, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetTransaction", s, err) }(time.Now())
return c.b.GetTransaction(txid)
}
func (c *blockChainWithMetrics) EstimateSmartFee(blocks int, conservative bool) (v float64, err error) {
defer func(s time.Time) { c.observeRPCLatency("EstimateSmartFee", s, err) }(time.Now())
return c.b.EstimateSmartFee(blocks, conservative)
}
func (c *blockChainWithMetrics) SendRawTransaction(tx string) (v string, err error) {
defer func(s time.Time) { c.observeRPCLatency("SendRawTransaction", s, err) }(time.Now())
return c.b.SendRawTransaction(tx)
}
func (c *blockChainWithMetrics) ResyncMempool(onNewTxAddr func(txid string, addr string)) (err error) {
defer func(s time.Time) { c.observeRPCLatency("ResyncMempool", s, err) }(time.Now())
return c.b.ResyncMempool(onNewTxAddr)
}
func (c *blockChainWithMetrics) GetMempoolTransactions(address string) (v []string, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetMempoolTransactions", s, err) }(time.Now())
return c.b.GetMempoolTransactions(address)
}
func (c *blockChainWithMetrics) GetMempoolSpentOutput(outputTxid string, vout uint32) (v string) {
return c.b.GetMempoolSpentOutput(outputTxid, vout)
}
func (c *blockChainWithMetrics) GetMempoolEntry(txid string) (v *bchain.MempoolEntry, err error) {
defer func(s time.Time) { c.observeRPCLatency("GetMempoolEntry", s, err) }(time.Now())
return c.b.GetMempoolEntry(txid)
}
func (c *blockChainWithMetrics) GetChainParser() bchain.BlockChainParser {
return c.b.GetChainParser()
}

View File

@ -88,7 +88,7 @@ func GetMetrics(coin string) (*Metrics, error) {
Buckets: []float64{1, 5, 10, 25, 50, 75, 100, 250},
ConstLabels: Labels{"coin": coin},
},
[]string{"method"},
[]string{"method", "error"},
)
metrics.IndexResyncErrors = prometheus.NewCounterVec(
prometheus.CounterOpts{