{"record":{"id":"4a8ebe1f841f957e","repo":"apache/hadoop","slug":"metric-name-contains-illegal-whitespace-chara","errorCode":null,"errorMessage":"Metric name '{}' contains illegal whitespace character","messagePattern":"Metric name '(.+?)' contains illegal whitespace character","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":445,"sourceCode":"    return tagsMap.values();\n  }\n\n  Collection<MutableMetric> metrics() {\n    return metricsMap.values();\n  }\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.","sourceCodeStart":427,"sourceCodeEnd":463,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MetricsRegistry.java#L427-L463","documentation":"MetricsRegistry.checkMetricName runs before every registration and rejects any name containing a whitespace character (Character.isWhitespace), throwing MetricsException(\"Metric name '<name>' contains illegal whitespace character\") even before the duplicate check runs.","triggerScenarios":"Any registry factory call (newCounter, newGauge, newRate, newQuantiles, ...) with a name containing a space, tab, or newline — e.g., newRate(\"Num Ops\").","commonSituations":"Human-readable labels pasted in as metric names; names built by string concatenation that accidentally include spaces; config-provided names with leading/trailing whitespace.","solutions":["Strip or replace whitespace from the name before registering it","Use camelCase identifiers for names and put the pretty label in the description","Sanitize config-sourced metric names once at startup"],"exampleFix":"// before\nregistry.newRate(\"Num Ops\", \"Number of operations\");\n\n// after\nregistry.newRate(\"NumOps\", \"Number of operations\");","handlingStrategy":"validation","validationCode":"static String sanitizeMetricName(String name) {\n  return name == null ? null : name.replaceAll(\"\\\\s+\", \"\");\n}\n// before every registration:\nString safe = sanitizeMetricName(name);\nif (!safe.equals(name)) throw new IllegalArgumentException(\"whitespace in metric name: \" + name);","typeGuard":"static boolean isValidMetricName(String name) {\n  return name != null && !name.isEmpty() && name.chars().noneMatch(Character::isWhitespace);\n}","tryCatchPattern":null,"preventionTips":["Use camelCase identifiers as metric names and put pretty labels in descriptions","Sanitize config-sourced names once at startup","Assert isValidMetricName in tests for every registry factory call"],"tags":["metrics2","hadoop","naming","argument-validation"],"backgroundTag":"invalid-metric-name","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}