{"record":{"id":"4ef32c6458004dc6","repo":"alibaba/Sentinel","slug":"incorrect-number-of-labels","errorCode":null,"errorMessage":"Incorrect number of labels.","messagePattern":"Incorrect number of labels\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sentinel-extension/sentinel-prometheus-metric-exporter/src/main/java/com/alibaba/csp/sentinel/metric/prom/types/GaugeMetricFamily.java","lineNumber":53,"sourceCode":"    public GaugeMetricFamily addMetric(List<String> labelValues, double value, long timestampMs) {\n        if (labelValues.size() != labelNames.size()) {\n            throw new IllegalArgumentException(\"Incorrect number of labels.\");\n        }\n        samples.add(new Sample(name, labelNames, labelValues, value, timestampMs));\n        return this;\n    }","sourceCodeStart":null,"sourceCodeEnd":null,"githubUrl":"https://github.com/alibaba/Sentinel/blob/a3f40ba8e900c8489bd520274739f17235a7721c/sentinel-prometheus-metric-exporter/src/main/java/com/alibaba/csp/sentinel/metric/prom/types/GaugeMetricFamily.java#L53","documentation":"GaugeMetricFamily (Sentinel's Prometheus exporter model, mirroring prometheus client_simpleclient's GaugeMetricFamily) requires that every sample's label values match the label names declared when the family was constructed. addMetric throws IllegalArgumentException(\"Incorrect number of labels.\") when labelValues.size() != labelNames.size(), because a Prometheus exposition-format sample must have exactly one value per declared label.","triggerScenarios":"Calling new GaugeMetricFamily(name, help, Arrays.asList(\"resource\",\"passQps\")) and then addMetric(Collections.singletonList(\"myResource\"), 1.0, ts) — one label value for two label names throws.","commonSituations":"Writing a custom Collector for the sentinel-prometheus-metric-exporter module; refactoring label sets (adding a label name without updating every addMetric call site); conditional label lists where a branch supplies fewer values.","solutions":["Compare sizes before adding: ensure labelValues.size() == the labelNames list passed to the constructor","Centralize the label name list in a constant and derive label value lists at the same place so they cannot drift","If a label is not applicable, pass an empty string (\"\"), not a missing value — Prometheus allows empty label values"],"exampleFix":"// before\nGaugeMetricFamily f = new GaugeMetricFamily(\"sentinel_flow\", \"help\", List.of(\"resource\"));\nf.addMetric(List.of(\"foo\", \"bar\"), 1.0, ts); // 2 values, 1 name -> throws\n\n// after\nGaugeMetricFamily f = new GaugeMetricFamily(\"sentinel_flow\", \"help\", List.of(\"resource\"));\nf.addMetric(List.of(\"foo\"), 1.0, ts);","handlingStrategy":"validation","validationCode":"if (labelValues.size() != LABEL_NAMES.size()) {\n    throw new IllegalStateException(\"expected \" + LABEL_NAMES.size() + \" labels, got \" + labelValues.size());\n}\nfamily.addMetric(labelValues, value, timestampMs);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Define label name lists as constants shared by constructor and addMetric call sites","Use empty string for absent labels rather than dropping the value"],"tags":["sentinel","prometheus","metrics","validation"],"backgroundTag":null,"analyzedSha":"a3f40ba8e900c8489bd520274739f17235a7721c","analyzedAt":"2026-08-14T11:10:30.678Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}