Skip to content

Commit fe46289

Browse files
authored
Add support for counter metric with external supplied value (#7894)
* 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]>
1 parent 73a1e5b commit fe46289

File tree

20 files changed

+326
-153
lines changed

20 files changed

+326
-153
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Changelog
22

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

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

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

1111
### Additions and Improvements
1212
- Fine tune already seen txs tracker when a tx is removed from the pool [#7755](https://github.com/hyperledger/besu/pull/7755)
@@ -16,6 +16,7 @@
1616
- 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)
1717
- Add a method to check if a metric category is enabled to the plugin API [#7832](https://github.com/hyperledger/besu/pull/7832)
1818
- Add account and state overrides to `eth_call` and `eth_estimateGas` [#7801](https://github.com/hyperledger/besu/pull/7801)
19+
- Add a new metric collector for counters which get their value from suppliers [#7894](https://github.com/hyperledger/besu/pull/7894)
1920

2021
### Bug fixes
2122
- Fix registering new metric categories from plugins [#7825](https://github.com/hyperledger/besu/pull/7825)

besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,7 @@ private void validatePrivacyPluginOptions() {
13321332
private void setReleaseMetrics() {
13331333
besuComponent
13341334
.getMetricsSystem()
1335-
.createLabelledGauge(
1335+
.createLabelledSuppliedGauge(
13361336
StandardMetricCategory.PROCESS, "release", "Release information", "version")
13371337
.labels(() -> 1, BesuInfo.version());
13381338
}

ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/MonitoredExecutors.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,13 @@ private static <T extends ThreadPoolExecutor> T newMonitoredExecutor(
153153
"Current number of threads in the thread pool",
154154
executor::getPoolSize);
155155

156-
metricsSystem.createLongGauge(
156+
metricsSystem.createCounter(
157157
BesuMetricCategory.EXECUTORS,
158158
metricName + "_completed_tasks_total",
159159
"Total number of tasks executed",
160160
executor::getCompletedTaskCount);
161161

162-
metricsSystem.createLongGauge(
162+
metricsSystem.createCounter(
163163
BesuMetricCategory.EXECUTORS,
164164
metricName + "_submitted_tasks_total",
165165
"Total number of tasks executed",

ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutor.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import org.hyperledger.besu.metrics.BesuMetricCategory;
2121
import org.hyperledger.besu.plugin.services.MetricsSystem;
2222
import org.hyperledger.besu.plugin.services.metrics.Counter;
23-
import org.hyperledger.besu.plugin.services.metrics.LabelledGauge;
2423
import org.hyperledger.besu.plugin.services.metrics.LabelledMetric;
24+
import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric;
2525
import org.hyperledger.besu.plugin.services.metrics.OperationTimer;
2626

2727
import java.util.Collection;
@@ -43,7 +43,7 @@ public class PeerTaskExecutor {
4343
private final LabelledMetric<Counter> timeoutCounter;
4444
private final LabelledMetric<Counter> invalidResponseCounter;
4545
private final LabelledMetric<Counter> internalExceptionCounter;
46-
private final LabelledGauge inflightRequestGauge;
46+
private final LabelledSuppliedMetric inflightRequestGauge;
4747
private final Map<String, AtomicInteger> inflightRequestCountByClassName;
4848

4949
public PeerTaskExecutor(
@@ -77,7 +77,7 @@ public PeerTaskExecutor(
7777
"Counter of the number of internal exceptions occurred",
7878
"taskName");
7979
inflightRequestGauge =
80-
metricsSystem.createLabelledGauge(
80+
metricsSystem.createLabelledSuppliedGauge(
8181
BesuMetricCategory.PEERS,
8282
"inflight_request_gauge",
8383
"Gauge of the number of inflight requests",

ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionPoolMetrics.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import org.hyperledger.besu.metrics.RunnableCounter;
2323
import org.hyperledger.besu.plugin.services.MetricsSystem;
2424
import org.hyperledger.besu.plugin.services.metrics.Counter;
25-
import org.hyperledger.besu.plugin.services.metrics.LabelledGauge;
2625
import org.hyperledger.besu.plugin.services.metrics.LabelledMetric;
26+
import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric;
2727

2828
import java.util.HashMap;
2929
import java.util.Map;
@@ -47,10 +47,10 @@ public class TransactionPoolMetrics {
4747
private final LabelledMetric<Counter> removedCounter;
4848
private final LabelledMetric<Counter> rejectedCounter;
4949
private final LabelledMetric<Counter> penalizedCounter;
50-
private final LabelledGauge spaceUsed;
51-
private final LabelledGauge transactionCount;
52-
private final LabelledGauge transactionCountByType;
53-
private final LabelledGauge uniqueSenderCount;
50+
private final LabelledSuppliedMetric spaceUsed;
51+
private final LabelledSuppliedMetric transactionCount;
52+
private final LabelledSuppliedMetric transactionCountByType;
53+
private final LabelledSuppliedMetric uniqueSenderCount;
5454
private final LabelledMetric<Counter> expiredMessagesCounter;
5555
private final Map<String, RunnableCounter> expiredMessagesRunnableCounters = new HashMap<>();
5656
private final LabelledMetric<Counter> alreadySeenTransactionsCounter;
@@ -103,29 +103,29 @@ public TransactionPoolMetrics(final MetricsSystem metricsSystem) {
103103
"layer");
104104

105105
spaceUsed =
106-
metricsSystem.createLabelledGauge(
106+
metricsSystem.createLabelledSuppliedGauge(
107107
BesuMetricCategory.TRANSACTION_POOL,
108108
"space_used",
109109
"The amount of space used by the transactions in the layer",
110110
"layer");
111111

112112
transactionCount =
113-
metricsSystem.createLabelledGauge(
113+
metricsSystem.createLabelledSuppliedGauge(
114114
BesuMetricCategory.TRANSACTION_POOL,
115115
"number_of_transactions",
116116
"The number of transactions currently present in the layer",
117117
"layer");
118118

119119
transactionCountByType =
120-
metricsSystem.createLabelledGauge(
120+
metricsSystem.createLabelledSuppliedGauge(
121121
BesuMetricCategory.TRANSACTION_POOL,
122122
"number_of_transactions_by_type",
123123
"The number of transactions, of a specified type, currently present in the layer",
124124
"layer",
125125
"type");
126126

127127
uniqueSenderCount =
128-
metricsSystem.createLabelledGauge(
128+
metricsSystem.createLabelledSuppliedGauge(
129129
BesuMetricCategory.TRANSACTION_POOL,
130130
"unique_senders",
131131
"The number of senders with at least one transaction currently present in the layer",

metrics/core/src/main/java/org/hyperledger/besu/metrics/noop/NoOpMetricsSystem.java

+27-24
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.hyperledger.besu.plugin.services.metrics.ExternalSummary;
2121
import org.hyperledger.besu.plugin.services.metrics.LabelledGauge;
2222
import org.hyperledger.besu.plugin.services.metrics.LabelledMetric;
23+
import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric;
2324
import org.hyperledger.besu.plugin.services.metrics.MetricCategory;
2425
import org.hyperledger.besu.plugin.services.metrics.OperationTimer;
2526

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

4344
/** The constant NO_OP_GAUGE. */
44-
public static final LabelledGauge NO_OP_GAUGE = new NoOpValueCollector();
45+
public static final LabelledSuppliedMetric NO_OP_GAUGE = new NoOpValueCollector();
4546

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

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

6768
/** The constant NO_OP_LABELLED_1_GAUGE. */
68-
public static final LabelledGauge NO_OP_LABELLED_1_GAUGE =
69-
new LabelledGaugeNoOpMetric(1, NO_OP_GAUGE);
69+
public static final LabelledSuppliedMetric NO_OP_LABELLED_1_GAUGE =
70+
new LabelledSuppliedNoOpMetric(1, NO_OP_GAUGE);
7071

7172
/** The constant NO_OP_LABELLED_2_GAUGE. */
72-
public static final LabelledGauge NO_OP_LABELLED_2_GAUGE =
73-
new LabelledGaugeNoOpMetric(2, NO_OP_GAUGE);
73+
public static final LabelledSuppliedMetric NO_OP_LABELLED_2_GAUGE =
74+
new LabelledSuppliedNoOpMetric(2, NO_OP_GAUGE);
7475

7576
/** The constant NO_OP_LABELLED_3_GAUGE. */
76-
public static final LabelledGauge NO_OP_LABELLED_3_GAUGE =
77-
new LabelledGaugeNoOpMetric(3, NO_OP_GAUGE);
77+
public static final LabelledSuppliedMetric NO_OP_LABELLED_3_GAUGE =
78+
new LabelledSuppliedNoOpMetric(3, NO_OP_GAUGE);
7879

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

161162
@Override
162-
public LabelledGauge createLabelledGauge(
163+
public LabelledSuppliedMetric createLabelledSuppliedCounter(
163164
final MetricCategory category,
164165
final String name,
165166
final String help,
166167
final String... labelNames) {
167-
return getLabelledGauge(labelNames.length);
168+
return getLabelledSuppliedMetric(labelNames.length);
169+
}
170+
171+
@Override
172+
public LabelledSuppliedMetric createLabelledSuppliedGauge(
173+
final MetricCategory category,
174+
final String name,
175+
final String help,
176+
final String... labelNames) {
177+
return getLabelledSuppliedMetric(labelNames.length);
168178
}
169179

170180
/**
@@ -173,7 +183,7 @@ public LabelledGauge createLabelledGauge(
173183
* @param labelCount the label count
174184
* @return the labelled gauge
175185
*/
176-
public static LabelledGauge getLabelledGauge(final int labelCount) {
186+
public static LabelledSuppliedMetric getLabelledSuppliedMetric(final int labelCount) {
177187
switch (labelCount) {
178188
case 1:
179189
return NO_OP_LABELLED_1_GAUGE;
@@ -182,7 +192,7 @@ public static LabelledGauge getLabelledGauge(final int labelCount) {
182192
case 3:
183193
return NO_OP_LABELLED_3_GAUGE;
184194
default:
185-
return new LabelledGaugeNoOpMetric(labelCount, NO_OP_GAUGE);
195+
return new LabelledSuppliedNoOpMetric(labelCount, NO_OP_GAUGE);
186196
}
187197
}
188198

@@ -237,8 +247,9 @@ public T labels(final String... labels) {
237247
}
238248
}
239249

240-
/** The Labelled gauge NoOp metric. */
241-
public static class LabelledGaugeNoOpMetric implements LabelledGauge {
250+
/** The Labelled supplied NoOp metric. */
251+
@SuppressWarnings("removal") // remove when deprecated LabelledGauge is removed
252+
public static class LabelledSuppliedNoOpMetric implements LabelledSuppliedMetric, LabelledGauge {
242253
/** The Label count. */
243254
final int labelCount;
244255

@@ -251,13 +262,14 @@ public static class LabelledGaugeNoOpMetric implements LabelledGauge {
251262
* @param labelCount the label count
252263
* @param fakeMetric the fake metric
253264
*/
254-
public LabelledGaugeNoOpMetric(final int labelCount, final LabelledGauge fakeMetric) {
265+
public LabelledSuppliedNoOpMetric(
266+
final int labelCount, final LabelledSuppliedMetric fakeMetric) {
255267
this.labelCount = labelCount;
256268
this.fakeMetric = fakeMetric;
257269
}
258270

259271
/** The Fake metric. */
260-
final LabelledGauge fakeMetric;
272+
final LabelledSuppliedMetric fakeMetric;
261273

262274
@Override
263275
public void labels(final DoubleSupplier valueSupplier, final String... labelValues) {
@@ -270,14 +282,5 @@ public void labels(final DoubleSupplier valueSupplier, final String... labelValu
270282
"The count of labels used must match the count of labels expected.");
271283
Preconditions.checkNotNull(valueSupplier, "No valueSupplier specified");
272284
}
273-
274-
@Override
275-
public boolean isLabelsObserved(final String... labelValues) {
276-
Preconditions.checkArgument(
277-
labelValues.length == labelCount,
278-
"The count of labels used must match the count of labels expected.");
279-
final String labelValuesString = String.join(",", labelValues);
280-
return labelValuesCache.contains(labelValuesString);
281-
}
282285
}
283286
}

metrics/core/src/main/java/org/hyperledger/besu/metrics/noop/NoOpValueCollector.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
*/
1515
package org.hyperledger.besu.metrics.noop;
1616

17-
import org.hyperledger.besu.plugin.services.metrics.LabelledGauge;
17+
import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric;
1818

1919
import java.util.ArrayList;
2020
import java.util.List;
2121
import java.util.function.DoubleSupplier;
2222

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

2727
/** Default constructor */
@@ -36,10 +36,4 @@ public synchronized void labels(final DoubleSupplier valueSupplier, final String
3636
}
3737
labelValuesCreated.add(labelValuesString);
3838
}
39-
40-
@Override
41-
public boolean isLabelsObserved(final String... labelValues) {
42-
final String labelValuesString = String.join(",", labelValues);
43-
return labelValuesCreated.contains(labelValuesString);
44-
}
4539
}

metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetryGauge.java

+4-46
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,14 @@
1616

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

19-
import java.util.Arrays;
2019
import java.util.List;
21-
import java.util.Map;
22-
import java.util.concurrent.ConcurrentHashMap;
23-
import java.util.function.DoubleSupplier;
2420

25-
import com.google.common.base.Preconditions;
26-
import io.opentelemetry.api.common.Attributes;
27-
import io.opentelemetry.api.common.AttributesBuilder;
2821
import io.opentelemetry.api.metrics.Meter;
29-
import io.opentelemetry.api.metrics.ObservableDoubleMeasurement;
3022

3123
/** The Open telemetry gauge. */
32-
public class OpenTelemetryGauge implements LabelledGauge {
33-
private final List<String> labelNames;
34-
private final Map<Attributes, DoubleSupplier> observationsMap = new ConcurrentHashMap<>();
35-
24+
@SuppressWarnings("removal") // remove when deprecated LabelledGauge is removed
25+
public class OpenTelemetryGauge extends OpenTelemetryLabelledSuppliedMetric
26+
implements LabelledGauge {
3627
/**
3728
* Instantiates a new Open telemetry gauge.
3829
*
@@ -46,41 +37,8 @@ public OpenTelemetryGauge(
4637
final String help,
4738
final Meter meter,
4839
final List<String> labelNames) {
49-
this.labelNames = labelNames;
40+
super(labelNames);
5041

5142
meter.gaugeBuilder(metricName).setDescription(help).buildWithCallback(this::updater);
5243
}
53-
54-
@Override
55-
public void labels(final DoubleSupplier valueSupplier, final String... labelValues) {
56-
Preconditions.checkArgument(
57-
labelValues.length == labelNames.size(),
58-
"label values and label names need the same number of elements");
59-
final Attributes labels = getLabels(labelValues);
60-
if (observationsMap.putIfAbsent(labels, valueSupplier) != null) {
61-
throw new IllegalStateException(
62-
"Already registered a gauge with labels " + Arrays.toString(labelValues));
63-
}
64-
}
65-
66-
@Override
67-
public boolean isLabelsObserved(final String... labelValues) {
68-
Preconditions.checkArgument(
69-
labelValues.length == labelNames.size(),
70-
"label values and label names need the same number of elements");
71-
return observationsMap.containsKey(getLabels(labelValues));
72-
}
73-
74-
private Attributes getLabels(final String... labelValues) {
75-
final AttributesBuilder labelsBuilder = Attributes.builder();
76-
for (int i = 0; i < labelNames.size(); i++) {
77-
labelsBuilder.put(labelNames.get(i), labelValues[i]);
78-
}
79-
return labelsBuilder.build();
80-
}
81-
82-
private void updater(final ObservableDoubleMeasurement measurement) {
83-
observationsMap.forEach(
84-
(labels, valueSupplier) -> measurement.record(valueSupplier.getAsDouble(), labels));
85-
}
8644
}

0 commit comments

Comments
 (0)