Skip to content

Commit

Permalink
Allow metrics configs to be loaded via resources
Browse files Browse the repository at this point in the history
Metrics configs embedded inside the agent aren’t directly accessible on the file system.  Loading as a resource avoids having to copy to an external location.
  • Loading branch information
tylerbenson committed Jan 3, 2019
1 parent bcb08ba commit 2125937
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/main/java/org/datadog/jmxfetch/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public class AppConfig {

// This is used by things like APM agent to provide configuration from resources
private List<String> instanceConfigResources;
// This is used by things like APM agent to provide metric configuration from resources
private List<String> metricConfigResources;
// This is used by things like APM agent to provide metric configuration from files
private List<String> metricConfigFiles;
// This is used by things like APM agent to provide global override for bean refresh period
Expand Down Expand Up @@ -229,6 +231,10 @@ public List<String> getInstanceConfigResources() {
return instanceConfigResources;
}

public List<String> getMetricConfigResources() {
return metricConfigResources;
}

public List<String> getMetricConfigFiles() {
return metricConfigFiles;
}
Expand All @@ -246,6 +252,7 @@ public Map<String, String> getGlobalTags() {
*/
public static AppConfig create(
List<String> instanceConfigResources,
List<String> metricConfigResources,
List<String> metricConfigFiles,
Integer checkPeriod,
Integer refreshBeansPeriod,
Expand All @@ -256,6 +263,7 @@ public static AppConfig create(
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;
Expand Down
47 changes: 45 additions & 2 deletions src/main/java/org/datadog/jmxfetch/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ClassCastException;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -38,6 +39,13 @@ public class Instance {
public static final String PROCESS_NAME_REGEX = "process_name_regex";
public static final String ATTRIBUTE = "Attribute: ";

private static final ThreadLocal<Yaml> YAML = new ThreadLocal<Yaml>() {
@Override
public Yaml initialValue() {
return new Yaml();
}
};

private Set<ObjectName> beans;
private LinkedList<String> beanScopes;
private LinkedList<Configuration> configurationList = new LinkedList<Configuration>();
Expand Down Expand Up @@ -149,6 +157,7 @@ public Instance(LinkedHashMap<String, Object> instanceMap, LinkedHashMap<String,
}

loadMetricConfigFiles(appConfig, configurationList);
loadMetricConfigResources(appConfig, configurationList);

String gcMetricConfig = "old-gc-default-jmx-metrics.yaml";

Expand All @@ -164,7 +173,7 @@ public Instance(LinkedHashMap<String, Object> instanceMap, LinkedHashMap<String,
}

private void loadDefaultConfig(String configResourcePath) {
ArrayList<LinkedHashMap<String, Object>> defaultConf = (ArrayList<LinkedHashMap<String, Object>>) new Yaml().load(this.getClass().getResourceAsStream(configResourcePath));
ArrayList<LinkedHashMap<String, Object>> defaultConf = (ArrayList<LinkedHashMap<String, Object>>) YAML.get().load(this.getClass().getResourceAsStream(configResourcePath));
for (LinkedHashMap<String, Object> conf : defaultConf) {
configurationList.add(new Configuration(conf));
}
Expand All @@ -178,7 +187,7 @@ private void loadMetricConfigFiles(AppConfig appConfig, LinkedList<Configuration
LOGGER.info("Reading metric config file " + yamlPath);
try {
yamlInputStream = new FileInputStream(yamlPath);
ArrayList<LinkedHashMap<String, Object>> confs = (ArrayList<LinkedHashMap<String, Object>>) new Yaml().load(yamlInputStream);
ArrayList<LinkedHashMap<String, Object>> confs = (ArrayList<LinkedHashMap<String, Object>>) YAML.get().load(yamlInputStream);
for (LinkedHashMap<String, Object> conf : confs) {
configurationList.add(new Configuration(conf));
}
Expand All @@ -199,6 +208,40 @@ private void loadMetricConfigFiles(AppConfig appConfig, LinkedList<Configuration
}
}

private void loadMetricConfigResources(AppConfig config, LinkedList<Configuration> configurationList) {
List<String> resourceConfigList = config.getMetricConfigResources();
if (resourceConfigList != null) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
for (String resourceName : resourceConfigList) {
LOGGER.info("Reading metric config resource " + resourceName);
InputStream inputStream = classLoader.getResourceAsStream(resourceName);
if (inputStream == null) {
LOGGER.warn("Cannot find metric config resource" + resourceName);
} else {
try {
LinkedHashMap<String, ArrayList<LinkedHashMap<String, Object>>> topYaml = (LinkedHashMap<String, ArrayList<LinkedHashMap<String, Object>>>) YAML.get().load(inputStream);
ArrayList<LinkedHashMap<String, Object>> jmxConf = topYaml.get("jmx_metrics");
if(jmxConf != null) {
for (LinkedHashMap<String, Object> conf : jmxConf) {
configurationList.add(new Configuration(conf));
}
} else {
LOGGER.warn("jmx_metrics block not found in resource " + resourceName);
}
} catch (Exception e) {
LOGGER.warn("Cannot parse yaml resource " + resourceName, e);
} finally {
try {
inputStream.close();
} catch (IOException e) {
// ignore
}
}
}
}
}
}

/**
* Format the instance tags defined in the YAML configuration file to a `LinkedHashMap`.
* Supported inputs: `List`, `Map`.
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/datadog/jmxfetch/TestLogInitialization.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public AppConfig call() throws Exception {
return AppConfig.create(
ImmutableList.of("org/datadog/jmxfetch/dd-java-agent-jmx.yaml"),
Collections.<String>emptyList(),
Collections.<String>emptyList(),
(int) TimeUnit.SECONDS.toMillis(30),
(int) TimeUnit.SECONDS.toMillis(30),
Collections.<String, String>emptyMap(),
Expand All @@ -51,6 +52,7 @@ public AppConfig call() throws Exception {
return AppConfig.create(
ImmutableList.of("org/datadog/jmxfetch/remote-jmx.yaml"),
Collections.<String>emptyList(),
Collections.<String>emptyList(),
(int) TimeUnit.SECONDS.toMillis(30),
(int) TimeUnit.SECONDS.toMillis(30),
Collections.<String, String>emptyMap(),
Expand Down

0 comments on commit 2125937

Please sign in to comment.