Skip to content

Commit

Permalink
Added the option to disable the embedded HTTP server.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Rivera committed Feb 3, 2016
1 parent 9e5b7bb commit 02e04a7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------- | -----
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/etsy/statsd/profiler/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,17 @@ 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);
}

/**
* Schedule profilers with a SchedulerExecutorService
*
* @param profilers Collection of profilers to schedule
* @param arguments
*/
private static void scheduleProfilers(Collection<Profiler> profilers, int httpPort) {
private static void scheduleProfilers(Collection<Profiler> profilers, Arguments arguments) {
// We need to convert to an ExitingScheduledExecutorService so the JVM shuts down
// when the main thread finishes
ScheduledExecutorService scheduledExecutorService = MoreExecutors.getExitingScheduledExecutorService(
Expand All @@ -73,7 +74,10 @@ private static void scheduleProfilers(Collection<Profiler> 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);
}
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/etsy/statsd/profiler/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> REQUIRED = Arrays.asList(SERVER, PORT);

Expand Down Expand Up @@ -56,6 +57,7 @@ public static Arguments parseArgs(final String args) {
public Map<String, String> remainingArgs;
public Class<? extends Reporter<?>> reporter;
public int httpPort;
public boolean httpServerEnabled;

private Arguments(Map<String, String> parsedArgs) {
server = parsedArgs.get(SERVER);
Expand All @@ -64,6 +66,7 @@ private Arguments(Map<String, String> 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);
Expand Down Expand Up @@ -112,7 +115,7 @@ private Set<Class<? extends Profiler>> 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;
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/etsy/statsd/profiler/ArgumentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 02e04a7

Please sign in to comment.