apache/hadoop · error · MetricsException

Tag already exists!

Error message

Tag {} already exists!

What it means

MetricsException thrown by MetricsRegistry.checkTagName() when a tag with the same name is added twice to the same metrics registry (e.g., two @Tag-annotated fields or two addTag calls with an identical name in one MetricsSource/record). Tags must be uniquely named within a registry. Fix: rename one of the duplicate tags, or reuse the existing tag instead of re-registering it. Commonly surfaces at startup of a MetricsSource whose annotation-defined tags collide, or when dynamic tag creation is called repeatedly with the same key.

Solutions

  1. Set each tag once per registry and reuse the existing value
  2. Give the second tag a different name
  3. 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

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


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/cf37ada4c65ecea1. Report an issue: GitHub.

Appendix: 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);
    }
  }

  @Override

View on GitHub (pinned to 2add963021)