Skip to content

Commit

Permalink
Add metrics to DefaultBlockchain to get TPS and Mgas/s (#7105)
Browse files Browse the repository at this point in the history
* Add gasUsedCounter and numberOfTransactionsCounter counters to DefaultBlockchain

Signed-off-by: Ameziane H <[email protected]>

* Make the attributes final

Signed-off-by: Ameziane H <[email protected]>

* Spotless

Signed-off-by: Ameziane H <[email protected]>

* Add a changelog entry

Signed-off-by: Ameziane H <[email protected]>

---------

Signed-off-by: Ameziane H <[email protected]>
Co-authored-by: Sally MacFarlane <[email protected]>
  • Loading branch information
ahamlat and macfarla authored May 31, 2024
1 parent 64de9f2 commit b0823c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -83,6 +84,9 @@ public class DefaultBlockchain implements MutableBlockchain {
private final Optional<Cache<Hash, List<TransactionReceipt>>> transactionReceiptsCache;
private final Optional<Cache<Hash, Difficulty>> totalDifficultyCache;

private final Counter gasUsedCounter;
private final Counter numberOfTransactionsCounter;

private DefaultBlockchain(
final Optional<Block> genesisBlock,
final BlockchainStorage blockchainStorage,
Expand Down Expand Up @@ -117,6 +121,7 @@ private DefaultBlockchain(
"blockchain_height",
"The current height of the canonical chain",
this::getChainHeadBlockNumber);

metricsSystem.createGauge(
BesuMetricCategory.BLOCKCHAIN,
"difficulty_total",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit b0823c4

Please sign in to comment.