refactor(forest): 简化指标度量工具的获取

指标度量工具获取本身已经有缓存,不需要再缓存一层
This commit is contained in:
2024-02-01 15:06:10 +08:00
parent dc98591457
commit 0504f8f05c

View File

@@ -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<Object> {
private static final Logger logger = LoggerFactory.getLogger(MetricsInterceptor.class);
private static final String PARSED_TAGS = "parsed_tags";
private final MeterRegistry meterRegistry;
private final MutableMap<String, Counter> counterCache =
Maps.mutable.<String, Counter>empty().asSynchronized();
private final MutableMap<String, Timer> timerCache =
Maps.mutable.<String, Timer>empty().asSynchronized();
public MetricsInterceptor(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
@@ -59,15 +54,11 @@ public class MetricsInterceptor implements Interceptor<Object> {
return tags;
}
private void increaseCounter(ForestRequest<?> request, ForestResponse<?> response, MutableList<Tag> 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<Tag> 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<Object> {
Interceptor.super.afterExecute(request, response);
MutableList<Tag> tags = (MutableList<Tag>) 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<Object> {
MutableList<Tag> tags = (MutableList<Tag>) 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<Object> {
MutableList<Tag> tags = (MutableList<Tag>) getAttribute(request, PARSED_TAGS);
if (ObjectUtil.isNotNull(tags)) {
increaseCounter(request, response, tags);
increaseCounter(response.statusCode(), tags);
}
}
}