Skip to content

Commit

Permalink
PromQL service: query API support RFC3399 time format. (#12644)
Browse files Browse the repository at this point in the history
  • Loading branch information
wankai123 authored Sep 25, 2024
1 parent 10cd558 commit 9cf79f7
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
with:
languages: ${{ matrix.language }}

- run: ./mvnw -q -Dmaven.test.skip=true clean install || ./mvnw -q -Dmaven.test.skip=true clean install
- run: ./mvnw -q -Dmaven.test.skip=true clean install -Dgpg.skip || ./mvnw -q -Dmaven.test.skip=true clean install -Dgpg.skip

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
36 changes: 18 additions & 18 deletions docs/en/api/promql-service.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ sum(http_requests_total{service='$service', layer='$layer'})
GET|POST /api/v1/query
```

| Parameter | Definition | Support | Optional |
|-----------|-------------------------------------------------------------------------------------------------------------------------------------|---------|------------|
| query | prometheus expression | yes | no |
| time | **The latest metrics value from current time to this time is returned. If time is empty, the default look-back time is 2 minutes.** | yes | yes |
| timeout | evaluation timeout | **no** | **ignore** |
| Parameter | Definition | Support | Optional |
|-----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|------------|
| query | prometheus expression | yes | no |
| time | **The latest metrics value from current time to this time is returned. If time is empty, the default look-back time is 2 minutes.** time format: RFC3399 or unix_timestamp in seconds | yes | yes |
| timeout | evaluation timeout | **no** | **ignore** |

For example:
```text
Expand Down Expand Up @@ -191,8 +191,8 @@ GET|POST /api/v1/query_range
| Parameter | Definition | Support | Optional |
|-----------|--------------------------------------------------------------------------------------|---------|------------|
| query | prometheus expression | yes | no |
| start | start timestamp, **seconds** | yes | no |
| end | end timestamp, **seconds** | yes | no |
| start | start timestamp, format: RFC3399 or unix_timestamp in seconds | yes | no |
| end | end timestamp, format: RFC3399 or unix_timestamp in seconds | yes | no |
| step | **SkyWalking will automatically fit Step(DAY, HOUR, MINUTE) through start and end.** | **no** | **ignore** |
| timeout | evaluation timeout | **no** | **ignore** |

Expand Down Expand Up @@ -256,11 +256,11 @@ Result:
GET|POST /api/v1/series
```

| Parameter | Definition | Support | Optional |
|-----------|------------------------------|---------|----------|
| match[] | series selector | yes | no |
| start | start timestamp, **seconds** | yes | no |
| end | end timestamp, **seconds** | yes | no |
| Parameter | Definition | Support | Optional |
|-----------|-----------------------------------------------------|---------|----------|
| match[] | series selector | yes | no |
| start | start, format: RFC3399 or unix_timestamp in seconds | yes | no |
| end | end, format: RFC3399 or unix_timestamp in seconds | yes | no |

For example:
```text
Expand Down Expand Up @@ -321,7 +321,7 @@ GET|POST /api/v1/labels
| Parameter | Definition | Support | Optional |
|-----------|---------------------------------------------------------------------------------|---------|----------|
| match[] | series selector | yes | yes |
| start | start timestamp | **no** | yes |
| start | start, format: RFC3399 or unix_timestamp in seconds | **no** | yes |
| end | end timestamp, if end time is not present, use current time as default end time | yes | yes |

For example:
Expand Down Expand Up @@ -351,11 +351,11 @@ Result:
GET /api/v1/label/<label_name>/values
```

| Parameter | Definition | Support | Optional |
|-----------|---------------------------------------------------------------------------------|---------|----------|
| match[] | series selector | yes | yes |
| start | start timestamp | **no** | yes |
| end | end timestamp, if end time is not present, use current time as default end time | yes | yes |
| Parameter | Definition | Support | Optional |
|-----------|---------------------------------------------------------------------------------------------------------------------|---------|----------|
| match[] | series selector | yes | yes |
| start | start, format: RFC3399 or unix_timestamp in seconds | **no** | yes |
| end | end, format: RFC3399 or unix_timestamp in seconds, if end time is not present, use current time as default end time | yes | yes |

For example:
```text
Expand Down
3 changes: 2 additions & 1 deletion docs/en/changes/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@
* Fix the previous analysis result missing in the ALS `k8s-mesh` analyzer.
* Fix `findEndpoint` query require `keyword` when using BanyanDB.
* Support to analysis the ztunnel mapped IP address in eBPF Access Log Receiver.
* Adapt BanyanDB Java Client 0.7.0-rc3.
* Adapt BanyanDB Java Client 0.7.0.
* Add SkyWalking Java Agent self observability dashboard.
* Add Component ID(5022) for the GoFrame framework.
* Bump up protobuf java dependencies to 3.25.5.
* BanyanDB: support using native term searching for `keyword` in query `findEndpoint` and `getAlarm`.
* BanyanDB: support TLS connection and configuration.
* PromQL service: query API support RFC3399 time format.

#### UI

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
import org.apache.skywalking.promql.rt.grammar.PromQLLexer;
import org.apache.skywalking.promql.rt.grammar.PromQLParser;
import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;

import static org.apache.skywalking.oap.query.promql.rt.PromOpUtils.formatDoubleValue;

Expand Down Expand Up @@ -582,7 +583,15 @@ private void buildScalarMatrixRsp(Duration duration, ParseResult parseResult, Ex
}

private static long formatTimestamp2Millis(String timestamp) {
return Double.valueOf(timestamp).longValue() * 1000;
long time;
try {
// if Unix timestamp in seconds, such as 1627756800
time = Double.valueOf(timestamp).longValue() * 1000;
} catch (NumberFormatException e) {
// if RFC3399 format, such as 2024-09-19T20:11:00.781Z
time = ISODateTimeFormat.dateTime().parseMillis(timestamp);
}
return time;
}

private List<String> buildLabelNames(Scope scope, ValueColumnMetadata.ValueColumn metaData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public ParseResult visitMetricRange(PromQLParser.MetricRangeContext ctx) {
}

String timeRange = ctx.DURATION().getText().toUpperCase();
long endTS = System.currentTimeMillis();
long endTS = this.duration.getEndTimestamp();
long startTS = endTS - formatDuration(timeRange).getMillis();
duration = DurationUtils.timestamp2Duration(startTS, endTS);
ParseResult result = visit(ctx.metricInstant());
Expand Down
15 changes: 14 additions & 1 deletion test/e2e-v2/cases/promql/promql-cases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ cases:
# traffics query
- query: curl -X GET http://${oap_host}:${oap_9090}/api/v1/series -d 'match[]=service_traffic{layer="GENERAL"}&start='$(($(date +%s)-1800))'&end='$(date +%s)
expected: expected/service-traffic.yml
#RFC3399
- query: curl -X GET http://${oap_host}:${oap_9090}/api/v1/series -d 'match[]=service_traffic{layer="GENERAL"}&start='$(($(date +%s)-1800))'&end='$(date -u +%Y-%m-%dT%H:%M:%S.111Z)
expected: expected/service-traffic.yml
- query: curl -X GET http://${oap_host}:${oap_9090}/api/v1/series -d 'match[]=instance_traffic{layer="GENERAL", service="e2e-service-provider"}&start='$(($(date +%s)-1800))'&end='$(date +%s)
expected: expected/instance-traffic.yml
- query: curl -X GET http://${oap_host}:${oap_9090}/api/v1/series -d 'match[]=endpoint_traffic{layer="GENERAL", service="e2e-service-provider", keyword="POST:/users"}&start='$(($(date +%s)-1800))'&end='$(date +%s)
Expand All @@ -29,11 +32,15 @@ cases:
- query: curl -X GET http://${oap_host}:${oap_9090}/api/v1/series -d 'match[]=endpoint_cpm&start='$(($(date +%s)-1800))'&end='$(date +%s)
expected: expected/endpoint-metric-series.yml
# metrics names query
- query: curl -X GET http://${oap_host}:${oap_9090}/api/v1/label/__name__/values
- query: curl -X GET http://${oap_host}:${oap_9090}/api/v1/label/__name__/values
expected: expected/metrics-names.yml
- query: curl -X GET http://${oap_host}:${oap_9090}/api/v1/label/__name__/values -d 'start='$(($(date +%s)-1800))'&end='$(date -u +%Y-%m-%dT%H:%M:%S.111Z)
expected: expected/metrics-names.yml
# labels query
- query: curl -X GET http://${oap_host}:${oap_9090}/api/v1/labels -d 'match[]=service_cpm'
expected: expected/service-metric-label.yml
- query: curl -X GET http://${oap_host}:${oap_9090}/api/v1/labels -d 'match[]=service_cpm&start='$(($(date +%s)-1800))'&end='$(date -u +%Y-%m-%dT%H:%M:%S.111Z)
expected: expected/service-metric-label.yml
- query: |
sleep 30;
curl -X GET http://${oap_host}:${oap_9090}/api/v1/labels -d 'match[]=service_percentile'
Expand All @@ -54,6 +61,9 @@ cases:
expected: expected/service-metric-vector.yml
- query: curl -X GET http://${oap_host}:${oap_9090}/api/v1/query -d 'query=service_sla{service="e2e-service-consumer", layer="GENERAL"}[30m]'
expected: expected/service-metric-matrix.yml
#RFC3399
- query: curl -X GET http://${oap_host}:${oap_9090}/api/v1/query -d 'query=service_sla{service="e2e-service-consumer", layer="GENERAL"}[30m]&time='$(date -u +%Y-%m-%dT%H:%M:%S.111Z)
expected: expected/service-metric-matrix.yml
- query: curl -X GET http://${oap_host}:${oap_9090}/api/v1/query -d 'query=service_percentile{service="e2e-service-consumer", layer="GENERAL", p="50,75,90"}'
expected: expected/service-metric-labeled-vector.yml
- query: curl -X GET http://${oap_host}:${oap_9090}/api/v1/query -d 'query=service_percentile_old{service="e2e-service-consumer", layer="GENERAL", labels="0,1,2", relabels="P50,P75,P90"}'
Expand All @@ -72,6 +82,9 @@ cases:
## query_range
- query: curl -X GET http://${oap_host}:${oap_9090}/api/v1/query_range -d 'query=service_sla{service="e2e-service-consumer", layer="GENERAL"}&start='$(($(date +%s)-1800))'&end='$(date +%s)
expected: expected/service-metric-matrix.yml
#RFC3399
- query: curl -X GET http://${oap_host}:${oap_9090}/api/v1/query_range -d 'query=service_sla{service="e2e-service-consumer", layer="GENERAL"}&start='$(($(date +%s)-1800))'&end='$(date -u +%Y-%m-%dT%H:%M:%S.111Z)
expected: expected/service-metric-matrix.yml
- query: curl -X GET http://${oap_host}:${oap_9090}/api/v1/query_range -d 'query=service_percentile{service="e2e-service-consumer", layer="GENERAL", p="50,75,90"}&start='$(($(date +%s)-1800))'&end='$(date +%s)
expected: expected/service-metric-labeled-matrix.yml
- query: curl -X GET http://${oap_host}:${oap_9090}/api/v1/query_range -d 'query=sum by (p) (service_percentile{service="e2e-service-consumer", layer="GENERAL", p="50,75,90"})&start='$(($(date +%s)-1800))'&end='$(date +%s)
Expand Down

0 comments on commit 9cf79f7

Please sign in to comment.