Skip to content

Commit 14b2518

Browse files
Merge branch '1.12.x' into 1.13.x
2 parents abc9567 + 12d3072 commit 14b2518

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

Lines changed: 10 additions & 0 deletions
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();
Lines changed: 72 additions & 0 deletions
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 io.micrometer.core.instrument.Gauge;
19+
import io.micrometer.core.instrument.MeterRegistry;
20+
import io.micrometer.prometheusmetrics.PrometheusConfig;
21+
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry;
22+
import org.openjdk.jmh.annotations.*;
23+
import org.openjdk.jmh.runner.Runner;
24+
import org.openjdk.jmh.runner.RunnerException;
25+
import org.openjdk.jmh.runner.options.OptionsBuilder;
26+
27+
import java.util.concurrent.TimeUnit;
28+
import java.util.concurrent.atomic.AtomicInteger;
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+
}
Lines changed: 26 additions & 0 deletions
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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.micrometer.common.lang.Nullable;
1919
import io.micrometer.common.util.internal.logging.InternalLogger;
2020
import io.micrometer.common.util.internal.logging.InternalLoggerFactory;
21+
import io.micrometer.common.util.internal.logging.WarnThenDebugLogger;
2122
import io.micrometer.core.annotation.Incubating;
2223
import io.micrometer.core.instrument.Meter.Id;
2324
import io.micrometer.core.instrument.config.MeterFilter;
@@ -66,6 +67,9 @@
6667
*/
6768
public abstract class MeterRegistry {
6869

70+
private static final WarnThenDebugLogger gaugeDoubleRegistrationLogger = new WarnThenDebugLogger(
71+
MeterRegistry.class);
72+
6973
// @formatter:off
7074
private static final EnumMap<TimeUnit, String> BASE_TIME_UNIT_STRING_CACHE = Arrays.stream(TimeUnit.values())
7175
.collect(
@@ -630,6 +634,7 @@ private Meter getOrCreateMeter(@Nullable DistributionStatisticConfig config,
630634

631635
Meter m = preFilterIdToMeterMap.get(originalId);
632636
if (m != null && !isStaleId(originalId)) {
637+
checkAndWarnAboutGaugeDoubleRegistration(m);
633638
return m;
634639
}
635640

@@ -642,6 +647,7 @@ private Meter getOrCreateMeter(@Nullable DistributionStatisticConfig config,
642647
if (isStaleId(originalId)) {
643648
unmarkStaleId(originalId);
644649
}
650+
checkAndWarnAboutGaugeDoubleRegistration(m);
645651
}
646652
else {
647653
if (isClosed()) {
@@ -699,6 +705,14 @@ private boolean unmarkStaleId(Id originalId) {
699705
return !stalePreFilterIds.isEmpty() && stalePreFilterIds.remove(originalId);
700706
}
701707

708+
private void checkAndWarnAboutGaugeDoubleRegistration(Meter meter) {
709+
if (meter instanceof Gauge) {
710+
gaugeDoubleRegistrationLogger.log(() -> String.format(
711+
"This Gauge has been already registered (%s), the Gauge registration will be ignored.",
712+
meter.getId()));
713+
}
714+
}
715+
702716
private boolean accept(Meter.Id id) {
703717
for (MeterFilter filter : filters) {
704718
MeterFilterReply reply = filter.accept(id);

0 commit comments

Comments
 (0)