File tree 2 files changed +23
-1
lines changed
main/java/io/micrometer/core/instrument
test/java/io/micrometer/core/instrument
2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change 37
37
import java .util .concurrent .TimeUnit ;
38
38
import java .util .concurrent .atomic .AtomicBoolean ;
39
39
import java .util .function .*;
40
+ import java .util .stream .Collectors ;
40
41
41
42
import static java .lang .String .format ;
42
43
import static java .util .Collections .emptyList ;
63
64
*/
64
65
public abstract class MeterRegistry {
65
66
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
+
66
79
protected final Clock clock ;
67
80
68
81
private final Object meterMapLock = new Object ();
@@ -287,7 +300,7 @@ protected String getConventionName(Meter.Id id) {
287
300
protected abstract DistributionStatisticConfig defaultHistogramConfig ();
288
301
289
302
private String getBaseTimeUnitStr () {
290
- return getBaseTimeUnit (). toString (). toLowerCase ( );
303
+ return BASE_TIME_UNIT_STRING_CACHE . get ( getBaseTimeUnit () );
291
304
}
292
305
293
306
/**
Original file line number Diff line number Diff line change 15
15
*/
16
16
package io .micrometer .core .instrument ;
17
17
18
+ import io .micrometer .core .Issue ;
18
19
import io .micrometer .core .instrument .config .MeterFilter ;
19
20
import io .micrometer .core .instrument .config .MeterFilterReply ;
20
21
import io .micrometer .core .instrument .distribution .DistributionStatisticConfig ;
@@ -205,4 +206,12 @@ void shouldNotLetRegisteringMetersTwice() {
205
206
.hasNoCause ();
206
207
}
207
208
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
+
208
217
}
You can’t perform that action at this time.
0 commit comments