{"record":{"id":"e0c2f51239f4c895","repo":"apache/hadoop","slug":"unsupported-gauge-type","errorCode":null,"errorMessage":"Unsupported gauge type: {}","messagePattern":"Unsupported gauge type: (.+?)","errorType":"exception","errorClass":"MetricsException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MethodMetric.java","lineNumber":122,"sourceCode":"  }\n\n  MutableMetric newGauge(final Class<?> t) {\n    if (isInt(t) || isLong(t) || isFloat(t) || isDouble(t)) {\n      return new MutableMetric() {\n        @Override public void snapshot(MetricsRecordBuilder rb, boolean all) {\n          try {\n            Object ret = method.invoke(obj, (Object[]) null);\n            if (isInt(t)) rb.addGauge(info, ((Integer) ret).intValue());\n            else if (isLong(t)) rb.addGauge(info, ((Long) ret).longValue());\n            else if (isFloat(t)) rb.addGauge(info, ((Float) ret).floatValue());\n            else rb.addGauge(info, ((Double) ret).doubleValue());\n          } catch (Exception ex) {\n            LOG.error(\"Error invoking method \"+ method.getName(), ex);\n          }\n        }\n      };\n    }\n    throw new MetricsException(\"Unsupported gauge type: \"+ t.getName());\n  }\n\n  MutableMetric newTag(Class<?> resType) {\n    if (resType == String.class) {\n      return new MutableMetric() {\n        @Override public void snapshot(MetricsRecordBuilder rb, boolean all) {\n          try {\n            Object ret = method.invoke(obj, (Object[]) null);\n            rb.tag(info, (String) ret);\n          } catch (Exception ex) {\n            LOG.error(\"Error invoking method \"+ method.getName(), ex);\n          }\n        }\n      };\n    }\n    throw new MetricsException(\"Unsupported tag type: \"+ resType.getName());\n  }\n","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MethodMetric.java#L104-L140","documentation":"MethodMetric.newGauge builds the implementation for an @Metric-annotated method with type GAUGE; supported return types are int/Integer, long/Long, float/Float, and double/Double (everything else in the supported chain falls back to Double). Any other type — String, boolean, BigDecimal — falls through to MetricsException('Unsupported gauge type: <class name>').","triggerScenarios":"@Metric (default type GAUGE, or Type.GAUGE) on a method returning String, boolean, java.math.BigDecimal, or any non-numeric type.","commonSituations":"Annotating convenience getters (String labels, boolean flags) as gauges; BigDecimal/Number subclasses from domain code used directly as metric methods.","solutions":["Change the method's return type to one of int, long, float, double","For boolean/string values, expose them as tags (String-returning method) instead of gauges","Convert the value inside the method (e.g., return bigDecimal.doubleValue())"],"exampleFix":"// before\n@Metric\npublic String queueSizeLabel() { return labels.get(\"q1\"); }\n\n// after\n@Metric\npublic long queueSize() { return sizes.get(\"q1\"); }","handlingStrategy":"type-guard","validationCode":"for (Method m : cls.getDeclaredMethods()) {\n  Metric ann = m.getAnnotation(Metric.class);\n  if (ann != null && !isGaugeType(m.getReturnType())) {\n    throw new IllegalStateException(\"@Metric gauge on unsupported type: \" + m);\n  }\n}","typeGuard":"static boolean isGaugeType(Class<?> t) {\n  return t == int.class || t == Integer.class\n      || t == long.class || t == Long.class\n      || t == float.class || t == Float.class\n      || t == double.class || t == Double.class;\n}","tryCatchPattern":null,"preventionTips":["Return primitives (int/long/float/double) from gauge methods; never String or boolean","Convert BigDecimal/Number inside the method before returning","Scan annotated methods with isGaugeType in a startup test"],"tags":["metrics2","hadoop","annotation","type-mismatch","reflection"],"backgroundTag":"unsupported-metric-return-type","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}