You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2023/02/24 03:40:02 UTC

[skywalking] branch master updated: Fix `DurationUtils.getDurationPoints` exceed, when `startTimeBucket` equals `endTimeBucket`. (#10441)

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 1a2f794472 Fix `DurationUtils.getDurationPoints` exceed, when `startTimeBucket` equals `endTimeBucket`. (#10441)
1a2f794472 is described below

commit 1a2f79447297739a4e7ab44e7254e8899bca4c12
Author: Wan Kai <wa...@foxmail.com>
AuthorDate: Fri Feb 24 11:39:55 2023 +0800

    Fix `DurationUtils.getDurationPoints` exceed, when `startTimeBucket` equals `endTimeBucket`. (#10441)
---
 docs/en/changes/changes.md                           |  1 +
 docs/en/setup/backend/configuration-vocabulary.md    |  9 ++++++++-
 .../oap/server/core/query/DurationUtils.java         |  3 +++
 .../skywalking/promql/rt/grammar/PromQLLexer.g4      |  2 +-
 .../oap/query/promql/handler/PromQLApiHandler.java   |  2 --
 .../skywalking/oap/query/promql/rt/PromOpUtils.java  | 20 ++++++++++++++++++++
 .../oap/query/promql/rt/PromQLExprQueryVisitor.java  |  5 +++--
 .../src/main/resources/application.yml               |  3 ++-
 8 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/docs/en/changes/changes.md b/docs/en/changes/changes.md
index b65ec2aaa7..8183dd8716 100644
--- a/docs/en/changes/changes.md
+++ b/docs/en/changes/changes.md
@@ -103,6 +103,7 @@
 * Support prometheus HTTP API and promQL.
 * `Scope` in the Entity of Metrics query v1 protocol is not required and automatical correction. The scope is determined based on the metric itself.
 * Add explicit `ReadTimeout` for ConsulConfigurationWatcher to avoid `IllegalArgumentException: Cache watchInterval=10sec >= networkClientReadTimeout=10000ms`.
+* Fix `DurationUtils.getDurationPoints` exceed, when `startTimeBucket` equals `endTimeBucket`.
 
 #### UI
 
diff --git a/docs/en/setup/backend/configuration-vocabulary.md b/docs/en/setup/backend/configuration-vocabulary.md
index 134719a116..8ab8681e6d 100644
--- a/docs/en/setup/backend/configuration-vocabulary.md
+++ b/docs/en/setup/backend/configuration-vocabulary.md
@@ -242,7 +242,7 @@ The Configuration Vocabulary lists all available configurations provided by `app
 | -                       | -             | maxQueryComplexity                                                                                                                                                       | Maximum complexity allowed for the GraphQL query that can be used to abort a query if the total number of data fields queried exceeds the defined threshold.                                                                                                                          [...]
 | -                       | -             | enableUpdateUITemplate                                                                                                                                                   | Allow user add,disable and update UI template.                                                                                                                                                                                                                                        [...]
 | -                       | -             | enableOnDemandPodLog                                                                                                                                                     | Ondemand Pod log: fetch the Pod logs on users' demand, the logs are fetched and displayed in real time, and are not persisted in any kind. This is helpful when users want to do some experiments and monitor the logs and see what's happing inside the service. Note: if you print  [...]
-| query                   | graphql       | -                                                                                                                                                                        | GraphQL query implementation.                                                                                                                                                                                                                                                         [...]
+| query-zipkin                   | default       | -                                                                                                                                                                        | This module is for Zipkin query API and support zipkin-lens UI                                                                                                                                                                                                                 [...]
 | -                       | -             | restHost                                                                                                                                                                 | Binding IP of RESTful services.                                                                                                                                                                                                                                                       [...]
 | -                       | -             | restPort                                                                                                                                                                 | Binding port of RESTful services.                                                                                                                                                                                                                                                     [...]
 | -                       | -             | restContextPath                                                                                                                                                          | Web context path of RESTful services.                                                                                                                                                                                                                                                 [...]
@@ -253,6 +253,13 @@ The Configuration Vocabulary lists all available configurations provided by `app
 | -                       | -             | namesMaxAge                                                                                                                                                       | The Cache-Control max-age (seconds) for serviceNames, remoteServiceNames and spanNames                                                                                                                                                                                                       [...]
 | -                       | -             | uiQueryLimit                                                                                                                                                   | Default traces query max size                                                                                                                                                                                                                                                                   [...]
 | -                       | -             | uiDefaultLookback                                                                                                                                                       | Default look back on the UI for search traces, 15 minutes in millis                                                                                                                                                                                                                    [...]
+| promql                   | default       | -                                                                                                                                                                        | This module is for PromQL API.                                                                                                                                                                                                                                                       [...]
+| -                       | -             | restHost                                                                                                                                                                 | Binding IP of RESTful services.                                                                                                                                                                                                                                                       [...]
+| -                       | -             | restPort                                                                                                                                                                 | Binding port of RESTful services.                                                                                                                                                                                                                                                     [...]
+| -                       | -             | restContextPath                                                                                                                                                          | Web context path of RESTful services.                                                                                                                                                                                                                                                 [...]
+| -                       | -             | restMaxThreads                                                                                                                                                           | Maximum thread number of RESTful services.                                                                                                                                                                                                                                            [...]
+| -                       | -             | restIdleTimeOut                                                                                                                                                          | Connector idle timeout of RESTful services (in milliseconds).                                                                                                                                                                                                                         [...]
+| -                       | -             | restAcceptQueueSize                                                                                                                                                      | Maximum request header size accepted.                                                                                                                                                                                                                                                 [...]
 | alarm                   | default       | -                                                                                                                                                                        | Read [alarm doc](backend-alarm.md) for more details.                                                                                                                                                                                                                                  [...]
 | telemetry               | -             | -                                                                                                                                                                        | Read [telemetry doc](backend-telemetry.md) for more details.                                                                                                                                                                                                                          [...]
 | -                       | none          | -                                                                                                                                                                        | No op implementation.                                                                                                                                                                                                                                                                 [...]
diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/DurationUtils.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/DurationUtils.java
index ac2cdaf6f7..cc53aa83f1 100644
--- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/DurationUtils.java
+++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/DurationUtils.java
@@ -88,6 +88,9 @@ public enum DurationUtils {
 
         List<PointOfTime> durations = new LinkedList<>();
         durations.add(new PointOfTime(startTimeBucket));
+        if (startTimeBucket == endTimeBucket) {
+            return durations;
+        }
 
         int i = 0;
         do {
diff --git a/oap-server/server-query-plugin/promql-plugin/src/main/antlr4/org/apache/skywalking/promql/rt/grammar/PromQLLexer.g4 b/oap-server/server-query-plugin/promql-plugin/src/main/antlr4/org/apache/skywalking/promql/rt/grammar/PromQLLexer.g4
index 087f9b213a..c82dd1a0c8 100644
--- a/oap-server/server-query-plugin/promql-plugin/src/main/antlr4/org/apache/skywalking/promql/rt/grammar/PromQLLexer.g4
+++ b/oap-server/server-query-plugin/promql-plugin/src/main/antlr4/org/apache/skywalking/promql/rt/grammar/PromQLLexer.g4
@@ -47,7 +47,7 @@ GT:          '>';
 
 // Literals
 NUMBER: Digit+ (DOT Digit+)?;
-DURATION: Digit+ ('s' | 'm' | 'h' | 'd' | 'w' | 'y');
+DURATION: Digit+ ('ms' | 's' | 'm' | 'h' | 'd' | 'w');
 NAME_STRING: NameLetter+;
 VALUE_STRING: '\'' .*? '\'' | '"' .*? '"';
 
diff --git a/oap-server/server-query-plugin/promql-plugin/src/main/java/org/apache/skywalking/oap/query/promql/handler/PromQLApiHandler.java b/oap-server/server-query-plugin/promql-plugin/src/main/java/org/apache/skywalking/oap/query/promql/handler/PromQLApiHandler.java
index 7a2dcc0eca..77ba7f0194 100644
--- a/oap-server/server-query-plugin/promql-plugin/src/main/java/org/apache/skywalking/oap/query/promql/handler/PromQLApiHandler.java
+++ b/oap-server/server-query-plugin/promql-plugin/src/main/java/org/apache/skywalking/oap/query/promql/handler/PromQLApiHandler.java
@@ -103,7 +103,6 @@ public class PromQLApiHandler {
     }
 
     @Get
-    @Post
     @Path("/api/v1/metadata")
     public HttpResponse metadata(
         @Param("limit") Optional<Integer> limit,
@@ -174,7 +173,6 @@ public class PromQLApiHandler {
      * reserve these param to keep consistent with API protocol.
      */
     @Get
-    @Post
     @Path("/api/v1/label/{label_name}/values")
     public HttpResponse labelValues(
         @Param("label_name") String labelName,
diff --git a/oap-server/server-query-plugin/promql-plugin/src/main/java/org/apache/skywalking/oap/query/promql/rt/PromOpUtils.java b/oap-server/server-query-plugin/promql-plugin/src/main/java/org/apache/skywalking/oap/query/promql/rt/PromOpUtils.java
index 03da30769d..44f6bbd92f 100644
--- a/oap-server/server-query-plugin/promql-plugin/src/main/java/org/apache/skywalking/oap/query/promql/rt/PromOpUtils.java
+++ b/oap-server/server-query-plugin/promql-plugin/src/main/java/org/apache/skywalking/oap/query/promql/rt/PromOpUtils.java
@@ -35,6 +35,8 @@ import org.apache.skywalking.oap.server.core.query.input.Duration;
 import org.apache.skywalking.oap.server.core.query.type.MetricsValues;
 import org.apache.skywalking.promql.rt.grammar.PromQLParser;
 import org.joda.time.DateTime;
+import org.joda.time.format.PeriodFormatter;
+import org.joda.time.format.PeriodFormatterBuilder;
 
 public class PromOpUtils {
     //Adopt skywalking time step.
@@ -252,4 +254,22 @@ public class PromOpUtils {
         DecimalFormat format = new DecimalFormat("#.##");
         return format.format(v);
     }
+
+    /**
+     * Format duration string to org.joda.time.Duration.
+     * Don't support year and month because the days vary in length.
+     * @param duration such as "5d", "30m", "5d30m, "1w, "1w5d"
+     * @return org.joda.time.Duration
+     */
+    public static org.joda.time.Duration formatDuration(String duration) {
+        PeriodFormatter f = new PeriodFormatterBuilder()
+            .appendWeeks().appendSuffix("w")
+            .appendDays().appendSuffix("d")
+            .appendHours().appendSuffix("h")
+            .appendMinutes().appendSuffix("m")
+            .appendSeconds().appendSuffix("s")
+            .appendMillis().appendSuffix("ms")
+            .toFormatter();
+        return f.parsePeriod(duration).toStandardDuration();
+    }
 }
diff --git a/oap-server/server-query-plugin/promql-plugin/src/main/java/org/apache/skywalking/oap/query/promql/rt/PromQLExprQueryVisitor.java b/oap-server/server-query-plugin/promql-plugin/src/main/java/org/apache/skywalking/oap/query/promql/rt/PromQLExprQueryVisitor.java
index 68c11aaf5e..05ac6940b0 100644
--- a/oap-server/server-query-plugin/promql-plugin/src/main/java/org/apache/skywalking/oap/query/promql/rt/PromQLExprQueryVisitor.java
+++ b/oap-server/server-query-plugin/promql-plugin/src/main/java/org/apache/skywalking/oap/query/promql/rt/PromQLExprQueryVisitor.java
@@ -58,6 +58,7 @@ import org.apache.skywalking.promql.rt.grammar.PromQLParser;
 import org.apache.skywalking.promql.rt.grammar.PromQLParserBaseVisitor;
 
 import static org.apache.skywalking.oap.query.promql.rt.PromOpUtils.buildMatrixValues;
+import static org.apache.skywalking.oap.query.promql.rt.PromOpUtils.formatDuration;
 import static org.apache.skywalking.oap.query.promql.rt.PromOpUtils.matrixBinaryOp;
 import static org.apache.skywalking.oap.query.promql.rt.PromOpUtils.matrixCompareOp;
 import static org.apache.skywalking.oap.query.promql.rt.PromOpUtils.matrixScalarBinaryOp;
@@ -247,9 +248,9 @@ public class PromQLExprQueryVisitor extends PromQLParserBaseVisitor<ParseResult>
             return result;
         }
 
-        String timeRange = "PT" + ctx.DURATION().getText().toUpperCase();
+        String timeRange = ctx.DURATION().getText().toUpperCase();
         long endTS = System.currentTimeMillis();
-        long startTS = endTS - java.time.Duration.parse(timeRange).toMillis();
+        long startTS = endTS - formatDuration(timeRange).getMillis();
         duration = timestamp2Duration(startTS, endTS);
         ParseResult result = visit(ctx.metricInstant());
         result.setRangeExpression(true);
diff --git a/oap-server/server-starter/src/main/resources/application.yml b/oap-server/server-starter/src/main/resources/application.yml
index 9e73b3ce7f..c79cffeb21 100644
--- a/oap-server/server-starter/src/main/resources/application.yml
+++ b/oap-server/server-starter/src/main/resources/application.yml
@@ -435,12 +435,13 @@ query-zipkin:
     # Default look back on the UI for search traces, 15 minutes in millis
     uiDefaultLookback: ${SW_QUERY_ZIPKIN_UI_DEFAULT_LOOKBACK:900000}
 
+#This module is for PromQL API.
 promql:
   selector: ${SW_PROMQL:default}
   default:
     # For HTTP server
     restHost: ${SW_PROMQL_REST_HOST:0.0.0.0}
-    restPort: ${SW_PROMQL_REST_PORT:9099}
+    restPort: ${SW_PROMQL_REST_PORT:9090}
     restContextPath: ${SW_PROMQL_REST_CONTEXT_PATH:/}
     restMaxThreads: ${SW_PROMQL_REST_MAX_THREADS:200}
     restIdleTimeOut: ${SW_PROMQL_REST_IDLE_TIMEOUT:30000}