MetricNameSetFilter implements MetricFilter { @Override public boolean matches(MetricName name, Metric metric) { for (String nameToMatch: metricNames) { boolean success; if (metric instanceof Counter) { success = matchInternal(nameToMatch, name, counterSuffixSet, false); } else if (metric instanceof Meter) { success = matchInternal(nameToMatch, name, meterSuffixSet, false); } else if (metric instanceof Histogram) { success = matchInternal(nameToMatch, name, histogramSuffixSet, false); } else if (metric instanceof Timer) { success = matchInternal(nameToMatch, name, timerSuffixSet, false); } else if (metric instanceof Compass) { success = matchInternal(nameToMatch, name, compassSuffixSet, true); if (!success) { success = matchCompassAddon(nameToMatch, name, (Compass)metric); } } else if (metric instanceof Gauge) { success = matchInternal(nameToMatch, name, gaugeSuffixSet, false); } else if (metric instanceof FastCompass) { success = matchInternal(nameToMatch, name, fastCompassSuffixSet, true); } else if (metric instanceof ClusterHistogram) { success = matchInternal(nameToMatch, name, clusterHistogramSuffixSet, false); } else if (metric == null) { if (nameToMatch.equals(name.getKey())) { success = true; } else { int lastIndexOfDot = name.getKey().lastIndexOf("."); if (lastIndexOfDot < 0) { success = false; } else { success = name.getKey().startsWith(nameToMatch) && nameToMatch.length() == lastIndexOfDot; } } } else { success = nameToMatch.equals(name.getKey()); } if (success) return true; } return false; } MetricNameSetFilter(String... names);  MetricNameSetFilter(final Set<String> metricNames); @Override boolean matches(MetricName name, Metric metric);  }
@Test public void testAccurateMatch() { Set<String> names = new HashSet<String>(); names.add("middleware.product.provider.qps"); names.add("shared.carts.my_cart.m1"); names.add("shared.carts.my_cart1.stddev"); names.add("shared.carts.my_cart2.error.count"); MetricNameSetFilter filter = new MetricNameSetFilter(names); MetricName mn = MetricName.build("shared.carts.my_cart"); MetricName mn1 = MetricName.build("shared.carts.my_cart1"); MetricName mn2 = MetricName.build("shared.carts.my_cart2"); Meter meter = MetricManager.getMeter("carts", mn); Histogram histogram = MetricManager.getHistogram("carts", mn1); Compass compass = MetricManager.getCompass("carts", mn2); Assert.assertTrue(filter.matches(mn, meter)); Assert.assertTrue(filter.matches(mn1, histogram)); Assert.assertTrue(filter.matches(mn2, compass)); }
@Test public void testMetricNameKeyMatch() { Set<String> names = new HashSet<String>(); names.add("middleware.product.provider.qps"); names.add("shared.carts.my_cart"); names.add("shared.carts.my_cart1"); names.add("shared.carts.my_cart2"); MetricNameSetFilter filter = new MetricNameSetFilter(names); MetricName mn = MetricName.build("shared.carts.my_cart"); MetricName mn1 = MetricName.build("shared.carts.my_cart1"); MetricName mn2 = MetricName.build("shared.carts.my_cart2"); Meter meter = MetricManager.getMeter("carts", mn); Histogram histogram = MetricManager.getHistogram("carts", mn1); Compass compass = MetricManager.getCompass("carts", mn2); Assert.assertTrue(filter.matches(mn, meter)); Assert.assertTrue(filter.matches(mn1, histogram)); Assert.assertTrue(filter.matches(mn2, compass)); }
@Test public void testMetricNamePrefixMatch() { Set<String> names = new HashSet<String>(); names.add("shared.carts.my_cart"); MetricNameSetFilter filter = new MetricNameSetFilter(names); MetricName mn = MetricName.build("shared.carts.my_cart.m1"); Assert.assertTrue(filter.matches(mn, null)); Set<String> names2 = new HashSet<String>(); names2.add("shared.carts.my_cart.m15"); MetricNameSetFilter filter2 = new MetricNameSetFilter(names2); MetricName mn2 = MetricName.build("shared.carts.my_cart.m1"); Assert.assertFalse(filter2.matches(mn2, null)); }
@Test public void testMatchClusterHistogram() { Set<String> names = new HashSet<String>(); names.add("shared.carts.rt"); MetricNameSetFilter filter = new MetricNameSetFilter(names); MetricName mn = MetricName.build("shared.carts.rt.cluster_percentile"); Assert.assertTrue(filter.matches(mn, null)); }