Skip to content

Commit

Permalink
Fixes
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 26, 2024
1 parent ffc7907 commit 1c2da80
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ public class NoOpMetricsSystem implements ObservableMetricsSystem {
/** The constant NO_OP_COUNTER. */
public static final Counter NO_OP_COUNTER = new NoOpCounter();

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

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

/** The constant NO_OP_OPERATION_TIMER. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ public LabelledSuppliedSummary createLabelledSuppliedSummary(
final String name,
final String help,
final String... labelNames) {
return null;
// not yet supported
return (LabelledSuppliedSummary) NoOpMetricsSystem.getLabelledSuppliedMetric(labelNames.length);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,18 @@
*/
abstract class AbstractPrometheusSummary extends CategorizedPrometheusCollector {
/** The Prometheus collector */
protected final Collector collector;
protected Collector collector;

/**
* Constructs a new AbstractPrometheusSummary.
*
* @param category The {@link MetricCategory} this collector is assigned to
* @param name The name of this collector
* @param help The help description for this collector
* @param labelNames The label names for this collector
*/
protected AbstractPrometheusSummary(
final MetricCategory category,
final String name,
final String help,
final String... labelNames) {
protected AbstractPrometheusSummary(final MetricCategory category, final String name) {
super(category, name);
this.collector = createCollector(help, labelNames);
}

/**
* Creates the actual Prometheus collector.
*
* @param help The help description for this collector
* @param labelNames The label names for this collector
* @return The created Prometheus collector
*/
protected abstract Collector createCollector(final String help, final String... labelNames);

/**
* Gets the identifier for this collector.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
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;

Expand All @@ -43,17 +42,14 @@ public PrometheusSuppliedSummary(
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();
super(category, name);
this.collector =
SummaryWithCallback.builder()
.name(name)
.help(help)
.labelNames(labelNames)
.callback(this::callback)
.build();
}

private void callback(final SummaryWithCallback.Callback callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,25 @@

import io.prometheus.metrics.core.datapoints.DistributionDataPoint;
import io.prometheus.metrics.core.metrics.Summary;
import io.prometheus.metrics.model.registry.Collector;

/**
* An implementation of Besu timer backed by a Prometheus summary. The summary provides a total
* count of durations and a sum of all observed durations, it calculates configurable quantiles over
* a sliding time window.
*/
class PrometheusTimer extends AbstractPrometheusSummary implements LabelledMetric<OperationTimer> {
private final Map<Double, Double> quantiles;

public PrometheusTimer(
final MetricCategory category,
final String name,
final String help,
final Map<Double, Double> quantiles,
final String... labelNames) {
super(category, name, help, labelNames);
this.quantiles = quantiles;
}

@Override
protected Collector createCollector(final String help, final String... labelNames) {
super(category, name);
final var summaryBuilder =
Summary.builder().name(this.prefixedName).help(help).labelNames(labelNames);
quantiles.forEach(summaryBuilder::quantile);
return summaryBuilder.build();
this.collector = summaryBuilder.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,15 @@ public void shouldOnlyObserveEnabledMetrics() throws InterruptedException {
final LabelledMetric<Counter> counterN =
localMetricSystem.createLabelledCounter(
NETWORK, "ABC", "Not that kind of network", "show");
assertThat(counterN).isSameAs(NoOpMetricsSystem.NO_OP_LABELLED_1_COUNTER);
assertThat(counterN).isInstanceOf(NoOpMetricsSystem.LabelCountingNoOpMetric.class);

counterN.labels("show").inc();
assertThat(localMetricSystem.streamObservations()).isEmpty();

// do a category we are watching
final LabelledMetric<Counter> counterR =
localMetricSystem.createLabelledCounter(RPC, "name", "Not useful", "method");
assertThat(counterR).isNotSameAs(NoOpMetricsSystem.NO_OP_LABELLED_1_COUNTER);
assertThat(counterR).isNotInstanceOf(NoOpMetricsSystem.LabelCountingNoOpMetric.class);

counterR.labels("op").inc();
assertThat(getObservation(localMetricSystem))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,15 @@ public void shouldOnlyObserveEnabledMetrics() {
// do a category we are not watching
final LabelledMetric<Counter> counterN =
localMetricSystem.createLabelledCounter(NETWORK, "ABC", "Not that kind of network", "show");
assertThat(counterN).isSameAs(NoOpMetricsSystem.NO_OP_LABELLED_1_COUNTER);
assertThat(counterN).isInstanceOf(NoOpMetricsSystem.LabelCountingNoOpMetric.class);

counterN.labels("show").inc();
assertThat(localMetricSystem.streamObservations()).isEmpty();

// do a category we are watching
final LabelledMetric<Counter> counterR =
localMetricSystem.createLabelledCounter(RPC, "name", "Not useful", "method");
assertThat(counterR).isNotSameAs(NoOpMetricsSystem.NO_OP_LABELLED_1_COUNTER);
assertThat(counterR).isNotInstanceOf(NoOpMetricsSystem.LabelCountingNoOpMetric.class);

counterR.labels("op").inc();
assertThat(localMetricSystem.streamObservations())
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 = '5YqXajl5nrSGiQcU9PXzAvmSE8q65pMtIg4ejSZ3meI='
knownHash = 'sLvLKRbqxri7hovTEcpiyF9eV4WnF8YZ1eGo5GF7mYw='
}
check.dependsOn('checkAPIChanges')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ default void createSummary(
final Supplier<ExternalSummary> summarySupplier) {
createLabelledSuppliedSummary(category, name, help).labels(summarySupplier);
}
;

/**
* Collect metrics from Guava cache.
Expand Down

0 comments on commit 1c2da80

Please sign in to comment.