Skip to content

Commit

Permalink
Fixed error with single timestamp query (#1244) (#1249)
Browse files Browse the repository at this point in the history
Signed-off-by: vamsi-amazon <[email protected]>

Signed-off-by: vamsi-amazon <[email protected]>
(cherry picked from commit ee949cc)

Co-authored-by: vamsi-amazon <[email protected]>
  • Loading branch information
opensearch-trigger-bot[bot] and vmmusings authored Jan 9, 2023
1 parent 31f55ca commit 0a37306
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public static String build(String metricName, Expression filterCondition) {
SeriesSelectionExpressionNodeVisitor seriesSelectionExpressionNodeVisitor
= new SeriesSelectionExpressionNodeVisitor();
String selectorQuery = filterCondition.accept(seriesSelectionExpressionNodeVisitor, null);
return metricName + "{" + selectorQuery + "}";
if (selectorQuery != null) {
return metricName + "{" + selectorQuery + "}";
}
}
return metricName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ void testTimeRangeResolverWithOutEndTimeInFilter() {
new PrometheusMetricTable(client, "prometheus_http_total_requests");


//Both endTime and startTime are set.
//Only endTime is set.
List<NamedExpression> finalProjectList = new ArrayList<>();
finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING)));
finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP)));
Expand Down Expand Up @@ -724,6 +724,35 @@ void testImplementWithRelationAndFilter() {
assertEquals(List.of(VALUE, TIMESTAMP), outputFields);
}

@Test
void testImplementWithRelationAndTimestampFilter() {
List<NamedExpression> finalProjectList = new ArrayList<>();
finalProjectList.add(DSL.named(VALUE, DSL.ref(VALUE, STRING)));
finalProjectList.add(DSL.named(TIMESTAMP, DSL.ref(TIMESTAMP, ExprCoreType.TIMESTAMP)));
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Long endTime = new Date(System.currentTimeMillis()).getTime();
PrometheusMetricTable prometheusMetricTable =
new PrometheusMetricTable(client, "prometheus_http_total_requests");
LogicalPlan logicalPlan = project(indexScan("prometheus_http_total_requests",
DSL.lte(DSL.ref("@timestamp", ExprCoreType.TIMESTAMP),
DSL.literal(
fromObjectValue(dateFormat.format(new Date(endTime)),
ExprCoreType.TIMESTAMP)))
), finalProjectList, null);
PhysicalPlan physicalPlan = prometheusMetricTable.implement(logicalPlan);
assertTrue(physicalPlan instanceof ProjectOperator);
assertTrue(((ProjectOperator) physicalPlan).getInput() instanceof PrometheusMetricScan);
PrometheusQueryRequest request
= ((PrometheusMetricScan) ((ProjectOperator) physicalPlan).getInput()).getRequest();
assertEquals((3600 / 250) + "s", request.getStep());
assertEquals("prometheus_http_total_requests",
request.getPromQl());
List<NamedExpression> projectList = ((ProjectOperator) physicalPlan).getProjectList();
List<String> outputFields
= projectList.stream().map(NamedExpression::getName).collect(Collectors.toList());
assertEquals(List.of(VALUE, TIMESTAMP), outputFields);
}

@Test
void testOptimize() {
PrometheusQueryRequest prometheusQueryRequest = new PrometheusQueryRequest();
Expand Down

0 comments on commit 0a37306

Please sign in to comment.