{"id":"d1e959b811dbca57","repo":"apache/kafka","slug":"meter-is-supported-only-for-windowedcount-or-windo","errorCode":null,"errorMessage":"Meter is supported only for WindowedCount or WindowedSum.","messagePattern":"Meter is supported only for WindowedCount or WindowedSum\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/common/metrics/stats/Meter.java","lineNumber":65,"sourceCode":"     * Construct a Meter with provided time unit\n     */\n    public Meter(TimeUnit unit, MetricName rateMetricName, MetricName totalMetricName) {\n        this(unit, new WindowedSum(), rateMetricName, totalMetricName);\n    }\n\n    /**\n     * Construct a Meter with seconds as time unit\n     */\n    public Meter(SampledStat rateStat, MetricName rateMetricName, MetricName totalMetricName) {\n        this(TimeUnit.SECONDS, rateStat, rateMetricName, totalMetricName);\n    }\n\n    /**\n     * Construct a Meter with provided time unit\n     */\n    public Meter(TimeUnit unit, SampledStat rateStat, MetricName rateMetricName, MetricName totalMetricName) {\n        if (!(rateStat instanceof WindowedSum)) {\n            throw new IllegalArgumentException(\"Meter is supported only for WindowedCount or WindowedSum.\");\n        }\n        this.total = new CumulativeSum();\n        this.rate = new Rate(unit, rateStat);\n        this.rateMetricName = rateMetricName;\n        this.totalMetricName = totalMetricName;\n    }\n\n    @Override\n    public List<NamedMeasurable> stats() {\n        return Arrays.asList(\n            new NamedMeasurable(totalMetricName, total),\n            new NamedMeasurable(rateMetricName, rate));\n    }\n\n    @Override\n    public void record(MetricConfig config, double value, long timeMs) {\n        rate.record(config, value, timeMs);\n        // Total metrics with Count stat should record 1.0 (as recorded in the count)","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/common/metrics/stats/Meter.java#L47-L83","documentation":"Thrown by the Meter constructor when the supplied `rateStat` is not a `WindowedSum` (which includes its subclass `WindowedCount`). Meter pairs a windowed rate with a `CumulativeSum` total, and its `record()` method branches on `rate.stat instanceof WindowedCount` to decide whether to record the event count (1.0) or the recorded value. Any other SampledStat (Avg, Max, Percentiles, etc.) would break that contract, so it is rejected.","triggerScenarios":"Calling `new Meter(unit, rateStat, rateMetricName, totalMetricName)` or `new Meter(rateStat, rateMetricName, totalMetricName)` with `rateStat` that is not a WindowedSum/WindowedCount (e.g. `new Avg()`, `new Max()`, a `Percentiles` instance).","commonSituations":"A developer tries to build a 'rate of average latency' or 'rate of max' meter by passing a different SampledStat. Copy-pasting Meter construction from sample code that used WindowedSum and substituting another stat. Misreading the message — it says 'WindowedCount or WindowedSum' because WindowedCount extends WindowedSum, so the instanceof check covers both.","solutions":["Pass `new WindowedSum()` (default; used by the convenience constructors `new Meter(rateName, totalName)`).","Pass `new WindowedCount()` if you want event-count rate rather than sum-of-values rate.","If you actually need a rate of Avg/Max/Percentile, register those as separate Sensor stats and compute rate manually — do not wrap them in a Meter."],"exampleFix":"// before\nnew Meter(new Avg(), rateName, totalName);\n\n// after\nnew Meter(new WindowedSum(), rateName, totalName);\n// or for event-count rate:\nnew Meter(new WindowedCount(), rateName, totalName);","handlingStrategy":"type-guard","validationCode":"if (!(rateStat instanceof WindowedSum)) {\n    throw new IllegalArgumentException(\n        \"Meter rateStat must be WindowedSum or WindowedCount, got \" + rateStat.getClass());\n}\nnew Meter(unit, rateStat, rateMetricName, totalMetricName);","typeGuard":"static SampledStat asMeterable(SampledStat s) {\n    if (s instanceof WindowedSum) return s;\n    throw new IllegalArgumentException(\n        \"Meter requires WindowedSum/WindowedCount, got \" + s.getClass());\n}","tryCatchPattern":"try {\n    new Meter(unit, rateStat, rateMetricName, totalMetricName);\n} catch (IllegalArgumentException e) {\n    // \"Meter is supported only for WindowedCount or WindowedSum.\"\n    new Meter(unit, new WindowedSum(), rateMetricName, totalMetricName);\n}","preventionTips":["Prefer the no-rateStat Meter constructors (they default to WindowedSum) unless you specifically need WindowedCount.","If you need count semantics, pass `new WindowedCount()` explicitly rather than an arbitrary SampledStat subclass.","Never reuse a Rate/Avg/Max SampledStat instance as a Meter's rateStat; only WindowedSum/WindowedCount are accepted."],"tags":["metrics","meter","rate","windowedsum","validation","java"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}