Skip to content

Commit

Permalink
Use vectors for accepted tx and block metrics (#2990)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored May 3, 2024
1 parent fc6f4e7 commit 91d9655
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 144 deletions.
68 changes: 32 additions & 36 deletions vms/avm/metrics/tx_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,75 +4,71 @@
package metrics

import (
"fmt"

"github.com/prometheus/client_golang/prometheus"

"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/vms/avm/txs"
)

var _ txs.Visitor = (*txMetrics)(nil)
const txLabel = "tx"

var (
_ txs.Visitor = (*txMetrics)(nil)

txLabels = []string{txLabel}
)

type txMetrics struct {
numBaseTxs,
numCreateAssetTxs,
numOperationTxs,
numImportTxs,
numExportTxs prometheus.Counter
numTxs *prometheus.CounterVec
}

func newTxMetrics(
namespace string,
registerer prometheus.Registerer,
) (*txMetrics, error) {
errs := wrappers.Errs{}
m := &txMetrics{
numBaseTxs: newTxMetric(namespace, "base", registerer, &errs),
numCreateAssetTxs: newTxMetric(namespace, "create_asset", registerer, &errs),
numOperationTxs: newTxMetric(namespace, "operation", registerer, &errs),
numImportTxs: newTxMetric(namespace, "import", registerer, &errs),
numExportTxs: newTxMetric(namespace, "export", registerer, &errs),
numTxs: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Name: "txs_accepted",
Help: "number of transactions accepted",
},
txLabels,
),
}
return m, errs.Err
}

func newTxMetric(
namespace string,
txName string,
registerer prometheus.Registerer,
errs *wrappers.Errs,
) prometheus.Counter {
txMetric := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: txName + "_txs_accepted",
Help: fmt.Sprintf("Number of %s transactions accepted", txName),
})
errs.Add(registerer.Register(txMetric))
return txMetric
return m, registerer.Register(m.numTxs)
}

func (m *txMetrics) BaseTx(*txs.BaseTx) error {
m.numBaseTxs.Inc()
m.numTxs.With(prometheus.Labels{
txLabel: "base",
}).Inc()
return nil
}

func (m *txMetrics) CreateAssetTx(*txs.CreateAssetTx) error {
m.numCreateAssetTxs.Inc()
m.numTxs.With(prometheus.Labels{
txLabel: "create_asset",
}).Inc()
return nil
}

func (m *txMetrics) OperationTx(*txs.OperationTx) error {
m.numOperationTxs.Inc()
m.numTxs.With(prometheus.Labels{
txLabel: "operation",
}).Inc()
return nil
}

func (m *txMetrics) ImportTx(*txs.ImportTx) error {
m.numImportTxs.Inc()
m.numTxs.With(prometheus.Labels{
txLabel: "import",
}).Inc()
return nil
}

func (m *txMetrics) ExportTx(*txs.ExportTx) error {
m.numExportTxs.Inc()
m.numTxs.With(prometheus.Labels{
txLabel: "export",
}).Inc()
return nil
}
91 changes: 49 additions & 42 deletions vms/platformvm/metrics/block_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,65 @@
package metrics

import (
"fmt"

"github.com/prometheus/client_golang/prometheus"

"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/vms/platformvm/block"
)

var _ block.Visitor = (*blockMetrics)(nil)
const blkLabel = "blk"

var (
_ block.Visitor = (*blockMetrics)(nil)

blkLabels = []string{blkLabel}
)

type blockMetrics struct {
txMetrics *txMetrics

numAbortBlocks,
numAtomicBlocks,
numCommitBlocks,
numProposalBlocks,
numStandardBlocks prometheus.Counter
numBlocks *prometheus.CounterVec
}

func newBlockMetrics(
namespace string,
registerer prometheus.Registerer,
) (*blockMetrics, error) {
txMetrics, err := newTxMetrics(namespace, registerer)
errs := wrappers.Errs{Err: err}
m := &blockMetrics{
txMetrics: txMetrics,
numAbortBlocks: newBlockMetric(namespace, "abort", registerer, &errs),
numAtomicBlocks: newBlockMetric(namespace, "atomic", registerer, &errs),
numCommitBlocks: newBlockMetric(namespace, "commit", registerer, &errs),
numProposalBlocks: newBlockMetric(namespace, "proposal", registerer, &errs),
numStandardBlocks: newBlockMetric(namespace, "standard", registerer, &errs),
if err != nil {
return nil, err
}
return m, errs.Err
}

func newBlockMetric(
namespace string,
blockName string,
registerer prometheus.Registerer,
errs *wrappers.Errs,
) prometheus.Counter {
blockMetric := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: blockName + "_blks_accepted",
Help: fmt.Sprintf("Number of %s blocks accepted", blockName),
})
errs.Add(registerer.Register(blockMetric))
return blockMetric
m := &blockMetrics{
txMetrics: txMetrics,
numBlocks: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Name: "blks_accepted",
Help: "number of blocks accepted",
},
blkLabels,
),
}
return m, registerer.Register(m.numBlocks)
}

func (m *blockMetrics) BanffAbortBlock(*block.BanffAbortBlock) error {
m.numAbortBlocks.Inc()
m.numBlocks.With(prometheus.Labels{
blkLabel: "abort",
}).Inc()
return nil
}

func (m *blockMetrics) BanffCommitBlock(*block.BanffCommitBlock) error {
m.numCommitBlocks.Inc()
m.numBlocks.With(prometheus.Labels{
blkLabel: "commit",
}).Inc()
return nil
}

func (m *blockMetrics) BanffProposalBlock(b *block.BanffProposalBlock) error {
m.numProposalBlocks.Inc()
m.numBlocks.With(prometheus.Labels{
blkLabel: "proposal",
}).Inc()
for _, tx := range b.Transactions {
if err := tx.Unsigned.Visit(m.txMetrics); err != nil {
return err
Expand All @@ -77,7 +72,9 @@ func (m *blockMetrics) BanffProposalBlock(b *block.BanffProposalBlock) error {
}

func (m *blockMetrics) BanffStandardBlock(b *block.BanffStandardBlock) error {
m.numStandardBlocks.Inc()
m.numBlocks.With(prometheus.Labels{
blkLabel: "standard",
}).Inc()
for _, tx := range b.Transactions {
if err := tx.Unsigned.Visit(m.txMetrics); err != nil {
return err
Expand All @@ -87,22 +84,30 @@ func (m *blockMetrics) BanffStandardBlock(b *block.BanffStandardBlock) error {
}

func (m *blockMetrics) ApricotAbortBlock(*block.ApricotAbortBlock) error {
m.numAbortBlocks.Inc()
m.numBlocks.With(prometheus.Labels{
blkLabel: "abort",
}).Inc()
return nil
}

func (m *blockMetrics) ApricotCommitBlock(*block.ApricotCommitBlock) error {
m.numCommitBlocks.Inc()
m.numBlocks.With(prometheus.Labels{
blkLabel: "commit",
}).Inc()
return nil
}

func (m *blockMetrics) ApricotProposalBlock(b *block.ApricotProposalBlock) error {
m.numProposalBlocks.Inc()
m.numBlocks.With(prometheus.Labels{
blkLabel: "proposal",
}).Inc()
return b.Tx.Unsigned.Visit(m.txMetrics)
}

func (m *blockMetrics) ApricotStandardBlock(b *block.ApricotStandardBlock) error {
m.numStandardBlocks.Inc()
m.numBlocks.With(prometheus.Labels{
blkLabel: "standard",
}).Inc()
for _, tx := range b.Transactions {
if err := tx.Unsigned.Visit(m.txMetrics); err != nil {
return err
Expand All @@ -112,6 +117,8 @@ func (m *blockMetrics) ApricotStandardBlock(b *block.ApricotStandardBlock) error
}

func (m *blockMetrics) ApricotAtomicBlock(b *block.ApricotAtomicBlock) error {
m.numAtomicBlocks.Inc()
m.numBlocks.With(prometheus.Labels{
blkLabel: "atomic",
}).Inc()
return b.Tx.Unsigned.Visit(m.txMetrics)
}
Loading

0 comments on commit 91d9655

Please sign in to comment.