From b0823c41fe20052a2f3416c97f6a5f5be44b1f4f Mon Sep 17 00:00:00 2001 From: ahamlat Date: Fri, 31 May 2024 05:27:37 +0200 Subject: [PATCH] Add metrics to DefaultBlockchain to get TPS and Mgas/s (#7105) * Add gasUsedCounter and numberOfTransactionsCounter counters to DefaultBlockchain Signed-off-by: Ameziane H * Make the attributes final Signed-off-by: Ameziane H * Spotless Signed-off-by: Ameziane H * Add a changelog entry Signed-off-by: Ameziane H --------- Signed-off-by: Ameziane H Co-authored-by: Sally MacFarlane --- CHANGELOG.md | 1 + .../ethereum/chain/DefaultBlockchain.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d4021309744..ebe02cc0a55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Breaking Changes ### Additions and Improvements +- Add two counters to DefaultBlockchain in order to be able to calculate TPS and Mgas/s [#7105](https://github.com/hyperledger/besu/pull/7105) ### Bug fixes - Make `eth_gasPrice` aware of the base fee market [#7102](https://github.com/hyperledger/besu/pull/7102) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/chain/DefaultBlockchain.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/chain/DefaultBlockchain.java index 032ccb9f21f..04eaaa6a353 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/chain/DefaultBlockchain.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/chain/DefaultBlockchain.java @@ -33,6 +33,7 @@ import org.hyperledger.besu.metrics.BesuMetricCategory; import org.hyperledger.besu.metrics.prometheus.PrometheusMetricsSystem; import org.hyperledger.besu.plugin.services.MetricsSystem; +import org.hyperledger.besu.plugin.services.metrics.Counter; import org.hyperledger.besu.util.InvalidConfigurationException; import org.hyperledger.besu.util.Subscribers; @@ -83,6 +84,9 @@ public class DefaultBlockchain implements MutableBlockchain { private final Optional>> transactionReceiptsCache; private final Optional> totalDifficultyCache; + private final Counter gasUsedCounter; + private final Counter numberOfTransactionsCounter; + private DefaultBlockchain( final Optional genesisBlock, final BlockchainStorage blockchainStorage, @@ -117,6 +121,7 @@ private DefaultBlockchain( "blockchain_height", "The current height of the canonical chain", this::getChainHeadBlockNumber); + metricsSystem.createGauge( BesuMetricCategory.BLOCKCHAIN, "difficulty_total", @@ -135,6 +140,10 @@ private DefaultBlockchain( "Gas used by the current chain head block", () -> getChainHeadHeader().getGasUsed()); + gasUsedCounter = + metricsSystem.createCounter( + BesuMetricCategory.BLOCKCHAIN, "chain_head_gas_used_counter", "Counter for Gas used"); + metricsSystem.createLongGauge( BesuMetricCategory.BLOCKCHAIN, "chain_head_gas_limit", @@ -147,6 +156,12 @@ private DefaultBlockchain( "Number of transactions in the current chain head block", () -> chainHeadTransactionCount); + numberOfTransactionsCounter = + metricsSystem.createCounter( + BesuMetricCategory.BLOCKCHAIN, + "chain_head_transaction_count_counter", + "Counter for the number of transactions"); + metricsSystem.createIntegerGauge( BesuMetricCategory.BLOCKCHAIN, "chain_head_ommer_count", @@ -524,6 +539,10 @@ private BlockAddedEvent handleNewHead( updater.setChainHead(newBlockHash); indexTransactionForBlock( updater, newBlockHash, blockWithReceipts.getBlock().getBody().getTransactions()); + gasUsedCounter.inc(blockWithReceipts.getHeader().getGasUsed()); + numberOfTransactionsCounter.inc( + blockWithReceipts.getBlock().getBody().getTransactions().size()); + return BlockAddedEvent.createForHeadAdvancement( blockWithReceipts.getBlock(), LogWithMetadata.generate(