From 91d9655356bdb1df8b66c0dd03c725d44c38e3bd Mon Sep 17 00:00:00 2001 From: Stephen Buttolph Date: Fri, 3 May 2024 17:09:59 -0400 Subject: [PATCH] Use vectors for accepted tx and block metrics (#2990) --- vms/avm/metrics/tx_metrics.go | 68 ++++++------- vms/platformvm/metrics/block_metrics.go | 91 +++++++++-------- vms/platformvm/metrics/tx_metrics.go | 128 ++++++++++++------------ 3 files changed, 143 insertions(+), 144 deletions(-) diff --git a/vms/avm/metrics/tx_metrics.go b/vms/avm/metrics/tx_metrics.go index 0e5cd184cc4b..8b9bf2c0ed47 100644 --- a/vms/avm/metrics/tx_metrics.go +++ b/vms/avm/metrics/tx_metrics.go @@ -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 } diff --git a/vms/platformvm/metrics/block_metrics.go b/vms/platformvm/metrics/block_metrics.go index 09239d8df40a..cc1acd7eb862 100644 --- a/vms/platformvm/metrics/block_metrics.go +++ b/vms/platformvm/metrics/block_metrics.go @@ -4,24 +4,22 @@ 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( @@ -29,45 +27,42 @@ func newBlockMetrics( 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 @@ -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 @@ -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 @@ -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) } diff --git a/vms/platformvm/metrics/tx_metrics.go b/vms/platformvm/metrics/tx_metrics.go index 70b032765bfc..5526a6a0be57 100644 --- a/vms/platformvm/metrics/tx_metrics.go +++ b/vms/platformvm/metrics/tx_metrics.go @@ -4,145 +4,141 @@ package metrics import ( - "fmt" - "github.com/prometheus/client_golang/prometheus" - "github.com/ava-labs/avalanchego/utils/wrappers" "github.com/ava-labs/avalanchego/vms/platformvm/txs" ) -var _ txs.Visitor = (*txMetrics)(nil) +const txLabel = "tx" + +var ( + _ txs.Visitor = (*txMetrics)(nil) + + txLabels = []string{txLabel} +) type txMetrics struct { - numAddDelegatorTxs, - numAddSubnetValidatorTxs, - numAddValidatorTxs, - numAdvanceTimeTxs, - numCreateChainTxs, - numCreateSubnetTxs, - numExportTxs, - numImportTxs, - numRewardValidatorTxs, - numRemoveSubnetValidatorTxs, - numTransformSubnetTxs, - numAddPermissionlessValidatorTxs, - numAddPermissionlessDelegatorTxs, - numTransferSubnetOwnershipTxs, - numBaseTxs prometheus.Counter + numTxs *prometheus.CounterVec } func newTxMetrics( namespace string, registerer prometheus.Registerer, ) (*txMetrics, error) { - errs := wrappers.Errs{} m := &txMetrics{ - numAddDelegatorTxs: newTxMetric(namespace, "add_delegator", registerer, &errs), - numAddSubnetValidatorTxs: newTxMetric(namespace, "add_subnet_validator", registerer, &errs), - numAddValidatorTxs: newTxMetric(namespace, "add_validator", registerer, &errs), - numAdvanceTimeTxs: newTxMetric(namespace, "advance_time", registerer, &errs), - numCreateChainTxs: newTxMetric(namespace, "create_chain", registerer, &errs), - numCreateSubnetTxs: newTxMetric(namespace, "create_subnet", registerer, &errs), - numExportTxs: newTxMetric(namespace, "export", registerer, &errs), - numImportTxs: newTxMetric(namespace, "import", registerer, &errs), - numRewardValidatorTxs: newTxMetric(namespace, "reward_validator", registerer, &errs), - numRemoveSubnetValidatorTxs: newTxMetric(namespace, "remove_subnet_validator", registerer, &errs), - numTransformSubnetTxs: newTxMetric(namespace, "transform_subnet", registerer, &errs), - numAddPermissionlessValidatorTxs: newTxMetric(namespace, "add_permissionless_validator", registerer, &errs), - numAddPermissionlessDelegatorTxs: newTxMetric(namespace, "add_permissionless_delegator", registerer, &errs), - numTransferSubnetOwnershipTxs: newTxMetric(namespace, "transfer_subnet_ownership", registerer, &errs), - numBaseTxs: newTxMetric(namespace, "base", 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) AddValidatorTx(*txs.AddValidatorTx) error { - m.numAddValidatorTxs.Inc() + m.numTxs.With(prometheus.Labels{ + txLabel: "add_validator", + }).Inc() return nil } func (m *txMetrics) AddSubnetValidatorTx(*txs.AddSubnetValidatorTx) error { - m.numAddSubnetValidatorTxs.Inc() + m.numTxs.With(prometheus.Labels{ + txLabel: "add_subnet_validator", + }).Inc() return nil } func (m *txMetrics) AddDelegatorTx(*txs.AddDelegatorTx) error { - m.numAddDelegatorTxs.Inc() + m.numTxs.With(prometheus.Labels{ + txLabel: "add_delegator", + }).Inc() return nil } func (m *txMetrics) CreateChainTx(*txs.CreateChainTx) error { - m.numCreateChainTxs.Inc() + m.numTxs.With(prometheus.Labels{ + txLabel: "create_chain", + }).Inc() return nil } func (m *txMetrics) CreateSubnetTx(*txs.CreateSubnetTx) error { - m.numCreateSubnetTxs.Inc() + m.numTxs.With(prometheus.Labels{ + txLabel: "create_subnet", + }).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 } func (m *txMetrics) AdvanceTimeTx(*txs.AdvanceTimeTx) error { - m.numAdvanceTimeTxs.Inc() + m.numTxs.With(prometheus.Labels{ + txLabel: "advance_time", + }).Inc() return nil } func (m *txMetrics) RewardValidatorTx(*txs.RewardValidatorTx) error { - m.numRewardValidatorTxs.Inc() + m.numTxs.With(prometheus.Labels{ + txLabel: "reward_validator", + }).Inc() return nil } func (m *txMetrics) RemoveSubnetValidatorTx(*txs.RemoveSubnetValidatorTx) error { - m.numRemoveSubnetValidatorTxs.Inc() + m.numTxs.With(prometheus.Labels{ + txLabel: "remove_subnet_validator", + }).Inc() return nil } func (m *txMetrics) TransformSubnetTx(*txs.TransformSubnetTx) error { - m.numTransformSubnetTxs.Inc() + m.numTxs.With(prometheus.Labels{ + txLabel: "transform_subnet", + }).Inc() return nil } func (m *txMetrics) AddPermissionlessValidatorTx(*txs.AddPermissionlessValidatorTx) error { - m.numAddPermissionlessValidatorTxs.Inc() + m.numTxs.With(prometheus.Labels{ + txLabel: "add_permissionless_validator", + }).Inc() return nil } func (m *txMetrics) AddPermissionlessDelegatorTx(*txs.AddPermissionlessDelegatorTx) error { - m.numAddPermissionlessDelegatorTxs.Inc() + m.numTxs.With(prometheus.Labels{ + txLabel: "add_permissionless_delegator", + }).Inc() return nil } func (m *txMetrics) TransferSubnetOwnershipTx(*txs.TransferSubnetOwnershipTx) error { - m.numTransferSubnetOwnershipTxs.Inc() + m.numTxs.With(prometheus.Labels{ + txLabel: "transfer_subnet_ownership", + }).Inc() return nil } func (m *txMetrics) BaseTx(*txs.BaseTx) error { - m.numBaseTxs.Inc() + m.numTxs.With(prometheus.Labels{ + txLabel: "base", + }).Inc() return nil }