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

Support list of filters + tests #39

Merged
merged 1 commit into from
Mar 14, 2015
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
16 changes: 6 additions & 10 deletions src/main/java/org/datadog/jmxfetch/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,27 @@
public class Configuration {

private LinkedHashMap<String, Object> conf;
private LinkedHashMap<String, Object> include;
private LinkedHashMap<String, Object> exclude;
private Filter include;
private Filter exclude;

/**
* A simple class to access configuration elements more easily
*/
@SuppressWarnings("unchecked")
public Configuration(LinkedHashMap<String, Object> conf) {
this.conf = conf;
this.include = (LinkedHashMap<String, Object>) (conf.get("include"));
this.exclude = (LinkedHashMap<String, Object>) (conf.get("exclude"));
if (this.exclude == null) {
this.exclude = new LinkedHashMap<String, Object>();
}
this.include = new Filter(conf.get("include"));
this.exclude = new Filter(conf.get("exclude"));
}

public LinkedHashMap<String, Object> getConf() {
return conf;
}

public LinkedHashMap<String, Object> getInclude() {
public Filter getInclude() {
return include;
}

public LinkedHashMap<String, Object> getExclude() {
public Filter getExclude() {
return exclude;
}

Expand Down
108 changes: 108 additions & 0 deletions src/main/java/org/datadog/jmxfetch/Filter.java
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);
}

}
135 changes: 79 additions & 56 deletions src/main/java/org/datadog/jmxfetch/JMXAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Copy link

Choose a reason for hiding this comment

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

Same here


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;
}

Expand Down Expand Up @@ -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) {
Copy link

Choose a reason for hiding this comment

The 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;
}
}
Expand All @@ -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 =
Expand Down Expand Up @@ -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;
}

Expand Down
Loading