{"record":{"id":"ce77d96228f58c46","repo":"apache/hadoop","slug":"unexpected-metrics-type-for","errorCode":null,"errorMessage":"Unexpected metrics type {} for {}","messagePattern":"Unexpected metrics type (.+?) for (.+?)","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":321,"sourceCode":"  /**\n   * Create a mutable rate metric (for throughput measurement).\n   * @param name  of the metric\n   * @param desc  description\n   * @param extended  produce extended stat (stdev/min/max etc.) if true\n   * @return a new mutable rate metric object\n   */\n  public MutableRate newRate(String name, String desc, boolean extended) {\n    return newRate(name, desc, extended, true);\n  }\n\n  @InterfaceAudience.Private\n  public synchronized MutableRate newRate(String name, String desc,\n      boolean extended, boolean returnExisting) {\n    if (returnExisting) {\n      MutableMetric rate = metricsMap.get(name);\n      if (rate != null) {\n        if (rate instanceof MutableRate) return (MutableRate) rate;\n        throw new MetricsException(\"Unexpected metrics type \"+ rate.getClass()\n                                   +\" for \"+ name);\n      }\n    }\n    checkMetricName(name);\n    MutableRate ret = new MutableRate(name, desc, extended);\n    metricsMap.put(name, ret);\n    return ret;\n  }\n\n  public synchronized MutableRatesWithAggregation newRatesWithAggregation(\n      String name) {\n    checkMetricName(name);\n    MutableRatesWithAggregation rates = new MutableRatesWithAggregation();\n    metricsMap.put(name, rates);\n    return rates;\n  }\n\n  public synchronized MutableRollingAverages newMutableRollingAverages(","sourceCodeStart":303,"sourceCodeEnd":339,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MetricsRegistry.java#L303-L339","documentation":"MetricsRegistry.newRate(name, desc, extended, returnExisting=true) is the get-or-create path for rate metrics: an existing registration under that name is returned only if it is a MutableRate. If the name maps to a different MutableMetric subclass (counter, gauge, quantiles), it throws MetricsException('Unexpected metrics type <class> for <name>').","triggerScenarios":"Calling newRate(\"Foo\", ..., true) after newCounter/newGauge/newQuantiles already registered a non-rate metric named \"Foo\" in the same registry.","commonSituations":"Name collisions inside one @Metrics-annotated class or registry — e.g., a MutableStat field and an implicit newRate sharing a name; refactors that changed a metric's type while the old declaration stayed.","solutions":["Rename one of the two metrics so each name has one type within the registry","If a rate is intended, remove the earlier counter/gauge/quantiles registration of that name","Audit the annotated class or registry for duplicate metric names and give them distinct names"],"exampleFix":"// before\nregistry.newCounter(\"ProcTime\", \"Processing time\");\nregistry.newRate(\"ProcTime\", \"Processing time\", true);  // throws\n\n// after\nregistry.newCounter(\"ProcTimeCount\", \"Processing count\");\nregistry.newRate(\"ProcTime\", \"Processing time\", true);","handlingStrategy":"validation","validationCode":"Map<String, Class<?>> registered = new ConcurrentHashMap<>();\n// record every registration: registered.put(\"Foo\", MutableRate.class);\nClass<?> existing = registered.get(\"Foo\");\nif (existing == null || existing == MutableRate.class) {\n  registry.newRate(\"Foo\", \"desc\", true);\n}","typeGuard":null,"tryCatchPattern":"try {\n  registry.newRate(\"Foo\", \"desc\", true);\n} catch (MetricsException e) {\n  // \"Unexpected metrics type\" => name already claimed by a non-rate metric; rename\n  registry.newRate(\"FooRate\", \"desc\", true);\n}","preventionTips":["Keep one metric type per name within a registry — decide ownership up front","Track registered names and their kinds in the component that owns the registry","Grep annotated classes for duplicate @Metric names during code review"],"tags":["metrics2","hadoop","metric-name-collision","type-mismatch"],"backgroundTag":"metric-name-collision","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}