Skip to content

Commit 12d3072

Browse files
Warn about gauge re-registration (#5688)
See gh-5616 See gh-5617
1 parent fe13573 commit 12d3072

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

benchmarks/benchmarks-core/src/jmh/java/io/micrometer/benchmark/core/CounterBenchmark.java

+10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ public void setup() {
5252
counter = registry.counter("counter");
5353
}
5454

55+
@Benchmark
56+
public void baseline() {
57+
// this method was intentionally left blank.
58+
}
59+
60+
@Benchmark
61+
public Counter retrieve() {
62+
return registry.counter("counter");
63+
}
64+
5565
@Benchmark
5666
public double countSum() {
5767
counter.increment();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2024 VMware, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micrometer.benchmark.core;
17+
18+
import java.util.concurrent.TimeUnit;
19+
import java.util.concurrent.atomic.AtomicInteger;
20+
21+
import io.micrometer.core.instrument.Gauge;
22+
import io.micrometer.core.instrument.MeterRegistry;
23+
import io.micrometer.prometheus.PrometheusConfig;
24+
import io.micrometer.prometheus.PrometheusMeterRegistry;
25+
import org.openjdk.jmh.annotations.*;
26+
import org.openjdk.jmh.runner.Runner;
27+
import org.openjdk.jmh.runner.RunnerException;
28+
import org.openjdk.jmh.runner.options.OptionsBuilder;
29+
30+
@Fork(1)
31+
@State(Scope.Benchmark)
32+
@BenchmarkMode(Mode.SampleTime)
33+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
34+
public class GaugeBenchmark {
35+
36+
private MeterRegistry registry;
37+
38+
private AtomicInteger stateObject;
39+
40+
@Setup
41+
public void setup() {
42+
registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
43+
stateObject = registry.gauge("test.gauge", new AtomicInteger());
44+
// emits warn because of double registration
45+
stateObject = registry.gauge("test.gauge", new AtomicInteger());
46+
// emits debug because of double registration and keeps emitting debug from now on
47+
stateObject = registry.gauge("test.gauge", new AtomicInteger());
48+
}
49+
50+
@Benchmark
51+
public AtomicInteger baseline() {
52+
stateObject = new AtomicInteger();
53+
return stateObject;
54+
}
55+
56+
@Benchmark
57+
public AtomicInteger gaugeReRegistrationWithoutBuilder() {
58+
stateObject = registry.gauge("test.gauge", new AtomicInteger());
59+
return stateObject;
60+
}
61+
62+
@Benchmark
63+
public Gauge gaugeReRegistrationWithBuilder() {
64+
stateObject = new AtomicInteger();
65+
return Gauge.builder("test.gauge", stateObject, AtomicInteger::doubleValue).register(registry);
66+
}
67+
68+
public static void main(String[] args) throws RunnerException {
69+
new Runner(new OptionsBuilder().include(GaugeBenchmark.class.getSimpleName()).build()).run();
70+
}
71+
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!--
2+
Copyright 2024 VMware, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<configuration>
17+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
18+
<encoder>
19+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
20+
</encoder>
21+
</appender>
22+
23+
<root level="warn">
24+
<appender-ref ref="STDOUT" />
25+
</root>
26+
</configuration>

micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java

+14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.micrometer.core.instrument;
1717

1818
import io.micrometer.common.lang.Nullable;
19+
import io.micrometer.common.util.internal.logging.WarnThenDebugLogger;
1920
import io.micrometer.core.annotation.Incubating;
2021
import io.micrometer.core.instrument.Meter.Id;
2122
import io.micrometer.core.instrument.config.MeterFilter;
@@ -64,6 +65,9 @@
6465
*/
6566
public abstract class MeterRegistry {
6667

68+
private static final WarnThenDebugLogger gaugeDoubleRegistrationLogger = new WarnThenDebugLogger(
69+
MeterRegistry.class);
70+
6771
// @formatter:off
6872
private static final EnumMap<TimeUnit, String> BASE_TIME_UNIT_STRING_CACHE = Arrays.stream(TimeUnit.values())
6973
.collect(
@@ -612,6 +616,7 @@ private Meter getOrCreateMeter(@Nullable DistributionStatisticConfig config,
612616
BiFunction<Id, /* Nullable Generic */ DistributionStatisticConfig, ? extends Meter> builder, Id originalId,
613617
Id mappedId, Function<Meter.Id, ? extends Meter> noopBuilder) {
614618
Meter m = meterMap.get(mappedId);
619+
checkAndWarnAboutGaugeDoubleRegistration(m);
615620

616621
if (m == null) {
617622
if (isClosed()) {
@@ -620,6 +625,7 @@ private Meter getOrCreateMeter(@Nullable DistributionStatisticConfig config,
620625

621626
synchronized (meterMapLock) {
622627
m = meterMap.get(mappedId);
628+
checkAndWarnAboutGaugeDoubleRegistration(m);
623629

624630
if (m == null) {
625631
if (!accept(mappedId)) {
@@ -654,6 +660,14 @@ private Meter getOrCreateMeter(@Nullable DistributionStatisticConfig config,
654660
return m;
655661
}
656662

663+
private void checkAndWarnAboutGaugeDoubleRegistration(Meter meter) {
664+
if (meter instanceof Gauge) {
665+
gaugeDoubleRegistrationLogger.log(() -> String.format(
666+
"This Gauge has been already registered (%s), the Gauge registration will be ignored.",
667+
meter.getId()));
668+
}
669+
}
670+
657671
private boolean accept(Meter.Id id) {
658672
for (MeterFilter filter : filters) {
659673
MeterFilterReply reply = filter.accept(id);

0 commit comments

Comments
 (0)