Skip to content

Commit

Permalink
add metrics
Browse files Browse the repository at this point in the history
Signed-off-by: ReyisaRuby <yisa@reddio.com>
  • Loading branch information
ReyisaRuby committed Jan 7, 2025
1 parent e4c96d2 commit c745a0f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
49 changes: 41 additions & 8 deletions core/txdb/txdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
. "github.com/yu-org/yu/core/types"
"github.com/yu-org/yu/infra/storage/kv"
"github.com/yu-org/yu/infra/storage/sql"
"github.com/yu-org/yu/metrics"
)

const (
Expand All @@ -32,7 +33,21 @@ type txnkvdb struct {
txnKV kv.KV
}

func (t *txnkvdb) GetTxn(txnHash Hash) (*SignedTxn, error) {
const (
txnType = "txn"
receiptType = "receipt"
successStatus = "success"
errStatus = "err"
)

func getStatusValue(err error) string {
if err == nil {
return successStatus
}
return err.Error()
}

func (t *txnkvdb) GetTxn(txnHash Hash) (txn *SignedTxn, err error) {
t.RLock()
defer t.RUnlock()
byt, err := t.txnKV.Get(txnHash.Bytes())
Expand All @@ -51,7 +66,7 @@ func (t *txnkvdb) ExistTxn(txnHash Hash) bool {
return t.txnKV.Exist(txnHash.Bytes())
}

func (t *txnkvdb) SetTxns(txns []*SignedTxn) error {
func (t *txnkvdb) SetTxns(txns []*SignedTxn) (err error) {
t.Lock()
defer t.Unlock()
kvtx, err := t.txnKV.NewKvTxn()
Expand Down Expand Up @@ -102,7 +117,10 @@ func NewTxDB(nodeTyp int, kvdb kv.Kvdb, kvdbConf *config.KVconf) (ItxDB, error)
return txdb, nil
}

func (bb *TxDB) GetTxn(txnHash Hash) (*SignedTxn, error) {
func (bb *TxDB) GetTxn(txnHash Hash) (stxn *SignedTxn, err error) {
defer func() {
metrics.TxnDBCounter.WithLabelValues(txnType, "getTxn", getStatusValue(err)).Inc()
}()
if bb.nodeType == LightNode {
return nil, nil
}
Expand All @@ -117,7 +135,10 @@ func (bb *TxDB) GetTxn(txnHash Hash) (*SignedTxn, error) {
return bb.txnKV.GetTxn(txnHash)
}

func (bb *TxDB) GetTxns(txnHashes []Hash) ([]*SignedTxn, error) {
func (bb *TxDB) GetTxns(txnHashes []Hash) (stxns []*SignedTxn, err error) {
defer func() {
metrics.TxnDBCounter.WithLabelValues(txnType, "getTxn", getStatusValue(err)).Inc()
}()
if bb.nodeType == LightNode {
return nil, nil
}
Expand Down Expand Up @@ -146,7 +167,10 @@ func (bb *TxDB) ExistTxn(txnHash Hash) bool {
return bb.txnKV.ExistTxn(txnHash)
}

func (bb *TxDB) SetTxns(txns []*SignedTxn) error {
func (bb *TxDB) SetTxns(txns []*SignedTxn) (err error) {
defer func() {
metrics.TxnDBCounter.WithLabelValues(txnType, "setTxns", getStatusValue(err)).Inc()
}()
if bb.nodeType == LightNode {
return nil
}
Expand All @@ -167,7 +191,10 @@ func (bb *TxDB) SetTxns(txns []*SignedTxn) error {
return bb.txnKV.SetTxns(txns)
}

func (bb *TxDB) SetReceipts(receipts map[Hash]*Receipt) error {
func (bb *TxDB) SetReceipts(receipts map[Hash]*Receipt) (err error) {
defer func() {
metrics.TxnDBCounter.WithLabelValues(receiptType, "setReceipts", getStatusValue(err)).Inc()
}()
if bb.enableUseSql {
for txHash, receipt := range receipts {
if err := bb.SetReceipt(txHash, receipt); err != nil {
Expand All @@ -179,7 +206,10 @@ func (bb *TxDB) SetReceipts(receipts map[Hash]*Receipt) error {
return bb.receiptKV.SetReceipts(receipts)
}

func (bb *TxDB) SetReceipt(txHash Hash, receipt *Receipt) error {
func (bb *TxDB) SetReceipt(txHash Hash, receipt *Receipt) (err error) {
defer func() {
metrics.TxnDBCounter.WithLabelValues(receiptType, "setReceipt", getStatusValue(err)).Inc()
}()
if bb.enableUseSql {
byt, err := receipt.Encode()
if err != nil {
Expand All @@ -193,7 +223,10 @@ func (bb *TxDB) SetReceipt(txHash Hash, receipt *Receipt) error {
return bb.receiptKV.SetReceipt(txHash, receipt)
}

func (bb *TxDB) GetReceipt(txHash Hash) (*Receipt, error) {
func (bb *TxDB) GetReceipt(txHash Hash) (rec *Receipt, err error) {
defer func() {
metrics.TxnDBCounter.WithLabelValues(receiptType, "getReceipt", getStatusValue(err)).Inc()
}()
if bb.enableUseSql {
var records []TxnDBSchema
err := bb.db.Db().Raw("select value from txndb where type = ? and key = ?", "receipt", string(txHash.Bytes())).Find(&records).Error
Expand Down
1 change: 1 addition & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,5 @@ func init() {
prometheus.MustRegister(TxpoolSizeGauge)
// prometheus.MustRegister(AppendBlockDuration, StartBlockDuration, EndBlockDuration, FinalizeBlockDuration)
prometheus.MustRegister(StateCommitDuration)
initTxnDBMetrics()
}
22 changes: 22 additions & 0 deletions metrics/txdb_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package metrics

import "github.com/prometheus/client_golang/prometheus"

const (
TypeLbl = "type"
OpLabel = "op"
StatusLbl = "status"
)

var (
TxnDBCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "yu",
Subsystem: "txndb",
Name: "op_counter",
Help: "Counter of txnDB",
}, []string{TypeLbl, OpLabel, StatusLbl})
)

func initTxnDBMetrics() {
prometheus.MustRegister(TxnDBCounter)
}

0 comments on commit c745a0f

Please sign in to comment.