Skip to content

Commit

Permalink
Rename ExtranalSummary to SuppliedSummary for consistency
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 Nov 22, 2024
1 parent f343ce5 commit 4370075
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() {}
Expand All @@ -96,16 +101,12 @@ public LabelledMetric<Counter> createLabelledCounter(
* @return the counter labelled metric
*/
public static LabelledMetric<Counter> 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
Expand All @@ -118,11 +119,13 @@ public LabelledMetric<OperationTimer> createSimpleLabelledTimer(
}

@Override
public void trackExternalSummary(
public LabelledSuppliedSummary createLabelledSuppliedSummary(
final MetricCategory category,
final String name,
final String help,
final Supplier<ExternalSummary> summarySupplier) {}
final String... labelNames) {
return getLabelledSuppliedSummary(labelNames.length);
}

@Override
public LabelledMetric<OperationTimer> createLabelledTimer(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -249,30 +261,35 @@ 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;

/** The Label values cache. */
final List<String> 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<ExternalSummary> 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),
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -246,11 +245,13 @@ public LabelledMetric<OperationTimer> createSimpleLabelledTimer(
}

@Override
public void trackExternalSummary(
public LabelledSuppliedSummary createLabelledSuppliedSummary(
final MetricCategory category,
final String name,
final String help,
final Supplier<ExternalSummary> summarySupplier) {}
final String... labelNames) {
return null;
}

@Override
public LabelledMetric<OperationTimer> createLabelledTimer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -166,17 +165,18 @@ public LabelledMetric<OperationTimer> createSimpleLabelledTimer(
}

@Override
public void trackExternalSummary(
public LabelledSuppliedSummary createLabelledSuppliedSummary(
final MetricCategory category,
final String name,
final String help,
final Supplier<ExternalSummary> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
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;
Expand All @@ -25,32 +29,55 @@
import io.prometheus.metrics.model.snapshots.Quantiles;
import io.prometheus.metrics.model.snapshots.SummarySnapshot;

class PrometheusExternalSummary extends AbstractPrometheusSummary {
class PrometheusSuppliedSummary extends AbstractPrometheusSummary
implements LabelledSuppliedSummary {
/** Map label values with the collector callback data */
protected final Map<List<String>, CallbackData> labelledCallbackData = new ConcurrentHashMap<>();

private final SummaryWithCallback summary;

public PrometheusExternalSummary(
public PrometheusSuppliedSummary(
final MetricCategory category,
final String name,
final String help,
final Supplier<ExternalSummary> summarySupplier) {
final String... labelNames) {
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());
})
.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<ExternalSummary> 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));
}

@Override
public String getIdentifier() {
return summary.getPrometheusName();
Expand All @@ -70,4 +97,6 @@ public void unregister(final PrometheusRegistry registry) {
protected SummarySnapshot collect() {
return summary.collect();
}

protected record CallbackData(Supplier<ExternalSummary> summarySupplier, String[] labelValues) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -98,11 +97,13 @@ public LabelledMetric<OperationTimer> createSimpleLabelledTimer(
}

@Override
public void trackExternalSummary(
public LabelledSuppliedSummary createLabelledSuppliedSummary(
final MetricCategory category,
final String name,
final String help,
final Supplier<ExternalSummary> summarySupplier) {}
final String... labelNames) {
return NoOpMetricsSystem.getLabelledSuppliedSummary(labelNames.length);
}

@Override
public void createGauge(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion plugin-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
Loading

0 comments on commit 4370075

Please sign in to comment.