From 0cbcd919ebb123cdccdb19a8f8b15df3fc62c2d7 Mon Sep 17 00:00:00 2001 From: Fabio Di Fabio Date: Thu, 28 Nov 2024 22:50:57 +0100 Subject: [PATCH] Shutdown MetricsSystem when stopping MetricsService (#7958) Signed-off-by: Fabio Di Fabio --- .../hyperledger/besu/metrics/MetricsService.java | 6 ++++-- .../opentelemetry/MetricsOtelPushService.java | 14 +++++++++++--- .../metrics/prometheus/MetricsHttpService.java | 1 + .../prometheus/MetricsPushGatewayService.java | 11 ++++------- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/MetricsService.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/MetricsService.java index 7eb37061503..ddeaa31c3b1 100644 --- a/metrics/core/src/main/java/org/hyperledger/besu/metrics/MetricsService.java +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/MetricsService.java @@ -15,6 +15,7 @@ package org.hyperledger.besu.metrics; import org.hyperledger.besu.metrics.opentelemetry.MetricsOtelPushService; +import org.hyperledger.besu.metrics.opentelemetry.OpenTelemetrySystem; import org.hyperledger.besu.metrics.prometheus.MetricsConfiguration; import org.hyperledger.besu.metrics.prometheus.MetricsHttpService; import org.hyperledger.besu.metrics.prometheus.MetricsPushGatewayService; @@ -48,13 +49,14 @@ static Optional create( return Optional.of( new MetricsHttpService(configuration, (PrometheusMetricsSystem) metricsSystem)); } else if (configuration.isPushEnabled()) { - return Optional.of(new MetricsPushGatewayService(configuration, metricsSystem)); + return Optional.of( + new MetricsPushGatewayService(configuration, (PrometheusMetricsSystem) metricsSystem)); } else { return Optional.empty(); } } else if (configuration.getProtocol() == MetricsProtocol.OPENTELEMETRY) { if (configuration.isEnabled()) { - return Optional.of(new MetricsOtelPushService()); + return Optional.of(new MetricsOtelPushService((OpenTelemetrySystem) metricsSystem)); } else { return Optional.empty(); } diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/MetricsOtelPushService.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/MetricsOtelPushService.java index 7b7c1850674..1a48fd33a96 100644 --- a/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/MetricsOtelPushService.java +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/MetricsOtelPushService.java @@ -26,9 +26,16 @@ public class MetricsOtelPushService implements MetricsService { private static final Logger LOG = LoggerFactory.getLogger(MetricsOtelPushService.class); - - /** Instantiates a new Metrics open telemetry push service. */ - public MetricsOtelPushService() {} + private final OpenTelemetrySystem metricsSystem; + + /** + * Instantiates a new Metrics open telemetry push service. + * + * @param metricsSystem The OpenTelemetry metrics system + */ + public MetricsOtelPushService(final OpenTelemetrySystem metricsSystem) { + this.metricsSystem = metricsSystem; + } @Override public CompletableFuture start() { @@ -39,6 +46,7 @@ public CompletableFuture start() { @Override public CompletableFuture stop() { + metricsSystem.shutdown(); return CompletableFuture.completedFuture(null); } diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/MetricsHttpService.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/MetricsHttpService.java index 48045e4415b..71135c44d13 100644 --- a/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/MetricsHttpService.java +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/MetricsHttpService.java @@ -121,6 +121,7 @@ private boolean hostIsInAllowlist(final String hostHeader) { @Override public CompletableFuture stop() { + metricsSystem.shutdown(); if (httpServer == null) { return CompletableFuture.completedFuture(null); } diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/MetricsPushGatewayService.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/MetricsPushGatewayService.java index ad60a2fd203..e992735fd7c 100644 --- a/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/MetricsPushGatewayService.java +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/MetricsPushGatewayService.java @@ -17,7 +17,6 @@ import static com.google.common.base.Preconditions.checkArgument; import org.hyperledger.besu.metrics.MetricsService; -import org.hyperledger.besu.plugin.services.MetricsSystem; import java.io.IOException; import java.util.Optional; @@ -37,7 +36,7 @@ public class MetricsPushGatewayService implements MetricsService { private PushGateway pushGateway; private ScheduledExecutorService scheduledExecutorService; private final MetricsConfiguration config; - private final MetricsSystem metricsSystem; + private final PrometheusMetricsSystem metricsSystem; /** * Instantiates a new Metrics push gateway service. @@ -46,7 +45,7 @@ public class MetricsPushGatewayService implements MetricsService { * @param metricsSystem the metrics system */ public MetricsPushGatewayService( - final MetricsConfiguration configuration, final MetricsSystem metricsSystem) { + final MetricsConfiguration configuration, final PrometheusMetricsSystem metricsSystem) { this.metricsSystem = metricsSystem; validateConfig(configuration); config = configuration; @@ -59,9 +58,6 @@ private void validateConfig(final MetricsConfiguration config) { checkArgument( !(config.isEnabled() && config.isPushEnabled()), "Metrics Push Gateway Service cannot run concurrent with the normal metrics."); - checkArgument( - metricsSystem instanceof PrometheusMetricsSystem, - "Push Gateway requires a Prometheus Metrics System."); } @Override @@ -73,7 +69,7 @@ public CompletableFuture start() { pushGateway = PushGateway.builder() - .registry(((PrometheusMetricsSystem) metricsSystem).getRegistry()) + .registry(metricsSystem.getRegistry()) .address(config.getPushHost() + ":" + config.getPushPort()) .job(config.getPrometheusJob()) .build(); @@ -90,6 +86,7 @@ public CompletableFuture start() { @Override public CompletableFuture stop() { + metricsSystem.shutdown(); final CompletableFuture resultFuture = new CompletableFuture<>(); try { // Calling shutdown now cancels the pending push, which is desirable.