From 416c4646606ca87ce83fdccf16cb0ef9b3bb50ca Mon Sep 17 00:00:00 2001 From: Leonid Koftun Date: Thu, 29 Feb 2024 19:11:42 +0100 Subject: [PATCH] Revert "feat: health checks" --- .../java/de/sldk/mc/HealthController.java | 28 -------- .../java/de/sldk/mc/MetricsController.java | 5 +- src/main/java/de/sldk/mc/MetricsServer.java | 10 +-- .../mc/health/ConcurrentHealthChecks.java | 33 ---------- .../java/de/sldk/mc/health/HealthCheck.java | 64 ------------------- .../java/de/sldk/mc/health/HealthChecks.java | 21 ------ .../kotlin/de/sldk/mc/PrometheusExporter.kt | 14 +--- .../mc/exporter/PrometheusExporterTest.java | 19 +----- 8 files changed, 10 insertions(+), 184 deletions(-) delete mode 100644 src/main/java/de/sldk/mc/HealthController.java delete mode 100644 src/main/java/de/sldk/mc/health/ConcurrentHealthChecks.java delete mode 100644 src/main/java/de/sldk/mc/health/HealthCheck.java delete mode 100644 src/main/java/de/sldk/mc/health/HealthChecks.java diff --git a/src/main/java/de/sldk/mc/HealthController.java b/src/main/java/de/sldk/mc/HealthController.java deleted file mode 100644 index a9718ce8..00000000 --- a/src/main/java/de/sldk/mc/HealthController.java +++ /dev/null @@ -1,28 +0,0 @@ -package de.sldk.mc; - -import de.sldk.mc.health.HealthChecks; -import org.eclipse.jetty.http.HttpStatus; -import org.eclipse.jetty.server.Handler; -import org.eclipse.jetty.server.Request; -import org.eclipse.jetty.server.Response; -import org.eclipse.jetty.util.Callback; - -public class HealthController extends Handler.Abstract { - - private final HealthChecks checks; - - private HealthController(final HealthChecks checks) { - this.checks = checks; - } - - public static Handler create(final HealthChecks checks) { - return new HealthController(checks); - } - - @Override - public boolean handle(Request request, Response response, Callback callback) throws Exception { - response.setStatus(checks.isHealthy() ? HttpStatus.OK_200 : HttpStatus.SERVICE_UNAVAILABLE_503); - callback.succeeded(); - return true; - } -} diff --git a/src/main/java/de/sldk/mc/MetricsController.java b/src/main/java/de/sldk/mc/MetricsController.java index 9643be20..029b3e6e 100644 --- a/src/main/java/de/sldk/mc/MetricsController.java +++ b/src/main/java/de/sldk/mc/MetricsController.java @@ -19,13 +19,10 @@ public class MetricsController extends Handler.Abstract { private final MetricRegistry metricRegistry = MetricRegistry.getInstance(); private final PrometheusExporter exporter; - private MetricsController(PrometheusExporter exporter) { + public MetricsController(PrometheusExporter exporter) { this.exporter = exporter; } - public static Handler create(final PrometheusExporter exporter) { - return new MetricsController(exporter); - } @Override public boolean handle(Request request, Response response, Callback callback) throws Exception { diff --git a/src/main/java/de/sldk/mc/MetricsServer.java b/src/main/java/de/sldk/mc/MetricsServer.java index c2ab4de1..b40f553e 100644 --- a/src/main/java/de/sldk/mc/MetricsServer.java +++ b/src/main/java/de/sldk/mc/MetricsServer.java @@ -1,7 +1,6 @@ package de.sldk.mc; import org.eclipse.jetty.http.pathmap.PathSpec; -import de.sldk.mc.health.HealthChecks; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.PathMappingsHandler; import org.eclipse.jetty.server.handler.gzip.GzipHandler; @@ -13,23 +12,20 @@ public class MetricsServer { private final String host; private final int port; private final PrometheusExporter prometheusExporter; - private final HealthChecks healthChecks; private Server server; - public MetricsServer(String host, int port, PrometheusExporter prometheusExporter, HealthChecks healthChecks) { + public MetricsServer(String host, int port, PrometheusExporter prometheusExporter) { this.host = host; this.port = port; this.prometheusExporter = prometheusExporter; - this.healthChecks = healthChecks; - } + } public void start() throws Exception { GzipHandler gzipHandler = new GzipHandler(); var pathMappings = new PathMappingsHandler(); - pathMappings.addMapping(PathSpec.from("/metrics"), MetricsController.create(prometheusExporter)); - pathMappings.addMapping(PathSpec.from("/health"), HealthController.create(healthChecks)); + pathMappings.addMapping(PathSpec.from("/metrics"), new MetricsController(prometheusExporter)); gzipHandler.setHandler(pathMappings); diff --git a/src/main/java/de/sldk/mc/health/ConcurrentHealthChecks.java b/src/main/java/de/sldk/mc/health/ConcurrentHealthChecks.java deleted file mode 100644 index 4767683d..00000000 --- a/src/main/java/de/sldk/mc/health/ConcurrentHealthChecks.java +++ /dev/null @@ -1,33 +0,0 @@ -package de.sldk.mc.health; - -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - -public final class ConcurrentHealthChecks implements HealthChecks { - - private final Set checks; - - private ConcurrentHealthChecks(final Set checks) { - this.checks = checks; - } - - public static HealthChecks create() { - return new ConcurrentHealthChecks(ConcurrentHashMap.newKeySet()); - } - - @Override - public boolean isHealthy() { - for (final HealthCheck check : checks) if (!check.isHealthy()) return false; - return true; - } - - @Override - public void add(final HealthCheck check) { - checks.add(check); - } - - @Override - public void remove(final HealthCheck check) { - checks.remove(check); - } -} diff --git a/src/main/java/de/sldk/mc/health/HealthCheck.java b/src/main/java/de/sldk/mc/health/HealthCheck.java deleted file mode 100644 index a1f78137..00000000 --- a/src/main/java/de/sldk/mc/health/HealthCheck.java +++ /dev/null @@ -1,64 +0,0 @@ -package de.sldk.mc.health; - -/** - * Health check. - */ -public interface HealthCheck { - - /** - * Checks if the current state is healthy. - * - * @return {@code true} if the state is healthy and {@code false} otherwise - */ - boolean isHealthy(); - - /** - * Creates a health compound check from the provided ones reporting healthy status if all the checks report it. - * - * @param checks merged health checks - * @return compound health check - */ - static HealthCheck allOf(final HealthCheck... checks) { - return new AllOf(checks); - } - - /** - * Creates a compound health check from the provided ones reporting healthy status if any check reports it. - * - * @param checks merged health checks - * @return compound health check - */ - static HealthCheck anyOf(final HealthCheck... checks) { - return new AnyOf(checks); - } - - final class AllOf implements HealthCheck { - private final HealthCheck[] checks; - - private AllOf(final HealthCheck[] checks) { - this.checks = checks; - } - - @Override - public boolean isHealthy() { - for (final HealthCheck check : checks) if (!check.isHealthy()) return false; - - return true; - } - } - - final class AnyOf implements HealthCheck { - private final HealthCheck[] checks; - - private AnyOf(final HealthCheck[] checks) { - this.checks = checks; - } - - @Override - public boolean isHealthy() { - for (final HealthCheck check : checks) if (check.isHealthy()) return true; - - return false; - } - } -} diff --git a/src/main/java/de/sldk/mc/health/HealthChecks.java b/src/main/java/de/sldk/mc/health/HealthChecks.java deleted file mode 100644 index 39c8af15..00000000 --- a/src/main/java/de/sldk/mc/health/HealthChecks.java +++ /dev/null @@ -1,21 +0,0 @@ -package de.sldk.mc.health; - -/** - * Dynamic compound health checks. - */ -public interface HealthChecks extends HealthCheck { - - /** - * Adds the provided health check to this one. - * - * @param check added health check - */ - void add(HealthCheck check); - - /** - * Removes the provided health check from this one. - * - * @param check removed health check - */ - void remove(HealthCheck check); -} diff --git a/src/main/kotlin/de/sldk/mc/PrometheusExporter.kt b/src/main/kotlin/de/sldk/mc/PrometheusExporter.kt index c7576784..9847d76d 100644 --- a/src/main/kotlin/de/sldk/mc/PrometheusExporter.kt +++ b/src/main/kotlin/de/sldk/mc/PrometheusExporter.kt @@ -3,13 +3,9 @@ package de.sldk.mc import de.sldk.mc.config.PrometheusExporterConfig -import de.sldk.mc.health.ConcurrentHealthChecks -import de.sldk.mc.health.HealthChecks -import org.bukkit.plugin.ServicePriority import org.bukkit.plugin.java.JavaPlugin import java.util.logging.Level - class PrometheusExporter : JavaPlugin() { private val config: PrometheusExporterConfig = PrometheusExporterConfig(this) private var server: MetricsServer? = null @@ -18,18 +14,14 @@ class PrometheusExporter : JavaPlugin() { override fun onEnable() { config.loadDefaultsAndSave() config.enableConfiguredMetrics() - - val healthChecks = ConcurrentHealthChecks.create() - getServer().servicesManager.register(HealthChecks::class.java, healthChecks, this, ServicePriority.Normal) - - startMetricsServer(healthChecks) + startMetricsServer() } - private fun startMetricsServer(healthChecks: HealthChecks) { + private fun startMetricsServer() { val host = config[PrometheusExporterConfig.HOST] val port = config[PrometheusExporterConfig.PORT] - server = MetricsServer(host, port, this, healthChecks) + server = MetricsServer(host, port, this) try { server?.start() diff --git a/src/test/java/de/sldk/mc/exporter/PrometheusExporterTest.java b/src/test/java/de/sldk/mc/exporter/PrometheusExporterTest.java index 0f9eec7d..a06e72ec 100644 --- a/src/test/java/de/sldk/mc/exporter/PrometheusExporterTest.java +++ b/src/test/java/de/sldk/mc/exporter/PrometheusExporterTest.java @@ -1,9 +1,10 @@ package de.sldk.mc.exporter; +import static org.assertj.core.api.Assertions.assertThat; + import de.sldk.mc.MetricsServer; import de.sldk.mc.PrometheusExporter; -import de.sldk.mc.health.ConcurrentHealthChecks; import io.prometheus.client.CollectorRegistry; import io.prometheus.client.Counter; import io.prometheus.client.exporter.common.TextFormat; @@ -20,8 +21,6 @@ import java.io.IOException; import java.net.ServerSocket; -import static org.assertj.core.api.Assertions.assertThat; - @ExtendWith(MockitoExtension.class) public class PrometheusExporterTest { @@ -35,9 +34,7 @@ public class PrometheusExporterTest { void setup() throws Exception { CollectorRegistry.defaultRegistry.clear(); metricsServerPort = getRandomFreePort(); - metricsServer = new MetricsServer( - "localhost", metricsServerPort, exporterMock, ConcurrentHealthChecks.create() - ); + metricsServer = new MetricsServer("localhost", metricsServerPort, exporterMock); metricsServer.start(); } @@ -86,14 +83,4 @@ void metrics_server_should_return_404_on_unknown_paths() { .statusCode(HttpStatus.NOT_FOUND_404); } - @Test - void metrics_server_should_return_200_on_health_check() { - String requestPath = URIUtil.newURI("http", "localhost", metricsServerPort, "/health", null); - - RestAssured.when() - .get(requestPath) - .then() - .statusCode(HttpStatus.OK_200); - } - }