{"record":{"id":"c010211171ba51c9","repo":"apache/hadoop","slug":"metric-name-already-exists","errorCode":null,"errorMessage":"Metric name {} already exists!","messagePattern":"Metric name (.+?) 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":450,"sourceCode":"  }\n\n  private void checkMetricName(String name) {\n    // Check for invalid characters in metric name\n    boolean foundWhitespace = false;\n    for (int i = 0; i < name.length(); i++) {\n      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    }","sourceCodeStart":432,"sourceCodeEnd":468,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MetricsRegistry.java#L432-L468","documentation":"The second half of checkMetricName: registering a metric name that already exists in this registry's metricsMap throws MetricsException('Metric name <name> already exists!'). Every new* factory method in MetricsRegistry enforces per-registry name uniqueness for metrics.","triggerScenarios":"Two new* calls with the same name on the same MetricsRegistry — e.g., @Metric(name=\"X\") on two members, or manual newRate(\"X\") after newCounter(\"X\").","commonSituations":"Copy-pasted metric declarations; a field metric and a method metric sharing a name; repeated component init writing into one shared registry.","solutions":["Re-use the existing metric object instead of registering the name again","Rename the second metric so names stay unique within the registry","Re-create the registry on re-init rather than re-registering the same names"],"exampleFix":"// before\nregistry.newCounter(\"Files\", \"Files\", 0L);\nregistry.newRate(\"Files\", \"Files\");  // throws: name exists\n\n// after\nregistry.newCounter(\"Files\", \"Files\", 0L);\nregistry.newRate(\"FilesRate\", \"Files rate\");","handlingStrategy":"validation","validationCode":"Set<String> usedNames = ConcurrentHashMap.newKeySet();\nif (!usedNames.add(name)) {\n  throw new IllegalStateException(\"metric name already registered: \" + name);\n}\nregistry.newRate(name, desc);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Register each name once per registry and reuse the metric object afterwards","Centralize metric creation in one factory so duplicates are caught immediately","Re-create the registry on re-init instead of re-registering the same names"],"tags":["metrics2","hadoop","metric-name-collision","naming"],"backgroundTag":"metric-name-collision","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}