You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wa...@apache.org on 2023/06/09 03:32:28 UTC

[skywalking] branch master updated: MQE: remove query `returnTypeOfMQE`, add new aggregation operator `latest` (#10906)

This is an automated email from the ASF dual-hosted git repository.

wankai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking.git


The following commit(s) were added to refs/heads/master by this push:
     new 260a7106dd MQE: remove query `returnTypeOfMQE`, add new aggregation operator `latest` (#10906)
260a7106dd is described below

commit 260a7106dd799aa9a94ab99dd5ee307a94a51b1d
Author: Wan Kai <wa...@foxmail.com>
AuthorDate: Fri Jun 9 11:32:16 2023 +0800

    MQE: remove query `returnTypeOfMQE`, add new aggregation operator `latest` (#10906)
---
 docs/en/api/metrics-query-expression.md            | 18 +++---
 docs/en/api/query-protocol.md                      |  2 -
 .../apache/skywalking/mqe/rt/grammar/MQELexer.g4   |  1 +
 .../apache/skywalking/mqe/rt/grammar/MQEParser.g4  |  2 +-
 .../oap/query/graphql/mqe/rt/MQEVisitor.java       | 67 ++++++----------------
 .../graphql/mqe/rt/operation/AggregationOp.java    |  8 +++
 .../graphql/resolver/MetricsExpressionQuery.java   | 22 -------
 .../src/main/resources/query-protocol              |  2 +-
 8 files changed, 40 insertions(+), 82 deletions(-)

diff --git a/docs/en/api/metrics-query-expression.md b/docs/en/api/metrics-query-expression.md
index aa192f5450..847bd061a9 100644
--- a/docs/en/api/metrics-query-expression.md
+++ b/docs/en/api/metrics-query-expression.md
@@ -72,7 +72,8 @@ The result type of the expression please refer to the following table.
 
 ### Binary Operation Rules
 The following table listed if the difference result types of the input expressions could do this operation and the result type after the operation.
-The expression could on the left or right side of the operator.
+The expression could on the left or right side of the operator. 
+**Note**: If the expressions on both sides of the operator are the `TIME_SERIES_VALUES with labels`, they should have the same labels for calculation.
 
 | Expression              | Expression                | Yes/No | ExpressionResultType     |
 |-------------------------|---------------------------|--------|--------------------------|
@@ -91,13 +92,14 @@ Expression:
 <Aggregation-Operator>(Expression)
 ```
 
-| Operator | Definition                     | ExpressionResultType |
-|----------|--------------------------------|----------------------|
-| avg      | average the result             | SINGLE_VALUE         |
-| count    | count number of the result     | SINGLE_VALUE         |
-| sum      | sum the result                 | SINGLE_VALUE         |
-| max      | select maximum from the result | SINGLE_VALUE         |
-| min      | select minimum from the result | SINGLE_VALUE         |
+| Operator | Definition                                        | ExpressionResultType |
+|----------|---------------------------------------------------|----------------------|
+| avg      | average the result                                | SINGLE_VALUE         |
+| count    | count number of the result                        | SINGLE_VALUE         |
+| latest   | select the latest non-null value from the result  | SINGLE_VALUE         |
+| sum      | sum the result                                    | SINGLE_VALUE         |
+| max      | select maximum from the result                    | SINGLE_VALUE         |
+| min      | select minimum from the result                    | SINGLE_VALUE         |
 
 For example:
 If we want to query the average value of the `service_cpm` metric, we can use the following expression:
diff --git a/docs/en/api/query-protocol.md b/docs/en/api/query-protocol.md
index 95cccd8bf5..0328e9f7d6 100644
--- a/docs/en/api/query-protocol.md
+++ b/docs/en/api/query-protocol.md
@@ -84,8 +84,6 @@ extend type Query {
     # Get the list of all available metrics in the current OAP server.
     # Param, regex, could be used to filter the metrics by name.
     listMetrics(regex: String): [MetricDefinition!]!
-    # The return type of the given expression, the MQEValues will be empty.
-    returnTypeOfMQE(expression: String!): ExpressionResult!
     execExpression(expression: String!, entity: Entity!, duration: Duration!): ExpressionResult!
 }
 ```
diff --git a/oap-server/server-query-plugin/mqe-grammar/src/main/antlr4/org/apache/skywalking/mqe/rt/grammar/MQELexer.g4 b/oap-server/server-query-plugin/mqe-grammar/src/main/antlr4/org/apache/skywalking/mqe/rt/grammar/MQELexer.g4
index 90a54d7fa9..ee7396498e 100644
--- a/oap-server/server-query-plugin/mqe-grammar/src/main/antlr4/org/apache/skywalking/mqe/rt/grammar/MQELexer.g4
+++ b/oap-server/server-query-plugin/mqe-grammar/src/main/antlr4/org/apache/skywalking/mqe/rt/grammar/MQELexer.g4
@@ -49,6 +49,7 @@ GT:          '>';
 // Aggregation operators
 AVG:         'avg';
 COUNT:       'count';
+LATEST:      'latest';
 MAX:         'max';
 MIN:         'min';
 SUM:         'sum';
diff --git a/oap-server/server-query-plugin/mqe-grammar/src/main/antlr4/org/apache/skywalking/mqe/rt/grammar/MQEParser.g4 b/oap-server/server-query-plugin/mqe-grammar/src/main/antlr4/org/apache/skywalking/mqe/rt/grammar/MQEParser.g4
index 17077e8da3..77892763fe 100644
--- a/oap-server/server-query-plugin/mqe-grammar/src/main/antlr4/org/apache/skywalking/mqe/rt/grammar/MQEParser.g4
+++ b/oap-server/server-query-plugin/mqe-grammar/src/main/antlr4/org/apache/skywalking/mqe/rt/grammar/MQEParser.g4
@@ -52,7 +52,7 @@ label:           labelName EQ labelValue;
 scalar:   INTEGER | DECIMAL;
 
 aggregation:
-    AVG | COUNT | SUM | MAX | MIN ;
+    AVG | COUNT | LATEST | SUM | MAX | MIN | ;
 
 // 0 parameter function
 function0:
diff --git a/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/mqe/rt/MQEVisitor.java b/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/mqe/rt/MQEVisitor.java
index 7dc497c87e..9bf42d760b 100644
--- a/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/mqe/rt/MQEVisitor.java
+++ b/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/mqe/rt/MQEVisitor.java
@@ -77,16 +77,6 @@ public class MQEVisitor extends MQEParserBaseVisitor<ExpressionResult> {
         this.duration = duration;
     }
 
-    /**
-     * This constructor is only used for parsing expresstions, won't query any metrics from backend.
-     */
-    public MQEVisitor() {
-        this.metricsQuery = null;
-        this.recordsQuery = null;
-        this.entity = null;
-        this.duration = null;
-    }
-
     @Override
     public ExpressionResult visitAddSubOp(MQEParser.AddSubOpContext ctx) {
         ExpressionResult left = visit(ctx.expression(0));
@@ -292,19 +282,14 @@ public class MQEVisitor extends MQEParserBaseVisitor<ExpressionResult> {
                                   int topN,
                                   Order order,
                                   ExpressionResult result) throws IOException {
-        List<SelectedRecord> selectedRecords;
-        // API `returnTypeOfMQE` not require entity and duration.
-        if (entity != null && duration != null && metricsQuery != null) {
-            TopNCondition topNCondition = new TopNCondition();
-            topNCondition.setName(metricName);
-            topNCondition.setTopN(topN);
-            topNCondition.setParentService(entity.getServiceName());
-            topNCondition.setOrder(order);
-            topNCondition.setNormal(entity.getNormal());
-            selectedRecords = metricsQuery.sortMetrics(topNCondition, duration);
-        } else {
-            selectedRecords = Collections.emptyList();
-        }
+        TopNCondition topNCondition = new TopNCondition();
+        topNCondition.setName(metricName);
+        topNCondition.setTopN(topN);
+        topNCondition.setParentService(entity.getServiceName());
+        topNCondition.setOrder(order);
+        topNCondition.setNormal(entity.getNormal());
+        List<SelectedRecord> selectedRecords = metricsQuery.sortMetrics(topNCondition, duration);
+
         List<MQEValue> mqeValueList = new ArrayList<>(selectedRecords.size());
         selectedRecords.forEach(selectedRecord -> {
             MQEValue mqeValue = new MQEValue();
@@ -352,18 +337,11 @@ public class MQEVisitor extends MQEParserBaseVisitor<ExpressionResult> {
     }
 
     private void queryMetrics(String metricName, ExpressionResult result) throws IOException {
-        MetricsValues metricsValues;
-        List<PointOfTime> times;
-        if (entity != null && duration != null && metricsQuery != null) {
-            MetricsCondition metricsCondition = new MetricsCondition();
-            metricsCondition.setName(metricName);
-            metricsCondition.setEntity(entity);
-            metricsValues = metricsQuery.readMetricsValues(metricsCondition, duration);
-            times = duration.assembleDurationPoints();
-        } else {
-            metricsValues = new MetricsValues();
-            times = Collections.emptyList();
-        }
+        MetricsCondition metricsCondition = new MetricsCondition();
+        metricsCondition.setName(metricName);
+        metricsCondition.setEntity(entity);
+        MetricsValues metricsValues = metricsQuery.readMetricsValues(metricsCondition, duration);
+        List<PointOfTime> times = duration.assembleDurationPoints();
         List<MQEValue> mqeValueList = new ArrayList<>(times.size());
         for (int i = 0; i < times.size(); i++) {
             long retTimestamp = DurationUtils.INSTANCE.parseToDateTime(duration.getStep(), times.get(i).getPoint())
@@ -386,19 +364,12 @@ public class MQEVisitor extends MQEParserBaseVisitor<ExpressionResult> {
     private void queryLabeledMetrics(String metricName,
                                      List<String> queryLabelList,
                                      ExpressionResult result) throws IOException {
-        List<MetricsValues> metricsValuesList;
-        List<PointOfTime> times;
-        if (entity != null && duration != null && metricsQuery != null) {
-            MetricsCondition metricsCondition = new MetricsCondition();
-            metricsCondition.setName(metricName);
-            metricsCondition.setEntity(entity);
-            metricsValuesList = metricsQuery.readLabeledMetricsValues(
-                metricsCondition, queryLabelList, duration);
-            times = duration.assembleDurationPoints();
-        } else {
-            metricsValuesList = Collections.emptyList();
-            times = Collections.emptyList();
-        }
+        MetricsCondition metricsCondition = new MetricsCondition();
+        metricsCondition.setName(metricName);
+        metricsCondition.setEntity(entity);
+        List<MetricsValues> metricsValuesList = metricsQuery.readLabeledMetricsValues(
+            metricsCondition, queryLabelList, duration);
+        List<PointOfTime> times = duration.assembleDurationPoints();
         metricsValuesList.forEach(metricsValues -> {
             List<MQEValue> mqeValueList = new ArrayList<>(times.size());
             for (int i = 0; i < times.size(); i++) {
diff --git a/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/mqe/rt/operation/AggregationOp.java b/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/mqe/rt/operation/AggregationOp.java
index 8d9088ae70..201f90e45a 100644
--- a/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/mqe/rt/operation/AggregationOp.java
+++ b/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/mqe/rt/operation/AggregationOp.java
@@ -18,6 +18,7 @@
 
 package org.apache.skywalking.oap.query.graphql.mqe.rt.operation;
 
+import com.google.common.collect.Streams;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
@@ -47,6 +48,13 @@ public class AggregationOp {
             case MQEParser.COUNT:
                 return aggregateResult(result, mqeValues -> OptionalDouble.of(
                     mqeValues.getValues().stream().filter(mqeValue -> !mqeValue.isEmptyValue()).count()));
+            case MQEParser.LATEST:
+                if (result.getType() != ExpressionResultType.TIME_SERIES_VALUES) {
+                    throw new IllegalExpressionException("LATEST can only be used in time series result.");
+                }
+                return selectResult(result, mqeValues -> Streams.findLast(mqeValues.getValues()
+                                                                                   .stream()
+                                                                                   .filter(mqeValue -> !mqeValue.isEmptyValue())));
             case MQEParser.MAX:
                 return selectResult(result, mqeValues -> mqeValues.getValues()
                                                                   .stream()
diff --git a/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/resolver/MetricsExpressionQuery.java b/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/resolver/MetricsExpressionQuery.java
index 4f8735575d..2a485d3c81 100644
--- a/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/resolver/MetricsExpressionQuery.java
+++ b/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/resolver/MetricsExpressionQuery.java
@@ -21,7 +21,6 @@ package org.apache.skywalking.oap.query.graphql.resolver;
 import graphql.kickstart.tools.GraphQLQueryResolver;
 import graphql.org.antlr.v4.runtime.misc.ParseCancellationException;
 import java.text.DecimalFormat;
-import java.util.Collections;
 import org.antlr.v4.runtime.CharStreams;
 import org.antlr.v4.runtime.CommonTokenStream;
 import org.antlr.v4.runtime.tree.ParseTree;
@@ -46,27 +45,6 @@ public class MetricsExpressionQuery implements GraphQLQueryResolver {
         this.valueFormat.setGroupingUsed(false);
     }
 
-    public ExpressionResult returnTypeOfMQE(String expression) {
-        MQELexer lexer = new MQELexer(
-            CharStreams.fromString(expression));
-        lexer.addErrorListener(new ParseErrorListener());
-        MQEParser parser = new MQEParser(new CommonTokenStream(lexer));
-        parser.addErrorListener(new ParseErrorListener());
-        ParseTree tree;
-        try {
-            tree = parser.expression();
-        } catch (ParseCancellationException e) {
-            ExpressionResult errorResult = new ExpressionResult();
-            errorResult.setType(ExpressionResultType.UNKNOWN);
-            errorResult.setError(e.getMessage());
-            return errorResult;
-        }
-        MQEVisitor visitor = new MQEVisitor();
-        ExpressionResult parseResult = visitor.visit(tree);
-        parseResult.setResults(Collections.emptyList());
-        return parseResult;
-    }
-
     public ExpressionResult execExpression(String expression, Entity entity, Duration duration) {
         MQELexer lexer = new MQELexer(
             CharStreams.fromString(expression));
diff --git a/oap-server/server-query-plugin/query-graphql-plugin/src/main/resources/query-protocol b/oap-server/server-query-plugin/query-graphql-plugin/src/main/resources/query-protocol
index d50222f27f..8700ead75d 160000
--- a/oap-server/server-query-plugin/query-graphql-plugin/src/main/resources/query-protocol
+++ b/oap-server/server-query-plugin/query-graphql-plugin/src/main/resources/query-protocol
@@ -1 +1 @@
-Subproject commit d50222f27f27df5a7632039962f0d77e715bd462
+Subproject commit 8700ead75dfff5f7da4c056dd5bbf8c3a2e0f3f4