refactor(forest): 简化指标度量工具的获取
指标度量工具获取本身已经有缓存,不需要再缓存一层
This commit is contained in:
@@ -6,7 +6,6 @@ import com.dtflys.forest.http.ForestQueryMap;
|
|||||||
import com.dtflys.forest.http.ForestRequest;
|
import com.dtflys.forest.http.ForestRequest;
|
||||||
import com.dtflys.forest.http.ForestResponse;
|
import com.dtflys.forest.http.ForestResponse;
|
||||||
import com.dtflys.forest.interceptor.Interceptor;
|
import com.dtflys.forest.interceptor.Interceptor;
|
||||||
import io.micrometer.core.instrument.Counter;
|
|
||||||
import io.micrometer.core.instrument.MeterRegistry;
|
import io.micrometer.core.instrument.MeterRegistry;
|
||||||
import io.micrometer.core.instrument.Tag;
|
import io.micrometer.core.instrument.Tag;
|
||||||
import io.micrometer.core.instrument.Timer;
|
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 Logger logger = LoggerFactory.getLogger(MetricsInterceptor.class);
|
||||||
private static final String PARSED_TAGS = "parsed_tags";
|
private static final String PARSED_TAGS = "parsed_tags";
|
||||||
private final MeterRegistry meterRegistry;
|
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) {
|
public MetricsInterceptor(MeterRegistry meterRegistry) {
|
||||||
this.meterRegistry = meterRegistry;
|
this.meterRegistry = meterRegistry;
|
||||||
@@ -59,15 +54,11 @@ public class MetricsInterceptor implements Interceptor<Object> {
|
|||||||
return tags;
|
return tags;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void increaseCounter(ForestRequest<?> request, ForestResponse<?> response, MutableList<Tag> tags) {
|
private void increaseCounter(Integer code, MutableList<Tag> tags) {
|
||||||
Counter counter = counterCache.getIfAbsentPut(
|
|
||||||
request.getUrl(),
|
|
||||||
meterRegistry.counter(
|
meterRegistry.counter(
|
||||||
MetricsInterceptor.FOREST_SEND_REQUEST,
|
MetricsInterceptor.FOREST_SEND_REQUEST,
|
||||||
tags.with(Tag.of("code", String.valueOf(response.getStatusCode())))
|
tags.with(Tag.of("code", String.valueOf(code)))
|
||||||
)
|
).increment();
|
||||||
);
|
|
||||||
counter.increment();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -81,8 +72,8 @@ public class MetricsInterceptor implements Interceptor<Object> {
|
|||||||
Interceptor.super.afterExecute(request, response);
|
Interceptor.super.afterExecute(request, response);
|
||||||
|
|
||||||
MutableList<Tag> tags = (MutableList<Tag>) getAttribute(request, PARSED_TAGS);
|
MutableList<Tag> tags = (MutableList<Tag>) getAttribute(request, PARSED_TAGS);
|
||||||
Timer timer = timerCache.getIfAbsentPut(request.getUrl(), meterRegistry.timer(FOREST_SEND_REQUEST_SECONDS, tags));
|
meterRegistry.timer(FOREST_SEND_REQUEST_SECONDS, tags)
|
||||||
timer.record(response.getTimeAsMillisecond(), TimeUnit.MILLISECONDS);
|
.record(response.getTimeAsMillisecond(), TimeUnit.MILLISECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -91,7 +82,7 @@ public class MetricsInterceptor implements Interceptor<Object> {
|
|||||||
|
|
||||||
MutableList<Tag> tags = (MutableList<Tag>) getAttribute(request, PARSED_TAGS);
|
MutableList<Tag> tags = (MutableList<Tag>) getAttribute(request, PARSED_TAGS);
|
||||||
if (ObjectUtil.isNotNull(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);
|
MutableList<Tag> tags = (MutableList<Tag>) getAttribute(request, PARSED_TAGS);
|
||||||
if (ObjectUtil.isNotNull(tags)) {
|
if (ObjectUtil.isNotNull(tags)) {
|
||||||
increaseCounter(request, response, tags);
|
increaseCounter(response.statusCode(), tags);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user