{"record":{"id":"6da7d3140264f931","repo":"vectordotdev/vector","slug":"failed-type-coercion-self-is-not-a-metric","errorCode":null,"errorMessage":"Failed type coercion, {self:?} is not a metric","messagePattern":"Failed type coercion, (.+?) is not a metric","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/vector-core/src/event/mod.rs","lineNumber":180,"sourceCode":"    /// Return self as a `LogEvent` if possible\n    ///\n    /// If the event is a `LogEvent`, then `Some(&log_event)` is returned, otherwise `None`.\n    pub fn maybe_as_log(&self) -> Option<&LogEvent> {\n        match self {\n            Event::Log(log) => Some(log),\n            _ => None,\n        }\n    }\n\n    /// Return self as a `Metric`\n    ///\n    /// # Panics\n    ///\n    /// This function panics if self is anything other than an `Event::Metric`.\n    pub fn as_metric(&self) -> &Metric {\n        match self {\n            Event::Metric(metric) => metric,\n            _ => panic!(\"Failed type coercion, {self:?} is not a metric\"),\n        }\n    }\n\n    /// Return self as a mutable `Metric`\n    ///\n    /// # Panics\n    ///\n    /// This function panics if self is anything other than an `Event::Metric`.\n    pub fn as_mut_metric(&mut self) -> &mut Metric {\n        match self {\n            Event::Metric(metric) => metric,\n            _ => panic!(\"Failed type coercion, {self:?} is not a metric\"),\n        }\n    }\n\n    /// Coerces self into `Metric`\n    ///\n    /// # Panics","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/vectordotdev/vector/blob/bdb87aeaa4c4ff27c0ba643c1c77b21bf2ef4013/lib/vector-core/src/event/mod.rs#L162-L198","documentation":"`Event::as_metric` borrows the event as a `Metric` and panics if the variant is not `Event::Metric`. Metric pipelines guarantee their events are metrics, so the library encodes that invariant with a panic; the message dumps the offending event for diagnosis.","triggerScenarios":"Calling `event.as_metric()` on a `Log` or `Trace` event; seen in roundtrip assertions and transform_one paths in sinks/transforms that assume metric input.","commonSituations":"A log or trace source feeding a metrics-only transform/sink (miswired topology), test suites reusing a generic event builder, and codec/serializer code shared between event kinds.","solutions":["Check the variant first (`matches!(event, Event::Metric(_))`) before calling `as_metric`","Use the fallible accessor for generic code and handle the non-metric case","Fix routing so metrics-only components receive only metric streams"],"exampleFix":"// before\nlet metric = event.as_metric();\n// after\nlet metric = match &event {\n    Event::Metric(m) => m,\n    _ => return Ok(None),\n};","handlingStrategy":"type-guard","validationCode":"if !matches!(event, Event::Metric(_)) { return; }","typeGuard":"fn is_metric(e: &Event) -> bool { matches!(e, Event::Metric(_)) }","tryCatchPattern":null,"preventionTips":["Match on the Event enum before coercion"],"tags":["rust","panic","type-coercion","metric"],"backgroundTag":"type-mismatch","analyzedSha":"bdb87aeaa4c4ff27c0ba643c1c77b21bf2ef4013","analyzedAt":"2026-09-16T02:53:35.741Z","contentChangedAt":"2026-09-16T02:53:35.741Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}