Skip to content

Update MultiGauge Gauges instead of remove/add - #7851

Merged
shakuzen merged 7 commits into
micrometer-metrics:mainfrom
shakuzen:multigauge-inplace-update
Aug 26, 2026
Merged

shakuzen merged 7 commits into
micrometer-metrics:mainfrom
shakuzen:multigauge-inplace-update

Conversation

@shakuzen

@shakuzen shakuzen commented Aug 19, 2026 •

Copy link
Copy Markdown
Member

Previously, the corresponding Gauge for a MultiGauge Row was removed and re-registered if overwrite was true. This introduces a timing for the Gauge to be missed in publishing (or in the case of the Prometheus registry, to be read multiple times). Instead, update the backing object for the Gauge by introducing a RowState and tracking tracking it in the MultiGauge.

This fixes #6851, but due to the nature of the changes, I'm not sure we should apply this to maintenance branches. #7508 has a fix for the Prometheus registry specifically that looks safer to apply to maintenance branches.

Previously, the corresponding Gauge for a MultiGauge Row was removed and re-registered if `overwrite` was `true`.
This introduces a timing for the Gauge to be missed in publishing.
Instead, update the backing object for the Gauge by introducing a RowState and tracking tracking it in the MultiGauge.
@shakuzen shakuzen added this to the 1.18.0-M2 milestone Aug 19, 2026
@shakuzen shakuzen added enhancement A general enhancement module: micrometer-core An issue that is related to our core module labels Aug 19, 2026
);
// @formatter:on
assertThat(registry.get("mg").tag("key", "2").gauges()).hasSize(1);
assertThat(registry.get("mg").tag("key", "2").gauge().value()).isEqualTo(6d);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this behavior is specified (we have no JavaDoc so practically nothing is specified). I think this should be a separate test. The other assertions in this test are about single row register calls. This seems to be about multiple rows that will be equivalent after the filter. We should improve the specification for MultiGauge (and remove Incubating since I think the ship has sailed on not supporting it), but I think that's a separate issue to this PR. I guess it is nice the behavior happens to be the same before and after the main code changes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also backport any relevant tests to maintenance branches if/when we document the behavior in a JavaDoc

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have this:

* Each row is registered as a gauge using this multi-gauge's common name and tags,
* combined with the row's unique tags. When {@code overwrite} is {@code true}, rows
* that were previously registered with the same tags are replaced with the latest
* values. When {@code overwrite} is {@code false}, previously registered rows with
* the same tags are left unchanged. Rows that are no longer present are removed.

I think we should call the effect of a MeterFilter out on this, as we did for other function-tracking meters:

"Re-registering" a gauge can happen indirectly for example as the result of a MeterFilter modifying the name and/or the tags of two different gauges so that they will be the same after the filter is applied.

I also think that even if nothing would be specified, the scenario is definitely possible so I think the test is still valid since we probably want to know if the behavior changes.

But I agree, this test is definitely more connected to #6146 and #6070 than to this issue, I just discovered this scenario during reviewing this PR and and saw we did not have this test case.

At this point, I wanted to create a PR to add this test to 1.16.x and here's the twist: it fails on 1.16.x, 1.17.x, and main. I think the current behavior on main is "wrong" and this PR "fixes" it. Let's discuss what to do (should we fix 1.16 and 1.17) on our next meeting. This also makes me wanting to add this test on top of these changes (in this PR or in a fast-follow one).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how I missed the JavaDoc when I wrote my original comment. After reading the JavaDoc, I would still say the behavior for duplicates within a register call is unclear if not completely unspecified.

I went down the rabbit hole on this a bit. I think we should decide the right behavior going forward and since this PR actually changes the behavior, perhaps it should be specified along with this PR. I didn't want to block getting this merged on that since it wasn't the point of this PR originally, but I think changing the behavior without making it clear what the expected behavior is seems wrong too.

I think the problem with the behavior in maintenance branches is that it depends on the previous state. You get a different outcome depending on whether the old rows contain the same ID as the duplicates being registered. If the old rows don't contain it, you get first row wins and subsequent rows result in a failed gauge registration. If the old row does contain the ID, you get last row wins and a remove/re-register for each row.

With the changes in this PR, you get consistent behavior that only depends on what you pass for overwrite. It's unclear from the JavaDoc that overwrite should apply to intra-batch duplicates, as it was originally added for controlling the behavior of inter-batch duplicates, but I think it's a reasonable thing to do until we understand a reason for allowing different behavior.

I'm not sure how much effort we should put into changing anything related to this on maintenance branches until we at least have a reported problem about it. I would focus our efforts on figuring out what is right going forward, and then specifying and testing that.

…o/micrometer/prometheusmetrics/PrometheusMeterRegistryTest.java

Co-authored-by: Jonatan Ivanov <jonatan.ivanov@gmail.com>
Signed-off-by: Tommy Ludwig <8924140+shakuzen@users.noreply.github.com>
@skrcode

skrcode commented Aug 22, 2026

Copy link
Copy Markdown

I ran JAIPilot Cloud against this exact PR head. It found one follow-up in MultiGauge.register(): avoid rebuilding a temporary HashSet for each registration while preserving row replacement and stale-gauge removal.

On a 200-row JMH workload using the same host and JDK 25, median time moved from 14.9035 to 8.4815 µs/op and allocation from 18,616 to 8,000 B/op. The focused 75-test suite and affected module builds passed.

Cloud-generated draft and full evidence: skrcode#2
PR directly onto this source branch: shakuzen#1

This is optional and workload-qualified; feel free to merge or cherry-pick if it fits the direction of this PR.

@shakuzen

Copy link
Copy Markdown
Member Author

@skrcode thanks for taking a look. I think it will be best to consider that optimization after this PR as a follow-up. MultiGauge.register is not expected to be on the hot path, and the proposed changes I think make the code harder to follow, even if they're still correct and have better performance. So we'll have to consider the tradeoff.

They explicitly test duplicates within one register call as opposed to duplicates across multiple register calls.
The behavior used to depend on the state of registered rows, so include a second call to register with the same rows but different values to check the behavior in all cases.
@shakuzen

Copy link
Copy Markdown
Member Author

@jonatan-ivanov I have attempted to specify the behavior in the updated register JavaDoc. Would you take a look, please?

@shakuzen

Copy link
Copy Markdown
Member Author

I think a follow-up PR should look at removing the @Incubating annotation and fully checking test coverage and specification of behavior. I left that out-of-scope for this PR. I think we're missing some test cases and there might even be more decisions to make about what is the right behavior with edge cases.

@shakuzen
shakuzen merged commit a4ba539 into micrometer-metrics:main Aug 26, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement A general enhancement module: micrometer-core An issue that is related to our core module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Registering/Updating a MultiGauge while a Prometheus scrape occurs can lead to DuplicateLabelsException

3 participants