千分尺Timer.start/stop和Timer.record之间的区别

San*_*yam 3 java grafana prometheus micrometer

我想检查数据库命中(多个数据库)和请求处理的延迟。更好的选择是Timer.Sample什么Timer.record

我使用 Micrometer 以 Prometheus 为基础。

Adi*_*tya 5

当您计算出事件所花费的持续时间时,请使用 Timer.record 。

default void record(Duration duration)
Run Code Online (Sandbox Code Playgroud)

当您想要传递 Sample 以确定发布指标的点(不一定是在完全相同的位置)时,通常会使用 Timer.Sample。您还可以使用 Timer 对象更精细地控制要发布的内容。这是一个两步过程。

  1. 在事件开始返回样本对象之前创建一个样本,使用

    static Sample start(Clock clock) {..}
    
    Run Code Online (Sandbox Code Playgroud)
  2. 当活动完成时使用 Sample.stop 停止示例并推送指标

    public long stop(Timer timer) {..}
    
    Run Code Online (Sandbox Code Playgroud)

例如来自TimedAspect的-

    Timer.Sample sample = Timer.start(registry);
    try {
        return pjp.proceed();
    } finally {
        sample.stop(Timer.builder(timed.value())
                .description(timed.description().isEmpty() ? null : timed.description())
                .tags(timed.extraTags())
                .tags(tagsBasedOnJoinpoint.apply(pjp))
                .publishPercentileHistogram(timed.histogram())
                .publishPercentiles(timed.percentiles().length == 0 ? null : timed.percentiles())
                .register(registry));
    }
Run Code Online (Sandbox Code Playgroud)