Skip to content

Commit

Permalink
Modify predicates to support both string and list values, rename noop…
Browse files Browse the repository at this point in the history
… to ignore, added default preprocessor example. (#515)
  • Loading branch information
akodali18 authored Apr 10, 2020
1 parent 32cedb3 commit f5f9a6f
Show file tree
Hide file tree
Showing 19 changed files with 302 additions and 134 deletions.
28 changes: 28 additions & 0 deletions pkg/etc/wavefront/wavefront-proxy/preprocessor_rules.yaml.default
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,34 @@
# replace : "$2"
# replaceInput : "$1$3" # optional, omit if you plan on just extracting the tag leaving the metric name intact

## Advanced predicates for finer control.
## Example: The below rule uses nested predicates to whitelists all "prod" metrics i.e.
## all -> returns true in case both "any" AND "none" is true.
## any -> returns true in case either nested "all" OR "startsWith" OR "equals" is true.
## none -> returns true in case nested comparison is false.
###################################################################################
#- rule: test-whitelist
# action: whitelistRegex
# if:
# all:
# - any:
# - all:
# - contains:
# scope: sourceName
# value: "prod"
# - startsWith
# scope: metricName
# value: "foometric."
# - startsWith:
# scope: metricName
# value: "foometric.prod."
# - equals:
# scope: env
# value: "prod"
# - none:
# - equals:
# scope: debug
# value: ["true"]

# rules for port 4242
'4242':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
import com.wavefront.agent.preprocessor.predicate.ReportPointContainsPredicate;
import com.wavefront.agent.preprocessor.predicate.ReportPointEndsWithPredicate;
import com.wavefront.agent.preprocessor.predicate.ReportPointEqualsPredicate;
import com.wavefront.agent.preprocessor.predicate.ReportPointInPredicate;
import com.wavefront.agent.preprocessor.predicate.ReportPointRegexMatchPredicate;
import com.wavefront.agent.preprocessor.predicate.ReportPointStartsWithPredicate;
import com.wavefront.agent.preprocessor.predicate.SpanContainsPredicate;
import com.wavefront.agent.preprocessor.predicate.SpanEndsWithPredicate;
import com.wavefront.agent.preprocessor.predicate.SpanEqualsPredicate;
import com.wavefront.agent.preprocessor.predicate.SpanInPredicate;
import com.wavefront.agent.preprocessor.predicate.SpanRegexMatchPredicate;
import com.wavefront.agent.preprocessor.predicate.SpanStartsWithPredicate;

Expand Down Expand Up @@ -42,7 +40,7 @@
public abstract class PreprocessorUtil {

private static final Pattern PLACEHOLDERS = Pattern.compile("\\{\\{(.*?)}}");
public static final String[] LOGICAL_OPS = {"all", "any", "none", "noop"};
public static final String[] LOGICAL_OPS = {"all", "any", "none", "ignore"};
public static final String V2_PREDICATE_KEY = "if";

/**
Expand Down Expand Up @@ -234,7 +232,7 @@ public static Predicate processLogicalOp(Map<String, Object> element, Class<?> r
}
}
return finalPred;
case "noop":
case "ignore":
// Always return true.
return Predicates.alwaysTrue();
default:
Expand All @@ -250,7 +248,7 @@ private static Predicate processComparisonOp(Map.Entry<String, Object> subElemen
throw new IllegalArgumentException("Argument [ + " + subElement.getKey() + "] can have only" +
" 2 elements, but found :: " + svpair.size() + ".");
}
String ruleVal = (String) svpair.get("value");
Object ruleVal = svpair.get("value");
String scope = (String) svpair.get("scope");
if (scope == null) {
throw new IllegalArgumentException("Argument [scope] can't be null/blank.");
Expand All @@ -270,8 +268,6 @@ private static Predicate processComparisonOp(Map.Entry<String, Object> subElemen
return new ReportPointEndsWithPredicate(scope, ruleVal);
case "regexMatch":
return new ReportPointRegexMatchPredicate(scope, ruleVal);
case "in":
return new ReportPointInPredicate(scope, ruleVal);
default:
throw new IllegalArgumentException("Unsupported comparison argument [" + subElement.getKey() + "].");
}
Expand All @@ -287,8 +283,6 @@ private static Predicate processComparisonOp(Map.Entry<String, Object> subElemen
return new SpanEndsWithPredicate(scope, ruleVal);
case "regexMatch":
return new SpanRegexMatchPredicate(scope, ruleVal);
case "in":
return new SpanInPredicate(scope, ruleVal);
default:
throw new IllegalArgumentException("Unsupported comparison argument [" + subElement.getKey() + "].");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.wavefront.agent.preprocessor.predicate;

import com.google.common.base.Preconditions;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;

/**
Expand All @@ -9,10 +14,13 @@
*/
public abstract class ComparisonPredicate<T> implements Predicate<T> {
protected final String scope;
protected final String value;
protected final List<String> value;

public ComparisonPredicate(String scope, String value) {
public ComparisonPredicate(String scope, Object value) {
this.scope = scope;
this.value = value;
Preconditions.checkArgument(value instanceof List || value instanceof String,
"Argument [value] should be of type [string] or [list].");
this.value = (value instanceof List) ? ((List) value) :
Collections.unmodifiableList(Arrays.asList((String) value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
*/
public class ReportPointContainsPredicate extends ComparisonPredicate<ReportPoint>{

public ReportPointContainsPredicate(String scope, String value) {
public ReportPointContainsPredicate(String scope, Object value) {
super(scope, value);
}

@Override
public boolean test(ReportPoint reportPoint) {
String pointVal = PreprocessorUtil.getReportableEntityComparableValue(scope, reportPoint);
if (pointVal != null) {
return pointVal.contains(value);
return value.stream().anyMatch(x -> pointVal.contains(x));
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
*/
public class ReportPointEndsWithPredicate extends ComparisonPredicate<ReportPoint>{

public ReportPointEndsWithPredicate(String scope, String value) {
public ReportPointEndsWithPredicate(String scope, Object value) {
super(scope, value);
}

@Override
public boolean test(ReportPoint reportPoint) {
String pointVal = PreprocessorUtil.getReportableEntityComparableValue(scope, reportPoint);
if (pointVal != null) {
return pointVal.endsWith(value);
return value.stream().anyMatch(x -> pointVal.endsWith(x));
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
*/
public class ReportPointEqualsPredicate extends ComparisonPredicate<ReportPoint>{

public ReportPointEqualsPredicate(String scope, String value) {
public ReportPointEqualsPredicate(String scope, Object value) {
super(scope, value);
}

@Override
public boolean test(ReportPoint reportPoint) {
String pointVal = PreprocessorUtil.getReportableEntityComparableValue(scope, reportPoint);
if (pointVal != null) {
return pointVal.equals(value);
return value.contains(pointVal);
}
return false;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.wavefront.agent.preprocessor.predicate;

import com.google.common.base.Preconditions;

import com.wavefront.agent.preprocessor.PreprocessorUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

import wavefront.report.ReportPoint;
Expand All @@ -13,17 +19,20 @@
*/
public class ReportPointRegexMatchPredicate extends ComparisonPredicate<ReportPoint>{

private final Pattern pattern;
public ReportPointRegexMatchPredicate(String scope, String value) {
private final List<Pattern> pattern;
public ReportPointRegexMatchPredicate(String scope, Object value) {
super(scope, value);
this.pattern = Pattern.compile(value);
this.pattern = new ArrayList<>();
for (String regex : this.value) {
this.pattern.add(Pattern.compile(regex));
}
}

@Override
public boolean test(ReportPoint reportPoint) {
String pointVal = PreprocessorUtil.getReportableEntityComparableValue(scope, reportPoint);
if (pointVal != null) {
return pattern.matcher(pointVal).matches();
return pattern.stream().anyMatch(x -> x.matcher(pointVal).matches());
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
*/
public class ReportPointStartsWithPredicate extends ComparisonPredicate<ReportPoint>{

public ReportPointStartsWithPredicate(String scope, String value) {
public ReportPointStartsWithPredicate(String scope, Object value) {
super(scope, value);
}

@Override
public boolean test(ReportPoint reportPoint) {
String pointVal = PreprocessorUtil.getReportableEntityComparableValue(scope, reportPoint);
if (pointVal != null) {
return pointVal.startsWith(value);
return value.stream().anyMatch(x -> pointVal.startsWith(x));
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
*/
public class SpanContainsPredicate extends ComparisonPredicate<Span>{

public SpanContainsPredicate(String scope, String value) {
public SpanContainsPredicate(String scope, Object value) {
super(scope, value);
}

@Override
public boolean test(Span span) {
List<String> spanVal = PreprocessorUtil.getReportableEntityComparableValue(scope, span);
if (spanVal != null) {
return spanVal.stream().anyMatch(v -> v.contains(value));
return spanVal.stream().anyMatch(sv -> value.stream().anyMatch(v -> sv.contains(v)));
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
*/
public class SpanEndsWithPredicate extends ComparisonPredicate<Span>{

public SpanEndsWithPredicate(String scope, String value) {
public SpanEndsWithPredicate(String scope, Object value) {
super(scope, value);
}

@Override
public boolean test(Span span) {
List<String> spanVal = PreprocessorUtil.getReportableEntityComparableValue(scope, span);
if (spanVal != null) {
return spanVal.stream().anyMatch(v -> v.endsWith(value));
return spanVal.stream().anyMatch(sv -> value.stream().anyMatch(v -> sv.endsWith(v)));
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
*/
public class SpanEqualsPredicate extends ComparisonPredicate<Span>{

public SpanEqualsPredicate(String scope, String value) {
public SpanEqualsPredicate(String scope, Object value) {
super(scope, value);
}

@Override
public boolean test(Span span) {
List<String> spanVal = PreprocessorUtil.getReportableEntityComparableValue(scope, span);
if (spanVal != null) {
return spanVal.contains(value);
return value.stream().anyMatch(v -> spanVal.contains(v));
}
return false;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.wavefront.agent.preprocessor.PreprocessorUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

Expand All @@ -14,17 +15,20 @@
*/
public class SpanRegexMatchPredicate extends ComparisonPredicate<Span>{

private final Pattern pattern;
public SpanRegexMatchPredicate(String scope, String value) {
private final List<Pattern> pattern;
public SpanRegexMatchPredicate(String scope, Object value) {
super(scope, value);
this.pattern = Pattern.compile(value);
this.pattern = new ArrayList<>();
for (String regex : this.value) {
this.pattern.add(Pattern.compile(regex));
}
}

@Override
public boolean test(Span span) {
List<String> spanVal = PreprocessorUtil.getReportableEntityComparableValue(scope, span);
if (spanVal != null) {
return spanVal.stream().anyMatch(v -> pattern.matcher(v).matches());
return spanVal.stream().anyMatch(sv -> pattern.stream().anyMatch(p -> p.matcher(sv).matches()));
}
return false;
}
Expand Down
Loading

0 comments on commit f5f9a6f

Please sign in to comment.