如何解决带有 RetryTemplate 最佳实践或建议的千分尺
我正在使用 micrometer java 库来收集指标,例如某些方法的执行时间等。 经过一段时间的思考,我对如何在使用 retryTemplate 时正确收集指标感到困惑。
示例:
// I have more methods like delete,search,etc where we can face the same issue
public void upsertDocument(WalRecord walRecord) {
...
retryTemplate.execute(context ->
arangoInitService.getarangoDb()
.collection(walRecord.getBaseType())
.insertDocument(data,upsertOptions));
...
}
因此,这里我们有不同的选项来收集“计时器”指标。
- 使用注解@Timed
@Timed
public void upsertDocument(WalRecord walRecord) {...}
看起来很简单,但我不喜欢它,因为在重试的情况下会总结执行时间。
- 使用千分尺库的 Timer 类,类似的东西
retryTemplate.execute(context -> {
...
Timer.Sample sample = Timer.start(registry);
retryTemplate.execute(context -> {
arangoInitService.getarangoDb()
.collection(walRecord.getBaseType())
.insertDocument(data,upsertOptions)
sample.stop(registry.timer("my.timer","response",response.status()));
});
...
}
现在,即使发生重试,我们也收集了正确的时间,但看起来我们不会收集失败的 insertDocument() 操作的指标!
public <T,E extends Throwable> T inTimerWithRetry(String metricName,RetryTemplate retryTemplate,MetricCoveredCallable<T> callable) throws E {
T res;
res = retryTemplate.execute(context -> {
String exception = "none";
Timer.Sample sample = null;
if (registry != null) {
sample = Timer.start(registry);
}
try {
return callable.call();
} catch (Throwable e) {
exception = e.getClass().getSimpleName();
throw (E) e;
} finally {
if (sample != null) {
sample.stop(Timer.builder(metricName)
.tags("exception",exception)
.register(registry));
}
}
});
return res;
}
这里是我的问题,你怎么看
- 做这样的包装值得吗?
- 有更好的解决方案吗?欢迎您提出任何改进建议
- 也许我做错了什么,并无缘无故地让它变得过于复杂?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。