{"record":{"id":"cf37ada4c65ecea1","repo":"apache/hadoop","slug":"tag-already-exists","errorCode":null,"errorMessage":"Tag {} already exists!","messagePattern":"Tag (.+?) already exists!","errorType":"exception","errorClass":"MetricsException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MetricsRegistry.java","lineNumber":456,"sourceCode":"      char c = name.charAt(i);\n      if (Character.isWhitespace(c)) {\n        foundWhitespace = true;\n        break;\n      }\n    }\n    if (foundWhitespace) {\n      throw new MetricsException(\"Metric name '\"+ name +\n          \"' contains illegal whitespace character\");\n    }\n    // Check if name has already been registered\n    if (metricsMap.containsKey(name)) {\n      throw new MetricsException(\"Metric name \"+ name +\" already exists!\");\n    }\n  }\n\n  private void checkTagName(String name) {\n    if (tagsMap.containsKey(name)) {\n      throw new MetricsException(\"Tag \"+ name +\" already exists!\");\n    }\n  }\n\n  /**\n   * Sample all the mutable metrics and put the snapshot in the builder\n   * @param builder to contain the metrics snapshot\n   * @param all get all the metrics even if the values are not changed.\n   */\n  public synchronized void snapshot(MetricsRecordBuilder builder, boolean all) {\n    for (MetricsTag tag : tags()) {\n      builder.add(tag);\n    }\n    for (MutableMetric metric : metrics()) {\n      metric.snapshot(builder, all);\n    }\n  }\n\n  @Override","sourceCodeStart":438,"sourceCodeEnd":474,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MetricsRegistry.java#L438-L474","documentation":"MetricsRegistry.checkTagName enforces per-registry uniqueness for tags: registering a tag name that already exists in tagsMap throws MetricsException('Tag <name> already exists!').","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nregistry.tag(\"Context\", \"Context\", \"mysource\");\nregistry.tag(\"Context\", \"Context\", \"mysource\");  // throws\n\n// after\nregistry.tag(\"Context\", \"Context\", \"mysource\");\nregistry.tag(\"Hostname\", \"Hostname\", myHost);","handlingStrategy":"validation","validationCode":"Set<String> usedTags = ConcurrentHashMap.newKeySet();\nif (!usedTags.add(tagName)) {\n  throw new IllegalStateException(\"tag already registered: \" + tagName);\n}\nregistry.tag(tagName, desc, value);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["metrics2","hadoop","metric-name-collision","tags","naming"],"backgroundTag":"metric-name-collision","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}