JMXReporter implements MetricReporter { static Hashtable<String, String> generateJmxTable(Map<String, String> variables) { Hashtable<String, String> ht = new Hashtable<>(variables.size()); for (Map.Entry<String, String> variable : variables.entrySet()) { ht.put(replaceInvalidChars(variable.getKey()), replaceInvalidChars(variable.getValue())); } return ht; } JMXReporter(); @Override void open(MetricConfig config); @Override void close(); int getPort(); @Override void notifyOfAddedMetric(Metric metric, String metricName, MetricGroup group); @Override void notifyOfRemovedMetric(Metric metric, String metricName, MetricGroup group); static final String ARG_PORT; }
@Test public void testGenerateTable() { Map<String, String> vars = new HashMap<>(); vars.put("key0", "value0"); vars.put("key1", "value1"); vars.put("\"key2,=;:?'", "\"value2 (test),=;:?'"); Hashtable<String, String> jmxTable = JMXReporter.generateJmxTable(vars); assertEquals("value0", jmxTable.get("key0")); assertEquals("value1", jmxTable.get("key1")); assertEquals("value2_(test)------", jmxTable.get("key2------")); }
@Test public void testHistogramReporting() throws Exception { MetricRegistryImpl registry = null; String histogramName = "histogram"; try { Configuration config = new Configuration(); config.setString(MetricOptions.REPORTERS_LIST, "jmx_test"); config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "jmx_test." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, JMXReporter.class.getName()); registry = new MetricRegistryImpl(MetricRegistryConfiguration.fromConfiguration(config)); TaskManagerMetricGroup metricGroup = new TaskManagerMetricGroup(registry, "localhost", "tmId"); TestingHistogram histogram = new TestingHistogram(); metricGroup.histogram(histogramName, histogram); MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); ObjectName objectName = new ObjectName(JMX_DOMAIN_PREFIX + "taskmanager." + histogramName, JMXReporter.generateJmxTable(metricGroup.getAllVariables())); MBeanInfo info = mBeanServer.getMBeanInfo(objectName); MBeanAttributeInfo[] attributeInfos = info.getAttributes(); assertEquals(11, attributeInfos.length); assertEquals(histogram.getCount(), mBeanServer.getAttribute(objectName, "Count")); assertEquals(histogram.getStatistics().getMean(), mBeanServer.getAttribute(objectName, "Mean")); assertEquals(histogram.getStatistics().getStdDev(), mBeanServer.getAttribute(objectName, "StdDev")); assertEquals(histogram.getStatistics().getMax(), mBeanServer.getAttribute(objectName, "Max")); assertEquals(histogram.getStatistics().getMin(), mBeanServer.getAttribute(objectName, "Min")); assertEquals(histogram.getStatistics().getQuantile(0.5), mBeanServer.getAttribute(objectName, "Median")); assertEquals(histogram.getStatistics().getQuantile(0.75), mBeanServer.getAttribute(objectName, "75thPercentile")); assertEquals(histogram.getStatistics().getQuantile(0.95), mBeanServer.getAttribute(objectName, "95thPercentile")); assertEquals(histogram.getStatistics().getQuantile(0.98), mBeanServer.getAttribute(objectName, "98thPercentile")); assertEquals(histogram.getStatistics().getQuantile(0.99), mBeanServer.getAttribute(objectName, "99thPercentile")); assertEquals(histogram.getStatistics().getQuantile(0.999), mBeanServer.getAttribute(objectName, "999thPercentile")); } finally { if (registry != null) { registry.shutdown(); } } }
@Test public void testMeterReporting() throws Exception { MetricRegistryImpl registry = null; String meterName = "meter"; try { Configuration config = new Configuration(); config.setString(MetricOptions.REPORTERS_LIST, "jmx_test"); config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "jmx_test." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, JMXReporter.class.getName()); registry = new MetricRegistryImpl(MetricRegistryConfiguration.fromConfiguration(config)); TaskManagerMetricGroup metricGroup = new TaskManagerMetricGroup(registry, "localhost", "tmId"); TestMeter meter = new TestMeter(); metricGroup.meter(meterName, meter); MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); ObjectName objectName = new ObjectName(JMX_DOMAIN_PREFIX + "taskmanager." + meterName, JMXReporter.generateJmxTable(metricGroup.getAllVariables())); MBeanInfo info = mBeanServer.getMBeanInfo(objectName); MBeanAttributeInfo[] attributeInfos = info.getAttributes(); assertEquals(2, attributeInfos.length); assertEquals(meter.getRate(), mBeanServer.getAttribute(objectName, "Rate")); assertEquals(meter.getCount(), mBeanServer.getAttribute(objectName, "Count")); } finally { if (registry != null) { registry.shutdown(); } } }