Skip to content

Commit 3c52eed

Browse files
authored
Support labelAvg function in the OAL engine (#12940)
1 parent 0a0890c commit 3c52eed

File tree

8 files changed

+186
-5
lines changed

8 files changed

+186
-5
lines changed

docs/en/changes/changes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
source tar from the website and publish them to your private maven repository.
1414
* [Breaking Change] Remove H2 as storage option permanently. BanyanDB 0.8(OAP 10.2 required) is easy, stable and
1515
production-ready. Don't need H2 as default storage anymore.
16+
* Support `labelAvg` function in the OAL engine.
17+
* Added `maxLabelCount` parameter in the `labelCount` function of OAL to limit the number of labels can be counted.
1618

1719
#### OAP Server
1820

docs/en/concepts-and-designs/oal.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,14 @@ In this case, see `p99`, `p95`, `p90`, `p75`, and `p50` of all incoming requests
103103
In this case, the p99 value of all incoming requests. The parameter is precise to a latency at p99, such as in the above case, and 120ms and 124ms are considered to produce the same response time.
104104

105105
- `labelCount`. The count of the label value.
106-
> drop_reason_count = from(CiliumService.*).filter(verdict == "dropped").labelCount(dropReason);
106+
> drop_reason_count = from(CiliumService.*).filter(verdict == "dropped").labelCount(dropReason, 100);
107107
108-
In this case, the count of the drop reason of each Cilium service.
108+
In this case, the count of the drop reason of each Cilium service, max support calculate `100` reasons(optional configuration).
109+
110+
- `labelAvg`. The avg of the label value.
111+
> drop_reason_avg = from(BrowserResourcePerf.*).labelAvg(name, duration, 100);
112+
113+
In this case, the avg of the duration of each browser resource file, max support calculate `100` resource file(optional configuration).
109114

110115
## Metrics name
111116
The metrics name for storage implementor, alarm and query modules. The type inference is supported by core.

oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/AggregationFuncStmt.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,8 @@ public Argument getLastArgument() {
6767
public Argument getNextFuncArg() {
6868
return funcArgs.get(argGetIdx++);
6969
}
70+
71+
public boolean hasNextArg() {
72+
return argGetIdx < funcArgs.size();
73+
}
7074
}

oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/DeepAnalysis.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics;
2424
import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.Arg;
2525
import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.ConstOne;
26+
import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.DefaultValue;
2627
import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.Entrance;
2728
import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.SourceFrom;
2829
import org.apache.skywalking.oap.server.core.storage.annotation.Column;
@@ -123,6 +124,12 @@ public AnalysisResult analysis(AnalysisResult result) {
123124
}
124125
} else if (annotation instanceof Arg) {
125126
entryMethod.addArg(parameterType, result.getAggregationFuncStmt().getNextFuncArg());
127+
} else if (annotation instanceof DefaultValue) {
128+
if (result.getAggregationFuncStmt().hasNextArg()) {
129+
entryMethod.addArg(parameterType, result.getAggregationFuncStmt().getNextFuncArg());
130+
} else {
131+
entryMethod.addArg(parameterType, ((DefaultValue) annotation).value());
132+
}
126133
} else {
127134
throw new IllegalArgumentException(
128135
"Entrance method:" + entranceMethod + " doesn't the expected annotation.");

oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/metrics/DataTable.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,18 @@ public void put(DataLabel labels, Long value) {
7070
* Accumulate the value with existing value in the same given key.
7171
*/
7272
public void valueAccumulation(String key, Long value) {
73+
this.valueAccumulation(key, value, 0);
74+
}
75+
76+
/**
77+
* Accumulate the value with existing value in the same given key, and limit the data size.
78+
*/
79+
public void valueAccumulation(String key, Long value, int maxDataSize) {
7380
Long element = data.get(key);
7481
if (element == null) {
82+
if (maxDataSize > 0 && data.size() >= maxDataSize) {
83+
return;
84+
}
7585
element = value;
7686
} else {
7787
element += value;
@@ -155,9 +165,16 @@ public void copyFrom(final DataTable source) {
155165
}
156166

157167
public DataTable append(DataTable dataTable) {
168+
return this.append(dataTable, 0);
169+
}
170+
171+
public DataTable append(DataTable dataTable, int maxDataSize) {
158172
dataTable.data.forEach((key, value) -> {
159173
Long current = this.data.get(key);
160174
if (current == null) {
175+
if (maxDataSize > 0 && data.size() >= maxDataSize) {
176+
return;
177+
}
161178
current = value;
162179
} else {
163180
current += value;
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.oap.server.core.analysis.metrics;
20+
21+
import lombok.Getter;
22+
import lombok.Setter;
23+
import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.Arg;
24+
import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.DefaultValue;
25+
import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.Entrance;
26+
import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.MetricsFunction;
27+
import org.apache.skywalking.oap.server.core.storage.annotation.BanyanDB;
28+
import org.apache.skywalking.oap.server.core.storage.annotation.Column;
29+
30+
import java.util.Objects;
31+
import java.util.Set;
32+
33+
@MetricsFunction(functionName = "labelAvg")
34+
public abstract class LabelAvgMetrics extends Metrics implements LabeledValueHolder {
35+
protected static final String SUMMATION = "datatable_summation";
36+
protected static final String COUNT = "datatable_count";
37+
protected static final String VALUE = "datatable_value";
38+
39+
protected static final String LABEL_NAME = "n";
40+
41+
@Getter
42+
@Setter
43+
@Column(name = SUMMATION, storageOnly = true)
44+
@BanyanDB.MeasureField
45+
protected DataTable summation;
46+
@Getter
47+
@Setter
48+
@Column(name = COUNT, storageOnly = true)
49+
@BanyanDB.MeasureField
50+
protected DataTable count;
51+
@Getter
52+
@Setter
53+
@Column(name = VALUE, dataType = Column.ValueDataType.LABELED_VALUE, storageOnly = true)
54+
@BanyanDB.MeasureField
55+
private DataTable value;
56+
57+
private boolean isCalculated;
58+
private int maxLabelCount;
59+
60+
public LabelAvgMetrics() {
61+
this.summation = new DataTable(30);
62+
this.count = new DataTable(30);
63+
this.value = new DataTable(30);
64+
}
65+
66+
@Entrance
67+
public final void combine(@Arg String label, @Arg long count, @DefaultValue("50") int maxLabelCount) {
68+
this.isCalculated = false;
69+
this.maxLabelCount = maxLabelCount;
70+
this.summation.valueAccumulation(label, count, maxLabelCount);
71+
this.count.valueAccumulation(label, 1L, maxLabelCount);
72+
}
73+
74+
@Override
75+
public boolean combine(Metrics metrics) {
76+
this.isCalculated = false;
77+
final LabelAvgMetrics labelCountMetrics = (LabelAvgMetrics) metrics;
78+
this.summation.append(labelCountMetrics.summation, labelCountMetrics.maxLabelCount);
79+
this.count.append(labelCountMetrics.count, labelCountMetrics.maxLabelCount);
80+
return true;
81+
}
82+
83+
@Override
84+
public void calculate() {
85+
if (isCalculated) {
86+
return;
87+
}
88+
89+
Set<String> keys = count.keys();
90+
for (String key : keys) {
91+
Long s = summation.get(key);
92+
if (Objects.isNull(s)) {
93+
continue;
94+
}
95+
Long c = count.get(key);
96+
if (Objects.isNull(c)) {
97+
continue;
98+
}
99+
long result = s / c;
100+
if (result == 0 && s > 0) {
101+
result = 1;
102+
}
103+
final DataLabel label = new DataLabel();
104+
label.put(LABEL_NAME, key);
105+
value.put(label, result);
106+
}
107+
}
108+
109+
@Override
110+
public DataTable getValue() {
111+
return this.value;
112+
}
113+
}

oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/metrics/LabelCountMetrics.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import lombok.Setter;
2323
import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.Arg;
2424
import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.ConstOne;
25+
import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.DefaultValue;
2526
import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.Entrance;
2627
import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.MetricsFunction;
2728
import org.apache.skywalking.oap.server.core.storage.annotation.BanyanDB;
@@ -49,23 +50,25 @@ public abstract class LabelCountMetrics extends Metrics implements LabeledValueH
4950
private DataTable value;
5051

5152
private boolean isCalculated;
53+
private int maxLabelCount;
5254

5355
public LabelCountMetrics() {
5456
this.dataset = new DataTable(30);
5557
this.value = new DataTable(30);
5658
}
5759

5860
@Entrance
59-
public final void combine(@Arg String label, @ConstOne long count) {
61+
public final void combine(@Arg String label, @ConstOne long count, @DefaultValue("50") int maxLabelCount) {
6062
this.isCalculated = false;
61-
this.dataset.valueAccumulation(label, count);
63+
this.maxLabelCount = maxLabelCount;
64+
this.dataset.valueAccumulation(label, count, maxLabelCount);
6265
}
6366

6467
@Override
6568
public boolean combine(Metrics metrics) {
6669
this.isCalculated = false;
6770
final LabelCountMetrics labelCountMetrics = (LabelCountMetrics) metrics;
68-
this.dataset.append(labelCountMetrics.dataset);
71+
this.dataset.append(labelCountMetrics.dataset, labelCountMetrics.maxLabelCount);
6972
return true;
7073
}
7174

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.oap.server.core.analysis.metrics.annotation;
20+
21+
import java.lang.annotation.ElementType;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
25+
26+
@Target(ElementType.PARAMETER)
27+
@Retention(RetentionPolicy.RUNTIME)
28+
public @interface DefaultValue {
29+
String value();
30+
}

0 commit comments

Comments
 (0)