对千分尺指标的困惑 - 量规不是应该在提交之前自动计算值吗?

Confusion about micrometer metrics - Isn't the gauge supposed to calculate the value automatically just before it is submitted?

我正在探索 micrometer 和 aws cloudwatch。
我认为存在一些理解上的差距 -
我创建了一个仪表,它应该 return 连接池中使用的连接数。

public MetricService(CloudWatchConfig config) {
    this.cloudwatchMeterRegistry = new CloudWatchMeterRegistry(config, Clock.SYSTEM, CloudWatchAsyncClient.create());

    gauge = Gauge.builder("ConnectionPoolGauge", this.connectionPool, value -> {
                Double usedConnections = 0.0;
                for (Map.Entry<String, Boolean> entry : value.entrySet()) {
                    if (entry.getValue().equals(Boolean.FALSE)) {
                        usedConnections++;
                    }
                }
                return usedConnections;
            })
            .tag("GaugeName", "Bhushan's Gauge")
            .strongReference(true)
            .baseUnit("UsedConnections")
            .description("Gauge to monitor connection pool")
            .register(Metrics.globalRegistry);

    Metrics.addRegistry(cloudwatchMeterRegistry);
}

如您所见,我目前正在构造函数中启动此量表。从外部传递 connectionPool 实例。
以下是一个使用连接的控制器方法 -

@GetMapping("/hello")
        public String hello() {
    // connectionPool.consumeConnection();
    // finally { connectionPool.releaseConnection();}
    }

步进间隔设置为10秒。我的理解是 - 每 10 秒,Micrometer 应该自动 执行传递给仪表的双重功能。
显然,这并没有发生。
我在这里看到了一些代码示例,它们明确设置了仪表值(在单独的线程或计划逻辑中)。

我还尝试使用仅实例化一次的计数器,但每次调用 hello 方法时我都会显式调用增量方法。我的预期是这个计数器会继续递增,但过了一会儿,它下降到 0 并再次开始计数。
我完全糊涂了。感谢有人能阐明这个概念。

编辑: 尝试了以下创建 Gauge 的方法 - 仍然没有成功。

cloudwatchMeterRegistry.gauge("ConnectionPoolGauge", this.connectionPool, value -> {
    Double usedConnections = 0.0;
    System.out.println("Inside Guage Value function." + value.entrySet());
    for (Map.Entry<String, Boolean> entry : value.entrySet()) {
        if (entry.getValue().equals(Boolean.FALSE)) {
            usedConnections++;
        }
    }
    return usedConnections;
});

这不是 return Gauge 的实例,因此我无法对其调用 value()。此外,该仪表在 AWS Cloudwatch 中不可见。我可以在同一个程序中创建的 cloudwatch 中看到计数器。

Micrometer takes the stance that gauges should be sampled and not be set, so there is no information about what might have occurred between samples. After all, any intermediate values set on a gauge are lost by the time the gauge value is reported to a metrics backend anyway, so there seems to be little value in setting those intermediate values in the first place.

If it helps, think of a Gauge as a "heisen-gauge" - a meter that only changes when it is observed. Every other meter type provided out-of-the-box accumulates intermediate counts toward the point where the data is sent to the metrics backend.

因此,在发布指标时会更新仪表,这里有一些解决此问题的技巧:

  1. 在你的CloudWatchMeterRegistrypublish方法中设置一个刹车点,看看它是否被调用。
  2. 您正在使用全局注册表 (Metrics.addRegistry) 并保留对 CloudWatchMeterRegistry (this.cloudwatchMeterRegistry = new CloudWatchMeterRegistry) 的引用。你不需要两者,我建议不要使用全局注册表并在你需要的任何地方注入你的注册表。
  3. 我不确定您在使用连接池做什么(您是否实现了自己的连接池?)但是开箱即用 support for HikariCP 并且 DBCP 正在发布您需要的 JMX 计数器可以绑定到千分尺。