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 <fabio.difabio@consensys.net>
  • Loading branch information
fab-10 committed Nov 22, 2024
1 parent f343ce5 commit 306a140
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 157 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 @@ -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
Expand All @@ -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<Observation> streamObservations() {
Expand Down

This file was deleted.

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
Loading

0 comments on commit 306a140

Please sign in to comment.