/** * Find the IncludeExcludePatterns for filtering a given metric. * * The result is the union of all the individual pattern entries * where their specified metric name patterns matches the actual metric name. */ public IncludeExcludePatterns metricToPatterns(String metric) { IncludeExcludePatterns foundPatterns = metricNameToPatterns.get(metric); if (foundPatterns != null) { return foundPatterns; } // Since the keys in the prototype can be regular expressions, // need to look at all of them and can potentially match multiple, // each having a different set of rules. foundPatterns = new IncludeExcludePatterns(); for (MeterFilterPattern meterPattern : includePatterns) { if (meterPattern.namePattern.matcher(metric).matches()) { foundPatterns.include.addAll(meterPattern.values); } } for (MeterFilterPattern meterPattern : excludePatterns) { if (meterPattern.namePattern.matcher(metric).matches()) { foundPatterns.exclude.addAll(meterPattern.values); } } metricNameToPatterns.put(metric, foundPatterns); return foundPatterns; }
/** * Find the IncludeExcludePatterns for filtering a given metric. * * The result is the union of all the individual pattern entries * where their specified metric name patterns matches the actual metric name. */ public IncludeExcludePatterns metricToPatterns(String metric) { IncludeExcludePatterns foundPatterns = metricNameToPatterns.get(metric); if (foundPatterns != null) { return foundPatterns; } // Since the keys in the prototype can be regular expressions, // need to look at all of them and can potentially match multiple, // each having a different set of rules. foundPatterns = new IncludeExcludePatterns(); for (MeterFilterPattern meterPattern : includePatterns) { if (meterPattern.namePattern.matcher(metric).matches()) { foundPatterns.include.addAll(meterPattern.values); } } for (MeterFilterPattern meterPattern : excludePatterns) { if (meterPattern.namePattern.matcher(metric).matches()) { foundPatterns.exclude.addAll(meterPattern.values); } } metricNameToPatterns.put(metric, foundPatterns); return foundPatterns; }
@Test public void testMetricToPatterns() { PrototypeMeasurementFilterSpecification spec = new PrototypeMeasurementFilterSpecification(); spec.getInclude().put("meterA", meterSpecA); spec.getInclude().put(".+B", meterSpecB); spec.getInclude().put(".+C.*", meterSpecC); PrototypeMeasurementFilter filter = new PrototypeMeasurementFilter(spec); PrototypeMeasurementFilter.MeterFilterPattern meterPatternA = new PrototypeMeasurementFilter.MeterFilterPattern("meterA", meterSpecA); PrototypeMeasurementFilter.MeterFilterPattern meterPatternC = new PrototypeMeasurementFilter.MeterFilterPattern(".+C.*", meterSpecC); final List<PrototypeMeasurementFilter.ValueFilterPattern> emptyList = new ArrayList<>(); Assertions.assertEquals( filter.metricToPatterns("meterA"), new PrototypeMeasurementFilter.IncludeExcludePatterns( meterPatternA.getValues(), emptyList)); Assertions.assertEquals( filter.metricToPatterns("meterBextra"), new PrototypeMeasurementFilter.IncludeExcludePatterns( emptyList, emptyList)); Assertions.assertEquals( filter.metricToPatterns("meterCthing"), new PrototypeMeasurementFilter.IncludeExcludePatterns( meterPatternC.getValues(), emptyList)); }
@Test public void testMetricToPatternsWithMultipleMeters() { PrototypeMeasurementFilterSpecification spec = new PrototypeMeasurementFilterSpecification(); spec.getInclude().put("meterA", meterSpecA); spec.getInclude().put("meter.+", meterSpecB); PrototypeMeasurementFilter filter = new PrototypeMeasurementFilter(spec); PrototypeMeasurementFilter.MeterFilterPattern meterPatternA = new PrototypeMeasurementFilter.MeterFilterPattern("ignored", meterSpecA); PrototypeMeasurementFilter.MeterFilterPattern meterPatternB = new PrototypeMeasurementFilter.MeterFilterPattern("ignored", meterSpecB); final List<PrototypeMeasurementFilter.ValueFilterPattern> emptyList = new ArrayList<>(); Assertions.assertEquals( filter.metricToPatterns("meterB"), new PrototypeMeasurementFilter.IncludeExcludePatterns( meterPatternB.getValues(), emptyList)); List<PrototypeMeasurementFilter.ValueFilterPattern> expect = new ArrayList<>(); expect.addAll(meterPatternA.getValues()); expect.addAll(meterPatternB.getValues()); PrototypeMeasurementFilter.IncludeExcludePatterns patterns = filter.metricToPatterns("meterA"); Assertions.assertEquals(new HashSet<>(expect), new HashSet<>(patterns.getInclude())); }
/** * Implements the MeasurementFilter interface. */ @SuppressWarnings("PMD.JUnit4TestShouldUseTestAnnotation") @Override public boolean test(Measurement measurement) { IncludeExcludePatterns patterns = metricToPatterns(measurement.id().name()); return patterns != null && patterns.test(measurement); }
/** * Implements the MeasurementFilter interface. */ @SuppressWarnings("PMD.JUnit4TestShouldUseTestAnnotation") @Override public boolean test(Measurement measurement) { IncludeExcludePatterns patterns = metricToPatterns(measurement.id().name()); return patterns != null && patterns.test(measurement); }