File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed
main/java/io/micrometer/core/instrument
test/java/io/micrometer/core/instrument Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change 3737import java .util .concurrent .TimeUnit ;
3838import java .util .concurrent .atomic .AtomicBoolean ;
3939import java .util .function .*;
40+ import java .util .stream .Collectors ;
4041
4142import static java .lang .String .format ;
4243import static java .util .Collections .emptyList ;
6364 */
6465public abstract class MeterRegistry {
6566
67+ // @formatter:off
68+ private static final EnumMap <TimeUnit , String > BASE_TIME_UNIT_STRING_CACHE = Arrays .stream (TimeUnit .values ())
69+ .collect (
70+ Collectors .toMap (
71+ Function .identity (),
72+ (timeUnit ) -> timeUnit .toString ().toLowerCase (),
73+ (k , v ) -> { throw new IllegalStateException ("Duplicate keys should not exist." ); },
74+ () -> new EnumMap <>(TimeUnit .class )
75+ )
76+ );
77+ // @formatter:on
78+
6679 protected final Clock clock ;
6780
6881 private final Object meterMapLock = new Object ();
@@ -287,7 +300,7 @@ protected String getConventionName(Meter.Id id) {
287300 protected abstract DistributionStatisticConfig defaultHistogramConfig ();
288301
289302 private String getBaseTimeUnitStr () {
290- return getBaseTimeUnit (). toString (). toLowerCase ( );
303+ return BASE_TIME_UNIT_STRING_CACHE . get ( getBaseTimeUnit () );
291304 }
292305
293306 /**
Original file line number Diff line number Diff line change 1515 */
1616package io .micrometer .core .instrument ;
1717
18+ import io .micrometer .core .Issue ;
1819import io .micrometer .core .instrument .config .MeterFilter ;
1920import io .micrometer .core .instrument .config .MeterFilterReply ;
2021import io .micrometer .core .instrument .distribution .DistributionStatisticConfig ;
@@ -205,4 +206,12 @@ void shouldNotLetRegisteringMetersTwice() {
205206 .hasNoCause ();
206207 }
207208
209+ @ Test
210+ @ Issue ("#4352" )
211+ void baseUnitStringShouldBeCachedAndReturnTheSameInstance () {
212+ Timer timer1 = registry .timer ("test.timer1" );
213+ Timer timer2 = registry .timer ("test.timer2" );
214+ assertThat (timer1 .getId ().getBaseUnit ()).isSameAs (timer2 .getId ().getBaseUnit ());
215+ }
216+
208217}
You can’t perform that action at this time.
0 commit comments