From 02e04a79da3e86d845e9e8310148681b10186ff4 Mon Sep 17 00:00:00 2001 From: Alejandro Rivera Date: Wed, 3 Feb 2016 21:58:25 +0800 Subject: [PATCH] Added the option to disable the embedded HTTP server. --- README.md | 5 ++++- .../java/com/etsy/statsd/profiler/Agent.java | 10 +++++++--- .../com/etsy/statsd/profiler/Arguments.java | 5 ++++- .../etsy/statsd/profiler/ArgumentsTest.java | 18 ++++++++++++++++++ 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c3a9565..871c2b0 100644 --- a/README.md +++ b/README.md @@ -58,10 +58,13 @@ packageWhitelist | Colon-delimited whitelist for packages to include (optional, packageBlacklist | Colon-delimited whitelist for packages to exclude (optional, defaults to exclude nothing) profilers | Colon-delimited list of profiler class names (optional, defaults to CPUProfiler and MemoryProfiler) reporter | Class name of the reporter to use (optional, defaults to StatsDReporter) +httpServerEnabled| Determines if the embedded HTTP server should be started. (optional, defaults to `true`) httpPort | The port on which to bind the embedded HTTP server (optional, defaults to 5005). If this port is already in use, the next free port will be taken. ### Embedded HTTP Server -statsd-jvm-profiler embeds an HTTP server to support simple interactions with the profiler while it is in operation. You can configure the port on which this server runs with the `httpPort` option. +statsd-jvm-profiler embeds an HTTP server to support simple interactions with the profiler while it is in operation. +You can configure the port on which this server runs with the `httpPort` option. +You can disable it altogether using the `httpServerEnabled=false` argument. Endpoint | Usage --------------- | ----- diff --git a/src/main/java/com/etsy/statsd/profiler/Agent.java b/src/main/java/com/etsy/statsd/profiler/Agent.java index a1822ce..66d2655 100644 --- a/src/main/java/com/etsy/statsd/profiler/Agent.java +++ b/src/main/java/com/etsy/statsd/profiler/Agent.java @@ -50,7 +50,7 @@ public static void premain(final String args, final Instrumentation instrumentat profilers.add(instantiate(profiler, Profiler.CONSTRUCTOR_PARAM_TYPES, reporter, arguments)); } - scheduleProfilers(profilers, arguments.httpPort); + scheduleProfilers(profilers, arguments); registerShutdownHook(profilers); } @@ -58,8 +58,9 @@ public static void premain(final String args, final Instrumentation instrumentat * Schedule profilers with a SchedulerExecutorService * * @param profilers Collection of profilers to schedule + * @param arguments */ - private static void scheduleProfilers(Collection profilers, int httpPort) { + private static void scheduleProfilers(Collection profilers, Arguments arguments) { // We need to convert to an ExitingScheduledExecutorService so the JVM shuts down // when the main thread finishes ScheduledExecutorService scheduledExecutorService = MoreExecutors.getExitingScheduledExecutorService( @@ -73,7 +74,10 @@ private static void scheduleProfilers(Collection profilers, int httpPo ScheduledFuture future = scheduledExecutorService.scheduleAtFixedRate(worker, EXECUTOR_DELAY, profiler.getPeriod(), profiler.getTimeUnit()); runningProfilers.put(profiler.getClass().getSimpleName(), future); } - ProfilerServer.startServer(runningProfilers, activeProfilers, httpPort, isRunning, errors); + + if (arguments.httpServerEnabled) { + ProfilerServer.startServer(runningProfilers, activeProfilers, arguments.httpPort, isRunning, errors); + } } /** diff --git a/src/main/java/com/etsy/statsd/profiler/Arguments.java b/src/main/java/com/etsy/statsd/profiler/Arguments.java index 381bea6..1288a5b 100644 --- a/src/main/java/com/etsy/statsd/profiler/Arguments.java +++ b/src/main/java/com/etsy/statsd/profiler/Arguments.java @@ -20,6 +20,7 @@ public class Arguments { private static final String PROFILERS = "profilers"; private static final String REPORTER = "reporter"; private static final String HTTP_PORT = "httpPort"; + private static final String HTTP_SEVER_ENABLED = "httpServerEnabled"; private static final Collection REQUIRED = Arrays.asList(SERVER, PORT); @@ -56,6 +57,7 @@ public static Arguments parseArgs(final String args) { public Map remainingArgs; public Class> reporter; public int httpPort; + public boolean httpServerEnabled; private Arguments(Map parsedArgs) { server = parsedArgs.get(SERVER); @@ -64,6 +66,7 @@ private Arguments(Map parsedArgs) { profilers = parseProfilerArg(parsedArgs.get(PROFILERS)); reporter = parserReporterArg(parsedArgs.get(REPORTER)); httpPort = Integer.parseInt(Optional.fromNullable(parsedArgs.get(HTTP_PORT)).or("5005")); + httpServerEnabled = Boolean.parseBoolean(Optional.fromNullable(parsedArgs.get(HTTP_SEVER_ENABLED)).or("true")); parsedArgs.remove(SERVER); parsedArgs.remove(PORT); @@ -112,7 +115,7 @@ private Set> parseProfilerArg(String profilerArg) { } if (parsedProfilers.isEmpty()) { - throw new IllegalArgumentException("At least one profiler must be run"); + throw new IllegalArgumentException("At least one profiler must be specified"); } return parsedProfilers; diff --git a/src/test/java/com/etsy/statsd/profiler/ArgumentsTest.java b/src/test/java/com/etsy/statsd/profiler/ArgumentsTest.java index fa6604b..beceb64 100644 --- a/src/test/java/com/etsy/statsd/profiler/ArgumentsTest.java +++ b/src/test/java/com/etsy/statsd/profiler/ArgumentsTest.java @@ -12,6 +12,8 @@ import java.util.Set; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; public class ArgumentsTest { @Rule @@ -144,4 +146,20 @@ public void testDefaultReporter() { assertEquals(StatsDReporter.class, arguments.reporter); } + + @Test + public void testHttpServerEnabledByDefault() throws Exception { + String args = "server=localhost,port=8125"; + Arguments arguments = Arguments.parseArgs(args); + + assertTrue(arguments.httpServerEnabled); + } + + @Test + public void testHttpServerDisabled() throws Exception { + String args = "server=localhost,port=8125,httpServerEnabled=false"; + Arguments arguments = Arguments.parseArgs(args); + + assertFalse(arguments.httpServerEnabled); + } } \ No newline at end of file