JmxMetricPoller implements MetricPoller { public List<Metric> poll(MetricFilter filter) { return poll(filter, false); } JmxMetricPoller(
      JmxConnector connector, ObjectName query, MetricFilter counters);  JmxMetricPoller(
      JmxConnector connector, List<ObjectName> queries, MetricFilter counters);  JmxMetricPoller(
      JmxConnector connector, List<ObjectName> queries, MetricFilter counters,
      boolean onlyNumericMetrics, List<Tag> defaultTags); List<Metric> poll(MetricFilter filter); List<Metric> poll(MetricFilter filter, boolean reset);  }
@Test public void testBasic() throws Exception { MetricPoller poller = new JmxMetricPoller( new LocalJmxConnector(), new ObjectName("java.lang:type=OperatingSystem"), MATCH_NONE); boolean found = false; List<Metric> metrics = poller.poll(MATCH_ALL); for (Metric m : metrics) { if ("AvailableProcessors".equals(m.getConfig().getName())) { found = true; Map<String, String> tags = m.getConfig().getTags().asMap(); assertEquals(tags.get("JmxDomain"), "java.lang"); assertEquals(tags.get("Jmx.type"), "OperatingSystem"); assertEquals(tags.get("ClassName"), "com.netflix.servo.publish.JmxMetricPoller"); assertEquals(tags.get(DataSourceType.KEY), "GAUGE"); } } assertTrue(found); }
@Test public void testCounterFilter() throws Exception { MetricPoller poller = new JmxMetricPoller( new LocalJmxConnector(), new ObjectName("java.lang:type=OperatingSystem"), MATCH_ALL); boolean found = false; List<Metric> metrics = poller.poll(MATCH_ALL); for (Metric m : metrics) { if ("AvailableProcessors".equals(m.getConfig().getName())) { found = true; Map<String, String> tags = m.getConfig().getTags().asMap(); assertEquals(tags.get("JmxDomain"), "java.lang"); assertEquals(tags.get("Jmx.type"), "OperatingSystem"); assertEquals(tags.get("ClassName"), "com.netflix.servo.publish.JmxMetricPoller"); assertEquals(tags.get(DataSourceType.KEY), "COUNTER"); } } assertTrue(found); }
@Test public void testTabularData() throws Exception { MapMXBean mapMXBean = new MapMXBean(); try { MetricPoller poller = new JmxMetricPoller( new LocalJmxConnector(), new ObjectName("com.netflix.servo.test:*"), MATCH_ALL); List<Metric> metrics = poller.poll(config -> config.getName().equals("Count")); assertEquals(metrics.size(), 2); Map<String, Integer> values = new HashMap<>(); for (Metric m : metrics) { values.put(m.getConfig().getTags().getTag("JmxCompositePath").getValue(), (Integer) m.getValue()); } assertEquals(values.get("Entry1"), (Integer) 111); assertEquals(values.get("Entry2"), (Integer) 222); } finally { mapMXBean.destroy(); } }
@Test public void testDefaultTags() throws Exception { MetricPoller poller = new JmxMetricPoller( new LocalJmxConnector(), Collections.singletonList(new ObjectName("java.lang:type=OperatingSystem")), MATCH_ALL, true, Collections.singletonList(Tags.newTag("HostName", "localhost"))); List<Metric> metrics = poller.poll(MATCH_ALL); for (Metric m : metrics) { Map<String, String> tags = m.getConfig().getTags().asMap(); assertEquals(tags.get("HostName"), "localhost"); } }
@Test public void testNonNumericMetrics() throws Exception { MapMXBean mapMXBean = new MapMXBean(); try { MetricPoller poller = new JmxMetricPoller( new LocalJmxConnector(), Collections.singletonList(new ObjectName("com.netflix.servo.test:*")), MATCH_ALL, false, null); List<Metric> metrics = poller.poll(config -> config.getName().equals("StringValue")); assertEquals(metrics.size(), 1); assertEquals(metrics.get(0).getValue(), "AStringResult"); } finally { mapMXBean.destroy(); } }