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 36
36
import java .util .concurrent .TimeUnit ;
37
37
import java .util .concurrent .atomic .AtomicBoolean ;
38
38
import java .util .function .*;
39
+ import java .util .stream .Collectors ;
39
40
40
41
import static java .lang .String .format ;
41
42
import static java .util .Collections .emptyList ;
59
60
*/
60
61
public abstract class MeterRegistry {
61
62
63
+ // @formatter:off
64
+ private static final EnumMap <TimeUnit , String > BASE_TIME_UNIT_STRING_CACHE = Arrays .stream (TimeUnit .values ())
65
+ .collect (
66
+ Collectors .toMap (
67
+ Function .identity (),
68
+ (timeUnit ) -> timeUnit .toString ().toLowerCase (),
69
+ (k , v ) -> { throw new IllegalStateException ("Duplicate keys should not exist." ); },
70
+ () -> new EnumMap <>(TimeUnit .class )
71
+ )
72
+ );
73
+ // @formatter:on
74
+
62
75
protected final Clock clock ;
63
76
64
77
private final Object meterMapLock = new Object ();
@@ -280,7 +293,7 @@ protected String getConventionName(Meter.Id id) {
280
293
protected abstract DistributionStatisticConfig defaultHistogramConfig ();
281
294
282
295
private String getBaseTimeUnitStr () {
283
- return getBaseTimeUnit (). toString (). toLowerCase ( );
296
+ return BASE_TIME_UNIT_STRING_CACHE . get ( getBaseTimeUnit () );
284
297
}
285
298
286
299
/**
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