micrometer-metrics/micrometer

Multigauge Documentation lacks overwrite=true

已关闭

#4,403 创建于 2023年11月22日

 (8 条评论) (0 个反应) (0 位负责人)Java (1,133 个派生)batch import
doc-updatehelp wanted

仓库指标

星标
 (4,882 个星标)
PR 合并指标
 (平均合并 1天 21小时) (30 天内合并 100 个 PR)

描述

Describe the bug It's a bug in the documentation only!

I tried the code from https://micrometer.io/docs/concepts#_multi_gauge it suggests that you run "register" periodically when running the query, and for me it is implied, that the "count" would be updated every time. For this to work, the second parameter "overwrite" in my understanding must be set to true, the default is false.

I suggest changing the doumentation to

statuses.register(
    resultSet.stream()
        .map(result -> Row.of(Tags.of("status", result.getAsString("status")), result.getAsInt("count")))
        .collect(toList()),
/* overwrite */ true
);

Environment Micrometer 1.12

To Reproduce

 @Test
    fun `min sample`() {
        val gauge = MultiGauge.builder("test").register(meterRegistry)
        gauge.register(
            listOf(MultiGauge.Row.of(Tags.of("status", "some status"), 1)),
        )

        printGauge()

        gauge.register(
            listOf(MultiGauge.Row.of(Tags.of("status", "some status"), 2)),
        )

        printGauge()

        gauge.register(
            listOf(MultiGauge.Row.of(Tags.of("status", "some status"), 3)),
            /* overwrite = */ true,
        )

        printGauge()
    }

    private fun printGauge() {
        println(
            meterRegistry
                .find("test")
                .gauges()
                .map { Pair(it.id.tags, it.value()) },
        )
    }
    
prints:

[([tag(status=some status)], 1.0)]
[([tag(status=some status)], 1.0)] // not updated
[([tag(status=some status)], 3.0)] // updated    

Additional context A related thought: Looking at the implementation of Multigauge.register, I see that gauges are deleted and recreated in the MeterRegistry. I can't see how this is multithreading save: If the metrics are collected by Prometheus just as the gauge is deleted, the gauge will be missing from the metrics. Should I open a ticket for this?

贡献者指南