diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/noop/NoOpMetricsSystem.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/noop/NoOpMetricsSystem.java index 68b5d52ffa3..8c76237ac4e 100644 --- a/metrics/core/src/main/java/org/hyperledger/besu/metrics/noop/NoOpMetricsSystem.java +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/noop/NoOpMetricsSystem.java @@ -21,6 +21,7 @@ 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.LabelledSuppliedSummary; import org.hyperledger.besu.plugin.services.metrics.MetricCategory; import org.hyperledger.besu.plugin.services.metrics.OperationTimer; @@ -67,15 +68,19 @@ public class NoOpMetricsSystem implements ObservableMetricsSystem { /** The constant NO_OP_LABELLED_1_GAUGE. */ public static final LabelledSuppliedMetric NO_OP_LABELLED_1_GAUGE = - new LabelledSuppliedNoOpMetric(1, NO_OP_GAUGE); + new LabelledSuppliedNoOpMetric(1); /** The constant NO_OP_LABELLED_2_GAUGE. */ public static final LabelledSuppliedMetric NO_OP_LABELLED_2_GAUGE = - new LabelledSuppliedNoOpMetric(2, NO_OP_GAUGE); + new LabelledSuppliedNoOpMetric(2); /** The constant NO_OP_LABELLED_3_GAUGE. */ public static final LabelledSuppliedMetric NO_OP_LABELLED_3_GAUGE = - new LabelledSuppliedNoOpMetric(3, NO_OP_GAUGE); + new LabelledSuppliedNoOpMetric(3); + + /** The constant NO_OP_LABELLED_1_SUMMARY. */ + public static final LabelledSuppliedSummary NO_OP_LABELLED_1_SUMMARY = + new LabelledSuppliedNoOpMetric(1); /** Default constructor */ public NoOpMetricsSystem() {} @@ -96,16 +101,12 @@ public LabelledMetric createLabelledCounter( * @return the counter labelled metric */ public static LabelledMetric getCounterLabelledMetric(final int labelCount) { - switch (labelCount) { - case 1: - return NO_OP_LABELLED_1_COUNTER; - case 2: - return NO_OP_LABELLED_2_COUNTER; - case 3: - return NO_OP_LABELLED_3_COUNTER; - default: - return new LabelCountingNoOpMetric<>(labelCount, NO_OP_COUNTER); - } + return switch (labelCount) { + case 1 -> NO_OP_LABELLED_1_COUNTER; + case 2 -> NO_OP_LABELLED_2_COUNTER; + case 3 -> NO_OP_LABELLED_3_COUNTER; + default -> new LabelCountingNoOpMetric<>(labelCount, NO_OP_COUNTER); + }; } @Override @@ -118,11 +119,13 @@ public LabelledMetric createSimpleLabelledTimer( } @Override - public void trackExternalSummary( + public LabelledSuppliedSummary createLabelledSuppliedSummary( final MetricCategory category, final String name, final String help, - final Supplier summarySupplier) {} + final String... labelNames) { + return getLabelledSuppliedSummary(labelNames.length); + } @Override public LabelledMetric createLabelledTimer( @@ -184,16 +187,25 @@ public LabelledSuppliedMetric createLabelledSuppliedGauge( * @return the labelled gauge */ public static LabelledSuppliedMetric getLabelledSuppliedMetric(final int labelCount) { - switch (labelCount) { - case 1: - return NO_OP_LABELLED_1_GAUGE; - case 2: - return NO_OP_LABELLED_2_GAUGE; - case 3: - return NO_OP_LABELLED_3_GAUGE; - default: - return new LabelledSuppliedNoOpMetric(labelCount, NO_OP_GAUGE); - } + return switch (labelCount) { + case 1 -> NO_OP_LABELLED_1_GAUGE; + case 2 -> NO_OP_LABELLED_2_GAUGE; + case 3 -> NO_OP_LABELLED_3_GAUGE; + default -> new LabelledSuppliedNoOpMetric(labelCount); + }; + } + + /** + * Gets labelled supplied histogram. + * + * @param labelCount the label count + * @return the labelled gauge + */ + public static LabelledSuppliedSummary getLabelledSuppliedSummary(final int labelCount) { + return switch (labelCount) { + case 1 -> NO_OP_LABELLED_1_SUMMARY; + default -> new LabelledSuppliedNoOpMetric(labelCount); + }; } @Override @@ -249,7 +261,8 @@ public T labels(final String... labels) { /** The Labelled supplied NoOp metric. */ @SuppressWarnings("removal") // remove when deprecated LabelledGauge is removed - public static class LabelledSuppliedNoOpMetric implements LabelledSuppliedMetric, LabelledGauge { + public static class LabelledSuppliedNoOpMetric + implements LabelledSuppliedMetric, LabelledGauge, LabelledSuppliedSummary { /** The Label count. */ final int labelCount; @@ -257,22 +270,26 @@ public static class LabelledSuppliedNoOpMetric implements LabelledSuppliedMetric final List labelValuesCache = new ArrayList<>(); /** - * Instantiates a new Labelled gauge NoOp metric. + * Instantiates a new Labelled supplied NoOp metric. * * @param labelCount the label count - * @param fakeMetric the fake metric */ - public LabelledSuppliedNoOpMetric( - final int labelCount, final LabelledSuppliedMetric fakeMetric) { + public LabelledSuppliedNoOpMetric(final int labelCount) { this.labelCount = labelCount; - this.fakeMetric = fakeMetric; } - /** The Fake metric. */ - final LabelledSuppliedMetric fakeMetric; - @Override public void labels(final DoubleSupplier valueSupplier, final String... labelValues) { + internalLabels(valueSupplier, labelValues); + } + + @Override + public void labels( + final Supplier summarySupplier, final String... labelValues) { + internalLabels(summarySupplier, labelValues); + } + + private void internalLabels(final Object valueSupplier, final String... labelValues) { final String labelValuesString = String.join(",", labelValues); Preconditions.checkArgument( !labelValuesCache.contains(labelValuesString), @@ -281,6 +298,7 @@ public void labels(final DoubleSupplier valueSupplier, final String... labelValu labelValues.length == labelCount, "The count of labels used must match the count of labels expected."); Preconditions.checkNotNull(valueSupplier, "No valueSupplier specified"); + labelValuesCache.add(labelValuesString); } } } diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetrySystem.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetrySystem.java index dfb3da6fb31..a93f0383860 100644 --- a/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetrySystem.java +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetrySystem.java @@ -20,9 +20,9 @@ import org.hyperledger.besu.metrics.StandardMetricCategory; import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem; import org.hyperledger.besu.plugin.services.metrics.Counter; -import org.hyperledger.besu.plugin.services.metrics.ExternalSummary; import org.hyperledger.besu.plugin.services.metrics.LabelledMetric; import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric; +import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedSummary; import org.hyperledger.besu.plugin.services.metrics.MetricCategory; import org.hyperledger.besu.plugin.services.metrics.OperationTimer; @@ -41,7 +41,6 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.function.DoubleSupplier; -import java.util.function.Supplier; import java.util.stream.Stream; import javax.inject.Singleton; @@ -246,11 +245,13 @@ public LabelledMetric createSimpleLabelledTimer( } @Override - public void trackExternalSummary( + public LabelledSuppliedSummary createLabelledSuppliedSummary( final MetricCategory category, final String name, final String help, - final Supplier summarySupplier) {} + final String... labelNames) { + return null; + } @Override public LabelledMetric createLabelledTimer( diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/AbstractPrometheusSummary.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/AbstractPrometheusSummary.java index 38ffa9eebfa..92008d8879c 100644 --- a/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/AbstractPrometheusSummary.java +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/AbstractPrometheusSummary.java @@ -23,9 +23,13 @@ import java.util.ArrayList; import java.util.stream.Stream; +import io.prometheus.metrics.model.registry.Collector; +import io.prometheus.metrics.model.registry.PrometheusRegistry; import io.prometheus.metrics.model.snapshots.SummarySnapshot; abstract class AbstractPrometheusSummary extends CategorizedPrometheusCollector { + /** The collector */ + protected final Collector collector; /** * Create a new collector assigned to the given category and with the given name, and computed the @@ -34,11 +38,42 @@ abstract class AbstractPrometheusSummary extends CategorizedPrometheusCollector * @param category The {@link MetricCategory} this collector is assigned to * @param name The name of this collector */ - protected AbstractPrometheusSummary(final MetricCategory category, final String name) { + protected AbstractPrometheusSummary( + final MetricCategory category, + final String name, + final String help, + final String... labelNames) { super(category, name); + this.collector = createCollector(help, labelNames); } - protected abstract SummarySnapshot collect(); + /** + * Create the actual collector + * + * @param help the help + * @param labelNames the label names + * @return the created collector + */ + protected abstract Collector createCollector(final String help, final String... labelNames); + + @Override + public String getIdentifier() { + return collector.getPrometheusName(); + } + + @Override + public void register(final PrometheusRegistry registry) { + registry.register(collector); + } + + @Override + public void unregister(final PrometheusRegistry registry) { + registry.unregister(collector); + } + + private SummarySnapshot collect() { + return (SummarySnapshot) collector.collect(); + } @Override public Stream streamObservations() { diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusExternalSummary.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusExternalSummary.java deleted file mode 100644 index e892fe79dc9..00000000000 --- a/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusExternalSummary.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright contributors to Besu. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ -package org.hyperledger.besu.metrics.prometheus; - -import org.hyperledger.besu.plugin.services.metrics.ExternalSummary; -import org.hyperledger.besu.plugin.services.metrics.MetricCategory; - -import java.util.function.Supplier; - -import io.prometheus.metrics.core.metrics.SummaryWithCallback; -import io.prometheus.metrics.model.registry.PrometheusRegistry; -import io.prometheus.metrics.model.snapshots.Quantile; -import io.prometheus.metrics.model.snapshots.Quantiles; -import io.prometheus.metrics.model.snapshots.SummarySnapshot; - -class PrometheusExternalSummary extends AbstractPrometheusSummary { - - private final SummaryWithCallback summary; - - public PrometheusExternalSummary( - final MetricCategory category, - final String name, - final String help, - final Supplier summarySupplier) { - super(category, name); - summary = - SummaryWithCallback.builder() - .name(name) - .help(help) - .callback( - cb -> { - final var externalSummary = summarySupplier.get(); - final var quantilesBuilder = Quantiles.builder(); - externalSummary.quantiles().stream() - .map(pq -> new Quantile(pq.quantile(), pq.value())) - .forEach(quantilesBuilder::quantile); - cb.call(externalSummary.count(), externalSummary.sum(), quantilesBuilder.build()); - }) - .build(); - } - - @Override - public String getIdentifier() { - return summary.getPrometheusName(); - } - - @Override - public void register(final PrometheusRegistry registry) { - registry.register(summary); - } - - @Override - public void unregister(final PrometheusRegistry registry) { - registry.unregister(summary); - } - - @Override - protected SummarySnapshot collect() { - return summary.collect(); - } -} diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusMetricsSystem.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusMetricsSystem.java index e31f3a91498..c4ff1f9cedd 100644 --- a/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusMetricsSystem.java +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusMetricsSystem.java @@ -21,9 +21,9 @@ import org.hyperledger.besu.metrics.StandardMetricCategory; import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem; import org.hyperledger.besu.plugin.services.metrics.Counter; -import org.hyperledger.besu.plugin.services.metrics.ExternalSummary; import org.hyperledger.besu.plugin.services.metrics.LabelledMetric; import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric; +import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedSummary; import org.hyperledger.besu.plugin.services.metrics.MetricCategory; import org.hyperledger.besu.plugin.services.metrics.OperationTimer; @@ -32,7 +32,6 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import java.util.function.Supplier; import java.util.stream.Stream; import com.google.common.cache.Cache; @@ -166,17 +165,18 @@ public LabelledMetric createSimpleLabelledTimer( } @Override - public void trackExternalSummary( + public LabelledSuppliedSummary createLabelledSuppliedSummary( final MetricCategory category, final String name, final String help, - final Supplier summarySupplier) { + final String... labelNames) { if (isCategoryEnabled(category)) { - final PrometheusExternalSummary externalSummary = - new PrometheusExternalSummary(category, name, help, summarySupplier); - - registerCollector(category, externalSummary); + final PrometheusSuppliedSummary summary = + new PrometheusSuppliedSummary(category, name, help, labelNames); + registerCollector(category, summary); + return summary; } + return NoOpMetricsSystem.getLabelledSuppliedSummary(labelNames.length); } @Override diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusSuppliedSummary.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusSuppliedSummary.java new file mode 100644 index 00000000000..788dbd898fc --- /dev/null +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusSuppliedSummary.java @@ -0,0 +1,82 @@ +/* + * Copyright contributors to Besu. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.metrics.prometheus; + +import org.hyperledger.besu.plugin.services.metrics.ExternalSummary; +import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedSummary; +import org.hyperledger.besu.plugin.services.metrics.MetricCategory; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Supplier; + +import io.prometheus.metrics.core.metrics.SummaryWithCallback; +import io.prometheus.metrics.model.registry.Collector; +import io.prometheus.metrics.model.snapshots.Quantile; +import io.prometheus.metrics.model.snapshots.Quantiles; + +class PrometheusSuppliedSummary extends AbstractPrometheusSummary + implements LabelledSuppliedSummary { + /** Map label values with the collector callback data */ + protected final Map, CallbackData> labelledCallbackData = new ConcurrentHashMap<>(); + + public PrometheusSuppliedSummary( + final MetricCategory category, + final String name, + final String help, + final String... labelNames) { + super(category, name, help, labelNames); + } + + @Override + protected Collector createCollector(final String help, final String... labelNames) { + return SummaryWithCallback.builder() + .name(name) + .help(help) + .labelNames(labelNames) + .callback(this::callback) + .build(); + } + + private void callback(final SummaryWithCallback.Callback callback) { + labelledCallbackData + .values() + .forEach( + callbackData -> { + final var externalSummary = callbackData.summarySupplier().get(); + final var quantilesBuilder = Quantiles.builder(); + externalSummary.quantiles().stream() + .map(pq -> new Quantile(pq.quantile(), pq.value())) + .forEach(quantilesBuilder::quantile); + callback.call( + externalSummary.count(), externalSummary.sum(), quantilesBuilder.build()); + }); + } + + @Override + public synchronized void labels( + final Supplier summarySupplier, final String... labelValues) { + final var valueList = List.of(labelValues); + if (labelledCallbackData.containsKey(valueList)) { + throw new IllegalArgumentException( + String.format("A collector has already been created for label values %s", valueList)); + } + + labelledCallbackData.put(valueList, new CallbackData(summarySupplier, labelValues)); + } + + protected record CallbackData(Supplier summarySupplier, String[] labelValues) {} +} diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusTimer.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusTimer.java index 76d24bbb0f4..bb27954f937 100644 --- a/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusTimer.java +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusTimer.java @@ -22,12 +22,10 @@ import io.prometheus.metrics.core.datapoints.DistributionDataPoint; import io.prometheus.metrics.core.metrics.Summary; -import io.prometheus.metrics.model.registry.PrometheusRegistry; -import io.prometheus.metrics.model.snapshots.SummarySnapshot; +import io.prometheus.metrics.model.registry.Collector; class PrometheusTimer extends AbstractPrometheusSummary implements LabelledMetric { - - private final io.prometheus.metrics.core.metrics.Summary summary; + private final Map quantiles; public PrometheusTimer( final MetricCategory category, @@ -35,36 +33,21 @@ public PrometheusTimer( final String help, final Map quantiles, final String... labelNames) { - super(category, name); + super(category, name, help, labelNames); + this.quantiles = quantiles; + } + + @Override + protected Collector createCollector(final String help, final String... labelNames) { final var summaryBuilder = Summary.builder().name(this.prefixedName).help(help).labelNames(labelNames); quantiles.forEach(summaryBuilder::quantile); - this.summary = summaryBuilder.build(); + return summaryBuilder.build(); } @Override public OperationTimer labels(final String... labels) { - final DistributionDataPoint metric = summary.labelValues(labels); + final DistributionDataPoint metric = ((Summary) collector).labelValues(labels); return () -> metric.startTimer()::observeDuration; } - - @Override - public String getIdentifier() { - return summary.getPrometheusName(); - } - - @Override - public void register(final PrometheusRegistry registry) { - registry.register(summary); - } - - @Override - public void unregister(final PrometheusRegistry registry) { - registry.unregister(summary); - } - - @Override - protected SummarySnapshot collect() { - return summary.collect(); - } } diff --git a/metrics/core/src/test-support/java/org/hyperledger/besu/metrics/StubMetricsSystem.java b/metrics/core/src/test-support/java/org/hyperledger/besu/metrics/StubMetricsSystem.java index c10c62132c1..4dfcd2fff9c 100644 --- a/metrics/core/src/test-support/java/org/hyperledger/besu/metrics/StubMetricsSystem.java +++ b/metrics/core/src/test-support/java/org/hyperledger/besu/metrics/StubMetricsSystem.java @@ -18,9 +18,9 @@ import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem; import org.hyperledger.besu.plugin.services.metrics.Counter; -import org.hyperledger.besu.plugin.services.metrics.ExternalSummary; import org.hyperledger.besu.plugin.services.metrics.LabelledMetric; import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric; +import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedSummary; import org.hyperledger.besu.plugin.services.metrics.MetricCategory; import org.hyperledger.besu.plugin.services.metrics.OperationTimer; @@ -30,7 +30,6 @@ import java.util.Map; import java.util.Set; import java.util.function.DoubleSupplier; -import java.util.function.Supplier; import java.util.stream.Stream; import com.google.common.cache.Cache; @@ -98,11 +97,13 @@ public LabelledMetric createSimpleLabelledTimer( } @Override - public void trackExternalSummary( + public LabelledSuppliedSummary createLabelledSuppliedSummary( final MetricCategory category, final String name, final String help, - final Supplier summarySupplier) {} + final String... labelNames) { + return NoOpMetricsSystem.getLabelledSuppliedSummary(labelNames.length); + } @Override public void createGauge( diff --git a/metrics/rocksdb/src/main/java/org/hyperledger/besu/metrics/rocksdb/RocksDBStats.java b/metrics/rocksdb/src/main/java/org/hyperledger/besu/metrics/rocksdb/RocksDBStats.java index c3c5fef2c11..123be667d7f 100644 --- a/metrics/rocksdb/src/main/java/org/hyperledger/besu/metrics/rocksdb/RocksDBStats.java +++ b/metrics/rocksdb/src/main/java/org/hyperledger/besu/metrics/rocksdb/RocksDBStats.java @@ -174,7 +174,7 @@ public static void registerRocksDBMetrics( for (final var histogramType : HISTOGRAM_TYPES) { - metricsSystem.trackExternalSummary( + metricsSystem.createSummary( KVSTORE_ROCKSDB_STATS, KVSTORE_ROCKSDB_STATS.getName() + "_" + histogramType.name().toLowerCase(Locale.ROOT), "RocksDB histogram for " + histogramType.name(), diff --git a/plugin-api/build.gradle b/plugin-api/build.gradle index 74148125519..9905aed7ef2 100644 --- a/plugin-api/build.gradle +++ b/plugin-api/build.gradle @@ -71,7 +71,7 @@ Calculated : ${currentHash} tasks.register('checkAPIChanges', FileStateChecker) { description = "Checks that the API for the Plugin-API project does not change without deliberate thought" files = sourceSets.main.allJava.files - knownHash = '8X9cB9p6OUuOyCMC2ezwb6kJwWfz0/5vk/UXqSyad8o=' + knownHash = 'CvTVR7kCUKOFXPhQrXVYQOIGuJryKRhs6Ty7tMGW5O0=' } check.dependsOn('checkAPIChanges') diff --git a/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/MetricsSystem.java b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/MetricsSystem.java index 12c0ab3849b..d1652701743 100644 --- a/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/MetricsSystem.java +++ b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/MetricsSystem.java @@ -19,6 +19,7 @@ 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.LabelledSuppliedSummary; import org.hyperledger.besu.plugin.services.metrics.MetricCategory; import org.hyperledger.besu.plugin.services.metrics.OperationTimer; @@ -225,7 +226,21 @@ default void createLongGauge( } /** - * Track a summary that is computed externally to this metric system. Useful when existing + * Create a summary with assigned labels, that is computed externally to this metric system. + * Useful when existing libraries calculate the summary data on their own, and we want to export + * that summary via the configured metric system. A notable example are RocksDB statistics. + * + * @param category The {@link MetricCategory} this external summary is assigned to. + * @param name A name for the metric. + * @param help A human readable description of the metric. + * @param labelNames An array of labels to assign to the supplier summary. + * @return The created labelled supplied summary + */ + LabelledSuppliedSummary createLabelledSuppliedSummary( + MetricCategory category, String name, String help, String... labelNames); + + /** + * Create a summary that is computed externally to this metric system. Useful when existing * libraries calculate the summary data on their own, and we want to export that summary via the * configured metric system. A notable example are RocksDB statistics. * @@ -234,8 +249,14 @@ default void createLongGauge( * @param help A human readable description of the metric. * @param summarySupplier A supplier to retrieve the summary data when needed. */ - void trackExternalSummary( - MetricCategory category, String name, String help, Supplier summarySupplier); + default void createSummary( + final MetricCategory category, + final String name, + final String help, + final Supplier summarySupplier) { + createLabelledSuppliedSummary(category, name, help).labels(summarySupplier); + } + ; /** * Collect metrics from Guava cache. diff --git a/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/metrics/LabelledSuppliedSummary.java b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/metrics/LabelledSuppliedSummary.java new file mode 100644 index 00000000000..de4cf7f933c --- /dev/null +++ b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/metrics/LabelledSuppliedSummary.java @@ -0,0 +1,28 @@ +/* + * Copyright contributors to Besu. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.plugin.services.metrics; + +import java.util.function.Supplier; + +/** The interface Labelled supplied summary. */ +public interface LabelledSuppliedSummary { + /** + * Labels. + * + * @param summarySupplier the summary supplier + * @param labelValues the label values + */ + void labels(final Supplier summarySupplier, final String... labelValues); +}