micrometer-metrics/micrometer

Multigauge Documentation lacks overwrite=true

Closed

#4,403 opened on Nov 22, 2023

 (8 comments) (0 reactions) (0 assignees)Java (1,131 forks)batch import
doc-updatehelp wanted

Repository metrics

Stars
 (4,882 stars)
PR merge metrics
 (Avg merge 1d 21h) (100 merged PRs in 30d)

Description

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?

Contributor guide