Skip to content

Commit

Permalink
Span Fix for month and quarter which prometheus doesn't support
Browse files Browse the repository at this point in the history
Signed-off-by: vamsi-amazon <[email protected]>
  • Loading branch information
vmmusings committed Nov 4, 2022
1 parent 3e30379 commit 0f8ed02
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ private JSONObject readResponse(Response response) throws IOException {
}
} else {
throw new RuntimeException(
String.format("Request to Prometheus is Unsuccessful with : %s", response.message()));
String.format("Request to Prometheus is Unsuccessful with : %s", Objects.requireNonNull(
response.body(), "Response body can't be null").string()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.apache.commons.lang3.StringUtils;
import org.opensearch.sql.ast.expression.SpanUnit;
import org.opensearch.sql.expression.NamedExpression;
import org.opensearch.sql.expression.span.SpanExpression;

Expand All @@ -26,29 +27,27 @@ public class StepParameterResolver {
* Extract step from groupByList or apply heuristic arithmetic
* on endTime and startTime.
*
*
* @param startTime startTime.
* @param endTime endTime.
* @param startTime startTime.
* @param endTime endTime.
* @param groupByList groupByList.
* @return Step String.
*/
public static String resolve(@NonNull Long startTime, @NonNull Long endTime,
List<NamedExpression> groupByList) {
List<NamedExpression> groupByList) {
Optional<SpanExpression> spanExpression = getSpanExpression(groupByList);
if (spanExpression.isPresent()) {
if (StringUtils.isEmpty(spanExpression.get().getUnit().getName())) {
throw new RuntimeException("Missing TimeUnit in the span expression");
} else {
return spanExpression.get().getValue().toString()
+ spanExpression.get().getUnit().getName();
return getTimespan(spanExpression.get());
}
} else {
return Math.max((endTime - startTime) / 250, 1) + "s";
}
}

private static Optional<SpanExpression> getSpanExpression(
List<NamedExpression> namedExpressionList) {
List<NamedExpression> namedExpressionList) {
if (namedExpressionList == null) {
return Optional.empty();
}
Expand All @@ -58,6 +57,15 @@ private static Optional<SpanExpression> getSpanExpression(
.findFirst();
}

private static String getTimespan(SpanExpression spanExpression) {
if (SpanUnit.M.equals(spanExpression.getUnit())) {
return spanExpression.getValue().valueOf(null).integerValue() * 30 + "d";
} else if (SpanUnit.Q.equals(spanExpression.getUnit())) {
return spanExpression.getValue().valueOf(null).integerValue() * 90 + "d";
} else {
return spanExpression.getValue().toString() + spanExpression.getUnit().getName();
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,33 @@

package org.opensearch.sql.prometheus.storage.querybuilders;

import static org.opensearch.sql.expression.DSL.named;

import java.util.Collections;
import java.util.Date;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.opensearch.sql.data.type.ExprCoreType;
import org.opensearch.sql.expression.DSL;
import org.opensearch.sql.prometheus.storage.querybuilder.StepParameterResolver;

public class StepParameterResolverTest {

@Test
void testNullChecks() {
StepParameterResolver stepParameterResolver = new StepParameterResolver();
Assertions.assertThrows(NullPointerException.class,
() -> stepParameterResolver.resolve(null, new Date().getTime(), Collections.emptyList()));
() -> StepParameterResolver.resolve(null, new Date().getTime(), Collections.emptyList()));
Assertions.assertThrows(NullPointerException.class,
() -> stepParameterResolver.resolve(new Date().getTime(), null, Collections.emptyList()));
() -> StepParameterResolver.resolve(new Date().getTime(), null, Collections.emptyList()));
String step = StepParameterResolver.resolve(new Date().getTime(), new Date().getTime() - 3600,
Collections.singletonList(named("span",
DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP),
DSL.literal(2), "M"))));
Assertions.assertEquals("60d", step);
step = StepParameterResolver.resolve(new Date().getTime(), new Date().getTime() - 3600,
Collections.singletonList(named("span",
DSL.span(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP),
DSL.literal(4), "Q"))));
Assertions.assertEquals("360d", step);
}
}

0 comments on commit 0f8ed02

Please sign in to comment.