Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve warning about Gauge re-registration in the docs #5617

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/concepts/gauges.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Note that, in this form, unlike other meter types, you do not get a reference to

This pattern should be less common than the `DoubleFunction` form. Remember that frequent setting of the observed `Number` results in a lot of intermediate values that never get published. Only the _instantaneous value_ of the gauge at publish time is ever sent to the monitoring system.

WARNING: Attempting to construct a gauge with a primitive number or one of its `java.lang` object forms is always incorrect. These numbers are immutable. Thus, the gauge cannot ever be changed. Attempting to "`re-register`" the gauge with a different number does not work, as the registry maintains only one meter for each unique combination of name and tags.
WARNING: Attempting to construct a gauge with a primitive number or one of its `java.lang` object forms is always incorrect. These numbers are immutable. Thus, the gauge cannot ever be changed. Attempting to "re-register" the gauge with a different number does not work, as the registry maintains only one meter for each unique combination of name and tags. "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.
jonatan-ivanov marked this conversation as resolved.
Show resolved Hide resolved

== Gauge Fluent Builder

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.micrometer.core.annotation.Timed;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.*;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micrometer.core.instrument.distribution.CountAtBucket;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import io.micrometer.core.instrument.distribution.HistogramSnapshot;
Expand Down Expand Up @@ -444,6 +445,31 @@ void strongReferenceGauges() {
assertThat(registry.get("strong.ref").gauge().value()).isEqualTo(1.0);
}

@Test
@DisplayName("gauges cannot be registered twice")
void gaugesCannotBeRegisteredTwice() {
AtomicInteger n1 = registry.gauge("my.gauge", new AtomicInteger(1));
AtomicInteger n2 = registry.gauge("my.gauge", new AtomicInteger(2));

assertThat(registry.get("my.gauge").gauges()).hasSize(1);
assertThat(registry.get("my.gauge").gauge().value()).isEqualTo(1);
assertThat(n1).isNotNull().hasValue(1);
assertThat(n2).isNotNull().hasValue(2);
}

@Test
@DisplayName("gauges cannot be registered effectively twice")
void gaugesCannotBeRegisteredEffectivelyTwice() {
registry.config().meterFilter(MeterFilter.ignoreTags("ignored"));
AtomicInteger n1 = registry.gauge("my.gauge", Tags.of("ignored", "1"), new AtomicInteger(1));
AtomicInteger n2 = registry.gauge("my.gauge", Tags.of("ignored", "2"), new AtomicInteger(2));

assertThat(registry.get("my.gauge").gauges()).hasSize(1);
assertThat(registry.get("my.gauge").gauge().value()).isEqualTo(1);
assertThat(n1).isNotNull().hasValue(1);
assertThat(n2).isNotNull().hasValue(2);
}

}

@DisplayName("long task timers")
Expand Down