Skip to content

Commit

Permalink
Add an 'exclude_tags' list to the configuration: fix #103
Browse files Browse the repository at this point in the history
This is usefull to filter out unique tags like host/client id, ...
This may/should be use in combination with the 'histogram' metric_type to avoid loosing metric points.
  • Loading branch information
hush-hush committed Dec 7, 2016
1 parent d25f838 commit 9f66b18
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/main/java/org/datadog/jmxfetch/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Filter {
HashMap<String, Object> filter;
Pattern domainRegex;
ArrayList<Pattern> beanRegexes = null;
ArrayList<String> excludeTags = null;

/**
* A simple class to manipulate include/exclude filter elements more easily
Expand Down Expand Up @@ -105,6 +106,21 @@ public ArrayList<Pattern> getBeanRegexes() {
return this.beanRegexes;
}

public ArrayList<String> getExcludeTags() {
// Return excluded tags as an ArrayList whether it's defined as a list or not

if (this.excludeTags == null) {
if (filter.get("exclude_tags") == null){
this.excludeTags = new ArrayList<String>();
} else {
final Object exclude_tags = filter.get("exclude_tags");
this.excludeTags = toStringArrayList(exclude_tags);
}
}

return this.excludeTags;
}

public String getDomain() {
return (String) filter.get("domain");
}
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/org/datadog/jmxfetch/JMXAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public abstract class JMXAttribute {
protected static final String ALIAS = "alias";
protected static final String METRIC_TYPE = "metric_type";
private final static Logger LOGGER = Logger.getLogger(JMXAttribute.class.getName());
private static final List<String> EXCLUDED_BEAN_PARAMS = Arrays.asList("domain", "domain_regex", "bean_name", "bean", "bean_regex", "attribute");
private static final List<String> EXCLUDED_BEAN_PARAMS = Arrays.asList("domain", "domain_regex", "bean_name", "bean", "bean_regex", "attribute", "exclude_tags");
private static final String FIRST_CAP_PATTERN = "(.)([A-Z][a-z]+)";
private static final String ALL_CAP_PATTERN = "([a-z0-9])([A-Z])";
private static final String METRIC_REPLACEMENT = "([^a-zA-Z0-9_.]+)|(^[^a-zA-Z]+)";
Expand Down Expand Up @@ -73,6 +73,23 @@ public abstract class JMXAttribute {
this.defaultTagsList = sanitizeParameters(beanParametersList);
}

/**
* Remove tags listed in the 'exclude_tags' list from configuration.
*/
private void applyTagsBlackList() {
Filter include = this.matchingConf.getInclude();
if (include != null) {

for (String excludedTagName : include.getExcludeTags()) {
for (String tag: this.defaultTagsList) {
if (tag.startsWith(excludedTagName + ":")) {
this.defaultTagsList.remove(tag);
}
}
}
}
}

public static HashMap<String, String> getBeanParametersHash(String beanParametersString) {
String[] beanParameters = beanParametersString.split(",");
HashMap<String, String> beanParamsMap = new HashMap<String, String>(beanParameters.length);
Expand Down Expand Up @@ -349,6 +366,9 @@ public Configuration getMatchingConf() {

public void setMatchingConf(Configuration matchingConf) {
this.matchingConf = matchingConf;

// Now that we have the matchingConf, we can filter out excluded tags
applyTagsBlackList();
}

MBeanAttributeInfo getAttribute() {
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 @@ -366,6 +366,31 @@ public void testMetricTypes() throws Exception {
assertMetric("test.counter", 0.0, commonTags, 5, "counter");
}

@Test
public void testExcludeTags() throws Exception {
// We expose a few metrics through JMX
SimpleTestJavaApp testApp = new SimpleTestJavaApp();
registerMBean(testApp, "org.datadog.jmxfetch.test:type=SimpleTestJavaApp");

// We do a first collection
initApplication("jmx_exclude_tags.yml");
run();
LinkedList<HashMap<String, Object>> metrics = getMetrics();

// We test for the presence and the value of the metrics we want to collect.
// Tags "type", "newTag" and "env" should be excluded
List<String> commonTags = Arrays.asList(
"instance:jmx_test_instance",
"jmx_domain:org.datadog.jmxfetch.test");

// 15 = 13 metrics from java.lang + the 2 collected (gauge and histogram)
assertEquals(15, metrics.size());

// There should only left 2 tags per metric
assertMetric("test1.gauge", 1000.0, commonTags, 2, "gauge");
assertMetric("test1.histogram", 424242, commonTags, 2, "histogram");
}

/**
* FIXME: Split this test in multiple sub-tests.
*/
Expand Down
22 changes: 22 additions & 0 deletions src/test/resources/jmx_exclude_tags.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
init_config:

instances:
- process_name_regex: .*surefire.*
name: jmx_test_instance
tags:
env: stage
newTag: test
conf:
- include:
domain: org.datadog.jmxfetch.test
exclude_tags:
- env
- type
- newTag
attribute:
ShouldBe1000:
metric_type: gauge
alias: test1.gauge
Int424242:
metric_type: histogram
alias: test1.histogram

0 comments on commit 9f66b18

Please sign in to comment.