{"record":{"id":"afb8b257c5aa96b2","repo":"vectordotdev/vector","slug":"keys-counts-were-unexpectedly-mismatched","errorCode":null,"errorMessage":"keys/counts were unexpectedly mismatched","messagePattern":"keys/counts were unexpectedly mismatched","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/vector-core/src/event/proto.rs","lineNumber":566,"sourceCode":"                    .unwrap_or(if pos { i16::MAX } else { i16::MIN })\n            })\n            .collect::<Vec<_>>();\n        let counts = sketch\n            .n\n            .into_iter()\n            .map(|n| n.try_into().unwrap_or(u16::MAX))\n            .collect::<Vec<_>>();\n        MetricSketch::AgentDDSketch(\n            AgentDDSketch::from_raw(\n                sketch.count,\n                sketch.min,\n                sketch.max,\n                sketch.sum,\n                sketch.avg,\n                &keys,\n                &counts,\n            )\n            .expect(\"keys/counts were unexpectedly mismatched\"),\n        )\n    }\n}\n\nimpl From<super::metadata::Secrets> for Secrets {\n    fn from(value: super::metadata::Secrets) -> Self {\n        Self {\n            entries: value.into_iter().map(|(k, v)| (k, v.to_string())).collect(),\n        }\n    }\n}\n\nimpl From<Secrets> for super::metadata::Secrets {\n    fn from(value: Secrets) -> Self {\n        let mut secrets = Self::new();\n        for (k, v) in value.entries {\n            secrets.insert(k, v);\n        }","sourceCodeStart":548,"sourceCodeEnd":584,"githubUrl":"https://github.com/vectordotdev/vector/blob/3708c39b12a93212ed8b8d7510b4cc7769cb5864/lib/vector-core/src/event/proto.rs#L548-L584","documentation":"When decoding a DDSketch-aggregated metric from Vector's protobuf representation, AgentDDSketch::from_raw is called with parallel keys/counts arrays and .expect(\"keys/counts were unexpectedly mismatched\") (lib/vector-core/src/event/proto.rs). from_raw validates that keys.len() == counts.len(); if the incoming metric sketch stores bins whose key and count vectors have different lengths, decoding panics.","triggerScenarios":"Receiving (via the event proto / v2v gRPC) an aggregated histogram sketch where the repeated keys field and counts field have unequal lengths — produced by a buggy or incompatible producer, hand-rolled proto emitters, or payload corruption. AgentDDSketch::from_raw returns Err on length mismatch and the expect converts it to a panic.","commonSituations":"Version skew between Vector nodes where the sketch proto layout changed; custom agents emitting Vector's metric proto with independently-written keys/counts loops; truncated/mangled payloads from proxies or buffers between Vector nodes.","solutions":["Align producer and consumer Vector versions so both sides agree on the sketch proto schema","If you emit the proto yourself, always build keys and counts from the same bins iteration so lengths match by construction","Validate incoming sketches (keys.len() == counts.len()) at the decode boundary and drop/skip malformed metrics instead of trusting them","Capture the offending payload and open an issue in vectordot/vector if stock Vector produced it"],"exampleFix":"// before\nMetricSketch::AgentDDSketch(\n    AgentDDSketch::from_raw(sketch.count, sketch.min, sketch.max, sketch.sum, sketch.avg, &keys, &counts)\n        .expect(\"keys/counts were unexpectedly mismatched\"),\n)\n\n// after: propagate the decode error\nlet sketch = AgentDDSketch::from_raw(sketch.count, sketch.min, sketch.max, sketch.sum, sketch.avg, &keys, &counts)\n    .map(MetricSketch::AgentDDSketch)\n    .map_err(|e| DecodeError::invalid_sketch(e))?;","handlingStrategy":"validation","validationCode":"// Before from_raw, check the invariant the function enforces\nif keys.len() != counts.len() {\n    return Err(DecodeError::invalid_sketch(\"keys/counts length mismatch\"));\n}","typeGuard":null,"tryCatchPattern":"// Handle from_raw's Result instead of expect\nmatch AgentDDSketch::from_raw(count, min, max, sum, avg, &keys, &counts) {\n    Ok(sketch) => MetricSketch::AgentDDSketch(sketch),\n    Err(e) => { warn!(error = %e, \"dropping malformed sketch\"); continue; }\n}","preventionTips":["Build keys and counts in one loop over the same bins when emitting the proto","Pin producer and consumer Vector versions in v2v pipelines","Treat length mismatch as data corruption: log, drop the metric, keep the pipeline alive"],"tags":["vector","protobuf","ddsketch","histogram","decode","data-corruption","panic"],"backgroundTag":"protobuf-decode-mismatch","analyzedSha":"3708c39b12a93212ed8b8d7510b4cc7769cb5864","analyzedAt":"2026-08-20T07:02:18.786Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}