Skip to content

Commit a1ef356

Browse files
committed
Support labelAvg function in the OAL engine
1 parent 0a0890c commit a1ef356

File tree

8 files changed

+190
-5
lines changed

8 files changed

+190
-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 `labelCount` 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: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
import org.apache.skywalking.oap.server.core.storage.annotation.ElasticSearch;
30+
31+
import java.util.Objects;
32+
import java.util.Set;
33+
34+
@MetricsFunction(functionName = "labelAvg")
35+
public abstract class LabelAvgMetrics extends Metrics implements LabeledValueHolder {
36+
protected static final String SUMMATION = "datatable_summation";
37+
protected static final String COUNT = "datatable_count";
38+
protected static final String VALUE = "datatable_value";
39+
40+
protected static final String LABEL_NAME = "n";
41+
42+
@Getter
43+
@Setter
44+
@Column(name = SUMMATION, storageOnly = true)
45+
@ElasticSearch.Column(legacyName = "summation")
46+
@BanyanDB.MeasureField
47+
protected DataTable summation;
48+
@Getter
49+
@Setter
50+
@Column(name = COUNT, storageOnly = true)
51+
@ElasticSearch.Column(legacyName = "count")
52+
@BanyanDB.MeasureField
53+
protected DataTable count;
54+
@Getter
55+
@Setter
56+
@Column(name = VALUE, dataType = Column.ValueDataType.LABELED_VALUE, storageOnly = true)
57+
@ElasticSearch.Column(legacyName = "value")
58+
@BanyanDB.MeasureField
59+
private DataTable value;
60+
61+
private boolean isCalculated;
62+
private int maxLabelCount;
63+
64+
public LabelAvgMetrics() {
65+
this.summation = new DataTable(30);
66+
this.count = new DataTable(30);
67+
this.value = new DataTable(30);
68+
}
69+
70+
@Entrance
71+
public final void combine(@Arg String label, @Arg long count, @DefaultValue("1024") int maxLabelCount) {
72+
this.isCalculated = false;
73+
this.maxLabelCount = maxLabelCount;
74+
this.summation.valueAccumulation(label, count, maxLabelCount);
75+
this.count.valueAccumulation(label, 1L, maxLabelCount);
76+
}
77+
78+
@Override
79+
public boolean combine(Metrics metrics) {
80+
this.isCalculated = false;
81+
final LabelAvgMetrics labelCountMetrics = (LabelAvgMetrics) metrics;
82+
this.summation.append(labelCountMetrics.summation, labelCountMetrics.maxLabelCount);
83+
this.count.append(labelCountMetrics.count, labelCountMetrics.maxLabelCount);
84+
return true;
85+
}
86+
87+
@Override
88+
public void calculate() {
89+
if (isCalculated) {
90+
return;
91+
}
92+
93+
Set<String> keys = count.keys();
94+
for (String key : keys) {
95+
Long s = summation.get(key);
96+
if (Objects.isNull(s)) {
97+
continue;
98+
}
99+
Long c = count.get(key);
100+
if (Objects.isNull(c)) {
101+
continue;
102+
}
103+
long result = s / c;
104+
if (result == 0 && s > 0) {
105+
result = 1;
106+
}
107+
final DataLabel label = new DataLabel();
108+
label.put(LABEL_NAME, key);
109+
value.put(label, result);
110+
}
111+
}
112+
113+
@Override
114+
public DataTable getValue() {
115+
return this.value;
116+
}
117+
}

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("1024") 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)