Repository navigation
Update MultiGauge Gauges instead of remove/add - #7851
Conversation
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.
| ); | ||
| // @formatter:on | ||
| assertThat(registry.get("mg").tag("key", "2").gauges()).hasSize(1); | ||
| assertThat(registry.get("mg").tag("key", "2").gauge().value()).isEqualTo(6d); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
We should also backport any relevant tests to maintenance branches if/when we document the behavior in a JavaDoc
There was a problem hiding this comment.
We do have this:
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).
There was a problem hiding this comment.
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>
|
I ran JAIPilot Cloud against this exact PR head. It found one follow-up in 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 This is optional and workload-qualified; feel free to merge or cherry-pick if it fits the direction of this PR. |
|
@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.
|
@jonatan-ivanov I have attempted to specify the behavior in the updated |
|
I think a follow-up PR should look at removing the |
Previously, the corresponding Gauge for a MultiGauge Row was removed and re-registered if
overwritewastrue. 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.