(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:
Ruth Linehan 2016-04-22 11:17:46 -07:00
parent b744e99749
commit 19b9208094

View file

@ -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,