Skip to content

Commit

Permalink
Add code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
faderskd committed Aug 29, 2023
1 parent a49f369 commit 9b6389a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -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<String, List<PrometheusResponse.Result>> 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<String, List<PrometheusResponse.Result>> metricsGroupedByName = groupMetricsByName(response);
return produceMetricsContainer(metricsGroupedByName);
} catch (Exception exception) {
logger.warn("Unable to read from Prometheus...", exception);
return MonitoringMetricsContainer.unavailable();
Expand All @@ -66,6 +59,23 @@ private PrometheusResponse queryPrometheus(String query) {
return response.getBody();
}

private static Map<String, List<PrometheusResponse.Result>> groupMetricsByName(PrometheusResponse response) {
return response.data().results().stream()
.map(RestTemplatePrometheusClient::remapNames)
.collect(Collectors.groupingBy(r -> r.metricName().name()));
}

private static MonitoringMetricsContainer produceMetricsContainer(Map<String, List<PrometheusResponse.Result>> metricsGroupedByName) {
MonitoringMetricsContainer metricsContainer = MonitoringMetricsContainer.createEmpty();

Stream<Pair<String, Double>> 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()) {
Expand Down

0 comments on commit 9b6389a

Please sign in to comment.