/** * Constructs from a specification. * * The nameRegex specifies the name of the Spectator meter itself. */ public MeterFilterPattern( String nameRegex, PrototypeMeasurementFilterSpecification.MeterFilterSpecification spec) { namePattern = Pattern.compile(nameRegex); if (spec == null) return; if (spec.getValues().isEmpty()) { values.add(new ValueFilterPattern(PrototypeMeasurementFilterSpecification.ValueFilterSpecification.ALL)); } for (PrototypeMeasurementFilterSpecification.ValueFilterSpecification value : spec.getValues()) { values.add(new ValueFilterPattern(value)); } }
@Test public void extraValueTagsOk() { PrototypeMeasurementFilter.ValueFilterPattern pattern = new PrototypeMeasurementFilter.ValueFilterPattern(valueSpecAxBy); List<Tag> tagsAxBy = Arrays.asList(new BasicTag("tagX", "X"), new BasicTag("tagA", "X"), new BasicTag("tagC", "C"), new BasicTag("tagB", "Y")); List<Tag> tagsByAx = Arrays.asList(tagsAxBy.get(3), tagsAxBy.get(2), tagsAxBy.get(1), tagsAxBy.get(0)); Assertions.assertTrue(pattern.test(tagsAxBy)); Assertions.assertTrue(pattern.test(tagsByAx)); }
@Test public void testPatternFromSpec() { List<PrototypeMeasurementFilter.TagFilterPattern> tagPatterns = Arrays.asList( new PrototypeMeasurementFilter.TagFilterPattern(Pattern.compile("tagA"), Pattern.compile("X")), new PrototypeMeasurementFilter.TagFilterPattern(Pattern.compile("tagB"), Pattern.compile("Y"))); PrototypeMeasurementFilter.MeterFilterPattern meterPattern = new PrototypeMeasurementFilter.MeterFilterPattern("meterA", meterSpecA); Assertions.assertEquals(meterPattern.getValues().size(), 1); Assertions.assertEquals(meterPattern.getValues().get(0).getTags(), tagPatterns); }
/** * Implements the MeasurementFilter interface. */ @SuppressWarnings("PMD.JUnit4TestShouldUseTestAnnotation") public boolean test(Measurement measurement) { boolean ok = include.isEmpty(); for (ValueFilterPattern pattern : include) { if (pattern.test(measurement.id().tags())) { ok = true; break; } } if (ok) { for (ValueFilterPattern pattern : exclude) { if (pattern.test(measurement.id().tags())) { return false; } } } return ok; } }
/** * Implements the MeasurementFilter interface. */ @SuppressWarnings("PMD.JUnit4TestShouldUseTestAnnotation") public boolean test(Measurement measurement) { boolean ok = include.isEmpty(); for (ValueFilterPattern pattern : include) { if (pattern.test(measurement.id().tags())) { ok = true; break; } } if (ok) { for (ValueFilterPattern pattern : exclude) { if (pattern.test(measurement.id().tags())) { return false; } } } return ok; } }
/** * Constructs from a specification. * * The nameRegex specifies the name of the Spectator meter itself. */ public MeterFilterPattern( String nameRegex, PrototypeMeasurementFilterSpecification.MeterFilterSpecification spec) { namePattern = Pattern.compile(nameRegex); if (spec == null) return; if (spec.getValues().isEmpty()) { values.add(new ValueFilterPattern(PrototypeMeasurementFilterSpecification.ValueFilterSpecification.ALL)); } for (PrototypeMeasurementFilterSpecification.ValueFilterSpecification value : spec.getValues()) { values.add(new ValueFilterPattern(value)); } }
/** * Implements the MeasurementFilter interface. */ @SuppressWarnings("PMD.JUnit4TestShouldUseTestAnnotation") public boolean test(Iterable<Tag> sourceTags) { for (TagFilterPattern tagPattern : this.tags) { if (!patternInList(tagPattern, sourceTags)) { return false; } } return true; }
@Test public void valueTagsOk() { PrototypeMeasurementFilter.ValueFilterPattern pattern = new PrototypeMeasurementFilter.ValueFilterPattern(valueSpecAxBy); List<Tag> tagsAxBy = Arrays.asList(new BasicTag("tagA", "X"), new BasicTag("tagB", "Y")); List<Tag> tagsByAx = Arrays.asList(tagsAxBy.get(1), tagsAxBy.get(0)); Assertions.assertTrue(pattern.test(tagsAxBy)); Assertions.assertTrue(pattern.test(tagsByAx)); }
/** * Implements the MeasurementFilter interface. */ @SuppressWarnings("PMD.JUnit4TestShouldUseTestAnnotation") public boolean test(Iterable<Tag> sourceTags) { for (TagFilterPattern tagPattern : this.tags) { if (!patternInList(tagPattern, sourceTags)) { return false; } } return true; }
@Test public void valueTagsMissing() { PrototypeMeasurementFilter.ValueFilterPattern pattern = new PrototypeMeasurementFilter.ValueFilterPattern(valueSpecAxBy); List<Tag> tagsAx = Collections.singletonList(new BasicTag("tagA", "X")); List<Tag> tagsAxZy = Arrays.asList(new BasicTag("tagA", "X"), new BasicTag("tagZ","Y")); List<Tag> tagsAyBy = Arrays.asList(new BasicTag("tagA", "Y"), new BasicTag("tagB", "Y")); Assertions.assertFalse(pattern.test(tagsAx)); Assertions.assertFalse(pattern.test(tagsAxZy)); Assertions.assertFalse(pattern.test(tagsAyBy)); }