我使用微米LongTaskTimer来记录当某事创造成功时的时间成本;当没有什么创造成功时,这些指标就有了价值。
@Configuration
@Slf4j
public class MetricConfiguration {
@Resource
private MeterRegistry meterRegistry;
@Bean(name = "creationTimer")
LongTaskTimer creationTimer() {
//TODO only apply to event,not for api
return LongTaskTimer.builder("creationTimer")
.description("timer for pipeline create process")
.publishPercentileHistogram()
.publishPercentiles(0)
.register(meterRegistry);
}
}
public class CreationWorker {
private LongTaskTimer.Sample createTimerSample;
private LongTaskTimer createTimer;
public CreationWorker(LongTaskTimer createTimer) {
this.createTimer = createTimer;
}
public boolean execute() {
if(dbStatus = 'toCreate')
return doCreate();
if(dbStatus = 'creating')
return checkCreated();
}
public boolean doCreate() {
createTimerSample = createTimer.start();
//then do along async create task
//update dbStatus='creating'
}
public boolean checkCreated() {
Status status = ...; //get async task status
if (status.equals("success"))) {
createTimerSample.stop();
return true
}
// I think need to clear the longTaskTimer
return false;
}
}
在我的本地测试环境中,异步任务永远不会“成功”,但creationTimer是有值的,我认为它需要在失败时清除longTaskTimer。但我不知道该怎么清理。
发布于 2021-07-07 01:49:16
我建议在失败场景中也停止样本,但注册一个标记,它将告诉您结果(成功/失败)。这样,您就可以跟踪成功事件、失败事件以及两者。
如果使用@Timed
:see here,这就是测微仪本身所做的事情
https://stackoverflow.com/questions/68222413
复制相似问题