Skip to content

Commit

Permalink
Use builder for AppConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerbenson committed May 16, 2019
1 parent 946373c commit 7baa1e3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/datadog/jmxfetch/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public App(AppConfig appConfig) {
public static void main(String[] args) {

// Load the config from the args
AppConfig config = new AppConfig();
AppConfig config = AppConfig.builder().build();
JCommander commander = null;
try {
// Try to parse the args using JCommander
Expand Down
45 changes: 17 additions & 28 deletions src/main/java/org/datadog/jmxfetch/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;

import lombok.Builder;
import lombok.ToString;
import org.datadog.jmxfetch.converter.ExitWatcherConverter;
import org.datadog.jmxfetch.converter.ReporterConverter;
import org.datadog.jmxfetch.reporter.ConsoleReporter;
Expand All @@ -20,6 +22,8 @@
import java.util.List;
import java.util.Map;

@Builder
@ToString
@Parameters(separators = "=")
public class AppConfig {
public static final String ACTION_COLLECT = "collect";
Expand Down Expand Up @@ -69,6 +73,7 @@ public class AppConfig {
description = "Level of verbosity",
validateWith = Log4JLevelValidator.class,
required = false)
@Builder.Default
private String logLevel = "INFO";

@Parameter(
Expand All @@ -87,6 +92,7 @@ public class AppConfig {
names = {"--tmp_directory", "-T"},
description = "Absolute path to a temporary directory",
required = false)
@Builder.Default
private String tmpDirectory = "/tmp";

@Parameter(
Expand All @@ -110,46 +116,53 @@ public class AppConfig {
description = "Sleeping time during two iterations in ms",
validateWith = PositiveIntegerValidator.class,
required = false)
@Builder.Default
private int checkPeriod = 15000;

@Parameter(
names = {"--thread_pool_size", "-t"},
description = "The size of the thread pool",
validateWith = PositiveIntegerValidator.class,
required = false)
@Builder.Default
private int threadPoolSize = DEFAULT_THREAD_POOL_SIZE;

@Parameter(
names = {"--reconnection_thread_pool_size", "-u"},
description = "The size of the reconnection thread pool",
validateWith = PositiveIntegerValidator.class,
required = false)
@Builder.Default
private int reconnectionThreadPoolSize = DEFAULT_THREAD_POOL_SIZE;

@Parameter(
names = {"--collection_timeout", "-x"},
description = "The concurrent collection timeout in seconds",
validateWith = PositiveIntegerValidator.class,
required = false)
@Builder.Default
private int collectionTimeout = DEFAULT_COLLECTION_TO_S;

@Parameter(
names = {"--reconnection_timeout", "-y"},
description = "The reconnection timeout in seconds",
validateWith = PositiveIntegerValidator.class,
required = false)
@Builder.Default
private int reconnectionTimeout = DEFAULT_RECONNECTION_TO_S;

@Parameter(
names = {"--ad_enabled", "--sd_enabled", "-w"},
description = "Enable Auto Discovery.",
required = false)
@Builder.Default
private boolean adEnabled = false;

@Parameter(
names = {"--ad_pipe", "--sd_pipe", "-P"},
description = "Auto Discovery pipe name.",
required = false)
@Builder.Default
private String adPipe = AD_PIPE_NAME;

@Parameter(
Expand All @@ -166,6 +179,7 @@ public class AppConfig {
+ "(default to null = no exit on file)",
converter = ExitWatcherConverter.class,
required = false)
@Builder.Default
private ExitWatcher exitWatcher = new ExitWatcher();

@Parameter(
Expand All @@ -174,7 +188,7 @@ public class AppConfig {
+ "list_everything, list_collected_attributes, list_matching_attributes, "
+ "list_not_matching_attributes, list_limited_attributes, list_jvms]",
required = true)
private List<String> action = null;
private List<String> action;

@Parameter(
names = {"--ipc_host", "-H"},
Expand All @@ -187,6 +201,7 @@ public class AppConfig {
description = "IPC port",
validateWith = PositiveIntegerValidator.class,
required = false)
@Builder.Default
private int ipcPort = 0;

// This is used by things like APM agent to provide configuration from resources
Expand All @@ -200,6 +215,7 @@ public class AppConfig {
// This is used by things like APM agent to provide tags that should be set with all metrics
private Map<String, String> globalTags;

@Builder.Default
private Status status = new Status();

/** Updates the status and returns a boolean describing if the status was indeed updated.. */
Expand Down Expand Up @@ -342,31 +358,4 @@ public Integer getRefreshBeansPeriod() {
public Map<String, String> getGlobalTags() {
return globalTags;
}

/** Factory method used by dd-tracer-agent to run jmxfetch in the same process. */
public static AppConfig create(
List<String> instanceConfigResources,
List<String> metricConfigResources,
List<String> metricConfigFiles,
Integer checkPeriod,
Integer refreshBeansPeriod,
Map<String, String> globalTags,
String reporter,
String logLocation,
String logLevel) {
AppConfig config = new AppConfig();
config.action = ImmutableList.of(ACTION_COLLECT);
config.instanceConfigResources = ImmutableList.copyOf(instanceConfigResources);
config.metricConfigResources = ImmutableList.copyOf(metricConfigResources);
config.metricConfigFiles = ImmutableList.copyOf(metricConfigFiles);
if (checkPeriod != null) {
config.checkPeriod = checkPeriod;
}
config.refreshBeansPeriod = refreshBeansPeriod;
config.globalTags = ImmutableMap.copyOf(globalTags);
config.reporter = ReporterFactory.getReporter(reporter);
config.logLocation = logLocation;
config.logLevel = logLevel;
return config;
}
}
2 changes: 1 addition & 1 deletion src/test/java/org/datadog/jmxfetch/TestCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.junit.BeforeClass;

public class TestCommon {
AppConfig appConfig = spy(new AppConfig());
AppConfig appConfig = spy(AppConfig.builder().build());
App app;
MBeanServer mbs;
ArrayList<ObjectName> objectNames = new ArrayList<ObjectName>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class TestParsingJCommander {
private static final String IPC_PORT = "5001";

private static AppConfig testCommand(String[] params) throws ParameterException {
AppConfig appConfig = new AppConfig();
AppConfig appConfig = AppConfig.builder().build();
new JCommander(appConfig, params);
return appConfig;
}
Expand Down

0 comments on commit 7baa1e3

Please sign in to comment.