Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maximum length for instance configs should be longer - improve tests. #147

Merged
merged 4 commits into from
Aug 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/main/java/org/datadog/jmxfetch/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.InputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.Math;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Enumeration;
Expand Down Expand Up @@ -45,6 +46,9 @@ public class App {
private static final String AD_LEGACY_CONFIG_SEP = "#### SERVICE-DISCOVERY ####";
private static final String AD_CONFIG_TERM = "#### AUTO-DISCOVERY TERM ####";
private static final String AD_LEGACY_CONFIG_TERM = "#### SERVICE-DISCOVERY TERM ####";
private static final int AD_MAX_NAME_LEN = 80;
private static final int AD_MAX_MAG_INSTANCES = 4; // 1000 instances ought to be enough for anyone

private static int loopCounter;
private AtomicBoolean reinit = new AtomicBoolean(false);
private ConcurrentHashMap<String, YamlParser> configs;
Expand Down Expand Up @@ -216,6 +220,8 @@ public boolean processAutoDiscovery(byte[] buffer) {
if (this.addConfig(name, yaml)){
reinit = true;
LOGGER.debug("Configuration added succesfully reinit in order");
} else {
LOGGER.debug("Unable to apply configuration.");
}
} catch(UnsupportedEncodingException e) {
LOGGER.debug("Unable to parse byte buffer to UTF-8 String.");
Expand All @@ -225,6 +231,10 @@ public boolean processAutoDiscovery(byte[] buffer) {
return reinit;
}

protected ArrayList<Instance> getInstances() {
return this.instances;
}

void start() {
// Main Loop that will periodically collect metrics from the JMX Server
long start_ms = System.currentTimeMillis();
Expand Down Expand Up @@ -414,19 +424,31 @@ public void doIteration() {
}

public boolean addConfig(String name, YamlParser config) {
// named groups not supported with Java6: "(?<check>.{1,30})_(?<version>\\d{0,30})"
Pattern pattern = Pattern.compile(AUTO_DISCOVERY_PREFIX+"(.{1,30})_(\\d{0,30})");
// named groups not supported with Java6:
// "AUTO_DISCOVERY_PREFIX(?<check>.{1,80})_(?<version>\\d{0,AD_MAX_MAG_INSTANCES})"
// + 2 cause of underscores.
if (name.length() > AUTO_DISCOVERY_PREFIX.length() +
AD_MAX_NAME_LEN + AD_MAX_MAG_INSTANCES + 2) {
LOGGER.debug("Name too long - skipping: " + name);
return false;
}
String patternText = AUTO_DISCOVERY_PREFIX+"(.{1," + AD_MAX_NAME_LEN +
"})_(\\d{0,"+ AD_MAX_MAG_INSTANCES +"})";

Pattern pattern = Pattern.compile(patternText);

Matcher matcher = pattern.matcher(name);
if (!matcher.find()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to have debug output here too, in case it were to happen

// bad name.
LOGGER.debug("Cannot match instance name: " + name);
return false;
}

// Java 6 doesn't allow name matching - group 1 is "check"
String check = matcher.group(1);
if (this.configs.containsKey(check)) {
// there was already a file config for the check.
LOGGER.debug("Key already present - skipping: " + name);
return false;
}

Expand Down
25 changes: 25 additions & 0 deletions src/test/java/org/datadog/jmxfetch/TestApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -627,4 +627,29 @@ public void testServiceDiscovery() throws Exception {
assertMetric("this.is.100.bar.baz", tags, 4);
assertMetric("org.datadog.jmxfetch.test.baz.hashmap.thisis0", tags, 4);
}

/**
* Test JMX Service Discovery.
*
*/
@Test
public void testServiceDiscoveryLong() throws Exception {
// We expose a few metrics through JMX
SimpleTestJavaApp test = new SimpleTestJavaApp();
registerMBean(test, "org.datadog.jmxfetch.test:foo=Bar,qux=Baz");
registerMBean(test, "org.datadog.jmxfetch.test:type=SimpleTestJavaApp,scope=Co|olScope,host=localhost,component=");
registerMBean(test, "org.apache.cassandra.metrics:keyspace=MyKeySpace,type=ColumnFamily,scope=MyColumnFamily,name=PendingTasks");
initApplication("jmx_alias_match.yaml", "jmx_sd_pipe_longname.txt");

// Collecting metrics
run();
LinkedList<HashMap<String, Object>> metrics = getMetrics();
ArrayList<Instance> instances = getInstances();

assertEquals(31, metrics.size());

// 2(jmx_alias_match) + 1 (jmx_sd_pipe_longname discards one)
assertEquals(2, instances.size());

}
}
7 changes: 7 additions & 0 deletions src/test/java/org/datadog/jmxfetch/TestCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ protected void run(){
}
}

/**
* Return configured instances
*/
protected ArrayList<Instance> getInstances() {
return app.getInstances();
}

/**
* Return JMXFetch reporter.
*/
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/jmx_sd_pipe.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ instances:
Hashmap.thisis0:
metric_type: gauge
alias: bean.parameters.should.not.match


#### AUTO-DISCOVERY TERM ####
44 changes: 44 additions & 0 deletions src/test/resources/jmx_sd_pipe_longname.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#### SERVICE-DISCOVERY ####
# jmx_0
init_config:

instances:
- process_name_regex: .*surefire.*
name: jmx_test_instance
conf:
- include:
bean: org.datadog.jmxfetch.test:type=SimpleTestJavaApp,scope=Co|olScope,host=localhost,component=
attribute:
ShouldBe100:
metric_type: gauge
alias: this.is.100
- include:
bean: org.datadog.jmxfetch.test:type=WrongType,scope=WrongScope,host=localhost,component=
attribute:
Hashmap.thisis0:
metric_type: gauge
alias: bean.parameters.should.not.match

#### SERVICE-DISCOVERY ####
# jmxbutthisisincrediblylongandshouldneverbeavalidnamebecausewhoneedsanamelikethishonestly_0
init_config:

instances:
- process_name_regex: .*surefire.*
name: jmx_test_instance
conf:
- include:
bean: org.datadog.jmxfetch.test:type=SimpleTestJavaApp,scope=Co|olScope,host=localhost,component=
attribute:
ShouldBe100:
metric_type: gauge
alias: this.is.100
- include:
bean: org.datadog.jmxfetch.test:type=WrongType,scope=WrongScope,host=localhost,component=
attribute:
Hashmap.thisis0:
metric_type: gauge
alias: bean.parameters.should.not.match


#### AUTO-DISCOVERY TERM ####