Skip to content

Commit

Permalink
cassandra_aliasing to comply with CASSANDRA-4009
Browse files Browse the repository at this point in the history
Create a `cassandra_aliasing` flag to select the appropriate aliasing
logic.
* `cassandra_aliasing` → compliant with CASSANDRA-4009
* default behavior
  • Loading branch information
yannmh committed Oct 28, 2015
1 parent 73acb20 commit 08b09ac
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 14 deletions.
10 changes: 9 additions & 1 deletion src/main/java/org/datadog/jmxfetch/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class Instance {
private boolean limitReached;
private Connection connection;
private AppConfig appConfig;
private Boolean cassandraAliasing;


public Instance(Instance instance, AppConfig appConfig) {
Expand Down Expand Up @@ -95,6 +96,13 @@ public Instance(LinkedHashMap<String, Object> yamlInstance, LinkedHashMap<String
}
}

// Alternative aliasing for CASSANDRA-4009 metrics
// More information: https://issues.apache.org/jira/browse/CASSANDRA-4009
this.cassandraAliasing = (Boolean) yaml.get("cassandra_aliasing");
if (this.cassandraAliasing == null){
this.cassandraAliasing = false;
}

// In case the configuration to match beans is not specified in the "instance" parameter but in the initConfig one
Object yamlConf = this.yaml.get("conf");
if (yamlConf == null && this.initConfig != null) {
Expand Down Expand Up @@ -221,7 +229,7 @@ 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, beanName, instanceName, connection, tags);
jmxAttribute = new JMXSimpleAttribute(attributeInfo, beanName, instanceName, connection, tags, cassandraAliasing);
} else if (COMPOSED_TYPES.contains(attributeType)) {
LOGGER.debug("Attribute: " + beanName + " : " + attributeInfo + " has attributeInfo complex type");
jmxAttribute = new JMXComplexAttribute(attributeInfo, beanName, instanceName, connection, tags);
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/org/datadog/jmxfetch/JMXAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,28 @@ public abstract class JMXAttribute {
protected String[] tags;
private Configuration matchingConf;
private LinkedList<String> defaultTagsList;
private Boolean cassandraAliasing;

JMXAttribute(MBeanAttributeInfo attribute, ObjectName beanName, String instanceName,
Connection connection, HashMap<String, String> instanceTags) {
Connection connection, HashMap<String, String> instanceTags, Boolean cassandraAliasing) {
this.attribute = attribute;
this.beanName = beanName;
this.matchingConf = null;
this.connection = connection;
this.attributeName = attribute.getName();
this.beanStringName = beanName.toString();
this.cassandraAliasing = cassandraAliasing;

// 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 = beanStringName.split(":");
String domain = splitBeanName[0];
String beanParameters = splitBeanName[1];
this.domain = domain;

HashMap<String, String> beanParametersHash = getBeanParametersHash(beanParameters);
LinkedList<String> beanParametersList = getBeanParametersList(instanceName, domain, beanParametersHash, instanceTags);
LinkedList<String> beanParametersList = getBeanParametersList(instanceName, beanParametersHash, instanceTags);

this.domain = domain;
this.beanParameters = beanParametersHash;
this.defaultTagsList = renameConflictingParameters(beanParametersList);
}
Expand All @@ -80,12 +83,12 @@ public static HashMap<String, String> getBeanParametersHash(String beanParameter
return beanParamsMap;
}

private static LinkedList<String> getBeanParametersList(String instanceName, String domain, Map<String, String> beanParameters, HashMap<String, String> instanceTags) {
private LinkedList<String> getBeanParametersList(String instanceName, Map<String, String> beanParameters, HashMap<String, String> instanceTags) {
LinkedList<String> beanTags = new LinkedList<String>();
beanTags.add("instance:" + instanceName);
beanTags.add("jmx_domain:" + domain);

if (domain.equals(CASSANDRA_DOMAIN)) {
if (renameCassandraMetrics()) {
beanTags.addAll(getCassandraBeanTags(beanParameters));
} else {
for (Map.Entry<String, String> param : beanParameters.entrySet()) {
Expand Down Expand Up @@ -119,6 +122,10 @@ private static LinkedList<String> renameConflictingParameters(LinkedList<String>
return defaultTagsList;
}

protected Boolean renameCassandraMetrics(){
return cassandraAliasing && domain.equals(CASSANDRA_DOMAIN);
}

private static Collection<String> getCassandraBeanTags(Map<String, String> beanParameters) {
Collection<String> tags = new LinkedList<String>();
for (Map.Entry<String, String> param : beanParameters.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class JMXComplexAttribute extends JMXAttribute {

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

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/datadog/jmxfetch/JMXSimpleAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class JMXSimpleAttribute extends JMXAttribute {
private String metricType;

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

@Override
Expand Down Expand Up @@ -105,7 +105,7 @@ private String getAlias() {
}

private String getCassandraAlias() {
if (getDomain().equals(CASSANDRA_DOMAIN)) {
if (renameCassandraMetrics()) {
Map<String, String> beanParameters = getBeanParameters();
String type = beanParameters.get("type");
String metricName = beanParameters.get("name");
Expand Down
25 changes: 22 additions & 3 deletions src/test/java/org/datadog/jmxfetch/TestApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public void testBeanTags() throws Exception {
}
}

/**
* Check JMXFetch Cassandra metric aliasing logic, i.e. compliant with CASSANDRA-4009
* when `cassandra4009` flag is enabled, or default.
*
* More information: https://issues.apache.org/jira/browse/CASSANDRA-4009
*/
@Test
public void testCassandraBean() throws Exception {
// We expose a few metrics through JMX
Expand All @@ -67,18 +73,31 @@ public void testCassandraBean() throws Exception {
run();
LinkedList<HashMap<String, Object>> metrics = getMetrics();

// 14 = 13 metrics from java.lang + 1 metric explicitly defined in the yaml config file
assertEquals(14, metrics.size());
// 14 = 2*13 metrics from java.lang + 2*1 metric explicitly defined in the yaml config file
assertEquals(28, metrics.size());

// Assert compliancy with CASSANDRA-4009
ArrayList<String> tags = new ArrayList<String>() {{
add("type:ColumnFamily");
add("keyspace:MyKeySpace");
add("ColumnFamily:MyColumnFamily");
add("jmx_domain:org.apache.cassandra.metrics");
add("instance:jmx_test_instance");
add("instance:jmx_first_instance");
}};

assertMetric("cassandra.pending_tasks.should_be100", tags, 5);

// Default behavior
tags = new ArrayList<String>() {{
add("type:ColumnFamily");
add("type:ColumnFamily");
add("keyspace:MyKeySpace");
add("jmx_domain:org.apache.cassandra.metrics");
add("instance:jmx_second_instance");
add("name:PendingTasks");
}};

assertMetric("cassandra.metrics.should_be1000", tags, 6);
}

@Test
Expand Down
10 changes: 9 additions & 1 deletion src/test/resources/jmx_cassandra.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ init_config:

instances:
- process_name_regex: .*surefire.*
name: jmx_test_instance
name: jmx_first_instance
cassandra_aliasing: true
conf:
- include:
bean: org.apache.cassandra.metrics:keyspace=MyKeySpace,type=ColumnFamily,scope=MyColumnFamily,name=PendingTasks
attribute:
- ShouldBe100
- process_name_regex: .*surefire.*
name: jmx_second_instance
conf:
- include:
bean: org.apache.cassandra.metrics:keyspace=MyKeySpace,type=ColumnFamily,scope=MyColumnFamily,name=PendingTasks
attribute:
- ShouldBe1000

0 comments on commit 08b09ac

Please sign in to comment.