diff --git a/polaris-plugins/polaris-plugins-observability/stat-common/src/main/java/com/tencent/polaris/plugins/stat/common/model/MetricValueAggregationStrategyCollections.java b/polaris-plugins/polaris-plugins-observability/stat-common/src/main/java/com/tencent/polaris/plugins/stat/common/model/MetricValueAggregationStrategyCollections.java index 16f0e2203..88d9c8466 100644 --- a/polaris-plugins/polaris-plugins-observability/stat-common/src/main/java/com/tencent/polaris/plugins/stat/common/model/MetricValueAggregationStrategyCollections.java +++ b/polaris-plugins/polaris-plugins-observability/stat-common/src/main/java/com/tencent/polaris/plugins/stat/common/model/MetricValueAggregationStrategyCollections.java @@ -37,6 +37,7 @@ public class MetricValueAggregationStrategyCollections { new UpstreamRequestSuccessStrategy(), new UpstreamRequestTimeoutStrategy(), new UpstreamRequestMaxTimeoutStrategy(), + new UpstreamRequestMinTimeoutStrategy(), }; RATE_LIMIT_STRATEGY = new MetricValueAggregationStrategy[]{ @@ -180,6 +181,48 @@ public double initMetricValue(InstanceGauge dataSource) { } } + /** + * 服务调用最小时延 + */ + public static class UpstreamRequestMinTimeoutStrategy implements MetricValueAggregationStrategy { + + @Override + public String getStrategyDescription() { + return "minimum request delay per period"; + } + + @Override + public String getStrategyName() { + return "upstream_rq_min_timeout"; + } + + @Override + public void updateMetricValue(StatMetric targetValue, InstanceGauge dataSource) { + if (null == dataSource.getDelay()) { + return; + } + + while (true) { + if (dataSource.getDelay() < targetValue.getValue()) { + if (targetValue.compareAndSet((long) targetValue.getValue(), dataSource.getDelay())) { + return; + } + } else { + return; + } + } + } + + @Override + public double initMetricValue(InstanceGauge dataSource) { + if (null == dataSource.getDelay()) { + return 0.0; + } + + return dataSource.getDelay(); + } + } + /** * 限流调用总请求数 */ diff --git a/polaris-plugins/polaris-plugins-observability/stat-prometheus/src/test/java/com/tencent/polaris/plugins/stat/prometheus/plugin/PrometheusReporterTest.java b/polaris-plugins/polaris-plugins-observability/stat-prometheus/src/test/java/com/tencent/polaris/plugins/stat/prometheus/plugin/PrometheusReporterTest.java index 714241bea..d1feabe93 100644 --- a/polaris-plugins/polaris-plugins-observability/stat-prometheus/src/test/java/com/tencent/polaris/plugins/stat/prometheus/plugin/PrometheusReporterTest.java +++ b/polaris-plugins/polaris-plugins-observability/stat-prometheus/src/test/java/com/tencent/polaris/plugins/stat/prometheus/plugin/PrometheusReporterTest.java @@ -19,8 +19,6 @@ import com.tencent.polaris.api.plugin.stat.CircuitBreakGauge; import com.tencent.polaris.api.plugin.stat.DefaultCircuitBreakResult; -import com.tencent.polaris.api.plugin.stat.DefaultRateLimitResult; -import com.tencent.polaris.api.plugin.stat.RateLimitGauge; import com.tencent.polaris.api.plugin.stat.StatInfo; import com.tencent.polaris.api.pojo.CircuitBreakerStatus; import com.tencent.polaris.api.pojo.InstanceGauge; @@ -38,16 +36,6 @@ import io.prometheus.client.Collector; import io.prometheus.client.Collector.MetricFamilySamples; import io.prometheus.client.CollectorRegistry; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Enumeration; -import java.util.List; -import java.util.Map; -import java.util.Random; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.Executors; - import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -55,6 +43,10 @@ import org.mockito.junit.MockitoJUnitRunner; import org.slf4j.Logger; +import java.util.*; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; + @RunWith(MockitoJUnitRunner.Silent.class) public class PrometheusReporterTest { @@ -198,7 +190,7 @@ public void testServiceCallSuccessStrategy() throws InterruptedException { } @Test - public void testServiceCallSumAndMaxStrategy() throws InterruptedException { + public void testServiceCallSumAndMaxAndMinStrategy() throws InterruptedException { List delayList = Collections.synchronizedList(new ArrayList<>()); int count = 20; batchDone(() -> { @@ -216,18 +208,24 @@ public void testServiceCallSumAndMaxStrategy() throws InterruptedException { handler.destroy(); int maxExpected = 0; + int minExpected = 10000; int sumExpected = 0; for (Integer i : delayList) { if (i > maxExpected) { maxExpected = i; } + if (i < minExpected) { + minExpected = i; + } sumExpected += i; } ServiceCallResult example = mockFixedLabelServiceCallResult(200, 1000); Double maxResult = getServiceCallMaxResult(example); + Double minResult = getServiceCallMinResult(example); Double sumResult = getServiceCallSumResult(example); Assert.assertEquals(new Double(maxExpected), maxResult); + Assert.assertEquals(new Double(minExpected), minResult); Assert.assertEquals(new Double(sumExpected), sumResult); } @@ -335,6 +333,11 @@ private Double getServiceCallMaxResult(ServiceCallResult example) { new MetricValueAggregationStrategyCollections.UpstreamRequestMaxTimeoutStrategy()); } + private Double getServiceCallMinResult(ServiceCallResult example) { + return getServiceCallResult(example, + new MetricValueAggregationStrategyCollections.UpstreamRequestMinTimeoutStrategy()); + } + private Double getServiceCallResult(ServiceCallResult example, MetricValueAggregationStrategy strategy) { CollectorRegistry registry = handler.getPromRegistry();