|
| 1 | +package org.hypertrace.graphql.spanprocessing.dao; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableBiMap; |
| 4 | +import io.reactivex.rxjava3.core.Single; |
| 5 | +import java.util.List; |
| 6 | +import java.util.NoSuchElementException; |
| 7 | +import java.util.Objects; |
| 8 | +import java.util.stream.Collectors; |
| 9 | +import lombok.Value; |
| 10 | +import lombok.experimental.Accessors; |
| 11 | +import org.hypertrace.graphql.spanprocessing.schema.rule.filter.SpanProcessingFilterField; |
| 12 | +import org.hypertrace.graphql.spanprocessing.schema.rule.filter.SpanProcessingLogicalFilter; |
| 13 | +import org.hypertrace.graphql.spanprocessing.schema.rule.filter.SpanProcessingLogicalOperator; |
| 14 | +import org.hypertrace.graphql.spanprocessing.schema.rule.filter.SpanProcessingRelationalFilter; |
| 15 | +import org.hypertrace.graphql.spanprocessing.schema.rule.filter.SpanProcessingRelationalOperator; |
| 16 | +import org.hypertrace.graphql.spanprocessing.schema.rule.filter.SpanProcessingRuleFilter; |
| 17 | +import org.hypertrace.span.processing.config.service.v1.Field; |
| 18 | +import org.hypertrace.span.processing.config.service.v1.LogicalOperator; |
| 19 | +import org.hypertrace.span.processing.config.service.v1.LogicalSpanFilterExpression; |
| 20 | +import org.hypertrace.span.processing.config.service.v1.RelationalOperator; |
| 21 | +import org.hypertrace.span.processing.config.service.v1.RelationalSpanFilterExpression; |
| 22 | +import org.hypertrace.span.processing.config.service.v1.SpanFilter; |
| 23 | +import org.hypertrace.span.processing.config.service.v1.SpanFilterValue; |
| 24 | + |
| 25 | +public class ConfigServiceSpanFilterConverter { |
| 26 | + |
| 27 | + private static final ImmutableBiMap<LogicalOperator, SpanProcessingLogicalOperator> |
| 28 | + LOGICAL_OPERATOR_OPERATOR_BI_MAP = |
| 29 | + ImmutableBiMap.of( |
| 30 | + LogicalOperator.LOGICAL_OPERATOR_AND, |
| 31 | + SpanProcessingLogicalOperator.AND, |
| 32 | + LogicalOperator.LOGICAL_OPERATOR_OR, |
| 33 | + SpanProcessingLogicalOperator.OR); |
| 34 | + |
| 35 | + private static final ImmutableBiMap<SpanProcessingLogicalOperator, LogicalOperator> |
| 36 | + OPERATOR_LOGICAL_OPERATOR_IMMUTABLE_BI_MAP = LOGICAL_OPERATOR_OPERATOR_BI_MAP.inverse(); |
| 37 | + |
| 38 | + private static final ImmutableBiMap<RelationalOperator, SpanProcessingRelationalOperator> |
| 39 | + RELATIONAL_OPERATOR_OPERATOR_BI_MAP = |
| 40 | + ImmutableBiMap.<RelationalOperator, SpanProcessingRelationalOperator>builder() |
| 41 | + .put( |
| 42 | + RelationalOperator.RELATIONAL_OPERATOR_CONTAINS, |
| 43 | + SpanProcessingRelationalOperator.CONTAINS) |
| 44 | + .put( |
| 45 | + RelationalOperator.RELATIONAL_OPERATOR_EQUALS, |
| 46 | + SpanProcessingRelationalOperator.EQUALS) |
| 47 | + .put( |
| 48 | + RelationalOperator.RELATIONAL_OPERATOR_NOT_EQUALS, |
| 49 | + SpanProcessingRelationalOperator.NOT_EQUALS) |
| 50 | + .put( |
| 51 | + RelationalOperator.RELATIONAL_OPERATOR_STARTS_WITH, |
| 52 | + SpanProcessingRelationalOperator.STARTS_WITH) |
| 53 | + .put( |
| 54 | + RelationalOperator.RELATIONAL_OPERATOR_ENDS_WITH, |
| 55 | + SpanProcessingRelationalOperator.ENDS_WITH) |
| 56 | + .put( |
| 57 | + RelationalOperator.RELATIONAL_OPERATOR_REGEX_MATCH, |
| 58 | + SpanProcessingRelationalOperator.REGEX_MATCH) |
| 59 | + .build(); |
| 60 | + |
| 61 | + private static final ImmutableBiMap<SpanProcessingRelationalOperator, RelationalOperator> |
| 62 | + OPERATOR_RELATIONAL_OPERATOR_IMMUTABLE_BI_MAP = RELATIONAL_OPERATOR_OPERATOR_BI_MAP.inverse(); |
| 63 | + |
| 64 | + private static final ImmutableBiMap<Field, SpanProcessingFilterField> |
| 65 | + FIELD_FILTER_FIELD_IMMUTABLE_BI_MAP = |
| 66 | + ImmutableBiMap.<Field, SpanProcessingFilterField>builder() |
| 67 | + .put(Field.FIELD_URL, SpanProcessingFilterField.URL) |
| 68 | + .put(Field.FIELD_SERVICE_NAME, SpanProcessingFilterField.SERVICE_NAME) |
| 69 | + .put(Field.FIELD_ENVIRONMENT_NAME, SpanProcessingFilterField.ENVIRONMENT_NAME) |
| 70 | + .build(); |
| 71 | + |
| 72 | + private static final ImmutableBiMap<SpanProcessingFilterField, Field> |
| 73 | + FILTER_FIELD_FIELD_IMMUTABLE_BI_MAP = FIELD_FILTER_FIELD_IMMUTABLE_BI_MAP.inverse(); |
| 74 | + |
| 75 | + public Single<SpanProcessingRuleFilter> convert(SpanFilter filter) { |
| 76 | + return Single.just(Objects.requireNonNull(convertFilter(filter))); |
| 77 | + } |
| 78 | + |
| 79 | + public SpanFilter convert(SpanProcessingRuleFilter filter) { |
| 80 | + if (filter == null) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + SpanFilter.Builder spanFilterBuilder = SpanFilter.newBuilder(); |
| 84 | + if (filter.logicalSpanFilter() != null) { |
| 85 | + spanFilterBuilder = |
| 86 | + spanFilterBuilder.setLogicalSpanFilter(convertLogicalFilter(filter.logicalSpanFilter())); |
| 87 | + } else if (filter.relationalSpanFilter() != null) { |
| 88 | + spanFilterBuilder = |
| 89 | + spanFilterBuilder.setRelationalSpanFilter( |
| 90 | + convertRelationalFilter(filter.relationalSpanFilter())); |
| 91 | + } |
| 92 | + return spanFilterBuilder.build(); |
| 93 | + } |
| 94 | + |
| 95 | + private SpanProcessingRuleFilter convertFilter(SpanFilter filter) { |
| 96 | + if (filter.equals(SpanFilter.getDefaultInstance())) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + return new ConvertedSpanProcessingRuleFilter( |
| 100 | + this.convertLogicalFilter(filter.getLogicalSpanFilter()), |
| 101 | + this.convertRelationalFilter(filter.getRelationalSpanFilter())); |
| 102 | + } |
| 103 | + |
| 104 | + private SpanProcessingLogicalFilter convertLogicalFilter( |
| 105 | + LogicalSpanFilterExpression logicalSpanFilterExpression) { |
| 106 | + if (logicalSpanFilterExpression.equals(LogicalSpanFilterExpression.getDefaultInstance())) { |
| 107 | + return null; |
| 108 | + } |
| 109 | + return new ConvertedSpanProcessingLogicalFilter( |
| 110 | + LOGICAL_OPERATOR_OPERATOR_BI_MAP.get(logicalSpanFilterExpression.getOperator()), |
| 111 | + logicalSpanFilterExpression.getOperandsList().stream() |
| 112 | + .map(this::convertFilter) |
| 113 | + .collect(Collectors.toUnmodifiableList())); |
| 114 | + } |
| 115 | + |
| 116 | + private LogicalSpanFilterExpression convertLogicalFilter( |
| 117 | + SpanProcessingLogicalFilter spanProcessingLogicalFilter) { |
| 118 | + return LogicalSpanFilterExpression.newBuilder() |
| 119 | + .setOperator( |
| 120 | + Objects.requireNonNull( |
| 121 | + OPERATOR_LOGICAL_OPERATOR_IMMUTABLE_BI_MAP.get( |
| 122 | + spanProcessingLogicalFilter.logicalOperator()))) |
| 123 | + .addAllOperands( |
| 124 | + spanProcessingLogicalFilter.spanFilters().stream() |
| 125 | + .map(this::convert) |
| 126 | + .collect(Collectors.toUnmodifiableList())) |
| 127 | + .build(); |
| 128 | + } |
| 129 | + |
| 130 | + private SpanProcessingRelationalFilter convertRelationalFilter( |
| 131 | + RelationalSpanFilterExpression relationalSpanFilterExpression) { |
| 132 | + if (relationalSpanFilterExpression.equals( |
| 133 | + RelationalSpanFilterExpression.getDefaultInstance())) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + return new ConvertedSpanProcessingRelationalFilter( |
| 137 | + RELATIONAL_OPERATOR_OPERATOR_BI_MAP.get(relationalSpanFilterExpression.getOperator()), |
| 138 | + relationalSpanFilterExpression.hasSpanAttributeKey() |
| 139 | + ? relationalSpanFilterExpression.getSpanAttributeKey() |
| 140 | + : null, |
| 141 | + relationalSpanFilterExpression.hasField() |
| 142 | + ? FIELD_FILTER_FIELD_IMMUTABLE_BI_MAP.get(relationalSpanFilterExpression.getField()) |
| 143 | + : null, |
| 144 | + convertSpanFilterValue(relationalSpanFilterExpression.getRightOperand())); |
| 145 | + } |
| 146 | + |
| 147 | + private Object convertSpanFilterValue(SpanFilterValue spanFilterValue) { |
| 148 | + switch (spanFilterValue.getValueCase()) { |
| 149 | + case STRING_VALUE: |
| 150 | + return spanFilterValue.getStringValue(); |
| 151 | + default: |
| 152 | + throw new NoSuchElementException("Unsupported right operand type"); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private RelationalSpanFilterExpression convertRelationalFilter( |
| 157 | + SpanProcessingRelationalFilter spanProcessingRelationalFilter) { |
| 158 | + RelationalSpanFilterExpression.Builder relationalSpanFilterExpressionBuilder = |
| 159 | + RelationalSpanFilterExpression.newBuilder() |
| 160 | + .setOperator( |
| 161 | + Objects.requireNonNull( |
| 162 | + OPERATOR_RELATIONAL_OPERATOR_IMMUTABLE_BI_MAP.get( |
| 163 | + spanProcessingRelationalFilter.relationalOperator()))) |
| 164 | + .setRightOperand(convertToSpanFilterValue(spanProcessingRelationalFilter.value())); |
| 165 | + |
| 166 | + if (spanProcessingRelationalFilter.key() != null) { |
| 167 | + relationalSpanFilterExpressionBuilder = |
| 168 | + relationalSpanFilterExpressionBuilder.setSpanAttributeKey( |
| 169 | + spanProcessingRelationalFilter.key()); |
| 170 | + } else { |
| 171 | + relationalSpanFilterExpressionBuilder = |
| 172 | + relationalSpanFilterExpressionBuilder.setField( |
| 173 | + Objects.requireNonNull( |
| 174 | + FILTER_FIELD_FIELD_IMMUTABLE_BI_MAP.get(spanProcessingRelationalFilter.field()))); |
| 175 | + } |
| 176 | + return relationalSpanFilterExpressionBuilder.build(); |
| 177 | + } |
| 178 | + |
| 179 | + private SpanFilterValue convertToSpanFilterValue(Object value) { |
| 180 | + SpanFilterValue.Builder spanFilterValueBuilder = SpanFilterValue.newBuilder(); |
| 181 | + if (String.class.equals(value.getClass())) { |
| 182 | + spanFilterValueBuilder = spanFilterValueBuilder.setStringValue(value.toString()); |
| 183 | + } |
| 184 | + return spanFilterValueBuilder.build(); |
| 185 | + } |
| 186 | + |
| 187 | + @Value |
| 188 | + @Accessors(fluent = true) |
| 189 | + private static class ConvertedSpanProcessingRelationalFilter |
| 190 | + implements SpanProcessingRelationalFilter { |
| 191 | + SpanProcessingRelationalOperator relationalOperator; |
| 192 | + String key; |
| 193 | + SpanProcessingFilterField field; |
| 194 | + Object value; |
| 195 | + } |
| 196 | + |
| 197 | + @Value |
| 198 | + @Accessors(fluent = true) |
| 199 | + private static class ConvertedSpanProcessingLogicalFilter implements SpanProcessingLogicalFilter { |
| 200 | + SpanProcessingLogicalOperator logicalOperator; |
| 201 | + List<SpanProcessingRuleFilter> spanFilters; |
| 202 | + } |
| 203 | + |
| 204 | + @Value |
| 205 | + @Accessors(fluent = true) |
| 206 | + private static class ConvertedSpanProcessingRuleFilter implements SpanProcessingRuleFilter { |
| 207 | + SpanProcessingLogicalFilter logicalSpanFilter; |
| 208 | + SpanProcessingRelationalFilter relationalSpanFilter; |
| 209 | + } |
| 210 | +} |
0 commit comments