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

Ya memory improvement: queryMBeansqueryNames #71

Merged
merged 1 commit into from
Aug 26, 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
6 changes: 3 additions & 3 deletions src/main/java/org/datadog/jmxfetch/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public MBeanAttributeInfo[] getAttributesForBean(ObjectName bean_name)
return mbs.getMBeanInfo(bean_name).getAttributes();
}

public Set<ObjectInstance> queryMBeans(ObjectName name) throws IOException {
public Set<ObjectName> queryNames(ObjectName name) throws IOException {
String scope = (name != null) ? name.toString() : "*:*";
LOGGER.debug("Querying beans on scope: " + scope);
return mbs.queryMBeans(name, null);
LOGGER.debug("Querying bean names on scope: " + scope);
return mbs.queryNames(name, null);
}

protected void createConnection() throws IOException {
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/org/datadog/jmxfetch/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.Set;

import javax.management.MBeanAttributeInfo;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.security.auth.login.FailedLoginException;

Expand All @@ -29,7 +28,7 @@ public class Instance {
private final static int MAX_RETURNED_METRICS = 350;
private final static int DEFAULT_REFRESH_BEANS_PERIOD = 600;

private Set<ObjectInstance> beans;
private Set<ObjectName> beans;
private LinkedList<String> beanScopes;
private LinkedList<Configuration> configurationList = new LinkedList<Configuration>();
private LinkedList<JMXAttribute> matchingAttributes;
Expand Down Expand Up @@ -181,14 +180,13 @@ private void getMatchingAttributes() {
reporter.displayInstanceName(this);
}

for (ObjectInstance bean : beans) {
for (ObjectName beanName : beans) {
if (limitReached) {
LOGGER.debug("Limit reached");
if (action.equals(AppConfig.ACTION_COLLECT)) {
break;
}
}
ObjectName beanName = bean.getObjectName();
MBeanAttributeInfo[] attributeInfos;

try {
Expand Down Expand Up @@ -218,10 +216,10 @@ private void getMatchingAttributes() {
String attributeType = attributeInfo.getType();
if (SIMPLE_TYPES.contains(attributeType)) {
LOGGER.debug("Attribute: " + beanName + " : " + attributeInfo + " has attributeInfo simple type");
jmxAttribute = new JMXSimpleAttribute(attributeInfo, bean, instanceName, connection, tags);
jmxAttribute = new JMXSimpleAttribute(attributeInfo, beanName, instanceName, connection, tags);
} else if (COMPOSED_TYPES.contains(attributeType)) {
LOGGER.debug("Attribute: " + beanName + " : " + attributeInfo + " has attributeInfo complex type");
jmxAttribute = new JMXComplexAttribute(attributeInfo, bean, instanceName, connection, tags);
jmxAttribute = new JMXComplexAttribute(attributeInfo, beanName, instanceName, connection, tags);
} else {
try {
LOGGER.debug("Attribute: " + beanName + " : " + attributeInfo + " has an unsupported type: " + attributeType);
Expand Down Expand Up @@ -271,17 +269,17 @@ public LinkedList<String> getBeansScopes(){
}

private void refreshBeansList() throws IOException {
this.beans = new HashSet<ObjectInstance>();
this.beans = new HashSet<ObjectName>();
try {
LinkedList<String> beanScopes = getBeansScopes();
for (String scope : beanScopes) {
ObjectName name = new ObjectName(scope);
this.beans.addAll(connection.queryMBeans(name));
this.beans.addAll(connection.queryNames(name));
}
}
catch (Exception e) {
LOGGER.error("Unable to compute a common bean scope, querying all beans as a fallback", e);
this.beans = connection.queryMBeans(null);
this.beans = connection.queryNames(null);
}
this.lastRefreshTime = System.currentTimeMillis();
}
Expand Down
31 changes: 15 additions & 16 deletions src/main/java/org/datadog/jmxfetch/JMXAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import javax.management.InstanceNotFoundException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.ReflectionException;

import org.apache.log4j.Logger;
Expand All @@ -31,29 +31,28 @@ public abstract class JMXAttribute {
private static final String DOT_UNDERSCORE = "_*\\._*";
private MBeanAttributeInfo attribute;
private Connection connection;
private ObjectInstance jmxInstance;
private ObjectName beanName;
private String domain;
private String beanName;
private String beanStringName;
private HashMap<String, String> beanParameters;
private String attributeName;
private LinkedHashMap<Object, Object> valueConversions;
protected String[] tags;
private Configuration matchingConf;
private LinkedList<String> defaultTagsList;

JMXAttribute(MBeanAttributeInfo attribute, ObjectInstance jmxInstance, String instanceName,
JMXAttribute(MBeanAttributeInfo attribute, ObjectName beanName, String instanceName,
Connection connection, HashMap<String, String> instanceTags) {
this.attribute = attribute;
this.jmxInstance = jmxInstance;
this.beanName = beanName;
this.matchingConf = null;
this.connection = connection;

this.beanName = jmxInstance.getObjectName().toString();
this.attributeName = attribute.getName();
this.beanStringName = beanName.toString();

// 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[] splitBeanName = this.beanName.split(":");
String[] splitBeanName = beanStringName.split(":");
String domain = splitBeanName[0];
String beanParameters = splitBeanName[1];
LinkedList<String> beanParametersList = getBeanParametersList(instanceName, domain, beanParameters, instanceTags);
Expand Down Expand Up @@ -120,7 +119,7 @@ static String convertMetricName(String metricName) {

@Override
public String toString() {
return "Bean name: " + beanName +
return "Bean name: " + beanStringName +
" - Attribute name: " + attributeName +
" - Attribute type: " + attribute.getType();
}
Expand All @@ -139,13 +138,13 @@ public int getMetricsCount() {
try {
return this.getMetrics().size();
} catch (Exception e) {
LOGGER.warn("Unable to get metrics from " + beanName);
LOGGER.warn("Unable to get metrics from " + beanStringName);
return 0;
}
}

Object getJmxValue() throws AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException, IOException {
return this.connection.getAttribute(this.jmxInstance.getObjectName(), this.attribute.getName());
return this.connection.getAttribute(this.beanName, this.attribute.getName());
}

boolean matchDomain(Configuration conf) {
Expand Down Expand Up @@ -214,7 +213,7 @@ private boolean matchBeanRegex(Filter filter, boolean matchIfNoRegex) {
}

for (Pattern beanRegex : beanRegexes) {
if(beanRegex.matcher(beanName).matches()) {
if(beanRegex.matcher(beanStringName).matches()) {
return true;
}
}
Expand All @@ -226,7 +225,7 @@ private boolean matchBeanName(Configuration configuration) {
boolean matchBeanAttr = true;
Filter include = configuration.getInclude();

if (!include.isEmptyBeanName() && !include.getBeanNames().contains(beanName)) {
if (!include.isEmptyBeanName() && !include.getBeanNames().contains(beanStringName)) {
return false;
}

Expand Down Expand Up @@ -260,7 +259,7 @@ private boolean excludeMatchBeanName(Configuration conf) {
Filter exclude = conf.getExclude();
ArrayList<String> beanNames = exclude.getBeanNames();

if(beanNames.contains(beanName)){
if(beanNames.contains(beanStringName)){
return true;
}

Expand Down Expand Up @@ -348,8 +347,8 @@ protected String[] getTags() {
return tags;
}

String getBeanName() {
return beanName;
String getBeanStringName() {
return beanStringName;
}

String getAttributeName() {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/datadog/jmxfetch/JMXComplexAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import javax.management.InstanceNotFoundException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.openmbean.CompositeData;

Expand All @@ -21,9 +21,9 @@ public class JMXComplexAttribute extends JMXAttribute {

private HashMap<String, HashMap<String, Object>> subAttributeList;

public JMXComplexAttribute(MBeanAttributeInfo attribute, ObjectInstance instance, String instanceName,
public JMXComplexAttribute(MBeanAttributeInfo attribute, ObjectName beanName, String instanceName,
Connection connection, HashMap<String, String> instanceTags) {
super(attribute, instance, instanceName, connection, instanceTags);
super(attribute, beanName, instanceName, connection, instanceTags);
this.subAttributeList = new HashMap<String, HashMap<String, Object>>();
}

Expand Down Expand Up @@ -118,9 +118,9 @@ private String getAlias(String subAttribute) {
if (include.getAttribute() instanceof LinkedHashMap<?, ?>) {
return ((LinkedHashMap<String, LinkedHashMap<String, String>>) (include.getAttribute())).get(subAttributeName).get("alias");
} else if (conf.get("metric_prefix") != null) {
return conf.get("metric_prefix") + "." + getBeanName().split(":")[0] + "." + subAttributeName;
return conf.get("metric_prefix") + "." + getBeanStringName().split(":")[0] + "." + subAttributeName;
}
return "jmx." + getBeanName().split(":")[0] + "." + subAttributeName;
return "jmx." + getBeanStringName().split(":")[0] + "." + subAttributeName;
}


Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/datadog/jmxfetch/JMXSimpleAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import javax.management.InstanceNotFoundException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.ReflectionException;

@SuppressWarnings("unchecked")
Expand All @@ -19,9 +19,9 @@ public class JMXSimpleAttribute extends JMXAttribute {
private String alias;
private String metricType;

public JMXSimpleAttribute(MBeanAttributeInfo attribute, ObjectInstance instance, String instanceName,
public JMXSimpleAttribute(MBeanAttributeInfo attribute, ObjectName beanName, String instanceName,
Connection connection, HashMap<String, String> instanceTags) {
super(attribute, instance, instanceName, connection, instanceTags);
super(attribute, beanName, instanceName, connection, instanceTags);
}

@Override
Expand Down Expand Up @@ -90,9 +90,9 @@ private String getAlias() {
LinkedHashMap<String, LinkedHashMap<String, String>> attribute = (LinkedHashMap<String, LinkedHashMap<String, String>>) (include.getAttribute());
alias = attribute.get(getAttribute().getName()).get("alias");
} else if (conf.get("metric_prefix") != null) {
alias = conf.get("metric_prefix") + "." + getBeanName().split(":")[0] + "." + getAttributeName();
alias = conf.get("metric_prefix") + "." + getBeanStringName().split(":")[0] + "." + getAttributeName();
} else {
alias = "jmx." + getBeanName().split(":")[0] + "." + getAttributeName();
alias = "jmx." + getBeanStringName().split(":")[0] + "." + getAttributeName();
}
alias = convertMetricName(alias);
return alias;
Expand Down