用的 Srping Boot Batch 的 application
想创建一个自定义的指标
report.download.batch.job.ng.slo.flag
batch的入口
final int NG_FLAG = 1;
final int OK_FLAG = 0;
@Scheduled(cron = "0 0 6 * * *")
public void perform() throws Exception {
...
setSloCustomMetrics();
}
自定义指标的函数
private void setSloCustomMetrics() {
LocalTime time = LocalTime.now();
// the time as seconds of day from this LocalTime
int toSecondEndTime = time.toSecondOfDay();
String todayDate = today();
String nowTime = String.valueOf(toSecondEndTime);
AtomicInteger isNotCompletedBeforeSLO = new AtomicInteger();
Gauge.builder("report.download.batch.job.ng.slo.flag", isNotCompletedBeforeSLO, AtomicInteger::get)
.description("The identifier that the SLO goal was not accomplished")
.tags("date", todayDate)
.tags("time", nowTime)
.register(Metrics.globalRegistry);
// after 10 A.M. '1' output
if (toSecondEndTime > 60 * 60 * 10) {
isNotCompletedBeforeSLO.set(NG_FLAG);
} else {
isNotCompletedBeforeSLO.set(OK_FLAG);
}
}
但是取到的值是 NaN
每次batch启动后都可以得到值,但过了段时间就NaN了
原因是 weak reference
可是系统内存的原因 不可以用 Gauge 自己的 strongreference(true)
在这种情况下 该怎么办呢 谢谢了
report_download_batch_job_ng_slo_flag{date="20210621",env="current",instance="batch-report-current#0",job="report_batch_micrometer",label="report_batch",space_id="8880d959-0e0f-4f44-8c62-c73f6ed55eda",time="53419"} NaN
report_download_batch_job_ng_slo_flag{date="20210621",env="current",instance="batch-report-current#0",job="report_batch_micrometer",label="report_batch",space_id="8880d959-0e0f-4f44-8c62-c73f6ed55eda",time="53514"} 0