Skip to content

Commit

Permalink
Add min and max metrics to selection profitability metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 committed Oct 28, 2024
1 parent 7712681 commit 7024a4c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import lombok.extern.slf4j.Slf4j;
import net.consensys.linea.bl.TransactionProfitabilityCalculator;
import net.consensys.linea.config.LineaProfitabilityConfiguration;
import net.consensys.linea.metrics.LineaMetricCategory;
import org.apache.tuweni.units.bigints.UInt256s;
import org.hyperledger.besu.datatypes.PendingTransaction;
import org.hyperledger.besu.datatypes.Transaction;
Expand All @@ -28,7 +27,6 @@
import org.hyperledger.besu.plugin.services.BesuConfiguration;
import org.hyperledger.besu.plugin.services.BlockchainService;
import org.hyperledger.besu.plugin.services.MetricsSystem;
import org.hyperledger.besu.plugin.services.metrics.Counter;
import org.hyperledger.besu.plugin.services.metrics.Histogram;
import org.hyperledger.besu.plugin.services.metrics.LabelledGauge;
import org.hyperledger.besu.plugin.services.metrics.LabelledMetric;
Expand All @@ -53,7 +51,6 @@ public class TransactionPoolProfitabilityMetrics {
private final BlockchainService blockchainService;

private final LabelledMetric<Histogram> profitabilityHistogram;
private final Counter invalidTransactionCount;

// Thread-safe references for gauge values
private final AtomicReference<Double> currentLowest = new AtomicReference<>(0.0);
Expand All @@ -75,14 +72,14 @@ public TransactionPoolProfitabilityMetrics(
// Min/Max/Avg gauges with DoubleSupplier
LabelledGauge lowestProfitabilityRatio =
metricsSystem.createLabelledGauge(
LineaMetricCategory.PROFITABILITY,
BesuMetricCategory.ETHEREUM,
"txpool_profitability_ratio_min",
"Lowest profitability ratio seen");
lowestProfitabilityRatio.labels(currentLowest::get);

LabelledGauge highestProfitabilityRatio =
metricsSystem.createLabelledGauge(
LineaMetricCategory.PROFITABILITY,
BesuMetricCategory.ETHEREUM,
"txpool_profitability_ratio_max",
"Highest profitability ratio seen");
highestProfitabilityRatio.labels(currentHighest::get);
Expand All @@ -95,12 +92,6 @@ public TransactionPoolProfitabilityMetrics(
"Histogram statistics of profitability ratios",
HISTOGRAM_BUCKETS,
"type");

this.invalidTransactionCount =
metricsSystem.createCounter(
LineaMetricCategory.PROFITABILITY,
"txpool_invalid_transaction_count",
"Number of transactions that couldn't be processed for profitability");
}

public void handleTransaction(final Transaction transaction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,41 @@

package net.consensys.linea.sequencer.txselection.metrics;

import java.util.concurrent.atomic.AtomicReference;

import lombok.extern.slf4j.Slf4j;
import org.hyperledger.besu.datatypes.Transaction;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.metrics.BesuMetricCategory;
import org.hyperledger.besu.plugin.services.MetricsSystem;
import org.hyperledger.besu.plugin.services.metrics.Histogram;
import org.hyperledger.besu.plugin.services.metrics.LabelledGauge;
import org.hyperledger.besu.plugin.services.metrics.LabelledMetric;

@Slf4j
public class SelectorProfitabilityMetrics {
private final LabelledMetric<Histogram> histogram;

// Thread-safe references for gauge values
private final AtomicReference<Double> currentLowest = new AtomicReference<>(0.0);
private final AtomicReference<Double> currentHighest = new AtomicReference<>(0.0);

public SelectorProfitabilityMetrics(final MetricsSystem metricsSystem) {
// Min/Max/Avg gauges with DoubleSupplier
LabelledGauge lowestProfitabilityRatio =
metricsSystem.createLabelledGauge(
BesuMetricCategory.ETHEREUM,
"selection_profitability_ratio_min",
"Lowest profitability ratio seen");
lowestProfitabilityRatio.labels(currentLowest::get);

LabelledGauge highestProfitabilityRatio =
metricsSystem.createLabelledGauge(
BesuMetricCategory.ETHEREUM,
"selection_profitability_ratio_max",
"Highest profitability ratio seen");
highestProfitabilityRatio.labels(currentHighest::get);

this.histogram =
metricsSystem.createLabelledHistogram(
BesuMetricCategory.ETHEREUM,
Expand All @@ -54,7 +76,7 @@ public void track(
effectivePriorityFee.getValue().doubleValue()
/ profitablePriorityFee.getValue().doubleValue();

histogram.labels(phase.name()).observe(ratio);
updateRunningStats(phase, ratio);

log.atTrace()
.setMessage(
Expand All @@ -69,4 +91,15 @@ public void track(
.addArgument(ratio)
.log();
}

private void updateRunningStats(final Phase phase, double ratio) {
// Update lowest seen
currentLowest.updateAndGet(current -> Math.min(current, ratio));

// Update highest seen
currentHighest.updateAndGet(current -> Math.max(current, ratio));

// Record the observation in summary
histogram.labels(phase.name()).observe(ratio);
}
}

0 comments on commit 7024a4c

Please sign in to comment.