Skip to content

Commit

Permalink
Upgrade Promethus lib to 1.x and adapt the code to the new version
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <[email protected]>

# Conflicts:
#	metrics/core/build.gradle
#	metrics/core/src/main/java/org/hyperledger/besu/metrics/noop/NoOpMetricsSystem.java
#	metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetrySystem.java
#	metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusMetricsSystem.java
#	metrics/rocksdb/src/main/java/org/hyperledger/besu/metrics/rocksdb/RocksDBStats.java
#	plugin-api/build.gradle
#	plugin-api/src/main/java/org/hyperledger/besu/plugin/services/MetricsSystem.java
  • Loading branch information
fab-10 committed Nov 13, 2024
1 parent c4a31c8 commit 52258c5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,18 @@ public interface PrometheusCollector {
*/
String getName();

/** Get the native Prometheus collector */
/**
* Register this collector to the specified registry
*
* @param registry the registry
*/
void register(final PrometheusRegistry registry);

/**
* Unregister this collector from the specified registry
*
* @param registry the registry
*/
void unregister(final PrometheusRegistry registry);

/**
Expand All @@ -57,6 +66,13 @@ static List<String> getLabelValues(final Labels labels) {
return labels.stream().map(Label::getValue).toList();
}

/**
* Add new values to an existing list of label values
*
* @param labelValues existing list of label values
* @param values the values to add
* @return a new list with new values appended to the original list
*/
static List<String> addLabelValues(final List<String> labelValues, final String... values) {
final var newList = new ArrayList<String>(labelValues.size() + values.length);
newList.addAll(labelValues);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@

class PrometheusGuavaCache extends CategorizedPrometheusCollector {
private static final String NAME = "__guavaCacheMetricsCollector__";
private static final CacheMetricsCollector cacheMetricsCollector = new CacheMetricsCollector();
private static final Set<String> cacheNames = new ConcurrentHashSet<>();
private static final AtomicBoolean collectorRegistered = new AtomicBoolean(false);

private final Cache<?, ?> cache;
private final Context context;

public PrometheusGuavaCache(
final MetricCategory category, final String name, final Cache<?, ?> cache) {
final MetricCategory category,
final Context context,
final String name,
final Cache<?, ?> cache) {
super(category, NAME);
if (cacheNames.contains(name)) {
if (context.alreadyExists(name)) {
throw new IllegalStateException("Cache already registered: " + name);
}
cacheNames.add(name);
this.cache = cache;
this.context = context;
}

@Override
Expand All @@ -51,23 +52,49 @@ public String getName() {

@Override
public void register(final PrometheusRegistry registry) {
cacheMetricsCollector.addCache(name, cache);
if (collectorRegistered.compareAndSet(false, true)) {
registry.register(cacheMetricsCollector);
}
context.registerCache(registry, name, cache);
}

@Override
public void unregister(final PrometheusRegistry registry) {
cacheMetricsCollector.removeCache(name);
cacheNames.remove(name);
if (cacheNames.isEmpty() && collectorRegistered.compareAndSet(true, false)) {
registry.unregister(cacheMetricsCollector);
}
context.unregisterCache(registry, name);
}

@Override
public Stream<Observation> streamObservations() {
return Stream.empty();
}

static class Context {
private final CacheMetricsCollector cacheMetricsCollector = new CacheMetricsCollector();
private final Set<String> cacheNames = new ConcurrentHashSet<>();
private final AtomicBoolean collectorRegistered = new AtomicBoolean(false);

boolean alreadyExists(final String name) {
return cacheNames.contains(name);
}

void registerCache(
final PrometheusRegistry registry, final String name, final Cache<?, ?> cache) {
cacheMetricsCollector.addCache(name, cache);
cacheNames.add(name);
if (collectorRegistered.compareAndSet(false, true)) {
registry.register(cacheMetricsCollector);
}
}

void unregisterCache(final PrometheusRegistry registry, final String name) {
cacheMetricsCollector.removeCache(name);
cacheNames.remove(name);
if (cacheNames.isEmpty() && collectorRegistered.compareAndSet(true, false)) {
registry.unregister(cacheMetricsCollector);
}
}

public void clear() {
cacheNames.forEach(cacheMetricsCollector::removeCache);
cacheNames.clear();
collectorRegistered.set(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@

import com.google.common.cache.Cache;
import com.google.common.collect.ImmutableSet;
import io.prometheus.metrics.instrumentation.guava.CacheMetricsCollector;
import io.prometheus.metrics.instrumentation.jvm.JvmBufferPoolMetrics;
import io.prometheus.metrics.instrumentation.jvm.JvmClassLoadingMetrics;
import io.prometheus.metrics.instrumentation.jvm.JvmCompilationMetrics;
Expand Down Expand Up @@ -69,9 +68,8 @@ public class PrometheusMetricsSystem implements ObservableMetricsSystem {
cachedCounters = new ConcurrentHashMap<>();
private final Map<String, LabelledMetric<OperationTimer>> cachedTimers =
new ConcurrentHashMap<>();
private final Map<MetricCategory, CacheMetricsCollector> guavaCacheCollectors =
new ConcurrentHashMap<>();
private final Set<String> guavaCacheNames = new ConcurrentHashSet<>();
private final PrometheusGuavaCache.Context guavaCacheCollectorContext =
new PrometheusGuavaCache.Context();

private final Set<MetricCategory> enabledCategories;
private final boolean timersEnabled;
Expand Down Expand Up @@ -213,11 +211,8 @@ public LabelledGauge createLabelledGauge(
public void createGuavaCacheCollector(
final MetricCategory category, final String name, final Cache<?, ?> cache) {
if (isCategoryEnabled(category)) {
if (guavaCacheNames.contains(name)) {
throw new IllegalStateException("Cache already registered: " + name);
}
guavaCacheNames.add(name);
final var cacheCollector = new PrometheusGuavaCache(category, name, cache);
final var cacheCollector =
new PrometheusGuavaCache(category, guavaCacheCollectorContext, name, cache);
registerCollector(category, cacheCollector);
}
}
Expand Down Expand Up @@ -262,7 +257,6 @@ public void shutdown() {
collectors.clear();
cachedCounters.clear();
cachedTimers.clear();
guavaCacheCollectors.clear();
guavaCacheNames.clear();
guavaCacheCollectorContext.clear();
}
}
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 = '/uVL++40w0GJqDavP8+z3Pv8qIdA+G9jvj4os2JvQ/4='
knownHash = 'aYWbsgPoKTGDgq9d4QUBvQEaZYbKNJGMiBufzyKnusA='
}
check.dependsOn('checkAPIChanges')

Expand Down

0 comments on commit 52258c5

Please sign in to comment.