MetricFiltering { public static boolean matches(MetricsFilter filter, MetricKey key) { if (filter == null) { return true; } @Nullable String stepName = key.stepName(); if (stepName == null) { if (!filter.steps().isEmpty()) { return false; } } else if (!matchesScope(stepName, filter.steps())) { return false; } return matchesName(key.metricName(), filter.names()); } private  MetricFiltering(); static boolean matches(MetricsFilter filter, MetricKey key); static boolean subPathMatches(String haystack, String needle); static boolean matchesScope(String actualScope, Set<String> scopes);  }
@Test public void testMatchCompositeStepNameFilters() { assertTrue( MetricFiltering.matches( MetricsFilter.builder() .addNameFilter(MetricNameFilter.named(MetricFilteringTest.class, "myMetricName")) .addStep("myStep") .build(), MetricKey.create( "myBigStep/myStep", MetricName.named(MetricFilteringTest.class, "myMetricName")))); assertFalse( MetricFiltering.matches( MetricsFilter.builder() .addNameFilter(MetricNameFilter.named(MetricFilteringTest.class, "myMetricName")) .addStep("myOtherStep") .build(), MetricKey.create( "myOtherStepNoMatch/myStep", MetricName.named(MetricFilteringTest.class, "myMetricName")))); }
@Test public void testMatchStepNameFilters() { assertTrue( MetricFiltering.matches( MetricsFilter.builder() .addNameFilter(MetricNameFilter.named(MetricFilteringTest.class, "myMetricName")) .addStep("myStep") .build(), MetricKey.create( "myStep", MetricName.named(MetricFilteringTest.class, "myMetricName")))); assertFalse( MetricFiltering.matches( MetricsFilter.builder() .addNameFilter(MetricNameFilter.named(MetricFilteringTest.class, "myMetricName")) .addStep("myOtherStep") .build(), MetricKey.create( "myStep", MetricName.named(MetricFilteringTest.class, "myMetricName")))); }
@Test public void testMatchClassNamespaceFilters() { assertTrue( MetricFiltering.matches( MetricsFilter.builder() .addNameFilter(MetricNameFilter.named(MetricFilteringTest.class, "myMetricName")) .build(), MetricKey.create( "anyStep", MetricName.named(MetricFilteringTest.class, "myMetricName")))); assertFalse( MetricFiltering.matches( MetricsFilter.builder() .addNameFilter(MetricNameFilter.named(MetricFilteringTest.class, "myMetricName")) .build(), MetricKey.create("anyStep", MetricName.named(MetricFiltering.class, "myMetricName")))); }
@Test public void testMatchStringNamespaceFilters() { assertTrue( MetricFiltering.matches( MetricsFilter.builder() .addNameFilter(MetricNameFilter.named("myNamespace", "myMetricName")) .build(), MetricKey.create("anyStep", MetricName.named("myNamespace", "myMetricName")))); assertFalse( MetricFiltering.matches( MetricsFilter.builder() .addNameFilter(MetricNameFilter.named("myOtherNamespace", "myMetricName")) .build(), MetricKey.create("anyStep", MetricName.named("myNamespace", "myMetricname")))); }