Skip to content

Commit

Permalink
Try histogram
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 Oct 28, 2024
1 parent 0148407 commit e659f4a
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
import org.hyperledger.besu.metrics.ObservableMetricsSystem;
import org.hyperledger.besu.metrics.Observation;
import org.hyperledger.besu.plugin.services.metrics.Counter;
import org.hyperledger.besu.plugin.services.metrics.Histogram;
import org.hyperledger.besu.plugin.services.metrics.LabelledGauge;
import org.hyperledger.besu.plugin.services.metrics.LabelledMetric;
import org.hyperledger.besu.plugin.services.metrics.MetricCategory;
import org.hyperledger.besu.plugin.services.metrics.OperationTimer;
import org.hyperledger.besu.plugin.services.metrics.Summary;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -46,7 +46,8 @@ public class NoOpMetricsSystem implements ObservableMetricsSystem {
/** The constant NO_OP_OPERATION_TIMER. */
public static final OperationTimer NO_OP_OPERATION_TIMER = () -> NO_OP_TIMING_CONTEXT;

public static final Summary NO_OP_SUMMARY = d -> {};
/** The constant NO_OP_HISTOGRAM. */
public static final Histogram NO_OP_HISTOGRAM = d -> {};

/** The constant NO_OP_LABELLED_1_COUNTER. */
public static final LabelledMetric<Counter> NO_OP_LABELLED_1_COUNTER =
Expand All @@ -64,8 +65,9 @@ public class NoOpMetricsSystem implements ObservableMetricsSystem {
public static final LabelledMetric<OperationTimer> NO_OP_LABELLED_1_OPERATION_TIMER =
new LabelCountingNoOpMetric<>(1, NO_OP_OPERATION_TIMER);

public static final LabelledMetric<Summary> NO_OP_LABELLED_1_SUMMARY =
new LabelCountingNoOpMetric<>(1, NO_OP_SUMMARY);
/** The constant NO_OP_LABELLED_1_HISTOGRAM. */
public static final LabelledMetric<Histogram> NO_OP_LABELLED_1_HISTOGRAM =
new LabelCountingNoOpMetric<>(1, NO_OP_HISTOGRAM);

/** The constant NO_OP_LABELLED_1_GAUGE. */
public static final LabelledGauge NO_OP_LABELLED_1_GAUGE =
Expand Down Expand Up @@ -149,11 +151,11 @@ public static LabelledMetric<OperationTimer> getOperationTimerLabelledMetric(
* @param labelCount the label count
* @return the operation timer labelled metric
*/
public static LabelledMetric<Summary> getSummaryLabelledMetric(final int labelCount) {
public static LabelledMetric<Histogram> getSummaryLabelledMetric(final int labelCount) {
if (labelCount == 1) {
return NO_OP_LABELLED_1_SUMMARY;
return NO_OP_LABELLED_1_HISTOGRAM;
} else {
return new LabelCountingNoOpMetric<>(labelCount, NO_OP_SUMMARY);
return new LabelCountingNoOpMetric<>(labelCount, NO_OP_HISTOGRAM);
}
}

Expand All @@ -165,10 +167,11 @@ public void createGauge(
final DoubleSupplier valueSupplier) {}

@Override
public LabelledMetric<Summary> createLabelledSummary(
public LabelledMetric<Histogram> createLabelledHistogram(
final MetricCategory category,
final String name,
final String help,
final double[] buckets,
final String... labelNames) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
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.Histogram;
import org.hyperledger.besu.plugin.services.metrics.LabelledGauge;
import org.hyperledger.besu.plugin.services.metrics.LabelledMetric;
import org.hyperledger.besu.plugin.services.metrics.MetricCategory;
import org.hyperledger.besu.plugin.services.metrics.OperationTimer;
import org.hyperledger.besu.plugin.services.metrics.Summary;

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
Expand Down Expand Up @@ -279,10 +279,11 @@ public void createGauge(
}

@Override
public LabelledMetric<Summary> createLabelledSummary(
public LabelledMetric<Histogram> createLabelledHistogram(
final MetricCategory category,
final String name,
final String help,
final double[] buckets,
final String... labelNames) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@
*/
package org.hyperledger.besu.metrics.prometheus;

import org.hyperledger.besu.plugin.services.metrics.Histogram;
import org.hyperledger.besu.plugin.services.metrics.LabelledMetric;
import org.hyperledger.besu.plugin.services.metrics.Summary;

class PrometheusSummary implements LabelledMetric<Summary> {
class PrometheusHistogram implements LabelledMetric<Histogram> {

private final io.prometheus.client.Summary summary;
private final io.prometheus.client.Histogram histogram;

public PrometheusSummary(final io.prometheus.client.Summary summary) {
this.summary = summary;
public PrometheusHistogram(final io.prometheus.client.Histogram histogram) {
this.histogram = histogram;
}

@Override
public Summary labels(final String... labels) {
return new UnlabelledSummary(summary.labels(labels));
public Histogram labels(final String... labels) {
return new UnlabelledHistogram(histogram.labels(labels));
}

private static class UnlabelledSummary implements Summary {
private final io.prometheus.client.Summary.Child amount;
private static class UnlabelledHistogram implements Histogram {
private final io.prometheus.client.Histogram.Child amount;

private UnlabelledSummary(final io.prometheus.client.Summary.Child amount) {
private UnlabelledHistogram(final io.prometheus.client.Histogram.Child amount) {
this.amount = amount;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public class PrometheusMetricsSystem implements ObservableMetricsSystem {
cachedCounters = new ConcurrentHashMap<>();
private final Map<String, LabelledMetric<OperationTimer>> cachedTimers =
new ConcurrentHashMap<>();
private final Map<String, LabelledMetric<org.hyperledger.besu.plugin.services.metrics.Summary>>
cachedSummaries = new ConcurrentHashMap<>();
private final Map<String, LabelledMetric<org.hyperledger.besu.plugin.services.metrics.Histogram>>
cachedHistograms = new ConcurrentHashMap<>();
private final Set<String> totalSuffixedCounters = new ConcurrentHashSet<>();

private final Set<MetricCategory> enabledCategories;
Expand Down Expand Up @@ -114,31 +114,22 @@ public LabelledMetric<org.hyperledger.besu.plugin.services.metrics.Counter> crea
}

@Override
public LabelledMetric<org.hyperledger.besu.plugin.services.metrics.Summary> createLabelledSummary(
final MetricCategory category,
final String name,
final String help,
final String... labelNames) {
public LabelledMetric<org.hyperledger.besu.plugin.services.metrics.Histogram>
createLabelledHistogram(
final MetricCategory category,
final String name,
final String help,
final double[] buckets,
final String... labelNames) {
final String metricName = convertToPrometheusName(category, name);
return cachedSummaries.computeIfAbsent(
return cachedHistograms.computeIfAbsent(
metricName,
k -> {
if (isCategoryEnabled(category)) {
final Summary summary =
Summary.build(metricName, help)
.quantile(0.0, 0)
.quantile(0.2, 0.02)
.quantile(0.5, 0.05)
.quantile(0.8, 0.02)
.quantile(0.95, 0.005)
.quantile(0.99, 0.001)
.quantile(1.0, 0)
.labelNames(labelNames)
.ageBuckets(5)
.maxAgeSeconds(2)
.create();
addCollectorUnchecked(category, summary);
return new PrometheusSummary(summary);
final Histogram histogram =
Histogram.build(metricName, help).labelNames(labelNames).buckets(buckets).create();
addCollectorUnchecked(category, histogram);
return new PrometheusHistogram(histogram);
} else {
return NoOpMetricsSystem.getSummaryLabelledMetric(labelNames.length);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import org.hyperledger.besu.plugin.services.metrics.Counter;
import org.hyperledger.besu.plugin.services.metrics.Histogram;
import org.hyperledger.besu.plugin.services.metrics.LabelledGauge;
import org.hyperledger.besu.plugin.services.metrics.LabelledMetric;
import org.hyperledger.besu.plugin.services.metrics.MetricCategory;
import org.hyperledger.besu.plugin.services.metrics.OperationTimer;
import org.hyperledger.besu.plugin.services.metrics.Summary;

import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -95,7 +95,7 @@ public void createGauge(
}

@Override
public LabelledMetric<Summary> createLabelledSummary(
public LabelledMetric<Histogram> createLabelledSummary(
final MetricCategory category,
final String name,
final String help,
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 = '1VIGlJuGiaEVUksIjTTHDt7SIjjJE9+DU8rYk/ze3XM='
knownHash = 'y0LCW10NiQfggaDirEwo8x9DlfbvN21dPajj+pqBy7Q='
}
check.dependsOn('checkAPIChanges')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
package org.hyperledger.besu.plugin.services;

import org.hyperledger.besu.plugin.services.metrics.Counter;
import org.hyperledger.besu.plugin.services.metrics.Histogram;
import org.hyperledger.besu.plugin.services.metrics.LabelledGauge;
import org.hyperledger.besu.plugin.services.metrics.LabelledMetric;
import org.hyperledger.besu.plugin.services.metrics.MetricCategory;
import org.hyperledger.besu.plugin.services.metrics.OperationTimer;
import org.hyperledger.besu.plugin.services.metrics.Summary;

import java.util.function.DoubleSupplier;
import java.util.function.IntSupplier;
Expand Down Expand Up @@ -161,6 +161,6 @@ default void createLongGauge(
createGauge(category, name, help, () -> (double) valueSupplier.getAsLong());
}

LabelledMetric<Summary> createLabelledSummary(
MetricCategory category, String name, String help, String... labelNames);
LabelledMetric<Histogram> createLabelledHistogram(
MetricCategory category, String name, String help, double[] buckets, String... labelNames);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package org.hyperledger.besu.plugin.services.metrics;

public interface Summary {
public interface Histogram {

void observe(double amount);
}

0 comments on commit e659f4a

Please sign in to comment.