From 9b6389a5bf4506646329f3049406e33e72f06747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20F=C4=85derski?= Date: Tue, 29 Aug 2023 13:43:39 +0200 Subject: [PATCH] Add code refactor --- .../prometheus/PrometheusMetricsProvider.java | 9 ++---- .../RestTemplatePrometheusClient.java | 30 ++++++++++++------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/hermes-management/src/main/java/pl/allegro/tech/hermes/management/infrastructure/prometheus/PrometheusMetricsProvider.java b/hermes-management/src/main/java/pl/allegro/tech/hermes/management/infrastructure/prometheus/PrometheusMetricsProvider.java index 41f1cf6495..76ec258c2f 100644 --- a/hermes-management/src/main/java/pl/allegro/tech/hermes/management/infrastructure/prometheus/PrometheusMetricsProvider.java +++ b/hermes-management/src/main/java/pl/allegro/tech/hermes/management/infrastructure/prometheus/PrometheusMetricsProvider.java @@ -7,9 +7,6 @@ import pl.allegro.tech.hermes.management.infrastructure.metrics.MonitoringTopicMetricsProvider; import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; - public class PrometheusMetricsProvider implements MonitoringSubscriptionMetricsProvider, MonitoringTopicMetricsProvider { @@ -38,11 +35,9 @@ public PrometheusMetricsProvider(PrometheusClient prometheusClient, String consu this.prometheusClient = prometheusClient; this.consumersMetricsPrefix = consumersMetricsPrefix.isEmpty() ? "" : consumersMetricsPrefix + "_"; this.frontendMetricsPrefix = frontendMetricsPrefix.isEmpty() ? "" : frontendMetricsPrefix + "_"; - this.subscriptionMetricsToQuery = Stream.of(SUBSCRIPTION_DELIVERED, SUBSCRIPTION_TIMEOUTS, + this.subscriptionMetricsToQuery = String.join("|", List.of(SUBSCRIPTION_DELIVERED, SUBSCRIPTION_TIMEOUTS, SUBSCRIPTION_THROUGHPUT, SUBSCRIPTION_OTHER_ERRORS, SUBSCRIPTION_BATCHES, - SUBSCRIPTION_STATUS_CODES) - .map(this::consumerMetricName) - .collect(Collectors.joining("|")); + SUBSCRIPTION_STATUS_CODES)); this.topicMetricsToQuery = String.join("|", List.of( frontendMetricName(TOPIC_RATE), consumerMetricName(TOPIC_DELIVERY_RATE), diff --git a/hermes-management/src/main/java/pl/allegro/tech/hermes/management/infrastructure/prometheus/RestTemplatePrometheusClient.java b/hermes-management/src/main/java/pl/allegro/tech/hermes/management/infrastructure/prometheus/RestTemplatePrometheusClient.java index 6db0df643c..6dd658c699 100644 --- a/hermes-management/src/main/java/pl/allegro/tech/hermes/management/infrastructure/prometheus/RestTemplatePrometheusClient.java +++ b/hermes-management/src/main/java/pl/allegro/tech/hermes/management/infrastructure/prometheus/RestTemplatePrometheusClient.java @@ -18,6 +18,7 @@ import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; +import java.util.stream.Stream; import static java.net.URLEncoder.encode; @@ -27,7 +28,6 @@ public class RestTemplatePrometheusClient implements PrometheusClient { private static final Logger logger = LoggerFactory.getLogger(RestTemplatePrometheusClient.class); private final URI prometheusUri; - private final RestTemplate restTemplate; public RestTemplatePrometheusClient(RestTemplate restTemplate, URI prometheusUri) { @@ -38,18 +38,11 @@ public RestTemplatePrometheusClient(RestTemplate restTemplate, URI prometheusUri @Override public MonitoringMetricsContainer readMetrics(String query) { try { - MonitoringMetricsContainer metricsContainer = MonitoringMetricsContainer.createEmpty(); PrometheusResponse response = queryPrometheus(query); Preconditions.checkState(response.isSuccess(), "Prometheus response does not contain valid data"); - Map> metricsGroupedByName = response.data().results().stream() - .map(RestTemplatePrometheusClient::remapNames) - .collect(Collectors.groupingBy(r -> r.metricName().name())); - metricsGroupedByName.entrySet().stream() - .map(RestTemplatePrometheusClient::sumResults) - .forEach(pair -> metricsContainer.addMetricValue(pair.getKey(), - MetricDecimalValue.of(pair.getValue().toString()))); - return metricsContainer; + Map> metricsGroupedByName = groupMetricsByName(response); + return produceMetricsContainer(metricsGroupedByName); } catch (Exception exception) { logger.warn("Unable to read from Prometheus...", exception); return MonitoringMetricsContainer.unavailable(); @@ -66,6 +59,23 @@ private PrometheusResponse queryPrometheus(String query) { return response.getBody(); } + private static Map> groupMetricsByName(PrometheusResponse response) { + return response.data().results().stream() + .map(RestTemplatePrometheusClient::remapNames) + .collect(Collectors.groupingBy(r -> r.metricName().name())); + } + + private static MonitoringMetricsContainer produceMetricsContainer(Map> metricsGroupedByName) { + MonitoringMetricsContainer metricsContainer = MonitoringMetricsContainer.createEmpty(); + + Stream> metricsSummedByName = metricsGroupedByName.entrySet().stream() + .map(RestTemplatePrometheusClient::sumResults); + metricsSummedByName.forEach(pair -> metricsContainer.addMetricValue( + pair.getKey(), + MetricDecimalValue.of(pair.getValue().toString()))); + return metricsContainer; + } + private static PrometheusResponse.Result remapNames(PrometheusResponse.Result r) { String suffix = ""; if (r.metricName().is2xxStatusCode()) {