From 0504f8f05c973b06726facef7e81d148c3bcc186 Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Thu, 1 Feb 2024 15:06:10 +0800 Subject: [PATCH] =?UTF-8?q?refactor(forest):=20=E7=AE=80=E5=8C=96=E6=8C=87?= =?UTF-8?q?=E6=A0=87=E5=BA=A6=E9=87=8F=E5=B7=A5=E5=85=B7=E7=9A=84=E8=8E=B7?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 指标度量工具获取本身已经有缓存,不需要再缓存一层 --- .../configuration/MetricsInterceptor.java | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/service-forest/src/main/java/com/lanyuanxiaoyao/service/forest/configuration/MetricsInterceptor.java b/service-forest/src/main/java/com/lanyuanxiaoyao/service/forest/configuration/MetricsInterceptor.java index a30bfc1..a07b48d 100644 --- a/service-forest/src/main/java/com/lanyuanxiaoyao/service/forest/configuration/MetricsInterceptor.java +++ b/service-forest/src/main/java/com/lanyuanxiaoyao/service/forest/configuration/MetricsInterceptor.java @@ -6,7 +6,6 @@ import com.dtflys.forest.http.ForestQueryMap; import com.dtflys.forest.http.ForestRequest; import com.dtflys.forest.http.ForestResponse; import com.dtflys.forest.interceptor.Interceptor; -import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Timer; @@ -34,10 +33,6 @@ public class MetricsInterceptor implements Interceptor { private static final Logger logger = LoggerFactory.getLogger(MetricsInterceptor.class); private static final String PARSED_TAGS = "parsed_tags"; private final MeterRegistry meterRegistry; - private final MutableMap counterCache = - Maps.mutable.empty().asSynchronized(); - private final MutableMap timerCache = - Maps.mutable.empty().asSynchronized(); public MetricsInterceptor(MeterRegistry meterRegistry) { this.meterRegistry = meterRegistry; @@ -59,15 +54,11 @@ public class MetricsInterceptor implements Interceptor { return tags; } - private void increaseCounter(ForestRequest request, ForestResponse response, MutableList tags) { - Counter counter = counterCache.getIfAbsentPut( - request.getUrl(), - meterRegistry.counter( - MetricsInterceptor.FOREST_SEND_REQUEST, - tags.with(Tag.of("code", String.valueOf(response.getStatusCode()))) - ) - ); - counter.increment(); + private void increaseCounter(Integer code, MutableList tags) { + meterRegistry.counter( + MetricsInterceptor.FOREST_SEND_REQUEST, + tags.with(Tag.of("code", String.valueOf(code))) + ).increment(); } @Override @@ -81,8 +72,8 @@ public class MetricsInterceptor implements Interceptor { Interceptor.super.afterExecute(request, response); MutableList tags = (MutableList) getAttribute(request, PARSED_TAGS); - Timer timer = timerCache.getIfAbsentPut(request.getUrl(), meterRegistry.timer(FOREST_SEND_REQUEST_SECONDS, tags)); - timer.record(response.getTimeAsMillisecond(), TimeUnit.MILLISECONDS); + meterRegistry.timer(FOREST_SEND_REQUEST_SECONDS, tags) + .record(response.getTimeAsMillisecond(), TimeUnit.MILLISECONDS); } @Override @@ -91,7 +82,7 @@ public class MetricsInterceptor implements Interceptor { MutableList tags = (MutableList) getAttribute(request, PARSED_TAGS); if (ObjectUtil.isNotNull(tags)) { - increaseCounter(request, response, tags); + increaseCounter(response.statusCode(), tags); } } @@ -101,7 +92,7 @@ public class MetricsInterceptor implements Interceptor { MutableList tags = (MutableList) getAttribute(request, PARSED_TAGS); if (ObjectUtil.isNotNull(tags)) { - increaseCounter(request, response, tags); + increaseCounter(response.statusCode(), tags); } } }