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

wusheng 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 a38628fced Fix MQE `top_n` global query. (#12163)
a38628fced is described below

commit a38628fced0a6f678df188799b60c29c0507bc02
Author: Wan Kai <wankai...@foxmail.com>
AuthorDate: Fri Apr 26 14:53:37 2024 +0800

    Fix MQE `top_n` global query. (#12163)
---
 docs/en/api/metrics-query-expression.md                     | 13 +++++++++++--
 docs/en/changes/changes.md                                  |  1 +
 .../oap/server/core/query/AggregationQueryService.java      |  5 ++++-
 .../oap/server/core/query/input/RecordCondition.java        |  2 +-
 .../oap/server/core/query/input/TopNCondition.java          |  2 +-
 5 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/docs/en/api/metrics-query-expression.md 
b/docs/en/api/metrics-query-expression.md
index c1641e01e7..95a7d88060 100644
--- a/docs/en/api/metrics-query-expression.md
+++ b/docs/en/api/metrics-query-expression.md
@@ -217,7 +217,15 @@ round(service_cpm / 60 , 2)
 The different operators could impact the `ExpressionResultType`, please refer 
to the above table.
 
 ## TopN Operation
-TopN Operation takes an expression and performs TopN calculation on its 
results.
+TopN Operation takes an expression and performs calculation to get the TopN of 
Services/Instances/Endpoints.
+The result depends on the `entity` condition in the query.
+- Global TopN: 
+  - The `entity` is empty.
+  - The result is the topN Services/Instances/Endpoints in the whole traffics. 
+  - **Notice**: If query the Endpoints metric, the global candidate set could 
be huge, please use it carefully. 
+- Service's Instances/Endpoints TopN: 
+  - The `serviceName` in the `entity` is not empty.
+  - The result is the topN Instances/Endpoints of the service.
 
 Expression:
 ```text
@@ -229,7 +237,8 @@ top_n(<metric_name>, <top_number>, <order>)
 `order` is the order of the top results. The value of `order` can be `asc` or 
`des`.
 
 For example:
-If we want to query the top 10 services with the highest `service_cpm` metric 
value, we can use the following expression:
+If we want to query the current service's top 10 instances with the highest 
`service_instance_cpm` metric value, we can use the following expression
+under specific service:
 
 ```text
 top_n(service_instance_cpm, 10, des)
diff --git a/docs/en/changes/changes.md b/docs/en/changes/changes.md
index a82d5e0930..e114bca05b 100644
--- a/docs/en/changes/changes.md
+++ b/docs/en/changes/changes.md
@@ -118,6 +118,7 @@
 * Support DoubleValue,IntValue,BoolValue in OTEL metrics attributes.
 * [Break Change] gGRPC metrics exporter unified the metric value type and 
support labeled metrics.
 * Add component definition(ID=152) for `c3p0`(JDBC3 Connection and Statement 
Pooling).
+* Fix MQE `top_n` global query. 
 
 #### UI
 
diff --git 
a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/AggregationQueryService.java
 
b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/AggregationQueryService.java
index d4a26b00ca..bac6dfbb99 100644
--- 
a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/AggregationQueryService.java
+++ 
b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/AggregationQueryService.java
@@ -60,8 +60,11 @@ public class AggregationQueryService implements Service {
         final String valueCName = 
ValueColumnMetadata.INSTANCE.getValueCName(condition.getName());
         List<KeyValue> additionalConditions = null;
         if (StringUtil.isNotEmpty(condition.getParentService())) {
+            if (condition.getNormal() == null) {
+                return Collections.emptyList();
+            }
             additionalConditions = new ArrayList<>(1);
-            final String serviceId = 
IDManager.ServiceID.buildId(condition.getParentService(), condition.isNormal());
+            final String serviceId = 
IDManager.ServiceID.buildId(condition.getParentService(), 
condition.getNormal());
             additionalConditions.add(new KeyValue(InstanceTraffic.SERVICE_ID, 
serviceId));
         }
         final List<SelectedRecord> selectedRecords = 
getAggregationQueryDAO().sortMetrics(
diff --git 
a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/input/RecordCondition.java
 
b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/input/RecordCondition.java
index 45a71b1939..eab7a1de3e 100644
--- 
a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/input/RecordCondition.java
+++ 
b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/input/RecordCondition.java
@@ -55,7 +55,7 @@ public class RecordCondition {
             final Entity entity = new Entity();
             entity.setScope(condition.getScope() == null ? Scope.Service : 
condition.getScope());
             entity.setServiceName(condition.getParentService());
-            entity.setNormal(condition.isNormal());
+            entity.setNormal(condition.getNormal());
             this.parentEntity = entity;
         }
         this.topN = condition.getTopN();
diff --git 
a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/input/TopNCondition.java
 
b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/input/TopNCondition.java
index 38c250905b..8ced87aead 100644
--- 
a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/input/TopNCondition.java
+++ 
b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/input/TopNCondition.java
@@ -46,7 +46,7 @@ public class TopNCondition {
      * Normal service is the service having installed agent or metrics 
reported directly. Unnormal service is
      * conjectural service, usually detected by the agent.
      */
-    private boolean normal;
+    private Boolean normal;
     /**
      * Indicate the metrics entity scope. Because this is a top list, don't 
need to set the Entity like the
      * MetricsCondition. Only accept scope = {@link Scope#Service} {@link 
Scope#ServiceInstance} and {@link

Reply via email to