Remove metrics from specific coin implementations

indexv1
Martin Boehm 2018-03-21 15:33:48 +01:00
parent a7d82761f9
commit f0552ec4ad
6 changed files with 39 additions and 63 deletions

View File

@ -14,7 +14,7 @@ import (
"github.com/juju/errors" "github.com/juju/errors"
) )
type blockChainFactory func(config json.RawMessage, pushHandler func(*bchain.MQMessage), metrics *common.Metrics) (bchain.BlockChain, error) type blockChainFactory func(config json.RawMessage, pushHandler func(*bchain.MQMessage)) (bchain.BlockChain, error)
var blockChainFactories = make(map[string]blockChainFactory) var blockChainFactories = make(map[string]blockChainFactory)
@ -39,11 +39,11 @@ func NewBlockChain(coin string, configfile string, pushHandler func(*bchain.MQMe
if err != nil { if err != nil {
return nil, errors.Annotatef(err, "Error parsing file %v", configfile) return nil, errors.Annotatef(err, "Error parsing file %v", configfile)
} }
bc, err := bcf(config, pushHandler, metrics) bc, err := bcf(config, pushHandler)
if err != nil { if err != nil {
return nil, err return nil, err
} }
bc.Initialize(bchain.NewMempool(bc, metrics)) bc.Initialize()
return &blockChainWithMetrics{b: bc, m: metrics}, nil return &blockChainWithMetrics{b: bc, m: metrics}, nil
} }
@ -53,11 +53,15 @@ type blockChainWithMetrics struct {
} }
func (c *blockChainWithMetrics) observeRPCLatency(method string, start time.Time, err error) { 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 var e string
if err != nil {
e = err.Error()
}
c.m.RPCLatency.With(common.Labels{"method": method, "error": e}).Observe(float64(time.Since(start)) / 1e6) // in milliseconds
} }
func (c *blockChainWithMetrics) Initialize(mempool *bchain.Mempool) error { func (c *blockChainWithMetrics) Initialize() error {
return c.b.Initialize(mempool) return c.b.Initialize()
} }
func (c *blockChainWithMetrics) Shutdown() error { func (c *blockChainWithMetrics) Shutdown() error {

View File

@ -2,7 +2,6 @@ package btc
import ( import (
"blockbook/bchain" "blockbook/bchain"
"blockbook/common"
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
@ -29,7 +28,6 @@ type BitcoinRPC struct {
Network string Network string
Mempool *bchain.Mempool Mempool *bchain.Mempool
ParseBlocks bool ParseBlocks bool
metrics *common.Metrics
mq *bchain.MQ mq *bchain.MQ
} }
@ -43,7 +41,7 @@ type configuration struct {
} }
// NewBitcoinRPC returns new BitcoinRPC instance. // NewBitcoinRPC returns new BitcoinRPC instance.
func NewBitcoinRPC(config json.RawMessage, pushHandler func(*bchain.MQMessage), metrics *common.Metrics) (bchain.BlockChain, error) { func NewBitcoinRPC(config json.RawMessage, pushHandler func(*bchain.MQMessage)) (bchain.BlockChain, error) {
var err error var err error
var c configuration var c configuration
err = json.Unmarshal(config, &c) err = json.Unmarshal(config, &c)
@ -62,7 +60,6 @@ func NewBitcoinRPC(config json.RawMessage, pushHandler func(*bchain.MQMessage),
user: c.RPCUser, user: c.RPCUser,
password: c.RPCPass, password: c.RPCPass,
ParseBlocks: c.Parse, ParseBlocks: c.Parse,
metrics: metrics,
} }
mq, err := bchain.NewMQ(c.ZeroMQBinding, pushHandler) mq, err := bchain.NewMQ(c.ZeroMQBinding, pushHandler)
@ -75,8 +72,8 @@ func NewBitcoinRPC(config json.RawMessage, pushHandler func(*bchain.MQMessage),
return s, nil return s, nil
} }
func (b *BitcoinRPC) Initialize(mempool *bchain.Mempool) error { func (b *BitcoinRPC) Initialize() error {
b.Mempool = mempool b.Mempool = bchain.NewMempool(b)
chainName, err := b.GetBlockChainInfo() chainName, err := b.GetBlockChainInfo()
if err != nil { if err != nil {
@ -289,7 +286,7 @@ func (b *BitcoinRPC) GetBestBlockHash() (string, error) {
res := resGetBestBlockHash{} res := resGetBestBlockHash{}
req := cmdGetBestBlockHash{Method: "getbestblockhash"} req := cmdGetBestBlockHash{Method: "getbestblockhash"}
err := b.Call(req.Method, &req, &res) err := b.Call(&req, &res)
if err != nil { if err != nil {
return "", err return "", err
@ -306,7 +303,7 @@ func (b *BitcoinRPC) GetBestBlockHeight() (uint32, error) {
res := resGetBlockCount{} res := resGetBlockCount{}
req := cmdGetBlockCount{Method: "getblockcount"} req := cmdGetBlockCount{Method: "getblockcount"}
err := b.Call(req.Method, &req, &res) err := b.Call(&req, &res)
if err != nil { if err != nil {
return 0, err return 0, err
@ -323,7 +320,7 @@ func (b *BitcoinRPC) GetBlockChainInfo() (string, error) {
res := resGetBlockChainInfo{} res := resGetBlockChainInfo{}
req := cmdGetBlockChainInfo{Method: "getblockchaininfo"} req := cmdGetBlockChainInfo{Method: "getblockchaininfo"}
err := b.Call(req.Method, &req, &res) err := b.Call(&req, &res)
if err != nil { if err != nil {
return "", err return "", err
@ -341,7 +338,7 @@ func (b *BitcoinRPC) GetBlockHash(height uint32) (string, error) {
res := resGetBlockHash{} res := resGetBlockHash{}
req := cmdGetBlockHash{Method: "getblockhash"} req := cmdGetBlockHash{Method: "getblockhash"}
req.Params.Height = height req.Params.Height = height
err := b.Call(req.Method, &req, &res) err := b.Call(&req, &res)
if err != nil { if err != nil {
return "", errors.Annotatef(err, "height %v", height) return "", errors.Annotatef(err, "height %v", height)
@ -360,7 +357,7 @@ func (b *BitcoinRPC) GetBlockHeader(hash string) (*bchain.BlockHeader, error) {
req := cmdGetBlockHeader{Method: "getblockheader"} req := cmdGetBlockHeader{Method: "getblockheader"}
req.Params.BlockHash = hash req.Params.BlockHash = hash
req.Params.Verbose = true req.Params.Verbose = true
err := b.Call(req.Method, &req, &res) err := b.Call(&req, &res)
if err != nil { if err != nil {
return nil, errors.Annotatef(err, "hash %v", hash) return nil, errors.Annotatef(err, "hash %v", hash)
@ -420,7 +417,7 @@ func (b *BitcoinRPC) GetBlockRaw(hash string) ([]byte, error) {
req := cmdGetBlock{Method: "getblock"} req := cmdGetBlock{Method: "getblock"}
req.Params.BlockHash = hash req.Params.BlockHash = hash
req.Params.Verbosity = 0 req.Params.Verbosity = 0
err := b.Call(req.Method, &req, &res) err := b.Call(&req, &res)
if err != nil { if err != nil {
return nil, errors.Annotatef(err, "hash %v", hash) return nil, errors.Annotatef(err, "hash %v", hash)
@ -440,7 +437,7 @@ func (b *BitcoinRPC) GetBlockList(hash string) (*bchain.Block, error) {
req := cmdGetBlock{Method: "getblock"} req := cmdGetBlock{Method: "getblock"}
req.Params.BlockHash = hash req.Params.BlockHash = hash
req.Params.Verbosity = 1 req.Params.Verbosity = 1
err := b.Call(req.Method, &req, &res) err := b.Call(&req, &res)
if err != nil { if err != nil {
return nil, errors.Annotatef(err, "hash %v", hash) return nil, errors.Annotatef(err, "hash %v", hash)
@ -472,7 +469,7 @@ func (b *BitcoinRPC) GetBlockFull(hash string) (*bchain.Block, error) {
req := cmdGetBlock{Method: "getblock"} req := cmdGetBlock{Method: "getblock"}
req.Params.BlockHash = hash req.Params.BlockHash = hash
req.Params.Verbosity = 2 req.Params.Verbosity = 2
err := b.Call(req.Method, &req, &res) err := b.Call(&req, &res)
if err != nil { if err != nil {
return nil, errors.Annotatef(err, "hash %v", hash) return nil, errors.Annotatef(err, "hash %v", hash)
@ -489,7 +486,7 @@ func (b *BitcoinRPC) GetMempool() ([]string, error) {
res := resGetMempool{} res := resGetMempool{}
req := cmdGetMempool{Method: "getrawmempool"} req := cmdGetMempool{Method: "getrawmempool"}
err := b.Call(req.Method, &req, &res) err := b.Call(&req, &res)
if err != nil { if err != nil {
return nil, err return nil, err
@ -508,7 +505,7 @@ func (b *BitcoinRPC) GetTransaction(txid string) (*bchain.Tx, error) {
req := cmdGetRawTransaction{Method: "getrawtransaction"} req := cmdGetRawTransaction{Method: "getrawtransaction"}
req.Params.Txid = txid req.Params.Txid = txid
req.Params.Verbose = true req.Params.Verbose = true
err := b.Call(req.Method, &req, &res) err := b.Call(&req, &res)
if err != nil { if err != nil {
return nil, errors.Annotatef(err, "txid %v", txid) return nil, errors.Annotatef(err, "txid %v", txid)
@ -547,7 +544,7 @@ func (b *BitcoinRPC) EstimateSmartFee(blocks int, conservative bool) (float64, e
} else { } else {
req.Params.EstimateMode = "ECONOMICAL" req.Params.EstimateMode = "ECONOMICAL"
} }
err := b.Call(req.Method, &req, &res) err := b.Call(&req, &res)
if err != nil { if err != nil {
return 0, err return 0, err
@ -565,7 +562,7 @@ func (b *BitcoinRPC) SendRawTransaction(tx string) (string, error) {
res := resSendRawTransaction{} res := resSendRawTransaction{}
req := cmdSendRawTransaction{Method: "sendrawtransaction"} req := cmdSendRawTransaction{Method: "sendrawtransaction"}
req.Params = []string{tx} req.Params = []string{tx}
err := b.Call(req.Method, &req, &res) err := b.Call(&req, &res)
if err != nil { if err != nil {
return "", err return "", err
@ -584,7 +581,7 @@ func (b *BitcoinRPC) GetMempoolEntry(txid string) (*bchain.MempoolEntry, error)
Method: "getmempoolentry", Method: "getmempoolentry",
Params: []string{txid}, Params: []string{txid},
} }
err := b.Call(req.Method, &req, &res) err := b.Call(&req, &res)
if err != nil { if err != nil {
return nil, err return nil, err
@ -595,16 +592,7 @@ func (b *BitcoinRPC) GetMempoolEntry(txid string) (*bchain.MempoolEntry, error)
return res.Result, nil return res.Result, nil
} }
func (b *BitcoinRPC) Call(method string, req interface{}, res interface{}) error { func (b *BitcoinRPC) Call(req interface{}, res interface{}) error {
start := time.Now()
err := b.call(req, res)
if err == nil {
b.metrics.RPCLatency.With(common.Labels{"method": method}).Observe(float64(time.Since(start)) / 1e6) // in milliseconds
}
return err
}
func (b *BitcoinRPC) call(req interface{}, res interface{}) error {
httpData, err := json.Marshal(req) httpData, err := json.Marshal(req)
if err != nil { if err != nil {
return err return err

View File

@ -3,7 +3,6 @@ package zec
import ( import (
"blockbook/bchain" "blockbook/bchain"
"blockbook/bchain/coins/btc" "blockbook/bchain/coins/btc"
"blockbook/common"
"encoding/json" "encoding/json"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
@ -16,8 +15,8 @@ type ZCashRPC struct {
*btc.BitcoinRPC *btc.BitcoinRPC
} }
func NewZCashRPC(config json.RawMessage, pushHandler func(*bchain.MQMessage), metrics *common.Metrics) (bchain.BlockChain, error) { func NewZCashRPC(config json.RawMessage, pushHandler func(*bchain.MQMessage)) (bchain.BlockChain, error) {
b, err := btc.NewBitcoinRPC(config, pushHandler, metrics) b, err := btc.NewBitcoinRPC(config, pushHandler)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -27,8 +26,8 @@ func NewZCashRPC(config json.RawMessage, pushHandler func(*bchain.MQMessage), me
return z, nil return z, nil
} }
func (z *ZCashRPC) Initialize(mempool *bchain.Mempool) error { func (z *ZCashRPC) Initialize() error {
z.Mempool = mempool z.Mempool = bchain.NewMempool(z)
chainName, err := z.GetBlockChainInfo() chainName, err := z.GetBlockChainInfo()
if err != nil { if err != nil {
@ -99,7 +98,7 @@ func (z *ZCashRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) {
req := untypedArrayParams{Method: "getblock"} req := untypedArrayParams{Method: "getblock"}
req.Params = append(req.Params, hash) req.Params = append(req.Params, hash)
req.Params = append(req.Params, true) req.Params = append(req.Params, true)
err := z.Call(req.Method, &req, &res) err := z.Call(&req, &res)
if err != nil { if err != nil {
return nil, errors.Annotatef(err, "hash %v", hash) return nil, errors.Annotatef(err, "hash %v", hash)
@ -131,7 +130,7 @@ func (z *ZCashRPC) GetTransaction(txid string) (*bchain.Tx, error) {
req := untypedArrayParams{Method: "getrawtransaction"} req := untypedArrayParams{Method: "getrawtransaction"}
req.Params = append(req.Params, txid) req.Params = append(req.Params, txid)
req.Params = append(req.Params, 1) req.Params = append(req.Params, 1)
err := z.Call(req.Method, &req, &res) err := z.Call(&req, &res)
if err != nil { if err != nil {
return nil, errors.Annotatef(err, "txid %v", txid) return nil, errors.Annotatef(err, "txid %v", txid)
@ -149,7 +148,7 @@ func (z *ZCashRPC) GetBlockHash(height uint32) (string, error) {
res := resGetBlockHash{} res := resGetBlockHash{}
req := untypedArrayParams{Method: "getblockhash"} req := untypedArrayParams{Method: "getblockhash"}
req.Params = append(req.Params, height) req.Params = append(req.Params, height)
err := z.Call(req.Method, &req, &res) err := z.Call(&req, &res)
if err != nil { if err != nil {
return "", errors.Annotatef(err, "height %v", height) return "", errors.Annotatef(err, "height %v", height)
@ -168,7 +167,7 @@ func (z *ZCashRPC) GetBlockHeader(hash string) (*bchain.BlockHeader, error) {
req := untypedArrayParams{Method: "getblockheader"} req := untypedArrayParams{Method: "getblockheader"}
req.Params = append(req.Params, hash) req.Params = append(req.Params, hash)
req.Params = append(req.Params, true) req.Params = append(req.Params, true)
err := z.Call(req.Method, &req, &res) err := z.Call(&req, &res)
if err != nil { if err != nil {
return nil, errors.Annotatef(err, "hash %v", hash) return nil, errors.Annotatef(err, "hash %v", hash)

View File

@ -1,7 +1,6 @@
package bchain package bchain
import ( import (
"blockbook/common"
"sync" "sync"
"time" "time"
@ -31,12 +30,11 @@ type Mempool struct {
txToInputOutput map[string]inputOutput txToInputOutput map[string]inputOutput
scriptToTx map[string][]outpoint // TODO rename all occurences scriptToTx map[string][]outpoint // TODO rename all occurences
inputs map[outpoint]string inputs map[outpoint]string
metrics *common.Metrics
} }
// NewMempool creates new mempool handler. // NewMempool creates new mempool handler.
func NewMempool(chain BlockChain, metrics *common.Metrics) *Mempool { func NewMempool(chain BlockChain) *Mempool {
return &Mempool{chain: chain, metrics: metrics} return &Mempool{chain: chain}
} }
// GetTransactions returns slice of mempool transactions for given output script. // GetTransactions returns slice of mempool transactions for given output script.
@ -83,7 +81,6 @@ func (m *Mempool) Resync(onNewTxAddr func(txid string, addr string)) error {
glog.V(1).Info("Mempool: resync") glog.V(1).Info("Mempool: resync")
txs, err := m.chain.GetMempool() txs, err := m.chain.GetMempool()
if err != nil { if err != nil {
m.metrics.MempoolResyncErrors.With(common.Labels{"error": err.Error()}).Inc()
return err return err
} }
parser := m.chain.GetChainParser() parser := m.chain.GetChainParser()
@ -95,7 +92,6 @@ func (m *Mempool) Resync(onNewTxAddr func(txid string, addr string)) error {
if !exists { if !exists {
tx, err := m.chain.GetTransaction(txid) tx, err := m.chain.GetTransaction(txid)
if err != nil { if err != nil {
m.metrics.MempoolResyncErrors.With(common.Labels{"error": err.Error()}).Inc()
glog.Error("cannot get transaction ", txid, ": ", err) glog.Error("cannot get transaction ", txid, ": ", err)
continue continue
} }
@ -126,8 +122,6 @@ func (m *Mempool) Resync(onNewTxAddr func(txid string, addr string)) error {
} }
} }
m.updateMappings(newTxToInputOutput, newScriptToTx, newInputs) m.updateMappings(newTxToInputOutput, newScriptToTx, newInputs)
d := time.Since(start) glog.Info("Mempool: resync finished in ", time.Since(start), ", ", len(m.txToInputOutput), " transactions in mempool")
glog.Info("Mempool: resync finished in ", d, ", ", len(m.txToInputOutput), " transactions in mempool")
m.metrics.MempoolResyncDuration.Observe(float64(d) / 1e6) // in milliseconds
return nil return nil
} }

View File

@ -87,7 +87,7 @@ func (e *RPCError) Error() string {
type BlockChain interface { type BlockChain interface {
// life-cycle methods // life-cycle methods
Initialize(mempool *Mempool) error Initialize() error
Shutdown() error Shutdown() error
// chain info // chain info
IsTestnet() bool IsTestnet() bool

View File

@ -16,7 +16,6 @@ type Metrics struct {
TxCacheEfficiency *prometheus.CounterVec TxCacheEfficiency *prometheus.CounterVec
RPCLatency *prometheus.HistogramVec RPCLatency *prometheus.HistogramVec
IndexResyncErrors *prometheus.CounterVec IndexResyncErrors *prometheus.CounterVec
MempoolResyncErrors *prometheus.CounterVec
IndexDBSize prometheus.Gauge IndexDBSize prometheus.Gauge
} }
@ -98,14 +97,6 @@ func GetMetrics(coin string) (*Metrics, error) {
}, },
[]string{"error"}, []string{"error"},
) )
metrics.MempoolResyncErrors = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "blockbook_mempool_resync_errors",
Help: "Number of errors of mempool resync operation",
ConstLabels: Labels{"coin": coin},
},
[]string{"error"},
)
metrics.IndexDBSize = prometheus.NewGauge( metrics.IndexDBSize = prometheus.NewGauge(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Name: "blockbook_index_db_size", Name: "blockbook_index_db_size",