micrometer-metrics/micrometer

Multigauge Documentation lacks overwrite=true

Chiusa

#4403 aperta il 22 nov 2023

 (8 commenti) (0 reazioni) (0 assegnatari)Java (1131 fork)batch import
doc-updatehelp wanted

Metriche repository

Star
 (4882 stelle)
Metriche merge PR
 (Merge medio 1g 21h) (100 PR mergiate in 30 g)

Descrizione

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?

Guida contributor