Skip to content

Commit 684bca2

Browse files
committed
feat:add min timeout metric strategy.
1 parent f261f9c commit 684bca2

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

polaris-plugins/polaris-plugins-observability/stat-common/src/main/java/com/tencent/polaris/plugins/stat/common/model/MetricValueAggregationStrategyCollections.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class MetricValueAggregationStrategyCollections {
3737
new UpstreamRequestSuccessStrategy(),
3838
new UpstreamRequestTimeoutStrategy(),
3939
new UpstreamRequestMaxTimeoutStrategy(),
40+
new UpstreamRequestMinTimeoutStrategy(),
4041
};
4142

4243
RATE_LIMIT_STRATEGY = new MetricValueAggregationStrategy[]{
@@ -180,6 +181,48 @@ public double initMetricValue(InstanceGauge dataSource) {
180181
}
181182
}
182183

184+
/**
185+
* 服务调用最小时延
186+
*/
187+
public static class UpstreamRequestMinTimeoutStrategy implements MetricValueAggregationStrategy<InstanceGauge> {
188+
189+
@Override
190+
public String getStrategyDescription() {
191+
return "minimum request delay per period";
192+
}
193+
194+
@Override
195+
public String getStrategyName() {
196+
return "upstream_rq_min_timeout";
197+
}
198+
199+
@Override
200+
public void updateMetricValue(StatMetric targetValue, InstanceGauge dataSource) {
201+
if (null == dataSource.getDelay()) {
202+
return;
203+
}
204+
205+
while (true) {
206+
if (dataSource.getDelay() < targetValue.getValue()) {
207+
if (targetValue.compareAndSet((long) targetValue.getValue(), dataSource.getDelay())) {
208+
return;
209+
}
210+
} else {
211+
return;
212+
}
213+
}
214+
}
215+
216+
@Override
217+
public double initMetricValue(InstanceGauge dataSource) {
218+
if (null == dataSource.getDelay()) {
219+
return 0.0;
220+
}
221+
222+
return dataSource.getDelay();
223+
}
224+
}
225+
183226
/**
184227
* 限流调用总请求数
185228
*/

polaris-plugins/polaris-plugins-observability/stat-prometheus/src/test/java/com/tencent/polaris/plugins/stat/prometheus/plugin/PrometheusReporterTest.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
import com.tencent.polaris.api.plugin.stat.CircuitBreakGauge;
2121
import com.tencent.polaris.api.plugin.stat.DefaultCircuitBreakResult;
22-
import com.tencent.polaris.api.plugin.stat.DefaultRateLimitResult;
23-
import com.tencent.polaris.api.plugin.stat.RateLimitGauge;
2422
import com.tencent.polaris.api.plugin.stat.StatInfo;
2523
import com.tencent.polaris.api.pojo.CircuitBreakerStatus;
2624
import com.tencent.polaris.api.pojo.InstanceGauge;
@@ -38,23 +36,17 @@
3836
import io.prometheus.client.Collector;
3937
import io.prometheus.client.Collector.MetricFamilySamples;
4038
import io.prometheus.client.CollectorRegistry;
41-
42-
import java.util.ArrayList;
43-
import java.util.Collections;
44-
import java.util.Enumeration;
45-
import java.util.List;
46-
import java.util.Map;
47-
import java.util.Random;
48-
import java.util.concurrent.CountDownLatch;
49-
import java.util.concurrent.Executors;
50-
5139
import org.junit.Assert;
5240
import org.junit.Before;
5341
import org.junit.Test;
5442
import org.junit.runner.RunWith;
5543
import org.mockito.junit.MockitoJUnitRunner;
5644
import org.slf4j.Logger;
5745

46+
import java.util.*;
47+
import java.util.concurrent.CountDownLatch;
48+
import java.util.concurrent.Executors;
49+
5850
@RunWith(MockitoJUnitRunner.Silent.class)
5951
public class PrometheusReporterTest {
6052

@@ -198,7 +190,7 @@ public void testServiceCallSuccessStrategy() throws InterruptedException {
198190
}
199191

200192
@Test
201-
public void testServiceCallSumAndMaxStrategy() throws InterruptedException {
193+
public void testServiceCallSumAndMaxAndMinStrategy() throws InterruptedException {
202194
List<Integer> delayList = Collections.synchronizedList(new ArrayList<>());
203195
int count = 20;
204196
batchDone(() -> {
@@ -216,18 +208,24 @@ public void testServiceCallSumAndMaxStrategy() throws InterruptedException {
216208
handler.destroy();
217209

218210
int maxExpected = 0;
211+
int minExpected = 10000;
219212
int sumExpected = 0;
220213
for (Integer i : delayList) {
221214
if (i > maxExpected) {
222215
maxExpected = i;
223216
}
217+
if (i < minExpected) {
218+
minExpected = i;
219+
}
224220
sumExpected += i;
225221
}
226222

227223
ServiceCallResult example = mockFixedLabelServiceCallResult(200, 1000);
228224
Double maxResult = getServiceCallMaxResult(example);
225+
Double minResult = getServiceCallMinResult(example);
229226
Double sumResult = getServiceCallSumResult(example);
230227
Assert.assertEquals(new Double(maxExpected), maxResult);
228+
Assert.assertEquals(new Double(minExpected), minResult);
231229
Assert.assertEquals(new Double(sumExpected), sumResult);
232230
}
233231

@@ -335,6 +333,11 @@ private Double getServiceCallMaxResult(ServiceCallResult example) {
335333
new MetricValueAggregationStrategyCollections.UpstreamRequestMaxTimeoutStrategy());
336334
}
337335

336+
private Double getServiceCallMinResult(ServiceCallResult example) {
337+
return getServiceCallResult(example,
338+
new MetricValueAggregationStrategyCollections.UpstreamRequestMinTimeoutStrategy());
339+
}
340+
338341
private Double getServiceCallResult(ServiceCallResult example,
339342
MetricValueAggregationStrategy<InstanceGauge> strategy) {
340343
CollectorRegistry registry = handler.getPromRegistry();

0 commit comments

Comments
 (0)