{"record":{"id":"55bbfae57aef315d","repo":"vectordotdev/vector","slug":"failed-type-coercion-self-is-not-a-log-refere","errorCode":null,"errorMessage":"Failed type coercion, {self:?} is not a log reference","messagePattern":"Failed type coercion, (.+?) is not a log reference","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/vector-core/src/event/ref.rs","lineNumber":28,"sourceCode":"pub enum EventRef<'a> {\n    /// Reference to a `LogEvent`\n    Log(&'a LogEvent),\n    /// Reference to a `Metric`\n    Metric(&'a Metric),\n    /// Reference to a `TraceEvent`\n    Trace(&'a TraceEvent),\n}\n\nimpl<'a> EventRef<'a> {\n    /// Extract the `LogEvent` reference in this.\n    ///\n    /// # Panics\n    ///\n    /// This will panic if this is not a `LogEvent` reference.\n    pub fn as_log(self) -> &'a LogEvent {\n        match self {\n            Self::Log(log) => log,\n            _ => panic!(\"Failed type coercion, {self:?} is not a log reference\"),\n        }\n    }\n\n    /// Convert this reference into a new `LogEvent` by cloning.\n    ///\n    /// # 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","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/vectordotdev/vector/blob/bdb87aeaa4c4ff27c0ba643c1c77b21bf2ef4013/lib/vector-core/src/event/ref.rs#L10-L46","documentation":"EventRef::as_log is a downcast from the EventRef enum (Log/Metric/Trace) to its Log variant. The library panics because there is no way to return a reference when the variant is wrong; the caller asserted the event kind via the method choice instead of matching. The panic message includes the Debug repr of the actual variant for diagnosis.","triggerScenarios":"Calling as_log on an EventRef built from an Event::Metric or Event::Trace, e.g. EventRef::from(&event).as_log() in a sink/transform that assumed logs.","commonSituations":"Custom transforms or sinks that call as_log unconditionally on every event flowing through a topology that also carries metrics or traces (e.g. a pipeline where a metrics source like `host_metrics` feeds the same component).","solutions":["Match on the EventRef/Event enum before coercing: only call as_log for EventRef::Log","Convert the component to accept the full Event and handle each variant","Use try-style handling: check with matches!(r, EventRef::Log(_)) before calling as_log"],"exampleFix":"// before\nlet log = EventRef::from(&event).as_log();\n// after\nif let EventRef::Log(log) = EventRef::from(&event) {\n    // use log\n}","handlingStrategy":"type-guard","validationCode":"assert!(matches!(event_ref, EventRef::Log(_)), \"expected log event, got {:?}\", event_ref);","typeGuard":"fn as_log_ref(r: EventRef<'_>) -> Option<&LogEvent> {\n    match r {\n        EventRef::Log(log) => Some(log),\n        _ => None,\n    }\n}","tryCatchPattern":null,"preventionTips":["Never call as_log/into_log without first matching on the EventRef variant","In components that receive Event, match on Event::Log/Event::Metric/Event::Trace before coercing","Write unit tests that feed metric and trace events through any custom log-handling code"],"tags":["rust","panic","type-coercion","event"],"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"}