Skip to content

Commit

Permalink
Log warning about gauge-re-registration
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatan-ivanov committed Nov 16, 2024
1 parent a0f4911 commit 4be104f
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micrometer.core.instrument;

import io.micrometer.common.lang.Nullable;
import io.micrometer.common.util.internal.logging.WarnThenDebugLogger;
import io.micrometer.core.annotation.Incubating;
import io.micrometer.core.instrument.Meter.Id;
import io.micrometer.core.instrument.config.MeterFilter;
Expand Down Expand Up @@ -64,6 +65,9 @@
*/
public abstract class MeterRegistry {

private static final WarnThenDebugLogger gaugeDoubleRegistrationLogger = new WarnThenDebugLogger(
MeterRegistry.class);

// @formatter:off
private static final EnumMap<TimeUnit, String> BASE_TIME_UNIT_STRING_CACHE = Arrays.stream(TimeUnit.values())
.collect(
Expand Down Expand Up @@ -648,12 +652,26 @@ private Meter getOrCreateMeter(@Nullable DistributionStatisticConfig config,
}
meterMap.put(mappedId, m);
}
else {
checkAndWarnAboutGaugeDoubleRegistration(m);
}
}
}
else {
checkAndWarnAboutGaugeDoubleRegistration(m);
}

return m;
}

private void checkAndWarnAboutGaugeDoubleRegistration(Meter meter) {
if (meter instanceof Gauge) {
gaugeDoubleRegistrationLogger.log(() -> String.format(
"This Gauge has been already registered (%s), the Gauge registration will be ignored.",
meter.getId()));
}
}

private boolean accept(Meter.Id id) {
for (MeterFilter filter : filters) {
MeterFilterReply reply = filter.accept(id);
Expand Down

0 comments on commit 4be104f

Please sign in to comment.