-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1442 from newrelic/weave-violation-filter
Add weave violation filter support. Resolves #1197
- Loading branch information
Showing
6 changed files
with
232 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
newrelic-weaver/src/main/java/com/newrelic/weave/WeaveViolationFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.newrelic.weave; | ||
|
||
import com.google.common.collect.Queues; | ||
import com.newrelic.weave.violation.WeaveViolation; | ||
import com.newrelic.weave.violation.WeaveViolationType; | ||
|
||
import java.util.Collection; | ||
import java.util.EnumSet; | ||
import java.util.Queue; | ||
|
||
/** | ||
* A class used to filter specific violations from being added to {@link com.newrelic.weave.weavepackage.PackageValidationResult} instances. | ||
* Instances of this class are created and stored with each {@link com.newrelic.weave.weavepackage.WeavePackageConfig} object (if | ||
* filters are configured for the instrumentation package). | ||
*/ | ||
public class WeaveViolationFilter { | ||
private final Collection<WeaveViolationType> typesToFilter = EnumSet.noneOf(WeaveViolationType.class); | ||
|
||
private final String weavePackage; | ||
|
||
public WeaveViolationFilter(String weavePackage, Collection<WeaveViolationType> types) { | ||
this.typesToFilter.addAll(types); | ||
this.weavePackage = weavePackage; | ||
} | ||
|
||
/** | ||
* Remove ignorable {@link WeaveViolation} instances from the supplied collection of violations. | ||
* | ||
* @param violations the collection of WeaveViolations to apply the filter to | ||
* @return a filtered collection of WeaveViolations | ||
*/ | ||
public Collection<WeaveViolation> filterViolationCollection(Collection<WeaveViolation> violations) { | ||
Queue<WeaveViolation> newViolationCollection = Queues.newConcurrentLinkedQueue(); | ||
for (WeaveViolation violation : violations) { | ||
if (!shouldIgnoreViolation(violation)) { | ||
newViolationCollection.add(violation); | ||
} | ||
} | ||
|
||
return newViolationCollection; | ||
} | ||
|
||
/** | ||
* Return true if the supplied {@link WeaveViolation} instance should be ignored based on the filter | ||
* | ||
* @param violation the WeaveViolation to check | ||
* @return true if this WeaveViolation should be filtered | ||
*/ | ||
public boolean shouldIgnoreViolation(WeaveViolation violation) { | ||
return typesToFilter.contains(violation.getType()); | ||
} | ||
|
||
/** | ||
* Return the weave package this filter belongs to | ||
* | ||
* @return the weave package name | ||
*/ | ||
public String getWeavePackage() { | ||
return this.weavePackage; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
newrelic-weaver/src/test/java/com/newrelic/weave/WeaveViolationFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.newrelic.weave; | ||
|
||
import com.newrelic.weave.violation.WeaveViolation; | ||
import com.newrelic.weave.violation.WeaveViolationType; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.EnumSet; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class WeaveViolationFilterTest { | ||
EnumSet<WeaveViolationType> violationTypes; | ||
|
||
@Before | ||
public void setup() { | ||
violationTypes = EnumSet.of(WeaveViolationType.METHOD_MISSING_REQUIRED_ANNOTATIONS, WeaveViolationType.CLASS_MISSING_REQUIRED_ANNOTATIONS); | ||
} | ||
|
||
|
||
@Test | ||
public void shouldIgnoreViolation_withIgnorableViolation_returnsTrue() { | ||
WeaveViolationFilter filter = new WeaveViolationFilter("com/nr/agent/instrumentation/SpringController_Instrumentation", violationTypes); | ||
|
||
WeaveViolation violation = new WeaveViolation(WeaveViolationType.METHOD_MISSING_REQUIRED_ANNOTATIONS, "com/nr/agent/instrumentation/SpringController_Instrumentation"); | ||
assertTrue(filter.shouldIgnoreViolation(violation)); | ||
|
||
violation = new WeaveViolation(WeaveViolationType.CLASS_MISSING_REQUIRED_ANNOTATIONS, "com/nr/agent/instrumentation/SpringController_Instrumentation"); | ||
assertTrue(filter.shouldIgnoreViolation(violation)); | ||
} | ||
|
||
@Test | ||
public void shouldIgnoreViolation_withNonIgnorableViolation_returnsFalse() { | ||
WeaveViolationFilter filter = new WeaveViolationFilter("com/nr/agent/instrumentation/SpringController_Instrumentation", violationTypes); | ||
|
||
WeaveViolation violation = new WeaveViolation(WeaveViolationType.FIELD_TYPE_MISMATCH, "com/nr/agent/instrumentation/SpringController_Instrumentation"); | ||
assertFalse(filter.shouldIgnoreViolation(violation)); | ||
} | ||
|
||
@Test | ||
public void filterViolationCollection_filtersIgnorableViolations() { | ||
WeaveViolationFilter filter = new WeaveViolationFilter("com/nr/agent/instrumentation/SpringController_Instrumentation", violationTypes); | ||
|
||
Collection<WeaveViolation> violations = new ArrayList<>(); | ||
violations.add(new WeaveViolation(WeaveViolationType.METHOD_MISSING_REQUIRED_ANNOTATIONS, "com/nr/agent/instrumentation/SpringController_Instrumentation")); | ||
violations.add(new WeaveViolation(WeaveViolationType.CLASS_MISSING_REQUIRED_ANNOTATIONS, "com/nr/agent/instrumentation/SpringController_Instrumentation")); | ||
violations.add(new WeaveViolation(WeaveViolationType.EXPECTED_NEW_FIELD_ANNOTATION, "com/nr/agent/instrumentation/SpringController_Instrumentation")); | ||
violations.add(new WeaveViolation(WeaveViolationType.CLASS_ACCESS_MISMATCH, "com/nr/agent/instrumentation/SpringController_Instrumentation")); | ||
|
||
assertEquals(4, violations.size()); | ||
|
||
Collection<WeaveViolation> filteredViolations = filter.filterViolationCollection(violations); | ||
assertEquals(2, filteredViolations.size()); | ||
assertTrue(filteredViolations.contains(new WeaveViolation(WeaveViolationType.EXPECTED_NEW_FIELD_ANNOTATION, "com/nr/agent/instrumentation/SpringController_Instrumentation"))); | ||
assertTrue(filteredViolations.contains(new WeaveViolation(WeaveViolationType.CLASS_ACCESS_MISMATCH, "com/nr/agent/instrumentation/SpringController_Instrumentation"))); | ||
} | ||
|
||
@Test | ||
public void getWeavePackage_returnsCorrectValue() { | ||
WeaveViolationFilter filter = new WeaveViolationFilter("com/nr/agent/instrumentation/SpringController_Instrumentation", violationTypes); | ||
assertEquals("com/nr/agent/instrumentation/SpringController_Instrumentation", filter.getWeavePackage()); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
newrelic-weaver/src/test/java/com/newrelic/weave/weavepackage/WeavePackageConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.newrelic.weave.weavepackage; | ||
|
||
import com.newrelic.weave.WeaveViolationFilter; | ||
import com.newrelic.weave.violation.WeaveViolation; | ||
import com.newrelic.weave.violation.WeaveViolationType; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class WeavePackageConfigTest { | ||
@Test | ||
public void ConfigBuilder_withConfiguredFilters_createsWeaveViolationFilter() { | ||
WeavePackageConfig.Builder builder = new WeavePackageConfig.Builder(); | ||
WeavePackageConfig config = builder.name("test") | ||
.weaveViolationFilters("METHOD_MISSING_REQUIRED_ANNOTATIONS,CLASS_MISSING_REQUIRED_ANNOTATIONS") | ||
.build(); | ||
|
||
WeaveViolationFilter filter = config.getWeaveViolationFilter(); | ||
assertEquals("test", filter.getWeavePackage()); | ||
assertTrue(filter.shouldIgnoreViolation(new WeaveViolation(WeaveViolationType.CLASS_MISSING_REQUIRED_ANNOTATIONS, "clazz"))); | ||
assertTrue(filter.shouldIgnoreViolation(new WeaveViolation(WeaveViolationType.METHOD_MISSING_REQUIRED_ANNOTATIONS, "clazz"))); | ||
} | ||
|
||
@Test | ||
public void ConfigBuilder_withoutConfiguredFilters_doesNotCreatesWeaveViolationFilter() { | ||
WeavePackageConfig.Builder builder = new WeavePackageConfig.Builder(); | ||
WeavePackageConfig config = builder.name("test").build(); | ||
|
||
assertNull(config.getWeaveViolationFilter()); | ||
} | ||
} |