我想测量处理某些数据需要多长时间:我的应用程序以固定速率从给定源读取该数据。在每个圆圈之前,我存储Instant.now()
.我读取数据,将单个时间戳添加到每个条目中。数据被存储、转换,在我通过 WebSockets 发送数据之前,我想测量now()
和初始时间戳之间的持续时间。
我试过了
long millis = Duration.between(dataEntry.getReceivedTimestamp(), Instant.now()).toMillis();
LOG.info(millis + "ms");
registry.timer("processingDuration").record(millis, TimeUnit.MILLISECONDS);
但是可视化这允许我只使用processingDuration_seconds_count
,_max
和_sum
。count
和sum
随着时间的推移而增加(当然(,max
大部分时间都是恒定的。那么,如何看待负载的更高和更低的平台呢?我尝试irate(processingDuration_seconds_sum[10m])
至少看到跳跃,但由于irate()
仅使用两个数据点,我仍然无法轻松识别更长的高负载周期。另外:图中的值约为 0.6,而记录的毫秒约为 5-10,所以我在这里丢失了实际值。
所以我尝试使用Gauge
代替 - 它应该允许增加和减少值:
registry.gauge("processingDurationGauge", millis);
我以为这会在记录的毫秒范围内上下波动,但它一直是 92。
如何测量整个数据时间?
使用计时器和record
是正确的解决方案。
long millis = Duration.between(dataEntry.getReceivedTimestamp(), Instant.now()).toMillis();
LOG.info(millis + "ms");
registry.timer("processingDuration").record(millis, TimeUnit.MILLISECONDS);
假设您每 30 秒抓取一次,您可以使用_sum
和_count
来获得平均记录的持续时间:
increase(processingDuration_seconds_sum[1m])/increase(processingDuration_seconds_count[1m])
如果您想比较当前持续时间与前一天平均值相比的行为:
((increase(processingDuration_seconds_sum[1m])/
increase(processingDuration_seconds_count[1m]))*1.50) >
increase(processingDuration_seconds_sum[24h])/
increase(processingDuration_seconds_count[24h])
这只会返回 1m 平均值大于每日平均值 1.5 倍的值。(我还没有测试过那个查询,但它应该明白了(。
问题是,long
线程不安全,请参阅此答案。按预期记录工作:
private final AtomicLong processingDuration;
// ...
// in constructor:
processingDuration = meterRegistry.gauge("processingDuration", new AtomicLong(0L));
// ...
// before finishing the data entries' handling:
long millis = Duration.between(dataEntry.getReceivedTimestamp(), Instant.now()).toMillis();
LOG.info(millis + "ms");
processingDuration.set(millis);