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

Maxime/add additional tags option #117

Merged
merged 1 commit into from
Dec 15, 2016
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
15 changes: 15 additions & 0 deletions src/main/java/org/datadog/jmxfetch/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Filter {
Pattern domainRegex;
ArrayList<Pattern> beanRegexes = null;
ArrayList<String> excludeTags = null;
HashMap<String, String> additionalTags = null;

/**
* A simple class to manipulate include/exclude filter elements more easily
Expand Down Expand Up @@ -121,6 +122,20 @@ public ArrayList<String> getExcludeTags() {
return this.excludeTags;
}

public HashMap<String, String> getAdditionalTags() {
// Return additional tags

if (this.additionalTags == null) {
if (filter.get("tags") == null){
this.additionalTags = new HashMap<String, String>();
} else {
this.additionalTags = (HashMap<String, String>)filter.get("tags");
}
}

return this.additionalTags;
}

public String getDomain() {
return (String) filter.get("domain");
}
Expand Down
38 changes: 29 additions & 9 deletions src/main/java/org/datadog/jmxfetch/JMXAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ 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", "exclude_tags");
private static final List<String> EXCLUDED_BEAN_PARAMS = Arrays.asList("domain", "domain_regex", "bean_name", "bean",
"bean_regex", "attribute", "exclude_tags", "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 @@ -91,6 +92,19 @@ private void applyTagsBlackList() {
}
}

/**
* Add alias tag from the 'tag_alias' configuration list
*/
private void addAdditionalTags() {
Filter include = this.matchingConf.getInclude();
if (include != null) {

for (Map.Entry<String, String> tag : include.getAdditionalTags().entrySet()) {
this.defaultTagsList.add(tag.getKey() + ":" + this.replaceByAlias(tag.getValue()));
}
}
}

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 @@ -368,8 +382,11 @@ public Configuration getMatchingConf() {
public void setMatchingConf(Configuration matchingConf) {
this.matchingConf = matchingConf;

// Now that we have the matchingConf, we can filter out excluded tags
applyTagsBlackList();
// Now that we have the matchingConf we can:
// - add additional tags
this.addAdditionalTags();
// - filter out excluded tags
this.applyTagsBlackList();
}

MBeanAttributeInfo getAttribute() {
Expand Down Expand Up @@ -454,12 +471,7 @@ private String getCassandraAlias() {
* returns a metric name `my.metric.bar.toto`
*/
private String getUserAlias(LinkedHashMap<String, LinkedHashMap<String, String>> attribute, String fullAttributeName){
String alias = attribute.get(fullAttributeName).get(ALIAS);

// Bean parameters
for (Map.Entry<String, String> param : beanParameters.entrySet()) {
alias = alias.replace("$" + param.getKey(), param.getValue());
}
String alias = this.replaceByAlias(attribute.get(fullAttributeName).get(ALIAS));

// Attribute & domain
alias = alias.replace("$attribute", fullAttributeName);
Expand All @@ -468,6 +480,14 @@ private String getUserAlias(LinkedHashMap<String, LinkedHashMap<String, String>>
return alias;
}

private String replaceByAlias(String alias){
// Bean parameters
for (Map.Entry<String, String> param : beanParameters.entrySet()) {
alias = alias.replace("$" + param.getKey(), param.getValue());
}
return alias;
}

/**
* Overload `getAlias` method.
*
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/org/datadog/jmxfetch/TestApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,37 @@ public void testExcludeTags() throws Exception {
assertMetric("test1.histogram", 424242, commonTags, 2, "histogram");
}

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

// We do a first collection
initApplication("jmx_additional_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",
"type:SimpleTestJavaApp",
"name:testName",
"simple:SimpleTestJavaApp",
"raw_value:value",
"unknown_tag:$does-not-exist",
"multiple:SimpleTestJavaApp-testName");

// 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, 8, "gauge");
assertMetric("test1.histogram", 424242, commonTags, 8, "histogram");
}

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

instances:
- process_name_regex: .*surefire.*
name: jmx_test_instance
conf:
- include:
domain: org.datadog.jmxfetch.test
tags:
simple: $type
raw_value: value
unknown_tag: $does-not-exist
multiple: $type-$name
attribute:
ShouldBe1000:
metric_type: gauge
alias: test1.gauge
Int424242:
metric_type: histogram
alias: test1.histogram