diff --git a/pom.xml b/pom.xml index 2d83aa4bd..692f335ad 100644 --- a/pom.xml +++ b/pom.xml @@ -41,7 +41,7 @@ 2.1.0 1.35 1.2.17 - 2.10.0 + 1.4 1.13 - com.fasterxml.jackson.core - jackson-annotations - ${jackson.version} - - - com.fasterxml.jackson.core - jackson-core - ${jackson.version} + com.google.code.gson + gson + ${gson.version} log4j diff --git a/src/main/java/org/datadog/jmxfetch/App.java b/src/main/java/org/datadog/jmxfetch/App.java index 07eb738c4..dca19d97d 100644 --- a/src/main/java/org/datadog/jmxfetch/App.java +++ b/src/main/java/org/datadog/jmxfetch/App.java @@ -3,7 +3,6 @@ import com.beust.jcommander.JCommander; import com.beust.jcommander.ParameterException; -import com.fasterxml.jackson.core.JsonProcessingException; import org.apache.commons.io.IOUtils; import org.apache.log4j.Appender; import org.apache.log4j.Level; @@ -65,7 +64,6 @@ public class App { private static final Charset UTF_8 = Charset.forName("UTF-8"); private static int loopCounter; - private int lastJsonConfigTs; private HashMap adJsonConfigs; private ConcurrentHashMap configs; private ConcurrentHashMap adPipeConfigs = @@ -78,7 +76,6 @@ public class App { private TaskProcessor recoveryProcessor; private AppConfig appConfig; - private HttpClient client; /** Application constructor. */ public App(AppConfig appConfig) { @@ -332,7 +329,7 @@ void start() { // Main Loop that will periodically collect metrics from the JMX Server FileInputStream adPipe = null; - if (appConfig.getAutoDiscoveryPipeEnabled()) { + if (appConfig.getAutoDiscoveryEnabled()) { LOGGER.info("Auto Discovery enabled"); adPipe = newAutoDiscoveryPipe(); try { @@ -351,11 +348,11 @@ void start() { return; } - if (adPipe == null && appConfig.getAutoDiscoveryPipeEnabled()) { + // any SD configs waiting in pipe? + if (adPipe == null && appConfig.getAutoDiscoveryEnabled()) { // If SD is enabled and the pipe is not open, retry opening pipe adPipe = newAutoDiscoveryPipe(); } - // any AutoDiscovery configs waiting? try { if (adPipe != null && adPipe.available() > 0) { byte[] buffer = new byte[0]; @@ -386,10 +383,6 @@ void start() { } setReinit(processAutoDiscovery(buffer)); } - - if (appConfig.remoteEnabled()) { - setReinit(getJsonConfigs()); - } } catch (IOException e) { LOGGER.warn( "Unable to read from pipe" @@ -408,8 +401,7 @@ void start() { doIteration(); } else { LOGGER.warn("No instance could be initiated. Retrying initialization."); - lastJsonConfigTs = 0; // reset TS to get AC instances - appConfig.getStatus().flush(); + appConfig.getStatus().flush(appConfig.getIPCPort()); configs = getConfigs(appConfig); init(true); } @@ -560,8 +552,10 @@ public TaskStatusHandler invoke( processFixedStatus(fixInstanceTasks, statuses); // update with statuses - processStatus(fixInstanceTasks, statuses); + // REVERT: open question??? + // processStatus(fixInstanceTasks, statuses); + appConfig.getStatus().flush(appConfig.getIPCPort()); } catch (Exception e) { // NADA } @@ -612,11 +606,6 @@ public boolean addConfig(String name, YamlParser config) { return true; } - /** Adds a configuration to the auto-discovery HTTP collected configuration list (JSON). */ - public boolean addJsonConfig(String name, String json) { - return false; - } - private ConcurrentHashMap getConfigs(AppConfig config) { ConcurrentHashMap configs = new ConcurrentHashMap(); @@ -684,50 +673,6 @@ private void loadResourceConfigs( } } - private boolean getJsonConfigs() { - HttpClient.HttpResponse response; - boolean update = false; - - if (this.client == null) { - return update; - } - - try { - String uripath = "agent/jmx/configs?timestamp=" + lastJsonConfigTs; - response = client.request("GET", "", uripath); - if (!response.isResponse2xx()) { - LOGGER.warn( - "Failed collecting JSON configs: [" - + response.getResponseCode() - + "] " - + response.getResponseBody()); - return update; - } else if (response.getResponseCode() == 204) { - LOGGER.debug("No configuration changes..."); - return update; - } - - InputStream jsonInputStream = IOUtils.toInputStream(response.getResponseBody(), UTF_8); - JsonParser parser = new JsonParser(jsonInputStream); - int timestamp = ((Integer) parser.getJsonTimestamp()).intValue(); - if (timestamp > lastJsonConfigTs) { - adJsonConfigs = (HashMap) parser.getJsonConfigs(); - lastJsonConfigTs = timestamp; - update = true; - LOGGER.info("update is in order - updating timestamp: " + lastJsonConfigTs); - for (String checkName : adJsonConfigs.keySet()) { - LOGGER.debug("received config for check '" + checkName + "'"); - } - } - } catch (JsonProcessingException e) { - LOGGER.error("error processing JSON response: " + e); - } catch (IOException e) { - LOGGER.error("unable to collect remote JMX configs: " + e); - } - - return update; - } - private void reportStatus( AppConfig appConfig, Reporter reporter, diff --git a/src/main/java/org/datadog/jmxfetch/AppConfig.java b/src/main/java/org/datadog/jmxfetch/AppConfig.java index 34843cf40..93cb3834a 100644 --- a/src/main/java/org/datadog/jmxfetch/AppConfig.java +++ b/src/main/java/org/datadog/jmxfetch/AppConfig.java @@ -5,6 +5,7 @@ import org.datadog.jmxfetch.converter.ExitWatcherConverter; import org.datadog.jmxfetch.converter.ReporterConverter; +import org.datadog.jmxfetch.converter.StatusConverter; import org.datadog.jmxfetch.reporter.ConsoleReporter; import org.datadog.jmxfetch.reporter.Reporter; import org.datadog.jmxfetch.reporter.ReporterFactory; @@ -150,7 +151,7 @@ public class AppConfig { description = "Absolute path of the status file. (default to null = no status file written)", required = false) - private String statusLocation; + private Status status = new Status(); @Parameter( names = {"--exit_file_location", "-e"}, @@ -169,12 +170,6 @@ public class AppConfig { required = true) private List action = null; - @Parameter( - names = {"--ipc_host", "-H"}, - description = "IPC host", - required = false) - private String ipcHost; - @Parameter( names = {"--ipc_port", "-I"}, description = "IPC port", @@ -200,18 +195,11 @@ public boolean updateStatus() { if (statusLocation != null) { status = new Status(statusLocation); return true; - } else if (ipcHost != null && ipcPort > 0) { - status = new Status(ipcHost, ipcPort); - return true; } return false; } - public boolean remoteEnabled() { - return (ipcHost != null && ipcPort > 0); - } - public String getStatusLocation() { return this.statusLocation; } @@ -264,7 +252,7 @@ public int getIpcPort() { return ipcPort; } - public boolean getAutoDiscoveryPipeEnabled() { + public boolean getAutoDiscoveryEnabled() { return adEnabled; } diff --git a/src/main/java/org/datadog/jmxfetch/HttpClient.java b/src/main/java/org/datadog/jmxfetch/HttpClient.java deleted file mode 100644 index a2de5c38b..000000000 --- a/src/main/java/org/datadog/jmxfetch/HttpClient.java +++ /dev/null @@ -1,143 +0,0 @@ -package org.datadog.jmxfetch; - -import org.apache.log4j.Logger; - -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URL; -import javax.net.ssl.HttpsURLConnection; -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; - -public class HttpClient { - private String token; - private TrustManager[] dummyTrustManager; - private SSLContext sc; - private String host; - private int port; - - private static final String USER_AGENT = "Datadog/JMXFetch"; - private static final Logger LOGGER = Logger.getLogger(Status.class.getName()); - - public static class HttpResponse { - private int responseCode; - private String responseBody; - - /** HttpResponse constructor for provided response code and response string. */ - public HttpResponse(int responseCode, String responseBody) { - this.responseCode = responseCode; - this.responseBody = responseBody; - } - - /** HttpResponse constructor for provided response code and response stream. */ - public HttpResponse(int responseCode, InputStreamReader responseStream) throws IOException { - String inputLine; - BufferedReader in = new BufferedReader(responseStream); - StringBuffer responseBuilder = new StringBuffer(); - - while ((inputLine = in.readLine()) != null) { - responseBuilder.append(inputLine); - } - in.close(); - - this.responseCode = responseCode; - this.responseBody = responseBuilder.toString(); - } - - public void setResponseCode(int responseCode) { - this.responseCode = responseCode; - } - - public int getResponseCode() { - return this.responseCode; - } - - public String getResponseBody() { - return this.responseBody; - } - - public boolean isResponse2xx() { - return (responseCode >= 200 && responseCode < 300); - } - } - - /** HttpClient constructor to provided host and port. */ - public HttpClient(String host, int port, boolean verify) { - this.host = host; - this.port = port; - this.token = System.getenv("SESSION_TOKEN"); - - if (!verify) { - try { - dummyTrustManager = - new TrustManager[] { - new X509TrustManager() { - public java.security.cert.X509Certificate[] getAcceptedIssuers() { - return null; - } - - public void checkClientTrusted( - java.security.cert.X509Certificate[] certs, - String authType) {} - - public void checkServerTrusted( - java.security.cert.X509Certificate[] certs, - String authType) {} - } - }; - sc = SSLContext.getInstance("SSL"); - sc.init(null, this.dummyTrustManager, new java.security.SecureRandom()); - HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); - } catch (Exception e) { - LOGGER.debug("session token unavailable - not setting"); - this.token = ""; - } - } - } - - /** only supports json bodies for now. */ - public HttpResponse request(String method, String body, String path) { - HttpClient.HttpResponse response = new HttpClient.HttpResponse(0, ""); - try { - String url = "https://" + host + ":" + port + "/" + path; - LOGGER.debug("attempting to connect to: " + url); - LOGGER.debug("with body: " + body); - - URL uri = new URL(url); - HttpsURLConnection con = (HttpsURLConnection) uri.openConnection(); - - // add request header - con.setRequestMethod(method.toUpperCase()); - con.setRequestProperty("Authorization", "Bearer " + this.token); - con.setRequestProperty("User-Agent", USER_AGENT); - if (method.toUpperCase().equals("GET")) { - con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); - } else { - con.setRequestProperty("Content-Type", "application/json"); - con.setDoOutput(true); - DataOutputStream wr = new DataOutputStream(con.getOutputStream()); - wr.writeBytes(body); - wr.flush(); - wr.close(); - } - - int responseCode = con.getResponseCode(); - if (responseCode < 200 || responseCode >= 300) { - LOGGER.debug("HTTP error stream: " + con.getErrorStream()); - response.setResponseCode(responseCode); - } else { - response = - new HttpClient.HttpResponse( - responseCode, new InputStreamReader(con.getInputStream())); - } - - } catch (Exception e) { - LOGGER.info("problem creating http request: " + e.toString()); - } - - return response; - } -} diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index 21b496dbd..ea4a0e128 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -73,7 +73,7 @@ public Yaml initialValue() { private long lastCollectionTime; private Integer minCollectionPeriod; private long lastRefreshTime; - private LinkedHashMap instanceMap; + private LinkedHashMap yaml; private LinkedHashMap initConfig; private String instanceName; private LinkedHashMap tags; @@ -87,9 +87,8 @@ public Yaml initialValue() { /** Constructor, instantiates Instance based of a previous instance and appConfig. */ public Instance(Instance instance, AppConfig appConfig) { - this( - instance.getInstanceMap() != null - ? new LinkedHashMap(instance.getInstanceMap()) + this(instance.getYaml() != null + ? new LinkedHashMap(instance.getYaml()) : null, instance.getInitConfig() != null ? new LinkedHashMap(instance.getInitConfig()) @@ -128,7 +127,7 @@ public Instance( this.refreshBeansPeriod = appConfig.getRefreshBeansPeriod(); } - this.minCollectionPeriod = (Integer) instanceMap.get("min_collection_interval"); + this.minCollectionPeriod = (Integer) yaml.get("min_collection_interval"); if (this.minCollectionPeriod == null && initConfig != null) { this.minCollectionPeriod = (Integer) initConfig.get("min_collection_interval"); } @@ -142,7 +141,7 @@ public Instance( this.lastCollectionTime = 0; this.lastRefreshTime = 0; this.limitReached = false; - Object maxReturnedMetrics = this.instanceMap.get("max_returned_metrics"); + Object maxReturnedMetrics = this.yaml.get("max_returned_metrics"); if (maxReturnedMetrics == null) { this.maxReturnedMetrics = MAX_RETURNED_METRICS; } else { @@ -182,7 +181,7 @@ public Instance( instanceConf = this.initConfig.get("conf"); } - if (instanceConf == null) { + if (yamlConf == null) { LOGGER.warn("Cannot find a \"conf\" section in " + this.instanceName); } else { for (LinkedHashMap conf : @@ -369,12 +368,12 @@ public void init(boolean forceNewConnection) /** Returns a string representation for the instance. */ @Override public String toString() { - if (this.instanceMap.get(PROCESS_NAME_REGEX) != null) { - return "process_regex: `" + this.instanceMap.get(PROCESS_NAME_REGEX) + "`"; - } else if (this.instanceMap.get("jmx_url") != null) { - return (String) this.instanceMap.get("jmx_url"); + if (this.yaml.get(PROCESS_NAME_REGEX) != null) { + return "process_regex: `" + this.yaml.get(PROCESS_NAME_REGEX) + "`"; + } else if (this.yaml.get("jmx_url") != null) { + return (String) this.yaml.get("jmx_url"); } else { - return this.instanceMap.get("host") + ":" + this.instanceMap.get("port"); + return this.yaml.get("host") + ":" + this.yaml.get("port"); } } @@ -637,8 +636,8 @@ private void refreshBeansList() throws IOException { /** Returns a string array listing the service check tags. */ public String[] getServiceCheckTags() { List tags = new ArrayList(); - if (this.instanceMap.get("host") != null) { - tags.add("jmx_server:" + this.instanceMap.get("host")); + if (this.yaml.get("host") != null) { + tags.add("jmx_server:" + this.yaml.get("host")); } if (this.tags != null) { for (Entry e : this.tags.entrySet()) { @@ -662,8 +661,8 @@ public String getName() { return this.instanceName; } - LinkedHashMap getInstanceMap() { - return this.instanceMap; + LinkedHashMap getYaml() { + return this.yaml; } LinkedHashMap getInitConfig() { diff --git a/src/main/java/org/datadog/jmxfetch/JsonParser.java b/src/main/java/org/datadog/jmxfetch/JsonParser.java deleted file mode 100644 index 27299db93..000000000 --- a/src/main/java/org/datadog/jmxfetch/JsonParser.java +++ /dev/null @@ -1,53 +0,0 @@ -package org.datadog.jmxfetch; - -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; - -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.HashMap; - -@SuppressWarnings("unchecked") -class JsonParser { - - private HashMap parsedJson; - - public JsonParser(InputStream jsonInputStream) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - InputStreamReader jsonInputStreamReader = new InputStreamReader(jsonInputStream); - parsedJson = mapper.readValue(jsonInputStreamReader, new TypeReference() {}); - } - - public JsonParser(JsonParser other) { - parsedJson = new HashMap((HashMap) other.getParsedJson()); - } - - public Object getJsonConfigs() { - return parsedJson.get("configs"); - } - - public Object getJsonTimestamp() { - return parsedJson.get("timestamp"); - } - - public Object getJsonInstances(String key) { - HashMap config = - (HashMap) - ((HashMap) parsedJson.get("configs")).get(key); - - return config.get("instances"); - } - - public Object getInitConfig(String key) { - HashMap config = - (HashMap) - ((HashMap) parsedJson.get("configs")).get(key); - - return config.get("init_config"); - } - - public Object getParsedJson() { - return parsedJson; - } -} diff --git a/src/main/java/org/datadog/jmxfetch/Status.java b/src/main/java/org/datadog/jmxfetch/Status.java index 657b02aa1..5ec69ffbe 100644 --- a/src/main/java/org/datadog/jmxfetch/Status.java +++ b/src/main/java/org/datadog/jmxfetch/Status.java @@ -1,51 +1,73 @@ package org.datadog.jmxfetch; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.apache.commons.io.FileUtils; -import org.apache.log4j.Logger; -import org.yaml.snakeyaml.Yaml; - import java.io.File; import java.util.HashMap; import java.util.LinkedList; +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.lang.System; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; +import javax.net.ssl.HttpsURLConnection; + +import org.apache.commons.io.FileUtils; +import org.apache.log4j.Logger; +import org.yaml.snakeyaml.Yaml; +import com.google.gson.Gson; public class Status { - public static final String STATUS_WARNING = "WARNING"; - public static final String STATUS_OK = "OK"; - public static final String STATUS_ERROR = "ERROR"; - private static final Logger LOGGER = Logger.getLogger(Status.class.getName()); - private static final String INITIALIZED_CHECKS = "initialized_checks"; - private static final String FAILED_CHECKS = "failed_checks"; - private static final String API_STATUS_PATH = "agent/jmx/status"; + public final static String STATUS_WARNING = "WARNING"; + public final static String STATUS_OK = "OK"; + public final static String STATUS_ERROR = "ERROR"; + private final static Logger LOGGER = Logger.getLogger(Status.class.getName()); + private final static String INITIALIZED_CHECKS = "initialized_checks"; + private final static String FAILED_CHECKS = "failed_checks"; private HashMap instanceStats; - private ObjectMapper mapper; + private TrustManager[] dummyTrustManager; + private SSLContext sc; private String statusFileLocation; - private HttpClient client; private boolean isEnabled; + private String token; /** Default constructor. */ public Status() { this(null); } - /** Status constructor for remote configuration host. */ - public Status(String host, int port) { - mapper = new ObjectMapper(); - client = new HttpClient(host, port, false); - configure(null, host, port); - } - - /** status constructor for provided status file location. */ public Status(String statusFileLocation) { - configure(statusFileLocation, null, 0); + configure(statusFileLocation); } - void configure(String statusFileLocation, String host, int port) { + void configure(String statusFileLocation) { this.statusFileLocation = statusFileLocation; this.instanceStats = new HashMap(); - this.isEnabled = (this.statusFileLocation != null || this.client != null); + try { + this.token = System.getenv("SESSION_TOKEN"); + dummyTrustManager = new TrustManager[] { + new X509TrustManager() { + public java.security.cert.X509Certificate[] getAcceptedIssuers() { + return null; + } + public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) { + } + + public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) { + } + } + }; + sc = SSLContext.getInstance("SSL"); + sc.init(null, this.dummyTrustManager, new java.security.SecureRandom()); + HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); + } catch (Exception e) { + LOGGER.debug("session token unavailable - not setting"); + this.token = ""; + } + this.isEnabled = (this.statusFileLocation != null || this.token != ""); this.clearStats(); } @@ -120,22 +142,47 @@ private String generateYaml() { return yaml.dump(status); } - private String generateJson() throws JsonProcessingException { + private String generateJson() { + Gson gson = new Gson(); HashMap status = new HashMap(); status.put("timestamp", System.currentTimeMillis()); status.put("checks", this.instanceStats); - return mapper.writeValueAsString(status); + return gson.toJson(status); + } + + private boolean postRequest(String body, int port) { + int responseCode = 0; + try { + String url = "https://localhost:" + port + "/agent/jmxstatus"; + + URL uri = new URL(url); + HttpsURLConnection con = (HttpsURLConnection) uri.openConnection(); + + //add reuqest header + con.setRequestMethod("POST"); + con.setRequestProperty("Content-Type", "application/json"); + con.setRequestProperty("Authorization", "Bearer "+ this.token); + + con.setDoOutput(true); + DataOutputStream wr = new DataOutputStream(con.getOutputStream()); + wr.writeBytes(body); + wr.flush(); + wr.close(); + + responseCode = con.getResponseCode(); + + } catch (Exception e) { + LOGGER.info("problem creating http request: " + e.toString()); + } + return (responseCode >= 200 && responseCode < 300); } - /** Flushes current status. */ - public void flush() { + public void flush(int port) { if (isEnabled()) { - if (this.client != null) { + if (port > 0) { + String json = generateJson(); try { - String json = generateJson(); - HttpClient.HttpResponse response = - this.client.request("POST", json, API_STATUS_PATH); - if (!response.isResponse2xx()) { + if (!this.postRequest(json, port)) { LOGGER.debug("Problem submitting JSON status: " + json); } } catch (Exception e) { diff --git a/src/main/java/org/datadog/jmxfetch/converter/StatusConverter.java b/src/main/java/org/datadog/jmxfetch/converter/StatusConverter.java new file mode 100644 index 000000000..16a144595 --- /dev/null +++ b/src/main/java/org/datadog/jmxfetch/converter/StatusConverter.java @@ -0,0 +1,12 @@ +package org.datadog.jmxfetch.converter; + +import org.datadog.jmxfetch.Status; + +import com.beust.jcommander.IStringConverter; + +public class StatusConverter implements IStringConverter { + + public Status convert(String value) { + return new Status(value); + } +} \ No newline at end of file diff --git a/src/test/java/org/datadog/jmxfetch/TestConfiguration.java b/src/test/java/org/datadog/jmxfetch/TestConfiguration.java index 65bf2d217..563d021df 100644 --- a/src/test/java/org/datadog/jmxfetch/TestConfiguration.java +++ b/src/test/java/org/datadog/jmxfetch/TestConfiguration.java @@ -6,7 +6,6 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; @@ -18,247 +17,159 @@ import org.junit.Test; public class TestConfiguration { - static LinkedList configurations = new LinkedList(); - static JsonParser adConfigs; - - /** - * Setup Configuration tests - * - * @throws FileNotFoundException - */ - @SuppressWarnings("unchecked") - @BeforeClass - public static void init() throws FileNotFoundException, IOException { - File f = new File("src/test/resources/", "jmx_bean_scope.yaml"); - String yamlPath = f.getAbsolutePath(); - FileInputStream yamlInputStream = new FileInputStream(yamlPath); - YamlParser fileConfig = new YamlParser(yamlInputStream); - ArrayList> configInstances = - ((ArrayList>) fileConfig.getYamlInstances()); - - for (LinkedHashMap config : configInstances) { - Object yamlConf = config.get("conf"); - for (LinkedHashMap conf : - (ArrayList>) (yamlConf)) { - configurations.add(new Configuration(conf)); - } - } - - // lets also collect auto-discovery configs - f = new File("src/test/resources/", "auto_discovery_configs.json"); - String jsonPath = f.getAbsolutePath(); - FileInputStream jsonInputStream = new FileInputStream(jsonPath); - adConfigs = new JsonParser(jsonInputStream); - } - - /** - * Stringify a bean pattern to comply with the representation of a MBean - * - * @throws SecurityException - * @throws NoSuchMethodException - * @throws InvocationTargetException - * @throws IllegalArgumentException - * @throws IllegalAccessException - */ - @Test - public void testAutoDiscoveryConfigs() - throws NoSuchMethodException, SecurityException, IllegalAccessException, - IllegalArgumentException, InvocationTargetException { - HashMap configs = (HashMap) adConfigs.getJsonConfigs(); - - assertEquals(configurations.size(), 4); - int nconfigs = 0; - for (String check : configs.keySet()) { - ArrayList> configInstances = - ((ArrayList>) adConfigs.getJsonInstances(check)); - for (LinkedHashMap config : configInstances) { - Object jsonConf = config.get("conf"); - for (LinkedHashMap conf : - (ArrayList>) (jsonConf)) { - configurations.add(new Configuration(conf)); - nconfigs++; - } - } - } - assertEquals(configurations.size(), 4 + nconfigs); - } - - /** - * Extract filters from the configuration list and index by domain name - * - * @throws SecurityException - * @throws NoSuchMethodException - * @throws InvocationTargetException - * @throws IllegalArgumentException - * @throws IllegalAccessException - */ - @SuppressWarnings("unchecked") - @Test - public void testFiltersByDomain() - throws NoSuchMethodException, SecurityException, IllegalAccessException, - IllegalArgumentException, InvocationTargetException { - // Private method reflection - Method getIncludeFiltersByDomain = - Configuration.class.getDeclaredMethod( - "getIncludeFiltersByDomain", LinkedList.class); - getIncludeFiltersByDomain.setAccessible(true); - - // Assert - HashMap> filtersByDomain = - (HashMap>) - getIncludeFiltersByDomain.invoke(null, configurations); - - // Only contains 'org.datadog.jmxfetch.test' domain - assertEquals(filtersByDomain.size(), 1); - assertTrue(filtersByDomain.containsKey("org.datadog.jmxfetch.test")); - - // 5 filters associated: 4 `include`s (but the last one is split in two) - assertEquals(filtersByDomain.get("org.datadog.jmxfetch.test").size(), 5); + static LinkedList configurations = new LinkedList(); + + /** + * Setup Configuration tests + * @throws FileNotFoundException + */ + @SuppressWarnings("unchecked") + @BeforeClass + public static void init() throws FileNotFoundException { + File f = new File("src/test/resources/", "jmx_bean_scope.yaml"); + String yamlPath = f.getAbsolutePath(); + FileInputStream yamlInputStream = new FileInputStream(yamlPath); + YamlParser fileConfig = new YamlParser(yamlInputStream); + ArrayList> configInstances = ((ArrayList>) fileConfig.getYamlInstances()); + + for (LinkedHashMap config : configInstances) { + Object yamlConf = config.get("conf"); + for (LinkedHashMap conf : (ArrayList>) (yamlConf)) { + configurations.add(new Configuration(conf)); + } + } } - /** - * Extract common bean keys among a given list of filters. - * - * @throws SecurityException - * @throws NoSuchMethodException - * @throws InvocationTargetException - * @throws IllegalArgumentException - * @throws IllegalAccessException - */ - @SuppressWarnings("unchecked") - @Test - public void testCommonBeanKeys() - throws FileNotFoundException, NoSuchMethodException, SecurityException, - IllegalAccessException, IllegalArgumentException, InvocationTargetException { - // Private method reflection - Method getIncludeFiltersByDomain = - Configuration.class.getDeclaredMethod( - "getIncludeFiltersByDomain", LinkedList.class); - getIncludeFiltersByDomain.setAccessible(true); - - Method getCommonBeanKeysByDomain = - Configuration.class.getDeclaredMethod("getCommonBeanKeysByDomain", HashMap.class); - getCommonBeanKeysByDomain.setAccessible(true); - - // Assert - HashMap> filtersByDomain = - (HashMap>) - getIncludeFiltersByDomain.invoke(null, configurations); - HashMap> parametersIntersectionByDomain = - (HashMap>) - getCommonBeanKeysByDomain.invoke(null, filtersByDomain); - - // Only contains 'org.datadog.jmxfetch.test' domain - assertEquals(parametersIntersectionByDomain.size(), 2); - assertTrue(parametersIntersectionByDomain.containsKey("org.datadog.jmxfetch.test")); - - // Parameters intersection should match: 'param', 'scope' and 'type' - Set parameters = parametersIntersectionByDomain.get("org.datadog.jmxfetch.test"); - assertEquals(parameters.size(), 3); - assertTrue(parameters.contains("param")); - assertTrue(parameters.contains("scope")); - assertTrue(parameters.contains("type")); - } - - /** - * Extract common bean keys among a given list of filters. - * - * @throws SecurityException - * @throws NoSuchMethodException - * @throws InvocationTargetException - * @throws IllegalArgumentException - * @throws IllegalAccessException - */ - @SuppressWarnings("unchecked") - @Test - public void testCommonScope() - throws NoSuchMethodException, SecurityException, IllegalAccessException, - IllegalArgumentException, InvocationTargetException { - // Private method reflection - Method getIncludeFiltersByDomain = - Configuration.class.getDeclaredMethod( - "getIncludeFiltersByDomain", LinkedList.class); - getIncludeFiltersByDomain.setAccessible(true); - - Method getCommonBeanKeysByDomain = - Configuration.class.getDeclaredMethod("getCommonBeanKeysByDomain", HashMap.class); - getCommonBeanKeysByDomain.setAccessible(true); - - Method getCommonScopeByDomain = - Configuration.class.getDeclaredMethod( - "getCommonScopeByDomain", HashMap.class, HashMap.class); - getCommonScopeByDomain.setAccessible(true); - - // Assert - HashMap> filtersByDomain = - (HashMap>) - getIncludeFiltersByDomain.invoke(null, configurations); - HashMap> parametersIntersectionByDomain = - (HashMap>) - getCommonBeanKeysByDomain.invoke(null, filtersByDomain); - HashMap> commonBeanScopeByDomain = - (HashMap>) - getCommonScopeByDomain.invoke( - null, parametersIntersectionByDomain, filtersByDomain); - - // Only contains 'org.datadog.jmxfetch.test' domain - assertEquals(commonBeanScopeByDomain.size(), 2); - assertTrue(commonBeanScopeByDomain.containsKey("org.datadog.jmxfetch.test")); - LinkedHashMap beanScope = - commonBeanScopeByDomain.get("org.datadog.jmxfetch.test"); - - // Bean scope contains 'scope' parameter only - assertEquals(beanScope.size(), 1); - assertTrue(beanScope.containsKey("scope")); - - // Common value is 'sameScope' - assertEquals(beanScope.get("scope"), "sameScope"); - } - - /** - * Stringify a bean pattern to comply with the representation of a MBean - * - * @throws SecurityException - * @throws NoSuchMethodException - * @throws InvocationTargetException - * @throws IllegalArgumentException - * @throws IllegalAccessException - */ - @Test - public void testBeanScopeToString() - throws NoSuchMethodException, SecurityException, IllegalAccessException, - IllegalArgumentException, InvocationTargetException { - // Private method reflection - Method beanScopeToString = - Configuration.class.getDeclaredMethod( - "beanScopeToString", String.class, LinkedHashMap.class); - beanScopeToString.setAccessible(true); - - // Mock parameters - LinkedHashMap beanScope = new LinkedHashMap(); - beanScope.put("type", "someType"); - beanScope.put("param", "someParam"); - - // No domain name, no parameters - assertEquals( - (String) beanScopeToString.invoke(null, null, new LinkedHashMap()), - "*:*"); - - // No domain but parameters - assertEquals( - (String) beanScopeToString.invoke(null, null, beanScope), - "*:type=someType,param=someParam,*"); - - // Domain name with no parameters - assertEquals( - (String) - beanScopeToString.invoke( - null, "org.datadog.com", new LinkedHashMap()), - "org.datadog.com:*"); - - // Domain name with parameters - assertEquals( - (String) beanScopeToString.invoke(null, "org.datadog.com", beanScope), - "org.datadog.com:type=someType,param=someParam,*"); - } + /** + * Extract filters from the configuration list and index by domain name + * @throws SecurityException + * @throws NoSuchMethodException + * @throws InvocationTargetException + * @throws IllegalArgumentException + * @throws IllegalAccessException + */ + @SuppressWarnings("unchecked") + @Test + public void testFiltersByDomain() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ + // Private method reflection + Method getIncludeFiltersByDomain = Configuration.class.getDeclaredMethod("getIncludeFiltersByDomain", LinkedList.class); + getIncludeFiltersByDomain.setAccessible(true); + + // Assert + HashMap> filtersByDomain = (HashMap>) getIncludeFiltersByDomain.invoke(null, configurations); + + // Only contains 'org.datadog.jmxfetch.test' domain + assertEquals(filtersByDomain.size(), 1); + assertTrue(filtersByDomain.containsKey("org.datadog.jmxfetch.test")); + + // 5 filters associated: 4 `include`s (but the last one is split in two) + assertEquals(filtersByDomain.get("org.datadog.jmxfetch.test").size(), 5); + } + + + /** + * Extract common bean keys among a given list of filters. + * @throws SecurityException + * @throws NoSuchMethodException + * @throws InvocationTargetException + * @throws IllegalArgumentException + * @throws IllegalAccessException + */ + @SuppressWarnings("unchecked") + @Test + public void testCommonBeanKeys() throws FileNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ + // Private method reflection + Method getIncludeFiltersByDomain = Configuration.class.getDeclaredMethod("getIncludeFiltersByDomain", LinkedList.class); + getIncludeFiltersByDomain.setAccessible(true); + + Method getCommonBeanKeysByDomain = Configuration.class.getDeclaredMethod("getCommonBeanKeysByDomain", HashMap.class); + getCommonBeanKeysByDomain.setAccessible(true); + + // Assert + HashMap> filtersByDomain = (HashMap>) getIncludeFiltersByDomain.invoke(null, configurations); + HashMap> parametersIntersectionByDomain = (HashMap>) getCommonBeanKeysByDomain.invoke(null, filtersByDomain); + + // Only contains 'org.datadog.jmxfetch.test' domain + assertEquals(parametersIntersectionByDomain.size(), 1); + assertTrue(parametersIntersectionByDomain.containsKey("org.datadog.jmxfetch.test")); + + // Parameters intersection should match: 'param', 'scope' and 'type' + Set parameters = parametersIntersectionByDomain.get("org.datadog.jmxfetch.test"); + assertEquals(parameters.size(), 3); + assertTrue(parameters.contains("param")); + assertTrue(parameters.contains("scope")); + assertTrue(parameters.contains("type")); + } + + /** + * Extract common bean keys among a given list of filters. + * @throws SecurityException + * @throws NoSuchMethodException + * @throws InvocationTargetException + * @throws IllegalArgumentException + * @throws IllegalAccessException + */ + @SuppressWarnings("unchecked") + @Test + public void testCommonScope() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ + // Private method reflection + Method getIncludeFiltersByDomain = Configuration.class.getDeclaredMethod("getIncludeFiltersByDomain", LinkedList.class); + getIncludeFiltersByDomain.setAccessible(true); + + Method getCommonBeanKeysByDomain = Configuration.class.getDeclaredMethod("getCommonBeanKeysByDomain", HashMap.class); + getCommonBeanKeysByDomain.setAccessible(true); + + Method getCommonScopeByDomain = Configuration.class.getDeclaredMethod("getCommonScopeByDomain", HashMap.class, HashMap.class); + getCommonScopeByDomain.setAccessible(true); + + // Assert + HashMap> filtersByDomain = (HashMap>) getIncludeFiltersByDomain.invoke(null, configurations); + HashMap> parametersIntersectionByDomain = (HashMap>) getCommonBeanKeysByDomain.invoke(null, filtersByDomain); + HashMap> commonBeanScopeByDomain = (HashMap>) getCommonScopeByDomain.invoke(null, parametersIntersectionByDomain, filtersByDomain); + + // Only contains 'org.datadog.jmxfetch.test' domain + assertEquals(commonBeanScopeByDomain.size(), 1); + assertTrue(commonBeanScopeByDomain.containsKey("org.datadog.jmxfetch.test")); + LinkedHashMap beanScope = commonBeanScopeByDomain.get("org.datadog.jmxfetch.test"); + + // Bean scope contains 'scope' parameter only + assertEquals(beanScope.size(), 1); + assertTrue(beanScope.containsKey("scope")); + + // Common value is 'sameScope' + assertEquals(beanScope.get("scope"), "sameScope"); + + } + + /** + * Stringify a bean pattern to comply with the representation of a MBean + * @throws SecurityException + * @throws NoSuchMethodException + * @throws InvocationTargetException + * @throws IllegalArgumentException + * @throws IllegalAccessException + */ + @Test + public void testBeanScopeToString() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ + // Private method reflection + Method beanScopeToString = Configuration.class.getDeclaredMethod("beanScopeToString", String.class, LinkedHashMap.class); + beanScopeToString.setAccessible(true); + + // Mock parameters + LinkedHashMap beanScope = new LinkedHashMap(); + beanScope.put("type", "someType"); + beanScope.put("param", "someParam"); + + // No domain name, no parameters + assertEquals((String) beanScopeToString.invoke(null, null, new LinkedHashMap()), "*:*"); + + // No domain but parameters + assertEquals((String) beanScopeToString.invoke(null, null, beanScope), "*:type=someType,param=someParam,*"); + + // Domain name with no parameters + assertEquals((String) beanScopeToString.invoke(null, "org.datadog.com", new LinkedHashMap()), "org.datadog.com:*"); + + // Domain name with parameters + assertEquals((String) beanScopeToString.invoke(null, "org.datadog.com", beanScope), "org.datadog.com:type=someType,param=someParam,*"); + } } diff --git a/src/test/java/org/datadog/jmxfetch/TestParsingJCommander.java b/src/test/java/org/datadog/jmxfetch/TestParsingJCommander.java index 6e57500c2..3c1fca8f9 100644 --- a/src/test/java/org/datadog/jmxfetch/TestParsingJCommander.java +++ b/src/test/java/org/datadog/jmxfetch/TestParsingJCommander.java @@ -24,8 +24,6 @@ public class TestParsingJCommander { private static final List MULTI_CHECK = Arrays.asList("jmx.yaml", "jmx-2.yaml"); private static final String STATUS_LOCATION = "/status/status_location"; private static final String EXIT_FILE_LOCATION = "/status/exit_locationt"; - private static final String IPC_HOSTNAME = "localhost"; - private static final String IPC_PORT = "5001"; private static AppConfig testCommand(String[] params) throws ParameterException { AppConfig appConfig = new AppConfig(); @@ -346,35 +344,11 @@ public void testParsingStatus() { AppConfig.ACTION_COLLECT }; AppConfig appConfig = testCommand(params); - assertTrue(appConfig.updateStatus()); assertNotNull(appConfig.getStatus()); assertEquals(STATUS_LOCATION, appConfig.getStatus().getStatusFileLocation()); assertTrue(appConfig.getStatus().isEnabled()); } - @Test - public void testParsingStatusIPC() { - String[] params = - new String[] { - "--reporter", - REPORTER_CONSOLE, - "--check", - SINGLE_CHECK, - "--conf_directory", - CONF_DIR, - "--ipc_host", - IPC_HOSTNAME, - "--ipc_port", - IPC_PORT, - AppConfig.ACTION_COLLECT - }; - AppConfig appConfig = testCommand(params); - assertTrue(appConfig.updateStatus()); - - assertNotNull(appConfig.getStatus()); - assertTrue(appConfig.getStatus().isEnabled()); - } - @Test public void testParsingExitWatcher() { String[] params = diff --git a/src/test/resources/auto_discovery_configs.json b/src/test/resources/auto_discovery_configs.json deleted file mode 100644 index 197f8f085..000000000 --- a/src/test/resources/auto_discovery_configs.json +++ /dev/null @@ -1,85 +0,0 @@ -{ - "configs": { - "jmx": { - "instances": [ - { - "process_name_regex": ".*surefire.*", - "name": "jmx_test_instance", - "conf": [ - { - "include": { - "scope": "sameScope", - "domain": "org.datadog.jmxfetch.test", - "type": "sameType", - "additional": "additionalParam", - "param": "sameParam" - } - }, - { - "include": { - "scope": "sameScope", - "domain": "org.datadog.jmxfetch.test", - "type": "sameType", - "param": "sameParam" - } - }, - { - "include": { - "scope": "sameScope", - "domain": "org.datadog.jmxfetch.test", - "type": [ - "sameType", - "notTheSameType" - ], - "param": "sameParam" - } - }, - { - "include": { - "bean": [ - "org.datadog.jmxfetch.test:scope=sameScope,param=sameParam,type=sameType", - "org.datadog.jmxfetch.test:scope=sameScope,param=notTheSameParam,type=sameType" - ] - } - } - ] - } - ], - "init_config": null - }, - "cassandra": { - "instances": [ - { - "process_name_regex": ".*surefire.*", - "name": "jmx_first_instance", - "conf": [ - { - "include": { - "attribute": [ - "ShouldBe100" - ], - "bean": "org.apache.cassandra.metrics:keyspace=MyKeySpace,type=ColumnFamily,scope=MyColumnFamily,name=PendingTasks" - } - } - ], - "cassandra_aliasing": true - }, - { - "process_name_regex": ".*surefire.*", - "name": "jmx_second_instance", - "conf": [ - { - "include": { - "attribute": [ - "ShouldBe1000" - ], - "bean": "org.apache.cassandra.metrics:keyspace=MyKeySpace,type=ColumnFamily,scope=MyColumnFamily,name=PendingTasks" - } - } - ] - } - ], - "init_config": null - } - } -} \ No newline at end of file