-
Notifications
You must be signed in to change notification settings - Fork 70
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
Support list of filters + tests #39
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package org.datadog.jmxfetch; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.ArrayList; | ||
import java.util.Set; | ||
import java.lang.ClassCastException; | ||
|
||
|
||
class Filter { | ||
LinkedHashMap<String, Object> filter; | ||
|
||
/** | ||
* A simple class to manipulate include/exclude filter elements more easily | ||
* A filter may contain: | ||
* - A domain (key: 'domain') | ||
* - Bean names (key: 'bean' or 'bean_name') | ||
* - Attributes (key: 'attribute') | ||
* - Additional bean parameters (other keys) | ||
*/ | ||
|
||
@SuppressWarnings("unchecked") | ||
public Filter(Object filter) { | ||
LinkedHashMap<String, Object> castFilter; | ||
if (filter != null) { | ||
castFilter = (LinkedHashMap<String, Object>) filter; | ||
} else{ | ||
castFilter = new LinkedHashMap<String, Object>(); | ||
} | ||
this.filter = castFilter; | ||
} | ||
|
||
public String toString() { | ||
return this.filter.toString(); | ||
} | ||
|
||
public Set<String> keySet() { | ||
return filter.keySet(); | ||
} | ||
|
||
|
||
@SuppressWarnings({ "unchecked", "serial" }) | ||
private static ArrayList<String> toStringArrayList(final Object toCast) { | ||
// Return object as an ArrayList wherever it's defined as | ||
// list or not | ||
// | ||
// ### Example | ||
// object: | ||
// - firstValue | ||
// - secondValue | ||
// ### OR | ||
// object: singleValue | ||
// ### | ||
try{ | ||
return (ArrayList<String>) toCast; | ||
} catch (ClassCastException e){ | ||
return new ArrayList<String>() {{ | ||
add(((String) toCast)); | ||
}}; | ||
} | ||
} | ||
|
||
|
||
public ArrayList<String> getBeanNames() { | ||
if (isEmptyBeanName()){ | ||
return new ArrayList<String>(); | ||
} | ||
final Object beanNames = (filter.get("bean") != null) ? filter.get("bean") : filter.get("bean_name"); | ||
// Return bean names as an ArrayList wherever it's defined as | ||
// list or not | ||
// | ||
// ### Example | ||
// bean: | ||
// - org.apache.cassandra.db:type=Caches,keyspace=system,cache=HintsColumnFamilyKeyCache | ||
// - org.datadog.jmxfetch.test:type=type=SimpleTestJavaApp | ||
// ### OR | ||
// bean: org.datadog.jmxfetch.test:type=type=SimpleTestJavaApp | ||
// ### | ||
return toStringArrayList(beanNames); | ||
} | ||
|
||
public String getDomain() { | ||
return (String) filter.get("domain"); | ||
} | ||
|
||
public Object getAttribute() { | ||
return filter.get("attribute"); | ||
} | ||
|
||
public ArrayList<String> getParameterValues(String parameterName) { | ||
// Return bean attributes values as an ArrayList wherever it's defined as | ||
// list or not | ||
// | ||
// ### Example | ||
// bean_parameter: | ||
// - exampleType1 | ||
// - exampleType2 | ||
// ### OR | ||
// bean_parameter: onlyOneType | ||
// ### | ||
final Object beanValues = filter.get(parameterName); | ||
return toStringArrayList(beanValues); | ||
} | ||
|
||
public boolean isEmptyBeanName() { | ||
return (filter.get("bean") == null && filter.get("bean_name") == null); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,11 +30,12 @@ public abstract class JMXAttribute { | |
private ObjectInstance jmxInstance; | ||
private String domain; | ||
private String beanName; | ||
private HashMap<String, String> beanParameters; | ||
private String attributeName; | ||
private LinkedHashMap<Object, Object> valueConversions; | ||
protected String[] tags; | ||
private Configuration matchingConf; | ||
private LinkedList<String> defaultTags; | ||
private LinkedList<String> defaultTagsList; | ||
|
||
JMXAttribute(MBeanAttributeInfo attribute, ObjectInstance jmxInstance, String instanceName, | ||
Connection connection, HashMap<String, String> instanceTags) { | ||
|
@@ -44,22 +45,44 @@ public abstract class JMXAttribute { | |
this.connection = connection; | ||
|
||
this.beanName = jmxInstance.getObjectName().toString(); | ||
this.attributeName = attribute.getName(); | ||
|
||
// A bean name is formatted like that: org.apache.cassandra.db:type=Caches,keyspace=system,cache=HintsColumnFamilyKeyCache | ||
// i.e. : domain:bean_parameter1,bean_parameter2 | ||
String[] split = this.beanName.split(":"); | ||
this.domain = split[0]; | ||
this.attributeName = attribute.getName(); | ||
String[] splitBeanName = this.beanName.split(":"); | ||
String domain = splitBeanName[0]; | ||
String beanParameters = splitBeanName[1]; | ||
LinkedList<String> defaultTags = getBeanTags(instanceName, domain, beanParameters, instanceTags); | ||
HashMap<String, String> beanParametersHash = getBeanParametersHash(defaultTags); | ||
|
||
this.domain = domain; | ||
this.beanParameters = beanParametersHash; | ||
this.defaultTagsList = defaultTags; | ||
} | ||
|
||
private static HashMap<String, String> getBeanParametersHash(LinkedList<String> beanParameters) { | ||
HashMap<String, String> beanParams = new HashMap<String, String>(); | ||
for (String param : beanParameters) { | ||
String[] paramSplit = param.split(":"); | ||
beanParams.put(new String(paramSplit[0]), new String(paramSplit[1])); | ||
} | ||
|
||
return beanParams; | ||
} | ||
|
||
LinkedList<String> beanTags = new LinkedList<String>(Arrays.asList(new String(split[1]).replace("=", ":").split(","))); | ||
|
||
private static LinkedList<String> getBeanTags(String instanceName, String domain, String beanParameters, HashMap<String, String> instanceTags) { | ||
LinkedList<String> beanTags = new LinkedList<String>(Arrays.asList(new String(beanParameters).replace("=", ":").split(","))); | ||
beanTags.add("instance:" + instanceName); | ||
beanTags.add("jmx_domain:" + domain); | ||
|
||
if (instanceTags != null) { | ||
for (Map.Entry<String, String> tag : instanceTags.entrySet()) { | ||
beanTags.add(tag.getKey() + ":" + tag.getValue()); | ||
} | ||
} | ||
this.defaultTags = beanTags; | ||
|
||
return beanTags; | ||
} | ||
|
||
static String convertMetricName(String metricName) { | ||
|
@@ -100,39 +123,39 @@ Object getJmxValue() throws AttributeNotFoundException, InstanceNotFoundExceptio | |
} | ||
|
||
boolean matchDomain(Configuration conf) { | ||
LinkedHashMap<String, Object> include = conf.getInclude(); | ||
return include.get("domain") == null || include.get("domain").equals(domain); | ||
String includeDomain = conf.getInclude().getDomain(); | ||
return includeDomain == null || includeDomain.equals(domain); | ||
} | ||
|
||
boolean excludeMatchDomain(Configuration conf) { | ||
LinkedHashMap<String, Object> exclude = conf.getExclude(); | ||
return exclude.get("domain") != null && exclude.get("domain").equals(domain); | ||
String excludeDomain = conf.getExclude().getDomain(); | ||
return excludeDomain != null && excludeDomain.equals(domain); | ||
} | ||
|
||
boolean excludeMatchBean(Configuration conf) { | ||
LinkedHashMap<String, Object> exclude = conf.getExclude(); | ||
String bean = (String) exclude.get("bean"); | ||
String confBeanName = (String) exclude.get("bean_name"); | ||
|
||
if (beanName.equals(bean) || beanName.equals(confBeanName)) { | ||
return true; | ||
} | ||
|
||
for (String bean_attr : exclude.keySet()) { | ||
if (EXCLUDED_BEAN_PARAMS.contains(bean_attr)) { | ||
continue; | ||
} | ||
|
||
HashMap<String, String> beanParams = new HashMap<String, String>(); | ||
for (String param : this.defaultTags) { | ||
String[] paramSplit = param.split(":"); | ||
beanParams.put(new String(paramSplit[0]), new String(paramSplit[1])); | ||
} | ||
|
||
if (exclude.get(bean_attr).equals(beanParams.get(bean_attr))) { | ||
return true; | ||
} | ||
} | ||
Filter exclude = conf.getExclude(); | ||
ArrayList<String> beanNames = exclude.getBeanNames(); | ||
|
||
if(beanNames.contains(beanName)){ | ||
return true; | ||
} | ||
|
||
for (String bean_attr : exclude.keySet()) { | ||
if (EXCLUDED_BEAN_PARAMS.contains(bean_attr)) { | ||
continue; | ||
} | ||
|
||
if (beanParameters.get(bean_attr) == null) { | ||
continue; | ||
} | ||
|
||
ArrayList<String> beanValues = exclude.getParameterValues(bean_attr); | ||
for (String beanVal : beanValues) { | ||
if (beanParameters.get(bean_attr).equals(beanVal)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
|
@@ -180,32 +203,31 @@ Object convertMetricValue(Object metricValue) { | |
} | ||
|
||
boolean matchBean(Configuration configuration) { | ||
boolean matchBeanName = false; | ||
LinkedHashMap<String, Object> include = configuration.getInclude(); | ||
if (include.get("bean") == null && include.get("bean_name") == null) { | ||
matchBeanName = true; | ||
} else if (include.get("bean") != null) { | ||
matchBeanName = include.get("bean").equals(beanName); | ||
} else if (include.get("bean_name") != null) { | ||
matchBeanName = include.get("bean_name").equals(beanName); | ||
} | ||
Filter include = configuration.getInclude(); | ||
|
||
if (!matchBeanName) { | ||
if (!include.isEmptyBeanName() && !include.getBeanNames().contains(beanName)) { | ||
return false; | ||
} | ||
|
||
for (String bean_attr : include.keySet()) { | ||
if (EXCLUDED_BEAN_PARAMS.contains(bean_attr)) { | ||
continue; | ||
} | ||
|
||
HashMap<String, String> beanParams = new HashMap<String, String>(); | ||
for (String param : this.defaultTags) { | ||
String[] paramSplit = param.split(":"); | ||
beanParams.put(new String(paramSplit[0]), new String(paramSplit[1])); | ||
if (beanParameters.get(bean_attr) == null) { | ||
continue; | ||
} | ||
|
||
if (beanParams.get(bean_attr) == null || !beanParams.get(bean_attr).equals(include.get(bean_attr))) { | ||
ArrayList<String> beanValues = include.getParameterValues(bean_attr); | ||
|
||
boolean matchBeanAttr = false; | ||
for (String beanVal : beanValues) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
if (!beanParameters.get(bean_attr).equals(beanVal)) { | ||
continue; | ||
} | ||
return true; | ||
} | ||
// We havent' found a match among our attribute values list | ||
if (!matchBeanAttr) { | ||
return false; | ||
} | ||
} | ||
|
@@ -215,7 +237,7 @@ boolean matchBean(Configuration configuration) { | |
@SuppressWarnings("unchecked") | ||
HashMap<Object, Object> getValueConversions() { | ||
if (valueConversions == null) { | ||
Object includedAttribute = matchingConf.getInclude().get("attribute"); | ||
Object includedAttribute = matchingConf.getInclude().getAttribute(); | ||
if (includedAttribute instanceof LinkedHashMap<?, ?>) { | ||
String attributeName = this.attribute.getName(); | ||
LinkedHashMap<String, LinkedHashMap<Object, Object>> attribute = | ||
|
@@ -251,20 +273,21 @@ protected String[] getTags() { | |
return tags; | ||
} | ||
|
||
LinkedHashMap<String, Object> include = matchingConf.getInclude(); | ||
Filter include = matchingConf.getInclude(); | ||
if (include != null) { | ||
if (include.get("attribute") instanceof LinkedHashMap<?, ?>) { | ||
LinkedHashMap<String, ArrayList<String>> attributeParams = ((LinkedHashMap<String, LinkedHashMap<String, ArrayList<String>>>)(include.get("attribute"))).get(attributeName); | ||
Object includeAttribute = include.getAttribute(); | ||
if (includeAttribute instanceof LinkedHashMap<?, ?>) { | ||
LinkedHashMap<String, ArrayList<String>> attributeParams = ((LinkedHashMap<String, LinkedHashMap<String, ArrayList<String>>>)includeAttribute).get(attributeName); | ||
if (attributeParams != null) { | ||
ArrayList<String> yamlTags = attributeParams.get("tags"); | ||
if ( yamlTags != null) { | ||
defaultTags.addAll(yamlTags); | ||
defaultTagsList.addAll(yamlTags); | ||
} | ||
} | ||
} | ||
} | ||
tags = new String[defaultTags.size()]; | ||
tags = defaultTags.toArray(tags); | ||
tags = new String[defaultTagsList.size()]; | ||
tags = defaultTagsList.toArray(tags); | ||
return tags; | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here