{"record":{"id":"b9ba9b942102c0fb","repo":"vectordotdev/vector","slug":"failed-type-coercion-self-is-not-a-metric-ref","errorCode":null,"errorMessage":"Failed type coercion, {self:?} is not a metric reference","messagePattern":"Failed type coercion, (.+?) is not a metric reference","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/vector-core/src/event/ref.rs","lineNumber":52,"sourceCode":"    /// # Panics\n    ///\n    /// This will panic if this is not a `LogEvent` reference.\n    pub fn into_log(self) -> LogEvent {\n        match self {\n            Self::Log(log) => log.clone(),\n            _ => panic!(\"Failed type coercion, {self:?} is not a log reference\"),\n        }\n    }\n\n    /// Extract the `Metric` reference in this.\n    ///\n    /// # Panics\n    ///\n    /// This will panic if this is not a `Metric` reference.\n    pub fn as_metric(self) -> &'a Metric {\n        match self {\n            Self::Metric(metric) => metric,\n            _ => panic!(\"Failed type coercion, {self:?} is not a metric reference\"),\n        }\n    }\n\n    /// Convert this reference into a new `Metric` by cloning.\n    ///\n    /// # Panics\n    ///\n    /// This will panic if this is not a `Metric` reference.\n    pub fn into_metric(self) -> Metric {\n        match self {\n            Self::Metric(metric) => metric.clone(),\n            _ => panic!(\"Failed type coercion, {self:?} is not a metric reference\"),\n        }\n    }\n}\n\nimpl<'a> From<&'a Event> for EventRef<'a> {\n    fn from(event: &'a Event) -> Self {","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/vectordotdev/vector/blob/bdb87aeaa4c4ff27c0ba643c1c77b21bf2ef4013/lib/vector-core/src/event/ref.rs#L34-L70","documentation":"EventRef::as_metric downcasts the EventRef enum to its Metric variant and panics if the reference actually holds a LogEvent or TraceEvent. Like the other coercions in this file it encodes a caller assertion rather than a recoverable Result. The Debug repr of the real variant is embedded in the panic.","triggerScenarios":"Calling as_metric on an EventRef built from Event::Log or Event::Trace, e.g. EventRef::from(&event).as_metric() in a metric sink receiving log events.","commonSituations":"Metric-processing code (transforms writing custom metric logic, metric sinks) hooked into a pipeline where log-producing sources (file, stdin, kafka) also route into it.","solutions":["Match on EventRef and only call as_metric for EventRef::Metric","Filter or route non-metric events before reaching this code","Guard with matches!(r, EventRef::Metric(_)) prior to coercion"],"exampleFix":"// before\nlet metric = EventRef::from(&event).as_metric();\n// after\nif let EventRef::Metric(metric) = EventRef::from(&event) {\n    // use metric\n}","handlingStrategy":"type-guard","validationCode":"assert!(matches!(event_ref, EventRef::Metric(_)), \"expected metric, got {:?}\", event_ref);","typeGuard":"fn as_metric_ref(r: EventRef<'_>) -> Option<&Metric> {\n    match r {\n        EventRef::Metric(m) => Some(m),\n        _ => None,\n    }\n}","tryCatchPattern":null,"preventionTips":["Only call as_metric on refs known to be EventRef::Metric","Filter log/trace events out before metric-processing code","Test metric sinks/transforms with log events routed in to confirm graceful handling"],"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"}