Skip to content

Commit

Permalink
Create a unique key for cached metric maps
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 5a577d2 commit 3bcfd84
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.DoubleSupplier;
import java.util.function.Supplier;
import java.util.stream.Stream;

Expand Down Expand Up @@ -65,9 +64,9 @@ public class PrometheusMetricsSystem implements ObservableMetricsSystem {
private final Map<MetricCategory, Collection<PrometheusCollector>> collectors =
new ConcurrentHashMap<>();
private final PrometheusRegistry registry = PrometheusRegistry.defaultRegistry;
private final Map<String, LabelledMetric<org.hyperledger.besu.plugin.services.metrics.Counter>>
cachedCounters = new ConcurrentHashMap<>();
private final Map<String, LabelledMetric<OperationTimer>> cachedTimers =
private final Map<CachedMetricKey, LabelledMetric<Counter>> cachedCounters =
new ConcurrentHashMap<>();
private final Map<CachedMetricKey, LabelledMetric<OperationTimer>> cachedTimers =
new ConcurrentHashMap<>();
private final PrometheusGuavaCache.Context guavaCacheCollectorContext =
new PrometheusGuavaCache.Context();
Expand Down Expand Up @@ -117,15 +116,14 @@ public LabelledMetric<Counter> createLabelledCounter(
final String help,
final String... labelNames) {
return cachedCounters.computeIfAbsent(
name,
(k) -> {
CachedMetricKey.of(category, name),
k -> {
if (isCategoryEnabled(category)) {
final var counter = new PrometheusCounter(category, name, help, labelNames);
registerCollector(category, counter);
return counter;
} else {
return NoOpMetricsSystem.getCounterLabelledMetric(labelNames.length);
}
return NoOpMetricsSystem.getCounterLabelledMetric(labelNames.length);
});
}

Expand All @@ -136,16 +134,15 @@ public LabelledMetric<OperationTimer> createLabelledTimer(
final String help,
final String... labelNames) {
return cachedTimers.computeIfAbsent(
name,
(k) -> {
CachedMetricKey.of(category, name),
k -> {
if (timersEnabled && isCategoryEnabled(category)) {
final var summary =
new PrometheusTimer(category, name, help, DEFAULT_SUMMARY_QUANTILES, labelNames);
registerCollector(category, summary);
return summary;
} else {
return NoOpMetricsSystem.getOperationTimerLabelledMetric(labelNames.length);
}
return NoOpMetricsSystem.getOperationTimerLabelledMetric(labelNames.length);
});
}

Expand All @@ -156,32 +153,18 @@ public LabelledMetric<OperationTimer> createSimpleLabelledTimer(
final String help,
final String... labelNames) {
return cachedTimers.computeIfAbsent(
name,
(k) -> {
CachedMetricKey.of(category, name),
k -> {
if (timersEnabled && isCategoryEnabled(category)) {
final var histogram =
new PrometheusSimpleTimer(category, name, help, new double[] {1D}, labelNames);
registerCollector(category, histogram);
return histogram;
} else {
return NoOpMetricsSystem.getOperationTimerLabelledMetric(labelNames.length);
}
return NoOpMetricsSystem.getOperationTimerLabelledMetric(labelNames.length);
});
}

@Override
public void createGauge(
final MetricCategory category,
final String name,
final String help,
final DoubleSupplier valueSupplier) {
if (isCategoryEnabled(category)) {
final var gauge = new PrometheusSuppliedGauge(category, name, help);
gauge.labels(valueSupplier);
registerCollector(category, gauge);
}
}

@Override
public void trackExternalSummary(
final MetricCategory category,
Expand Down Expand Up @@ -278,4 +261,10 @@ public void shutdown() {
cachedTimers.clear();
guavaCacheCollectorContext.clear();
}

private record CachedMetricKey(MetricCategory category, String name) {
static CachedMetricKey of(final MetricCategory category, final String name) {
return new CachedMetricKey(category, name);
}
}
}
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 = 'vI9RTAGD6sRzAaMp1BMJY7x4cB3wh7FA4rYvDGYRceg='
knownHash = '8X9cB9p6OUuOyCMC2ezwb6kJwWfz0/5vk/UXqSyad8o='
}
check.dependsOn('checkAPIChanges')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,13 @@ LabelledMetric<OperationTimer> createSimpleLabelledTimer(
* @param help A human readable description of the metric.
* @param valueSupplier A supplier for the double value to be presented.
*/
void createGauge(MetricCategory category, String name, String help, DoubleSupplier valueSupplier);
default void createGauge(
final MetricCategory category,
final String name,
final String help,
final DoubleSupplier valueSupplier) {
createLabelledSuppliedGauge(category, name, help).labels(valueSupplier);
}

/**
* Creates a gauge for displaying integer values.
Expand Down

0 comments on commit 3bcfd84

Please sign in to comment.