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); } } }