prometheus - 在普罗米修斯中显示任意@Timed指标

标签 prometheus spring-boot-actuator micrometer

我正在尝试将时间指标添加到我的 spring-boot web 应用程序中。目前,该应用程序使用 micrometer、prometheus 和 spring-boot-actuator。

我可以通过 http://localhost:8080/actuator/prometheus 连接到我的应用程序并查看默认指标列表,例如:

# HELP jvm_gc_memory_allocated_bytes_total Incremented for an increase in the size of the young generation memory pool after one GC to before the next
# TYPE jvm_gc_memory_allocated_bytes_total counter
jvm_gc_memory_allocated_bytes_total{application="my app",} 0.0
# HELP jvm_classes_loaded_classes The number of classes that are currently loaded in the Java virtual machine
# TYPE jvm_classes_loaded_classes gauge
jvm_classes_loaded_classes{application="my app",} 7581.0
...

太棒了!

但现在我想将计时指标附加到我的一个 Controller 的方法中。我希望这就像在类中添加一个 @Timed 注释一样简单:

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.micrometer.core.annotation.Timed;

@RestController
@Timed
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @GetMapping("/greeting")
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        doWork();
        return buildResponse(name);
    }

    private void doWork() {
        try {
            Thread.sleep((long) (1000 * Math.random()));
        } catch (InterruptedException e) {
        }
    }

    private Greeting buildResponse(String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }
}

但是,这似乎没有向 http://localhost:8080/actuator/prometheus 端点添加任何指标。

我需要采取哪些步骤才能让端点报告 GreetingController 类的计时指标,尤其是 doWorkbuildResponse 方法?

-- 更新--

我已经设法获得了我想要的结果,但比我预期的要多做一些工作,我希望有更简单的方法。我明确地将 Timer 对象添加到我想要测量的每个方法中,如下所示:

import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @Autowired
    MeterRegistry meterRegistry;

    @GetMapping("/greeting")
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        doWork();
        return buildResponse(name);
    }

    private void doWork() {
        Timer timer = meterRegistry.timer(this.getClass().getSimpleName() + ".doWork");
        timer.record(() -> {
            try {
                Thread.sleep((long) (1000 * Math.random()));
            } catch (InterruptedException e) {
            }
        });
    }

    private Greeting buildResponse(String name) {
        AtomicReference<Greeting> response = new AtomicReference<>();

        Timer timer = meterRegistry.timer(this.getClass().getSimpleName() + ".buildResponse");
        timer.record(() -> {
            response.set(new Greeting(counter.incrementAndGet(), String.format(template, name)));
        });

        return response.get();
    }
}

但至少这让我得到了我想要的结果:

# HELP GreetingController_buildResponse_seconds  
# TYPE GreetingController_buildResponse_seconds summary
GreetingController_buildResponse_seconds_count{application="my app",} 10.0
GreetingController_buildResponse_seconds_sum{application="my app",} 0.001531409
# HELP GreetingController_buildResponse_seconds_max  
# TYPE GreetingController_buildResponse_seconds_max gauge
GreetingController_buildResponse_seconds_max{application="my app",} 2.52253E-4
# HELP GreetingController_doWork_seconds_max  
# TYPE GreetingController_doWork_seconds_max gauge
GreetingController_doWork_seconds_max{application="my app",} 0.941169892
# HELP GreetingController_doWork_seconds  
# TYPE GreetingController_doWork_seconds summary
GreetingController_doWork_seconds_count{application="my app",} 10.0
GreetingController_doWork_seconds_sum{application="my app",} 4.767700907

有没有更简洁的方法?

最佳答案

注册TimedAspect作为配置类中的 bean:

@Bean
public TimedAspect timedAspect(MeterRegistry meterRegistry) {
    return new TimedAspect(meterRegistry);
}

关于prometheus - 在普罗米修斯中显示任意@Timed指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64613043/

相关文章:

kubernetes - Prometheus:监控外部 K8s 集群的更好选择

security - Spring Boot - 仅保护执行器端点

Prometheus 几乎没有使用在 Grafana 中未显示的计数器

spring - 如何在 Spring Boot 2.0 执行器端点的 `@Selector` 中使用 `@WriteOperation` ?

spring-boot - spring boot 2 actuator jvm.gc.memory.allocated 公制

java - 如何通过 ObjectMapper 捕获序列化和反序列化的延迟指标?

gradle - 无法让 Micrometer Prometheus 下载到我的 Grails 2 应用程序中

prometheus - Prometheus 中某些标签中的数据缺失时发出警报

java - 使用 Java 客户端检测 Prometheus 指标

具有多个服务器的普罗米修斯