Skip to content

Commit 4ded18b

Browse files
committed
pull request jmxtrans#2 add typeName support to Query.
1 parent 58e2cf6 commit 4ded18b

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

samples/activemq/queues-graphite.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
"EnqueueCount",
1818
"Subscriptions"
1919
],
20+
"typeNames" : ["Destination"],
2021
"resultAlias" : "ActiveMQ.Queue",
2122

2223
"outputWriters" : [ {
2324
"@class" : "com.googlecode.jmxtrans.model.output.GraphiteWriter",
2425
"settings" : {
2526
"port" : 2003,
26-
"host" : "192.168.1.88",
27-
"typeNames" : ["Destination"]
27+
"host" : "192.168.1.88"
2828
}
2929
} ]
3030
} ]

samples/activemq/queues.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
"EnqueueCount",
2020
"Subscriptions"
2121
],
22-
22+
"typeNames" : ["Destination"],
23+
"resultAlias" : "ActiveMQ.Queue",
2324
"outputWriters":[
2425
{
25-
"@class":"com.googlecode.jmxtrans.model.output.StdOutWriter",
26+
"@class":"com.googlecode.jmxtrans.model.output.KeyOutWriter",
2627
"settings":{
28+
"outputFile" : "logs/output.txt"
2729
}
2830
}
2931
]

src/com/googlecode/jmxtrans/model/Query.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5+
import java.util.Set;
56

67
import org.apache.commons.lang.builder.EqualsBuilder;
78
import org.apache.commons.lang.builder.HashCodeBuilder;
@@ -21,7 +22,7 @@
2122
* @author jon
2223
*/
2324
@JsonSerialize(include=Inclusion.NON_NULL)
24-
@JsonPropertyOrder(value={"obj", "attr", "resultAlias", "keys", "outputWriters"})
25+
@JsonPropertyOrder(value={"obj", "attr", "typeNames", "resultAlias", "keys", "outputWriters"})
2526
public class Query {
2627

2728
private Server server;
@@ -32,6 +33,7 @@ public class Query {
3233
private List<String> keys;
3334
private List<OutputWriter> outputWriters;
3435
private List<Result> results;
36+
private Set<String> typeNames;
3537

3638
public Query() { }
3739

@@ -77,6 +79,22 @@ public String getResultAlias() {
7779
return resultAlias;
7880
}
7981

82+
public void setTypeNames(Set<String> typeNames) {
83+
this.typeNames = typeNames;
84+
}
85+
86+
/**
87+
* The list of type names used in a JMX bean string when querying with a wildcard
88+
* which is used to expose the actual type name value to the key string. e.g. for this JMX name
89+
*
90+
* typeName=name=PS Eden Space,type=MemoryPool
91+
*
92+
* If you add a typeName("name"), then it'll retrieve 'PS Eden Space' from the string
93+
*/
94+
public Set<String> getTypeNames() {
95+
return typeNames;
96+
}
97+
8098
public void setAttr(List<String> attr) {
8199
this.attr = attr;
82100
PropertyResolver.resolveList(this.attr);

src/com/googlecode/jmxtrans/util/JmxUtils.java

+28-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.rmi.UnmarshalException;
77
import java.util.ArrayList;
88
import java.util.HashMap;
9+
import java.util.HashSet;
910
import java.util.List;
1011
import java.util.Map;
1112
import java.util.Map.Entry;
@@ -600,7 +601,7 @@ public static String getKeyString(Query query, Result result, Entry<String, Obje
600601

601602
sb.append(".");
602603

603-
String typeName = cleanupStr(getConcatedTypeNameValues(typeNames, result.getTypeName()));
604+
String typeName = cleanupStr(getConcatedTypeNameValues(query, typeNames, result.getTypeName()));
604605
if (typeName != null) {
605606
sb.append(typeName);
606607
sb.append(".");
@@ -629,7 +630,7 @@ public static String getKeyString2(Query query, Result result, Entry<String, Obj
629630

630631
sb.append(".");
631632

632-
String typeName = cleanupStr(getConcatedTypeNameValues(typeNames, result.getTypeName()));
633+
String typeName = cleanupStr(getConcatedTypeNameValues(query, typeNames, result.getTypeName()));
633634
if (typeName != null) {
634635
sb.append(typeName);
635636
sb.append(".");
@@ -639,7 +640,8 @@ public static String getKeyString2(Query query, Result result, Entry<String, Obj
639640
return sb.toString();
640641
}
641642

642-
/**
643+
644+
/**
643645
* Replaces all . with _ and removes all spaces and double/single quotes.
644646
*/
645647
public static String cleanupStr(String name) {
@@ -677,6 +679,29 @@ public static String getConcatedTypeNameValues(List<String> typeNames, String ty
677679
return StringUtils.chomp(sb.toString(), "_");
678680
}
679681

682+
683+
/**
684+
* Given a typeName string, get the first match from the typeNames setting.
685+
* In other words, suppose you have:
686+
*
687+
* typeName=name=PS Eden Space,type=MemoryPool
688+
*
689+
* If you addTypeName("name"), then it'll retrieve 'PS Eden Space' from the string
690+
*/
691+
public static String getConcatedTypeNameValues(Query query, List<String> typeNames, String typeName) {
692+
Set<String> queryTypeNames = query.getTypeNames();
693+
if (queryTypeNames != null && queryTypeNames.size() > 0) {
694+
List<String> allNames = new ArrayList<String>(queryTypeNames);
695+
for (String name : typeNames) {
696+
if (!allNames.contains(name)) {
697+
allNames.add(name);
698+
}
699+
}
700+
return getConcatedTypeNameValues(allNames, typeName);
701+
} else {
702+
return getConcatedTypeNameValues(typeNames, typeName);
703+
}
704+
}
680705
/** */
681706
private static String getTypeNameValue(String typeName, String[] tokens) {
682707
boolean foundIt = false;

0 commit comments

Comments
 (0)