From a23ab1818dba1f8a276f718598b988fdec567aea Mon Sep 17 00:00:00 2001 From: Dorin Date: Fri, 13 Sep 2019 11:26:33 +0100 Subject: [PATCH] remove the http server and its dependencies as they clash with readings deps --- pom.xml | 9 +- .../java/com/etsy/statsd/profiler/Agent.java | 5 - .../profiler/server/ProfilerServer.java | 48 ------ .../profiler/server/RequestHandler.java | 140 ------------------ .../profiler/server/ProfilerServerTest.java | 117 --------------- 5 files changed, 2 insertions(+), 317 deletions(-) delete mode 100644 src/main/java/com/etsy/statsd/profiler/server/ProfilerServer.java delete mode 100644 src/main/java/com/etsy/statsd/profiler/server/RequestHandler.java delete mode 100644 src/test/java/com/etsy/statsd/profiler/server/ProfilerServerTest.java diff --git a/pom.xml b/pom.xml index 942be18..1c450a8 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.etsy statsd-jvm-profiler - 2.1.2-SNAPSHOT + 2.1.3-SNAPSHOT jar statsd-jvm-profiler @@ -57,12 +57,7 @@ org.influxdb influxdb-java - 2.0 - - - io.vertx - vertx-core - 2.1.5 + 2.14 junit diff --git a/src/main/java/com/etsy/statsd/profiler/Agent.java b/src/main/java/com/etsy/statsd/profiler/Agent.java index 65e989f..c8ab502 100644 --- a/src/main/java/com/etsy/statsd/profiler/Agent.java +++ b/src/main/java/com/etsy/statsd/profiler/Agent.java @@ -1,7 +1,6 @@ package com.etsy.statsd.profiler; import com.etsy.statsd.profiler.reporter.Reporter; -import com.etsy.statsd.profiler.server.ProfilerServer; import com.etsy.statsd.profiler.worker.ProfilerShutdownHookWorker; import com.etsy.statsd.profiler.worker.ProfilerThreadFactory; import com.etsy.statsd.profiler.worker.ProfilerWorkerThread; @@ -74,10 +73,6 @@ private static void scheduleProfilers(Collection profilers, Arguments ScheduledFuture future = scheduledExecutorService.scheduleAtFixedRate(worker, EXECUTOR_DELAY, profiler.getPeriod(), profiler.getTimeUnit()); runningProfilers.put(profiler.getClass().getSimpleName(), future); } - - if (arguments.httpServerEnabled) { - ProfilerServer.startServer(runningProfilers, activeProfilers, arguments.httpPort, isRunning, errors); - } } /** diff --git a/src/main/java/com/etsy/statsd/profiler/server/ProfilerServer.java b/src/main/java/com/etsy/statsd/profiler/server/ProfilerServer.java deleted file mode 100644 index 2fad6e6..0000000 --- a/src/main/java/com/etsy/statsd/profiler/server/ProfilerServer.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.etsy.statsd.profiler.server; - -import com.etsy.statsd.profiler.Profiler; -import org.vertx.java.core.AsyncResult; -import org.vertx.java.core.Handler; -import org.vertx.java.core.Vertx; -import org.vertx.java.core.VertxFactory; -import org.vertx.java.core.http.HttpServer; - -import java.util.List; -import java.util.Map; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.atomic.AtomicReference; -import java.util.logging.Logger; - -/** - * Sets up a simple embedded HTTP server for interacting with the profiler while it runs - * - * @author Andrew Johnson - */ -public final class ProfilerServer { - private static final Logger LOGGER = Logger.getLogger(ProfilerServer.class.getName()); - private static final Vertx VERTX = VertxFactory.newVertx(); - - private ProfilerServer() { } - - /** - * Start an embedded HTTP server - * - * @param activeProfilers The active profilers - * @param port The port on which to bind the server - */ - public static void startServer(final Map> runningProfilers, final Map activeProfilers, final int port, final AtomicReference isRunning, final List errors) { - final HttpServer server = VERTX.createHttpServer(); - server.requestHandler(RequestHandler.getMatcher(runningProfilers, activeProfilers, isRunning, errors)); - server.listen(port, new Handler>() { - @Override - public void handle(AsyncResult event) { - if (event.failed()) { - server.close(); - startServer(runningProfilers, activeProfilers, port + 1, isRunning, errors); - } else if (event.succeeded()) { - LOGGER.info("Profiler server started on port " + port); - } - } - }); - } -} diff --git a/src/main/java/com/etsy/statsd/profiler/server/RequestHandler.java b/src/main/java/com/etsy/statsd/profiler/server/RequestHandler.java deleted file mode 100644 index aa1f6d9..0000000 --- a/src/main/java/com/etsy/statsd/profiler/server/RequestHandler.java +++ /dev/null @@ -1,140 +0,0 @@ -package com.etsy.statsd.profiler.server; - -import com.etsy.statsd.profiler.Profiler; -import com.google.common.base.Function; -import com.google.common.base.Joiner; -import com.google.common.base.Predicate; -import com.google.common.collect.Collections2; -import org.vertx.java.core.Handler; -import org.vertx.java.core.http.HttpServerRequest; -import org.vertx.java.core.http.RouteMatcher; - -import java.util.*; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.atomic.AtomicReference; - -/** - * Handler for HTTP requests to the profiler server - * - * @author Andrew Johnson - */ -public final class RequestHandler { - private RequestHandler() { } - - /** - * Construct a RouteMatcher for the supported routes - * - * @param activeProfilers The active profilers - * @return A RouteMatcher that matches all supported routes - */ - public static RouteMatcher getMatcher(final Map> runningProfilers, Map activeProfilers, AtomicReference isRunning, List errors) { - RouteMatcher matcher = new RouteMatcher(); - matcher.get("/profilers", RequestHandler.handleGetProfilers(runningProfilers)); - matcher.get("/disable/:profiler", RequestHandler.handleDisableProfiler(runningProfilers)); - matcher.get("/status/profiler/:profiler", RequestHandler.handleProfilerStatus(activeProfilers)); - matcher.get("/errors", RequestHandler.handleErrorMessages(errors)); - matcher.get("/isRunning", RequestHandler.isRunning(isRunning)); - return matcher; - } - - /** - * Handle a GET to /isRunning - * - * @return A Handler that returns all running profilers - */ - public static Handler isRunning(final AtomicReference isRunning) { - return new Handler() { - @Override - public void handle(HttpServerRequest httpServerRequest) { - httpServerRequest.response().end(String.format("isRunning: %b", isRunning.get())); - } - }; - } - - /** - * Handle a GET to /profilers - * - * @return A Handler that handles a request to the /profilers endpoint - */ - public static Handler handleGetProfilers(final Map> runningProfilers) { - return new Handler() { - @Override - public void handle(HttpServerRequest httpServerRequest) { - httpServerRequest.response().end(Joiner.on("\n").join(getEnabledProfilers(runningProfilers))); - } - }; - } - - /** - * Handle a GET to /errors - * - * @return The last 10 error stacktraces - */ - public static Handler handleErrorMessages(final List errors) { - return new Handler() { - @Override - public void handle(HttpServerRequest httpServerRequest) { - httpServerRequest.response().end("Errors: " + Joiner.on("\n").join(errors)); - } - }; - } - - /** - * Handle a GET to /disable/:profiler - * - * @param activeProfilers The active profilers - * @return A Handler that handles a request to the /disable/:profiler endpoint - */ - public static Handler handleDisableProfiler(final Map> activeProfilers) { - return new Handler() { - @Override - public void handle(HttpServerRequest httpServerRequest) { - String profilerToDisable = httpServerRequest.params().get("profiler"); - ScheduledFuture future = activeProfilers.get(profilerToDisable); - future.cancel(false); - httpServerRequest.response().end(String.format("Disabled profiler %s", profilerToDisable)); - } - }; - } - - /** - * Handle a GET to /status/profiler/:profiler - * - * @param activeProfilers The active profilers - * @return A Handler that handles a request to the /disable/:profiler endpoint - */ - public static Handler handleProfilerStatus(final Map activeProfilers) { - return new Handler() { - @Override - public void handle(HttpServerRequest httpServerRequest) { - String profilerName = httpServerRequest.params().get("profiler"); - Profiler profiler = activeProfilers.get(profilerName); - httpServerRequest.response().end(String.format("Recorded stats %d\n", profiler.getRecordedStats())); - } - }; - } - - /** - * Get all enabled profilers - * @param activeProfilers The active profilers - * @return A sorted List containing the names of profilers that are currently running - */ - private static List getEnabledProfilers(final Map> activeProfilers) { - Collection profilers = Collections2.transform(Collections2.filter(activeProfilers.entrySet(), new Predicate>>() { - @Override - public boolean apply(Map.Entry> input) { - return !input.getValue().isDone(); - } - }), new Function>, String>() { - @Override - public String apply(Map.Entry> input) { - return input.getKey(); - } - }); - - List result = new ArrayList<>(profilers); - Collections.sort(result); - return result; - - } -} diff --git a/src/test/java/com/etsy/statsd/profiler/server/ProfilerServerTest.java b/src/test/java/com/etsy/statsd/profiler/server/ProfilerServerTest.java deleted file mode 100644 index d6628af..0000000 --- a/src/test/java/com/etsy/statsd/profiler/server/ProfilerServerTest.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.etsy.statsd.profiler.server; - -import com.etsy.statsd.profiler.Profiler; -import com.etsy.statsd.profiler.profilers.MockProfiler1; -import com.etsy.statsd.profiler.profilers.MockProfiler2; -import com.etsy.statsd.profiler.worker.ProfilerThreadFactory; -import com.etsy.statsd.profiler.worker.ProfilerWorkerThread; -import com.google.common.base.Joiner; -import com.google.common.util.concurrent.MoreExecutors; -import org.apache.http.HttpEntity; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpRequestBase; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.util.EntityUtils; -import org.junit.Before; -import org.junit.Test; - -import java.io.IOException; -import java.util.*; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.ScheduledThreadPoolExecutor; -import java.util.concurrent.atomic.AtomicReference; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -public class ProfilerServerTest { - private Map activeProfilers; - private int port; - private AtomicReference isRunning; - private List errors; - private CloseableHttpClient client; - - @Before - public void setup() throws IOException { - MockProfiler1 profiler1 = new MockProfiler1(new HashSet()); - MockProfiler2 profiler2 = new MockProfiler2(new HashSet()); - - activeProfilers = new HashMap<>(); - activeProfilers.put("MockProfiler1", profiler1); - activeProfilers.put("MockProfiler2", profiler2); - - port = 8080; - - isRunning = new AtomicReference<>(true); - errors = new ArrayList<>(); - errors.add("example error"); - - Map> runningProfilers = new HashMap<>(); - ProfilerWorkerThread worker1 = new ProfilerWorkerThread(profiler1, errors); - ProfilerWorkerThread worker2 = new ProfilerWorkerThread(profiler2, errors); - ScheduledExecutorService scheduledExecutorService = MoreExecutors.getExitingScheduledExecutorService( - (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(2, new ProfilerThreadFactory())); - ScheduledFuture future1 = scheduledExecutorService.scheduleAtFixedRate(worker1, 0, profiler1.getPeriod(), profiler1.getTimeUnit()); - ScheduledFuture future2 = scheduledExecutorService.scheduleAtFixedRate(worker2, 0, profiler2.getPeriod(), profiler2.getTimeUnit()); - runningProfilers.put("MockProfiler1", future1); - runningProfilers.put("MockProfiler2", future2); - - ProfilerServer.startServer(runningProfilers, activeProfilers, port, isRunning, errors); - client = HttpClients.createDefault(); - } - - @Test - public void testDisableProfiler() throws IOException { - String profilerString = "MockProfiler1\nMockProfiler2"; - httpRequestTest("profilers", profilerString); - - String profilerToDisable = "MockProfiler1"; - httpRequestTest(String.format("disable/%s", profilerToDisable), String.format("Disabled profiler %s", profilerToDisable)); - - profilerString = "MockProfiler2"; - httpRequestTest("profilers", profilerString); - } - - @Test - public void testProfilerStatus() throws IOException { - String profilerName = "MockProfiler1"; - long recordedStats = activeProfilers.get(profilerName).getRecordedStats(); - httpRequestTest(String.format("status/profiler/%s", profilerName), String.format("Recorded stats %d\n", recordedStats)); - } - - @Test - public void testProfilers() throws IOException { - String profilerString = "MockProfiler1\nMockProfiler2"; - httpRequestTest("profilers", profilerString); - } - - @Test - public void testErrors() throws IOException { - String errorString = Joiner.on("\n").join(errors); - httpRequestTest("errors", String.format("Errors: %s", errorString)); - } - - @Test - public void testIsRunning() throws IOException { - httpRequestTest("isRunning", String.format("isRunning: %s", isRunning.get().toString())); - } - - private void httpRequestTest(String path, String expectedBody) throws IOException { - HttpRequestBase get = new HttpGet(String.format("http://localhost:%d/%s", port, path)); - CloseableHttpResponse response = client.execute(get); - - int statusCode = response.getStatusLine().getStatusCode(); - assertEquals(200, statusCode); - - HttpEntity entity = response.getEntity(); - assertNotNull(entity); - String responseBody = EntityUtils.toString(entity); - assertEquals(expectedBody, responseBody); - - response.close(); - } -}