(TK-316) Use upstream implementation logic for getOrAddTimer
We are subclassing `Timer` to create our own `ClientTimer` instances. Unfortunately, the method we would like to use to register these on the MetricRegistry, `getOrAdd()`, is private. Instead, we have to have our own `getOrAddTimer()` to handle getting the timer if it has already been registered, or registering a new one. This commit updates our implementation of `getOrAddTimer()` to match the logic of `getOrAdd()`.
This commit is contained in:
parent
b744e99749
commit
19b9208094
1 changed files with 17 additions and 11 deletions
|
@ -32,18 +32,24 @@ public class Metrics {
|
|||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Metrics.class);
|
||||
|
||||
synchronized private static ClientTimer getOrAddTimer(MetricRegistry metricRegistry,
|
||||
String name, ClientTimer newTimer) {
|
||||
Metric timer = metricRegistry.getMetrics().get(name);
|
||||
|
||||
if ( timer == null ) {
|
||||
return metricRegistry.register(name, newTimer);
|
||||
} else if ( ClientTimer.class.isInstance(timer) ) {
|
||||
return (ClientTimer) timer;
|
||||
} else {
|
||||
throw new IllegalArgumentException(name +
|
||||
" is already used for a different type of metric");
|
||||
private static ClientTimer getOrAddTimer(MetricRegistry metricRegistry,
|
||||
String name,
|
||||
ClientTimer newTimer) {
|
||||
final Map<String, Metric> metrics = metricRegistry.getMetrics();
|
||||
final Metric metric = metrics.get(name);
|
||||
if ( metric instanceof ClientTimer ) {
|
||||
return (ClientTimer) metric;
|
||||
} else if ( metric == null ) {
|
||||
try {
|
||||
return metricRegistry.register(name, newTimer);
|
||||
} catch (IllegalArgumentException e) {
|
||||
final Metric added = metricRegistry.getMetrics().get(name);
|
||||
if ( added instanceof ClientTimer ) {
|
||||
return (ClientTimer) added;
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException(name +" is already used for a different type of metric");
|
||||
}
|
||||
|
||||
private static ArrayList<Timer.Context> startBytesReadMetricIdTimers(MetricRegistry registry,
|
||||
|
|
Loading…
Reference in a new issue