Support parse PromQL expression has empty labels in the braces for metadata query. (#10599)

This commit is contained in:
Wan Kai 2023-03-28 11:04:52 +08:00 committed by GitHub
parent de047de87c
commit ec25814354
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 9 deletions

View File

@ -20,6 +20,7 @@
* [Breaking Change] Support cross-thread trace profiling. The data structure and query APIs are changed.
* Fix PromQL HTTP API `/api/v1/labels` response missing `service` label.
* Fix possible NPE when initialize `IntList`.
* Support parse PromQL expression has empty labels in the braces for metadata query.
#### UI
* Revert: cpm5d function. This feature is cancelled from backend.

View File

@ -36,7 +36,7 @@ mulDivMod: MUL | DIV | MOD;
compare: (DEQ | NEQ | LTE | LT | GTE | GT) BOOL?;
metricName: NAME_STRING;
metricInstant: metricName | metricName L_BRACE labelList R_BRACE;
metricInstant: metricName | metricName L_BRACE labelList? R_BRACE;
metricRange: metricInstant L_BRACKET DURATION R_BRACKET;
labelName: NAME_STRING;

View File

@ -18,6 +18,8 @@
package org.apache.skywalking.promql.rt.parser;
import java.util.Arrays;
import java.util.Collection;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
@ -27,20 +29,49 @@ import org.apache.skywalking.oap.query.promql.rt.result.ParseResultType;
import org.apache.skywalking.promql.rt.grammar.PromQLLexer;
import org.apache.skywalking.promql.rt.grammar.PromQLParser;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
public class PromQLMatchVisitorTest {
@Test
public void testMatchVisitor() {
PromQLLexer lexer = new PromQLLexer(
CharStreams.fromString("service_cpm{service='serviceA', layer='GENERAL'}"));
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{
"service_cpm",
ParseResultType.MATCH,
"service_cpm",
0
},
{
"service_cpm{}",
ParseResultType.MATCH,
"service_cpm",
0
},
{
"service_cpm{service='serviceA', layer='GENERAL'}",
ParseResultType.MATCH,
"service_cpm",
2
}
});
}
@ParameterizedTest
@MethodSource("data")
public void testMatchVisitor(
String expression,
ParseResultType wantType,
String metricName,
int labelsSize) {
PromQLLexer lexer = new PromQLLexer(CharStreams.fromString(expression));
CommonTokenStream tokens = new CommonTokenStream(lexer);
PromQLParser parser = new PromQLParser(tokens);
ParseTree tree = parser.expression();
PromQLMatchVisitor visitor = new PromQLMatchVisitor();
MatcherSetResult parseResult = visitor.visit(tree);
Assertions.assertEquals(ParseResultType.MATCH, parseResult.getResultType());
Assertions.assertEquals("service_cpm", parseResult.getMetricName());
Assertions.assertEquals(2, parseResult.getLabelMap().size());
Assertions.assertEquals(wantType, parseResult.getResultType());
Assertions.assertEquals(metricName, parseResult.getMetricName());
Assertions.assertEquals(labelsSize, parseResult.getLabelMap().size());
}
}