Skip to content

Commit

Permalink
Add support for counter metric with external supplied value (#7894)
Browse files Browse the repository at this point in the history
* Add support for counter metric with external supplied value

Signed-off-by: Fabio Di Fabio <[email protected]>

* Update CHANGELOG

Signed-off-by: Fabio Di Fabio <[email protected]>

---------

Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 authored Nov 20, 2024
1 parent 73a1e5b commit fe46289
Show file tree
Hide file tree
Showing 20 changed files with 326 additions and 153 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Changelog

## [Unreleased]
- Added isLabelsObserved to LabelledGauge in plugin-api. Default implementation returns false.

### Breaking Changes
- Removed Retesteth rpc service and commands [#7833](https://github.com/hyperledger/besu/pull/7783)

### Upcoming Breaking Changes
- `MetricSystem::createLabelledGauge` is deprecated and will be removed in a future release, replace it with `MetricSystem::createLabelledSuppliedGauge`

### Additions and Improvements
- Fine tune already seen txs tracker when a tx is removed from the pool [#7755](https://github.com/hyperledger/besu/pull/7755)
Expand All @@ -16,6 +16,7 @@
- Add a method to get all the transaction in the pool, to the `TransactionPoolService`, to easily access the transaction pool content from plugins [#7813](https://github.com/hyperledger/besu/pull/7813)
- Add a method to check if a metric category is enabled to the plugin API [#7832](https://github.com/hyperledger/besu/pull/7832)
- Add account and state overrides to `eth_call` and `eth_estimateGas` [#7801](https://github.com/hyperledger/besu/pull/7801)
- Add a new metric collector for counters which get their value from suppliers [#7894](https://github.com/hyperledger/besu/pull/7894)

### Bug fixes
- Fix registering new metric categories from plugins [#7825](https://github.com/hyperledger/besu/pull/7825)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,7 @@ private void validatePrivacyPluginOptions() {
private void setReleaseMetrics() {
besuComponent
.getMetricsSystem()
.createLabelledGauge(
.createLabelledSuppliedGauge(
StandardMetricCategory.PROCESS, "release", "Release information", "version")
.labels(() -> 1, BesuInfo.version());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ private static <T extends ThreadPoolExecutor> T newMonitoredExecutor(
"Current number of threads in the thread pool",
executor::getPoolSize);

metricsSystem.createLongGauge(
metricsSystem.createCounter(
BesuMetricCategory.EXECUTORS,
metricName + "_completed_tasks_total",
"Total number of tasks executed",
executor::getCompletedTaskCount);

metricsSystem.createLongGauge(
metricsSystem.createCounter(
BesuMetricCategory.EXECUTORS,
metricName + "_submitted_tasks_total",
"Total number of tasks executed",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import org.hyperledger.besu.metrics.BesuMetricCategory;
import org.hyperledger.besu.plugin.services.MetricsSystem;
import org.hyperledger.besu.plugin.services.metrics.Counter;
import org.hyperledger.besu.plugin.services.metrics.LabelledGauge;
import org.hyperledger.besu.plugin.services.metrics.LabelledMetric;
import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric;
import org.hyperledger.besu.plugin.services.metrics.OperationTimer;

import java.util.Collection;
Expand All @@ -43,7 +43,7 @@ public class PeerTaskExecutor {
private final LabelledMetric<Counter> timeoutCounter;
private final LabelledMetric<Counter> invalidResponseCounter;
private final LabelledMetric<Counter> internalExceptionCounter;
private final LabelledGauge inflightRequestGauge;
private final LabelledSuppliedMetric inflightRequestGauge;
private final Map<String, AtomicInteger> inflightRequestCountByClassName;

public PeerTaskExecutor(
Expand Down Expand Up @@ -77,7 +77,7 @@ public PeerTaskExecutor(
"Counter of the number of internal exceptions occurred",
"taskName");
inflightRequestGauge =
metricsSystem.createLabelledGauge(
metricsSystem.createLabelledSuppliedGauge(
BesuMetricCategory.PEERS,
"inflight_request_gauge",
"Gauge of the number of inflight requests",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.hyperledger.besu.metrics.RunnableCounter;
import org.hyperledger.besu.plugin.services.MetricsSystem;
import org.hyperledger.besu.plugin.services.metrics.Counter;
import org.hyperledger.besu.plugin.services.metrics.LabelledGauge;
import org.hyperledger.besu.plugin.services.metrics.LabelledMetric;
import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -47,10 +47,10 @@ public class TransactionPoolMetrics {
private final LabelledMetric<Counter> removedCounter;
private final LabelledMetric<Counter> rejectedCounter;
private final LabelledMetric<Counter> penalizedCounter;
private final LabelledGauge spaceUsed;
private final LabelledGauge transactionCount;
private final LabelledGauge transactionCountByType;
private final LabelledGauge uniqueSenderCount;
private final LabelledSuppliedMetric spaceUsed;
private final LabelledSuppliedMetric transactionCount;
private final LabelledSuppliedMetric transactionCountByType;
private final LabelledSuppliedMetric uniqueSenderCount;
private final LabelledMetric<Counter> expiredMessagesCounter;
private final Map<String, RunnableCounter> expiredMessagesRunnableCounters = new HashMap<>();
private final LabelledMetric<Counter> alreadySeenTransactionsCounter;
Expand Down Expand Up @@ -103,29 +103,29 @@ public TransactionPoolMetrics(final MetricsSystem metricsSystem) {
"layer");

spaceUsed =
metricsSystem.createLabelledGauge(
metricsSystem.createLabelledSuppliedGauge(
BesuMetricCategory.TRANSACTION_POOL,
"space_used",
"The amount of space used by the transactions in the layer",
"layer");

transactionCount =
metricsSystem.createLabelledGauge(
metricsSystem.createLabelledSuppliedGauge(
BesuMetricCategory.TRANSACTION_POOL,
"number_of_transactions",
"The number of transactions currently present in the layer",
"layer");

transactionCountByType =
metricsSystem.createLabelledGauge(
metricsSystem.createLabelledSuppliedGauge(
BesuMetricCategory.TRANSACTION_POOL,
"number_of_transactions_by_type",
"The number of transactions, of a specified type, currently present in the layer",
"layer",
"type");

uniqueSenderCount =
metricsSystem.createLabelledGauge(
metricsSystem.createLabelledSuppliedGauge(
BesuMetricCategory.TRANSACTION_POOL,
"unique_senders",
"The number of senders with at least one transaction currently present in the layer",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.hyperledger.besu.plugin.services.metrics.ExternalSummary;
import org.hyperledger.besu.plugin.services.metrics.LabelledGauge;
import org.hyperledger.besu.plugin.services.metrics.LabelledMetric;
import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric;
import org.hyperledger.besu.plugin.services.metrics.MetricCategory;
import org.hyperledger.besu.plugin.services.metrics.OperationTimer;

Expand All @@ -41,7 +42,7 @@ public class NoOpMetricsSystem implements ObservableMetricsSystem {
public static final Counter NO_OP_COUNTER = new NoOpCounter();

/** The constant NO_OP_GAUGE. */
public static final LabelledGauge NO_OP_GAUGE = new NoOpValueCollector();
public static final LabelledSuppliedMetric NO_OP_GAUGE = new NoOpValueCollector();

private static final OperationTimer.TimingContext NO_OP_TIMING_CONTEXT = () -> 0;

Expand All @@ -65,16 +66,16 @@ public class NoOpMetricsSystem implements ObservableMetricsSystem {
new LabelCountingNoOpMetric<>(1, NO_OP_OPERATION_TIMER);

/** The constant NO_OP_LABELLED_1_GAUGE. */
public static final LabelledGauge NO_OP_LABELLED_1_GAUGE =
new LabelledGaugeNoOpMetric(1, NO_OP_GAUGE);
public static final LabelledSuppliedMetric NO_OP_LABELLED_1_GAUGE =
new LabelledSuppliedNoOpMetric(1, NO_OP_GAUGE);

/** The constant NO_OP_LABELLED_2_GAUGE. */
public static final LabelledGauge NO_OP_LABELLED_2_GAUGE =
new LabelledGaugeNoOpMetric(2, NO_OP_GAUGE);
public static final LabelledSuppliedMetric NO_OP_LABELLED_2_GAUGE =
new LabelledSuppliedNoOpMetric(2, NO_OP_GAUGE);

/** The constant NO_OP_LABELLED_3_GAUGE. */
public static final LabelledGauge NO_OP_LABELLED_3_GAUGE =
new LabelledGaugeNoOpMetric(3, NO_OP_GAUGE);
public static final LabelledSuppliedMetric NO_OP_LABELLED_3_GAUGE =
new LabelledSuppliedNoOpMetric(3, NO_OP_GAUGE);

/** Default constructor */
public NoOpMetricsSystem() {}
Expand Down Expand Up @@ -159,12 +160,21 @@ public void createGuavaCacheCollector(
final MetricCategory category, final String name, final Cache<?, ?> cache) {}

@Override
public LabelledGauge createLabelledGauge(
public LabelledSuppliedMetric createLabelledSuppliedCounter(
final MetricCategory category,
final String name,
final String help,
final String... labelNames) {
return getLabelledGauge(labelNames.length);
return getLabelledSuppliedMetric(labelNames.length);
}

@Override
public LabelledSuppliedMetric createLabelledSuppliedGauge(
final MetricCategory category,
final String name,
final String help,
final String... labelNames) {
return getLabelledSuppliedMetric(labelNames.length);
}

/**
Expand All @@ -173,7 +183,7 @@ public LabelledGauge createLabelledGauge(
* @param labelCount the label count
* @return the labelled gauge
*/
public static LabelledGauge getLabelledGauge(final int labelCount) {
public static LabelledSuppliedMetric getLabelledSuppliedMetric(final int labelCount) {
switch (labelCount) {
case 1:
return NO_OP_LABELLED_1_GAUGE;
Expand All @@ -182,7 +192,7 @@ public static LabelledGauge getLabelledGauge(final int labelCount) {
case 3:
return NO_OP_LABELLED_3_GAUGE;
default:
return new LabelledGaugeNoOpMetric(labelCount, NO_OP_GAUGE);
return new LabelledSuppliedNoOpMetric(labelCount, NO_OP_GAUGE);
}
}

Expand Down Expand Up @@ -237,8 +247,9 @@ public T labels(final String... labels) {
}
}

/** The Labelled gauge NoOp metric. */
public static class LabelledGaugeNoOpMetric implements LabelledGauge {
/** The Labelled supplied NoOp metric. */
@SuppressWarnings("removal") // remove when deprecated LabelledGauge is removed
public static class LabelledSuppliedNoOpMetric implements LabelledSuppliedMetric, LabelledGauge {
/** The Label count. */
final int labelCount;

Expand All @@ -251,13 +262,14 @@ public static class LabelledGaugeNoOpMetric implements LabelledGauge {
* @param labelCount the label count
* @param fakeMetric the fake metric
*/
public LabelledGaugeNoOpMetric(final int labelCount, final LabelledGauge fakeMetric) {
public LabelledSuppliedNoOpMetric(
final int labelCount, final LabelledSuppliedMetric fakeMetric) {
this.labelCount = labelCount;
this.fakeMetric = fakeMetric;
}

/** The Fake metric. */
final LabelledGauge fakeMetric;
final LabelledSuppliedMetric fakeMetric;

@Override
public void labels(final DoubleSupplier valueSupplier, final String... labelValues) {
Expand All @@ -270,14 +282,5 @@ public void labels(final DoubleSupplier valueSupplier, final String... labelValu
"The count of labels used must match the count of labels expected.");
Preconditions.checkNotNull(valueSupplier, "No valueSupplier specified");
}

@Override
public boolean isLabelsObserved(final String... labelValues) {
Preconditions.checkArgument(
labelValues.length == labelCount,
"The count of labels used must match the count of labels expected.");
final String labelValuesString = String.join(",", labelValues);
return labelValuesCache.contains(labelValuesString);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
*/
package org.hyperledger.besu.metrics.noop;

import org.hyperledger.besu.plugin.services.metrics.LabelledGauge;
import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric;

import java.util.ArrayList;
import java.util.List;
import java.util.function.DoubleSupplier;

/** The NoOp value collector. */
public class NoOpValueCollector implements LabelledGauge {
public class NoOpValueCollector implements LabelledSuppliedMetric {
private final List<String> labelValuesCreated = new ArrayList<>();

/** Default constructor */
Expand All @@ -36,10 +36,4 @@ public synchronized void labels(final DoubleSupplier valueSupplier, final String
}
labelValuesCreated.add(labelValuesString);
}

@Override
public boolean isLabelsObserved(final String... labelValues) {
final String labelValuesString = String.join(",", labelValues);
return labelValuesCreated.contains(labelValuesString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,14 @@

import org.hyperledger.besu.plugin.services.metrics.LabelledGauge;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.DoubleSupplier;

import com.google.common.base.Preconditions;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.metrics.ObservableDoubleMeasurement;

/** The Open telemetry gauge. */
public class OpenTelemetryGauge implements LabelledGauge {
private final List<String> labelNames;
private final Map<Attributes, DoubleSupplier> observationsMap = new ConcurrentHashMap<>();

@SuppressWarnings("removal") // remove when deprecated LabelledGauge is removed
public class OpenTelemetryGauge extends OpenTelemetryLabelledSuppliedMetric
implements LabelledGauge {
/**
* Instantiates a new Open telemetry gauge.
*
Expand All @@ -46,41 +37,8 @@ public OpenTelemetryGauge(
final String help,
final Meter meter,
final List<String> labelNames) {
this.labelNames = labelNames;
super(labelNames);

meter.gaugeBuilder(metricName).setDescription(help).buildWithCallback(this::updater);
}

@Override
public void labels(final DoubleSupplier valueSupplier, final String... labelValues) {
Preconditions.checkArgument(
labelValues.length == labelNames.size(),
"label values and label names need the same number of elements");
final Attributes labels = getLabels(labelValues);
if (observationsMap.putIfAbsent(labels, valueSupplier) != null) {
throw new IllegalStateException(
"Already registered a gauge with labels " + Arrays.toString(labelValues));
}
}

@Override
public boolean isLabelsObserved(final String... labelValues) {
Preconditions.checkArgument(
labelValues.length == labelNames.size(),
"label values and label names need the same number of elements");
return observationsMap.containsKey(getLabels(labelValues));
}

private Attributes getLabels(final String... labelValues) {
final AttributesBuilder labelsBuilder = Attributes.builder();
for (int i = 0; i < labelNames.size(); i++) {
labelsBuilder.put(labelNames.get(i), labelValues[i]);
}
return labelsBuilder.build();
}

private void updater(final ObservableDoubleMeasurement measurement) {
observationsMap.forEach(
(labels, valueSupplier) -> measurement.record(valueSupplier.getAsDouble(), labels));
}
}
Loading

0 comments on commit fe46289

Please sign in to comment.