1111import java .lang .management .ManagementFactory ;
1212import javax .management .MBeanServer ;
1313import javax .management .ObjectName ;
14- import org .json .simple .parser .ParseException ;
1514import org .junit .Test ;
1615import org .junit .Before ;
1716import org .junit .BeforeClass ;
@@ -39,78 +38,78 @@ public void setUp() throws Exception {
3938 }
4039
4140 @ Test (expected =IllegalArgumentException .class )
42- public void testRulesMustHaveNameWithHelp () throws ParseException {
41+ public void testRulesMustHaveNameWithHelp () throws Exception {
4342 JmxCollector jc = new JmxCollector ("{`rules`: [{`help`: `foo`}] }" .replace ('`' , '"' ));
4443 }
4544
4645 @ Test (expected =IllegalArgumentException .class )
47- public void testRulesMustHaveNameWithLabels () throws ParseException {
46+ public void testRulesMustHaveNameWithLabels () throws Exception {
4847 JmxCollector jc = new JmxCollector ("{`rules`: [{`labels`: {}}] }" .replace ('`' , '"' ));
4948 }
5049
5150 @ Test (expected =IllegalArgumentException .class )
52- public void testRulesMustHavePatternWithName () throws ParseException {
51+ public void testRulesMustHavePatternWithName () throws Exception {
5352 JmxCollector jc = new JmxCollector ("{`rules`: [{`name`: `foo`}] }" .replace ('`' , '"' ));
5453 }
5554
5655 @ Test
57- public void testNameIsReplacedOnMatch () throws ParseException {
56+ public void testNameIsReplacedOnMatch () throws Exception {
5857 JmxCollector jc = new JmxCollector (
5958 "{`rules`: [{`pattern`: `^hadoop<service=DataNode, name=DataNodeActivity-ams-hdd001-50010><>replaceBlockOpMinTime:`, `name`: `foo`}]}" .replace ('`' , '"' )).register (registry );
6059 assertEquals (200 , registry .getSampleValue ("foo" , new String []{}, new String []{}), .001 );
6160 }
6261
6362 @ Test
64- public void testSnakeCaseAttrName () throws ParseException {
63+ public void testSnakeCaseAttrName () throws Exception {
6564 JmxCollector jc = new JmxCollector (
6665 "{`rules`: [{`pattern`: `^hadoop<service=DataNode, name=DataNodeActivity-ams-hdd001-50010><>replace_block_op_min_time:`, `name`: `foo`, `attrNameSnakeCase`: true}]}" .replace ('`' , '"' )).register (registry );
6766 assertEquals (200 , registry .getSampleValue ("foo" , new String []{}, new String []{}), .001 );
6867 }
6968
7069 @ Test
71- public void testLabelsAreSet () throws ParseException {
70+ public void testLabelsAreSet () throws Exception {
7271 JmxCollector jc = new JmxCollector (
7372 "{`rules`: [{`pattern`: `^hadoop<service=DataNode, name=DataNodeActivity-ams-hdd001-50010><>replaceBlockOpMinTime:`, `name`: `foo`, `labels`: {`l`: `v`}}]}" .replace ('`' , '"' )).register (registry );
7473 assertEquals (200 , registry .getSampleValue ("foo" , new String []{"l" }, new String []{"v" }), .001 );
7574 }
7675
7776 @ Test
78- public void testEmptyLabelsAreIgnored () throws ParseException {
77+ public void testEmptyLabelsAreIgnored () throws Exception {
7978 JmxCollector jc = new JmxCollector (
8079 "{`rules`: [{`pattern`: `^hadoop<service=DataNode, name=DataNodeActivity-ams-hdd001-50010><>replaceBlockOpMinTime:`, `name`: `foo`, `labels`: {``: `v`, `l`: ``}}]}" .replace ('`' , '"' )).register (registry );
8180 assertEquals (200 , registry .getSampleValue ("foo" , new String []{}, new String []{}), .001 );
8281 }
8382
8483 @ Test
85- public void testLowercaseOutputName () throws ParseException {
84+ public void testLowercaseOutputName () throws Exception {
8685 JmxCollector jc = new JmxCollector (
8786 "{`lowercaseOutputName`: true, `rules`: [{`pattern`: `^hadoop<service=DataNode, name=DataNodeActivity-ams-hdd001-50010><>replaceBlockOpMinTime:`, `name`: `Foo`}]}" .replace ('`' , '"' )).register (registry );
8887 assertEquals (200 , registry .getSampleValue ("foo" , new String []{}, new String []{}), .001 );
8988 }
9089
9190 @ Test
92- public void testLowercaseOutputLabelNames () throws ParseException {
91+ public void testLowercaseOutputLabelNames () throws Exception {
9392 JmxCollector jc = new JmxCollector (
9493 "{`lowercaseOutputLabelNames`: true, `rules`: [{`pattern`: `^hadoop<service=DataNode, name=DataNodeActivity-ams-hdd001-50010><>replaceBlockOpMinTime:`, `name`: `Foo` , `labels`: {`ABC`: `DEF`}}]}" .replace ('`' , '"' )).register (registry );
9594 assertEquals (200 , registry .getSampleValue ("Foo" , new String []{"abc" }, new String []{"DEF" }), .001 );
9695 }
9796
9897 @ Test
99- public void testNameAndLabelsFromPattern () throws ParseException {
98+ public void testNameAndLabelsFromPattern () throws Exception {
10099 JmxCollector jc = new JmxCollector (
101100 "{`rules`: [{`pattern`: `^hadoop<(service)=(DataNode), name=DataNodeActivity-ams-hdd001-50010><>(replaceBlockOpMinTime):`, `name`: `hadoop_$3`, `labels`: {`$1`: `$2`}}]}" .replace ('`' , '"' )).register (registry );
102101 assertEquals (200 , registry .getSampleValue ("hadoop_replaceBlockOpMinTime" , new String []{"service" }, new String []{"DataNode" }), .001 );
103102 }
104103
105104 @ Test
106- public void testNameAndLabelSanatized () throws ParseException {
105+ public void testNameAndLabelSanatized () throws Exception {
107106 JmxCollector jc = new JmxCollector (
108107 "{`rules`: [{`pattern`: `^(hadoop<service=DataNode, )name=DataNodeActivity-ams-hdd001-50010><>replaceBlockOpMinTime:`, `name`: `$1`, `labels`: {`$1`: `$1`}}]}" .replace ('`' , '"' )).register (registry );
109108 assertEquals (200 , registry .getSampleValue ("hadoop_service_DataNode_" , new String []{"hadoop_service_DataNode_" }, new String []{"hadoop<service=DataNode, " }), .001 );
110109 }
111110
112111 @ Test
113- public void testHelpFromPattern () throws ParseException {
112+ public void testHelpFromPattern () throws Exception {
114113 JmxCollector jc = new JmxCollector (
115114 "{`rules`: [{`pattern`: `^(hadoop)<service=DataNode, name=DataNodeActivity-ams-hdd001-50010><>replaceBlockOpMinTime:`, `name`: `foo`, `help`: `bar $1`}]}" .replace ('`' , '"' )).register (registry );
116115 for (Collector .MetricFamilySamples mfs : jc .collect ()) {
@@ -122,22 +121,22 @@ public void testHelpFromPattern() throws ParseException {
122121 }
123122
124123 @ Test
125- public void stopsOnFirstMatchingRule () throws ParseException {
124+ public void stopsOnFirstMatchingRule () throws Exception {
126125 JmxCollector jc = new JmxCollector (
127126 "{`rules`: [{`pattern`: `.*`, `name`: `foo`}, {`pattern`: `.*`, `name`: `bar`}] }" .replace ('`' , '"' )).register (registry );
128127 assertNotNull (registry .getSampleValue ("foo" , new String []{}, new String []{}));
129128 assertNull (registry .getSampleValue ("bar" , new String []{}, new String []{}));
130129 }
131130
132131 @ Test
133- public void stopsOnEmptyName () throws ParseException {
132+ public void stopsOnEmptyName () throws Exception {
134133 JmxCollector jc = new JmxCollector (
135134 "{`rules`: [{`pattern`: `.*`, `name`: ``}, {`pattern`: `.*`, `name`: `foo`}] }" .replace ('`' , '"' )).register (registry );
136135 assertNull (registry .getSampleValue ("foo" , new String []{}, new String []{}));
137136 }
138137
139138 @ Test
140- public void defaultExportTest () throws ParseException {
139+ public void defaultExportTest () throws Exception {
141140 JmxCollector jc = new JmxCollector ("{}" ).register (registry );
142141
143142 // Test JVM bean.
@@ -153,13 +152,37 @@ public void defaultExportTest() throws ParseException {
153152 }
154153
155154 @ Test
156- public void testDefaultExportLowercaseOutputName () throws ParseException {
155+ public void testWhitelist () throws Exception {
156+ JmxCollector jc = new JmxCollector ("{`whitelistObjectNames`: [`java.lang:*`, `java.lang:*`, `org.apache.cassandra.concurrent:*`]}" .replace ('`' , '"' )).register (registry );
157+
158+ // Test what should and shouldn't be present.
159+ assertNotNull (registry .getSampleValue ("java_lang_OperatingSystem_ProcessCpuTime" , new String []{}, new String []{}));
160+ assertNotNull (registry .getSampleValue ("org_apache_cassandra_concurrent_CONSISTENCY_MANAGER_ActiveCount" , new String []{}, new String []{}));
161+
162+ assertNull (registry .getSampleValue ("org_apache_cassandra_metrics_Compaction_Value" , new String []{"name" }, new String []{"CompletedTasks" }));
163+ assertNull (registry .getSampleValue ("hadoop_DataNode_replaceBlockOpMinTime" , new String []{"name" }, new String []{"DataNodeActivity-ams-hdd001-50010" }));
164+ }
165+
166+ @ Test
167+ public void testBlacklist () throws Exception {
168+ JmxCollector jc = new JmxCollector ("{`whitelistObjectNames`: [`java.lang:*`, `org.apache.cassandra.concurrent:*`], `blacklistObjectNames`: [`org.apache.cassandra.concurrent:*`]}" .replace ('`' , '"' )).register (registry );
169+
170+ // Test what should and shouldn't be present.
171+ assertNotNull (registry .getSampleValue ("java_lang_OperatingSystem_ProcessCpuTime" , new String []{}, new String []{}));
172+
173+ assertNull (registry .getSampleValue ("org_apache_cassandra_concurrent_CONSISTENCY_MANAGER_ActiveCount" , new String []{}, new String []{}));
174+ assertNull (registry .getSampleValue ("org_apache_cassandra_metrics_Compaction_Value" , new String []{"name" }, new String []{"CompletedTasks" }));
175+ assertNull (registry .getSampleValue ("hadoop_DataNode_replaceBlockOpMinTime" , new String []{"name" }, new String []{"DataNodeActivity-ams-hdd001-50010" }));
176+ }
177+
178+ @ Test
179+ public void testDefaultExportLowercaseOutputName () throws Exception {
157180 JmxCollector jc = new JmxCollector ("{`lowercaseOutputName`: true}" .replace ('`' , '"' )).register (registry );
158181 assertNotNull (registry .getSampleValue ("java_lang_operatingsystem_processcputime" , new String []{}, new String []{}));
159182 }
160183
161184 @ Test
162- public void testServletRequestPattern () throws ParseException {
185+ public void testServletRequestPattern () throws Exception {
163186 JmxCollector jc = new JmxCollector (
164187 "{`rules`: [{`pattern`: `Catalina<j2eeType=Servlet, WebModule=//([-a-zA-Z0-9+&@#/%?=~_|!:.,;]*[-a-zA-Z0-9+&@#/%=~_|]), name=([-a-zA-Z0-9+/$%~_-|!.]*), J2EEApplication=none, J2EEServer=none><>RequestCount:`,`name`: `tomcat_request_servlet_count`,`labels`: {`module`:`$1`,`servlet`:`$2` },`help`: `Tomcat servlet request count`,`type`: `COUNTER`,`attrNameSnakeCase`: false}]}" .replace ('`' , '"' )).register (registry );
165188 assertEquals (1.0 , registry .getSampleValue ("tomcat_request_servlet_count" , new String []{"module" , "servlet" }, new String []{"localhost/host-manager" , "HTMLHostManager" }), .001 );
0 commit comments