Skip to content

Commit

Permalink
Fix environment and service name match for unary span filter expressi…
Browse files Browse the repository at this point in the history
…on (#234)
  • Loading branch information
AnandShivansh authored Aug 29, 2024
1 parent 829a71c commit aec20fe
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.hypertrace.span.processing.config.service.v1.RelationalSpanFilterExpression;
import org.hypertrace.span.processing.config.service.v1.SpanFilter;
import org.hypertrace.span.processing.config.service.v1.SpanFilterValue;
import org.hypertrace.span.processing.config.service.v1.UnarySpanFilterExpression;
import org.hypertrace.span.processing.config.service.v1.UnarySpanFilterExpression.LeftOperandCase;
import org.hypertrace.span.processing.config.service.v1.UpdateExcludeSpanRule;
import org.hypertrace.span.processing.config.service.v1.UpdateExcludeSpanRuleRequest;

Expand Down Expand Up @@ -70,6 +72,9 @@ private void validateSpanFilter(SpanFilter filter) {
case RELATIONAL_SPAN_FILTER:
validateRelationalSpanFilter(filter);
break;
case UNARY_SPAN_FILTER:
validateUnarySpanFilter(filter);
break;
default:
throw Status.INVALID_ARGUMENT
.withDescription("Unexpected filter case: " + printMessage(filter))
Expand All @@ -96,6 +101,19 @@ private void validateRelationalSpanFilter(SpanFilter filter) {
}
}

private void validateUnarySpanFilter(SpanFilter filter) {
validateNonDefaultPresenceOrThrow(
filter.getUnarySpanFilter(), UnarySpanFilterExpression.OPERATOR_FIELD_NUMBER);

final LeftOperandCase leftOperandCase = filter.getUnarySpanFilter().getLeftOperandCase();
if (LeftOperandCase.FIELD.equals(leftOperandCase)) {
throw Status.INVALID_ARGUMENT
.withDescription(
"Unary operator is not supported on Field operand: " + printMessage(filter))
.asRuntimeException();
}
}

private void validateRegex(String regex) {
try {
Pattern.compile(regex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.hypertrace.span.processing.config.service.v1.Field;
import org.hypertrace.span.processing.config.service.v1.ListValue;
import org.hypertrace.span.processing.config.service.v1.LogicalOperator;
import org.hypertrace.span.processing.config.service.v1.LogicalSpanFilterExpression;
import org.hypertrace.span.processing.config.service.v1.RelationalOperator;
import org.hypertrace.span.processing.config.service.v1.RelationalSpanFilterExpression;
import org.hypertrace.span.processing.config.service.v1.SpanFilter;
Expand All @@ -16,18 +17,24 @@
public class SpanFilterMatcher {

public boolean matchesEnvironment(SpanFilter spanFilter, Optional<String> environment) {
if (spanFilter.hasUnarySpanFilter()) {
return true;
}
if (spanFilter.hasRelationalSpanFilter()) {
return matchesEnvironment(spanFilter.getRelationalSpanFilter(), environment);
} else {
if (spanFilter
.getLogicalSpanFilter()
.getOperator()
.equals(LogicalOperator.LOGICAL_OPERATOR_AND)) {
if (hasNoRelationalFilters(spanFilter.getLogicalSpanFilter())) {
return true;
}
return spanFilter.getLogicalSpanFilter().getOperandsList().stream()
.filter(SpanFilter::hasRelationalSpanFilter)
.allMatch(filter -> matchesEnvironment(filter.getRelationalSpanFilter(), environment));
} else {
if (spanFilter.getLogicalSpanFilter().getOperandsCount() == 0) {
if (hasNoRelationalFilters(spanFilter.getLogicalSpanFilter())) {
return true;
}
return spanFilter.getLogicalSpanFilter().getOperandsList().stream()
Expand All @@ -38,18 +45,24 @@ public boolean matchesEnvironment(SpanFilter spanFilter, Optional<String> enviro
}

public boolean matchesServiceName(SpanFilter spanFilter, String serviceName) {
if (spanFilter.hasUnarySpanFilter()) {
return true;
}
if (spanFilter.hasRelationalSpanFilter()) {
return matchesServiceName(spanFilter.getRelationalSpanFilter(), serviceName);
} else {
if (spanFilter
.getLogicalSpanFilter()
.getOperator()
.equals(LogicalOperator.LOGICAL_OPERATOR_AND)) {
if (hasNoRelationalFilters(spanFilter.getLogicalSpanFilter())) {
return true;
}
return spanFilter.getLogicalSpanFilter().getOperandsList().stream()
.filter(SpanFilter::hasRelationalSpanFilter)
.allMatch(filter -> matchesServiceName(filter.getRelationalSpanFilter(), serviceName));
} else {
if (spanFilter.getLogicalSpanFilter().getOperandsCount() == 0) {
if (hasNoRelationalFilters(spanFilter.getLogicalSpanFilter())) {
return true;
}
return spanFilter.getLogicalSpanFilter().getOperandsList().stream()
Expand All @@ -59,6 +72,12 @@ public boolean matchesServiceName(SpanFilter spanFilter, String serviceName) {
}
}

private boolean hasNoRelationalFilters(LogicalSpanFilterExpression logicalSpanFilterExpression) {
return logicalSpanFilterExpression.getOperandsCount() == 0
|| logicalSpanFilterExpression.getOperandsList().stream()
.noneMatch(SpanFilter::hasRelationalSpanFilter);
}

private boolean matchesEnvironment(
RelationalSpanFilterExpression relationalSpanFilterExpression, Optional<String> environment) {
if (environment.isEmpty()) {
Expand Down

0 comments on commit aec20fe

Please sign in to comment.