Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is_present function in MQE for check if the list metrics has value or not #11627

Merged
merged 4 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/en/api/metrics-query-expression.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,21 @@ The following example would return the content of the **service_cpm** metric.
view_as_seq(not_existing, service_cpm)
```

### IsPresent Operation
IsPresent operation represents that in a list of metrics, if any expression has a value, it would return `1` in the result; otherwise, it would return `0`.

Expression:
```text
is_present([<expression_1>, <expression_2>, ...])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the expressions SINGLE VALUE or TIMER SERIES VALUE? Or either?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both are supported.

```

For example:
When the meter does not exist or the metrics has no value, it would return `0`.
However, if the metrics list contains meter with values, it would return `1`.
```text
is_present(not_existing, existing_without_value, exiting_with_value)
mrproliu marked this conversation as resolved.
Show resolved Hide resolved
```

#### Result Type
The result type is determined by the type of selected not-null metric expression.
wankai123 marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
1 change: 1 addition & 0 deletions docs/en/changes/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#### OAP Server
* Add `layer` parameter to the global topology graphQL query.
* Add `is_present` function in MQE for check if the list metrics has a value or not.

#### UI

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ TOP_N: 'top_n';
// ViewAsSeq
VIEW_AS_SEQ: 'view_as_seq';

// IsPresent
IS_PRESENT: 'is_present';

// Relabels
RELABELS: 'relabels';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ trend:
topN: TOP_N;

logical_operator:
VIEW_AS_SEQ;
VIEW_AS_SEQ | IS_PRESENT;

relabels: RELABELS;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@
import org.apache.skywalking.mqe.rt.grammar.MQEParser;
import org.apache.skywalking.mqe.rt.grammar.MQEParserBaseVisitor;
import org.apache.skywalking.mqe.rt.type.ExpressionResult;
import org.apache.skywalking.mqe.rt.type.ExpressionResultType;
import org.apache.skywalking.mqe.rt.type.MQEValue;
import org.apache.skywalking.mqe.rt.type.MQEValues;
import org.apache.skywalking.mqe.rt.type.Metadata;
import org.apache.skywalking.oap.server.library.util.CollectionUtils;

import java.util.Objects;

public class LogicalFunctionOp {

public static ExpressionResult doOP(int opType, MQEParser.ExpressionListContext expressionListContext,
MQEParserBaseVisitor<ExpressionResult> visitor) throws IllegalExpressionException {
switch (opType) {
case MQEParser.VIEW_AS_SEQ:
return viewAsSeq(expressionListContext, visitor);
case MQEParser.IS_PRESENT:
return isPresent(expressionListContext, visitor);
}

throw new IllegalExpressionException("Unsupported function.");
Expand All @@ -57,4 +65,31 @@ private static ExpressionResult viewAsSeq(MQEParser.ExpressionListContext expres
return firstResult;
}

private static ExpressionResult isPresent(MQEParser.ExpressionListContext expressionListContext,
MQEParserBaseVisitor<ExpressionResult> visitor) {
boolean present = false;
for (MQEParser.ExpressionContext expContext : expressionListContext.expression()) {
final ExpressionResult result = visitor.visit(expContext);
// the expression data exist and contains not empty value
if (result != null) {
present = result.getResults().stream().filter(Objects::nonNull).anyMatch(r ->
r.getValues().stream().anyMatch(v -> !v.isEmptyValue()));
}
if (present) {
break;
wankai123 marked this conversation as resolved.
Show resolved Hide resolved
}
}

final ExpressionResult result = new ExpressionResult();
result.setType(ExpressionResultType.SINGLE_VALUE);

MQEValue mqeValue = new MQEValue();
MQEValues mqeValues = new MQEValues();
mqeValues.setMetric(new Metadata());
mqeValues.getValues().add(mqeValue);
result.getResults().add(mqeValues);
mqeValue.setDoubleValue(present ? 1 : 0);
mqeValue.setEmptyValue(false);
return result;
}
}
24 changes: 24 additions & 0 deletions test/e2e-v2/cases/mqe/expected/isPresent-OP-false.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

type: SINGLE_VALUE
results:
- metric:
labels: []
values:
- id: null
value: "0"
traceid: null
error: null
24 changes: 24 additions & 0 deletions test/e2e-v2/cases/mqe/expected/isPresent-OP-true.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

type: SINGLE_VALUE
results:
- metric:
labels: []
values:
- id: null
value: "1"
traceid: null
error: null
6 changes: 6 additions & 0 deletions test/e2e-v2/cases/mqe/mqe-cases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ cases:
- query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression="view_as_seq(mq_service_consume_sla,service_sla)" --service-name=e2e-service-provider
expected: expected/viewAsSeq-OP.yml

# isPresent-OP
- query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression="is_present(service_sla)" --service-name=e2e-service-provider
expected: expected/isPresent-OP-true.yml
- query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression="is_present(not_exist_meter,mq_service_consume_cpm)" --service-name=e2e-service-provider
expected: expected/isPresent-OP-false.yml

# trend-OP
- query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics exec --expression="increase(service_resp_time,2)" --service-name=e2e-service-provider
expected: expected/trend-OP.yml
Expand Down
Loading