apache/hadoop · error · MetricsException
Tag {} already exists!
Error message
Tag {} already exists! What it means
MetricsRegistry.checkTagName enforces per-registry uniqueness for tags: registering a tag name that already exists in tagsMap throws MetricsException('Tag <name> already exists!').
Source
Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MetricsRegistry.java:456
char c = name.charAt(i);
if (Character.isWhitespace(c)) {
foundWhitespace = true;
break;
}
}
if (foundWhitespace) {
throw new MetricsException("Metric name '"+ name +
"' contains illegal whitespace character");
}
// Check if name has already been registered
if (metricsMap.containsKey(name)) {
throw new MetricsException("Metric name "+ name +" already exists!");
}
}
private void checkTagName(String name) {
if (tagsMap.containsKey(name)) {
throw new MetricsException("Tag "+ name +" already exists!");
}
}
/**
* Sample all the mutable metrics and put the snapshot in the builder
* @param builder to contain the metrics snapshot
* @param all get all the metrics even if the values are not changed.
*/
public synchronized void snapshot(MetricsRecordBuilder builder, boolean all) {
for (MetricsTag tag : tags()) {
builder.add(tag);
}
for (MutableMetric metric : metrics()) {
metric.snapshot(builder, all);
}
}
@OverrideView on GitHub (pinned to 2add963021)
Solutions
- Set each tag once per registry and reuse the existing value
- Give the second tag a different name
- Re-create the registry instead of re-declaring tags into it
Example fix
// before
registry.tag("Context", "Context", "mysource");
registry.tag("Context", "Context", "mysource"); // throws
// after
registry.tag("Context", "Context", "mysource");
registry.tag("Hostname", "Hostname", myHost); Defensive patterns
Strategy: validation
Validate before calling
Set<String> usedTags = ConcurrentHashMap.newKeySet();
if (!usedTags.add(tagName)) {
throw new IllegalStateException("tag already registered: " + tagName);
}
registry.tag(tagName, desc, value); Prevention
- Set context and other tags once per registry at construction
- Guard re-init paths so tag declarations are not replayed into a live registry
- Prefer one owner class for the registry and its tags
When it happens
Trigger: Registering two tags with the same name in one registry — e.g., registry.tag("Context", ..., "mysource") followed by another tag("Context", ...) on the same registry.
Common situations: Re-initializing a component that re-declares its context/context-record tags into a shared registry; copy-pasted tag declarations; merging two metric classes into one registry.
Related errors
- Metric name {} already exists!
- Unexpected metrics type {} for {}
- Unsupported add(value) for metric {}
- Metric name '{}' contains illegal whitespace character
- Illegal tag pattern: {}
AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22).
Data as JSON: /api/errors/cf37ada4c65ecea1.
Report an issue: GitHub.