From cd1d2e50e9061987a686689236e7ab13c43b7996 Mon Sep 17 00:00:00 2001 From: "maciej.moscicki" Date: Tue, 6 Aug 2024 13:33:05 +0200 Subject: [PATCH] register gauge based on state object --- .../tech/hermes/common/metric/ConsistencyMetrics.java | 8 +++++--- .../domain/consistency/DcConsistencyService.java | 7 ++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/hermes-common/src/main/java/pl/allegro/tech/hermes/common/metric/ConsistencyMetrics.java b/hermes-common/src/main/java/pl/allegro/tech/hermes/common/metric/ConsistencyMetrics.java index fac81b07b5..4324ec0e68 100644 --- a/hermes-common/src/main/java/pl/allegro/tech/hermes/common/metric/ConsistencyMetrics.java +++ b/hermes-common/src/main/java/pl/allegro/tech/hermes/common/metric/ConsistencyMetrics.java @@ -2,16 +2,18 @@ import io.micrometer.core.instrument.MeterRegistry; +import java.util.function.ToDoubleFunction; + public class ConsistencyMetrics { private final MeterRegistry meterRegistry; ConsistencyMetrics(MeterRegistry meterRegistry) { this.meterRegistry = meterRegistry; + } - public void registerStorageConsistencyGauge(boolean isStorageConsistent) { - double value = isStorageConsistent ? 1 : 0; - meterRegistry.gauge("storage.consistency", value); + public void registerStorageConsistencyGauge(T stateObject, ToDoubleFunction valueFunction) { + meterRegistry.gauge("storage.consistency", stateObject, valueFunction); } } diff --git a/hermes-management/src/main/java/pl/allegro/tech/hermes/management/domain/consistency/DcConsistencyService.java b/hermes-management/src/main/java/pl/allegro/tech/hermes/management/domain/consistency/DcConsistencyService.java index e76a25b864..9218362f0c 100644 --- a/hermes-management/src/main/java/pl/allegro/tech/hermes/management/domain/consistency/DcConsistencyService.java +++ b/hermes-management/src/main/java/pl/allegro/tech/hermes/management/domain/consistency/DcConsistencyService.java @@ -36,6 +36,7 @@ import java.util.concurrent.Future; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Function; import static java.util.Collections.emptyList; @@ -52,7 +53,7 @@ public class DcConsistencyService { private final List> topicRepositories; private final List> subscriptionRepositories; private final ObjectMapper objectMapper; - private final MetricsFacade metricsFacade; + private final AtomicBoolean isStorageConsistent = new AtomicBoolean(true); public DcConsistencyService(RepositoryManager repositoryManager, ObjectMapper objectMapper, @@ -62,7 +63,6 @@ public DcConsistencyService(RepositoryManager repositoryManager, this.topicRepositories = repositoryManager.getRepositories(TopicRepository.class); this.subscriptionRepositories = repositoryManager.getRepositories(SubscriptionRepository.class); this.objectMapper = objectMapper; - this.metricsFacade = metricsFacade; this.executor = Executors.newFixedThreadPool( properties.getThreadPoolSize(), new ThreadFactoryBuilder() @@ -79,6 +79,7 @@ public DcConsistencyService(RepositoryManager repositoryManager, properties.getInitialRefreshDelay().getSeconds(), properties.getRefreshInterval().getSeconds(), TimeUnit.SECONDS); + metricsFacade.consistency().registerStorageConsistencyGauge(isStorageConsistent, isConsistent -> isConsistent.get() ? 1 : 0); } } @@ -94,7 +95,7 @@ private void reportConsistency() { List inconsistentGroups = listInconsistentGroups(groups); long durationSeconds = (System.currentTimeMillis() - start) / 1000; logger.info("Consistency check finished in {}s, number of inconsistent groups: {}", durationSeconds, inconsistentGroups.size()); - metricsFacade.consistency().registerStorageConsistencyGauge(inconsistentGroups.isEmpty()); + isStorageConsistent.set(inconsistentGroups.isEmpty()); } public List listInconsistentGroups(Set groupNames) {