DropWizardMetricMaker: Don't fail when same metric name is defined

Since change Id45682c30 it is possible for plugins to define a custom
prefix for metric names. In the situation where multiple plugins define
metrics with the same name, due to those plugins sharing a common base
implementation, this causes an IllegalStateException with the message:

  metric prefix/metric-name already defined

Adjust the check to only throw IllegalStateException when the same metric
is defined with a different description.

Change-Id: I04602b93ee82c5348cc62ba669c740175e605d39
Signed-off-by: David Pursehouse <dpursehouse@collab.net>
This commit is contained in:
David Pursehouse
2017-02-07 13:53:12 +09:00
parent c3bbd56a24
commit e78672336c

View File

@@ -337,10 +337,15 @@ public class DropWizardMetricMaker extends MetricMaker {
private synchronized void define(String name, Description desc) {
if (descriptions.containsKey(name)) {
throw new IllegalStateException(String.format(
"metric %s already defined", name));
ImmutableMap<String, String> annotations = descriptions.get(name);
if (!desc.getAnnotations().get(Description.DESCRIPTION).equals(
annotations.get(Description.DESCRIPTION))) {
throw new IllegalStateException(String.format(
"metric %s already defined", name));
}
} else {
descriptions.put(name, desc.getAnnotations());
}
descriptions.put(name, desc.getAnnotations());
}
private static final Pattern METRIC_NAME_PATTERN = Pattern