{"id":"0590ab10593f857b","repo":"apache/kafka","slug":"keyvalue-needs-to-be-specified-in-pairs","errorCode":null,"errorMessage":"keyValue needs to be specified in pairs","messagePattern":"keyValue needs to be specified in pairs","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/common/metrics/internals/MetricsUtils.java","lineNumber":59,"sourceCode":"                return timeMs / (60.0 * 1000.0);\n            case HOURS:\n                return timeMs / (60.0 * 60.0 * 1000.0);\n            case DAYS:\n                return timeMs / (24.0 * 60.0 * 60.0 * 1000.0);\n            default:\n                throw new IllegalStateException(\"Unknown unit: \" + unit);\n        }\n    }\n\n    /**\n     * Create a set of tags using the supplied key and value pairs. The order of the tags will be kept.\n     *\n     * @param keyValue the key and value pairs for the tags; must be an even number\n     * @return the map of tags that can be supplied to the {@link Metrics} methods; never null\n     */\n    public static Map<String, String> getTags(String... keyValue) {\n        if ((keyValue.length % 2) != 0)\n            throw new IllegalArgumentException(\"keyValue needs to be specified in pairs\");\n        Map<String, String> tags = new LinkedHashMap<>(keyValue.length / 2);\n\n        for (int i = 0; i < keyValue.length; i += 2)\n            tags.put(keyValue[i], keyValue[i + 1]);\n        return tags;\n    }\n}\n","sourceCodeStart":41,"sourceCodeEnd":67,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/common/metrics/internals/MetricsUtils.java#L41-L67","documentation":"Thrown by MetricsUtils.getTags(String... keyValue) when the varargs array length is odd. The method pairs consecutive entries as (key, value, key, value, ...) to build an ordered LinkedHashMap of tags; an odd count leaves a dangling key with no value, which the library treats as a programmer error and rejects with IllegalArgumentException.","triggerScenarios":"Calling MetricsUtils.getTags(\"k1\", \"v1\", \"k2\") or any invocation where the number of string arguments is not even. Reached wherever tag maps are built from a flat varargs list before constructing a MetricName.","commonSituations":"A typo or trailing comma in a varargs tag list; dynamically building the tag array from a collection and forgetting the last value; refactor that appended a key without its value; copy-paste of a tag pair with one element dropped.","solutions":["Ensure the getTags(...) call always passes an even number of arguments: every key immediately followed by its value.","When building the array dynamically from a Map, interleave keys and values in iteration and assert the result length is even.","Replace the varargs call with a direct Map.of(...) or LinkedHashMap construction if the pairs are static.","Add a unit test asserting getTags(\"k\",\"v\",\"k2\",\"v2\") returns the expected map to catch regressions."],"exampleFix":"// before\nMap<String,String> tags = MetricsUtils.getTags(\"broker\", \"1\", \"client\");\n\n// after\nMap<String,String> tags = MetricsUtils.getTags(\"broker\", \"1\", \"client\", \"producer-1\");","handlingStrategy":"validation","validationCode":"if (keyValue == null || keyValue.length % 2 != 0) {\n    throw new IllegalArgumentException(\"keyValue must be an even-length key/value sequence\");\n}\nMap<String, String> tags = MetricsUtils.getTags(keyValue);","typeGuard":"static boolean isEvenKeyValuePairs(String[] keyValue) {\n    return keyValue != null && keyValue.length % 2 == 0;\n}","tryCatchPattern":null,"preventionTips":["Pass tags as literal key,value,key,value pairs and double-check the count.","Prefer building a Map and using the Map-based metricName overloads over hand-rolled varargs.","Wrap tag construction in a helper that asserts even length once at the call site."],"tags":["metrics","tags","input-validation","varargs"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}