vectordotdev/vector · error

Failed type coercion

Error message

Failed type coercion, {self:?} is not a log reference

What it means

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.

Solutions

  1. Match on the EventRef/Event enum before coercing: only call as_log for EventRef::Log
  2. Convert the component to accept the full Event and handle each variant
  3. Use try-style handling: check with matches!(r, EventRef::Log(_)) before calling as_log

Example fix

// before
let log = EventRef::from(&event).as_log();
// after
if let EventRef::Log(log) = EventRef::from(&event) {
    // use log
}
Defensive patterns

Strategy: type-guard

Validate before calling

assert!(matches!(event_ref, EventRef::Log(_)), "expected log event, got {:?}", event_ref);

Type guard

fn as_log_ref(r: EventRef<'_>) -> Option<&LogEvent> {
    match r {
        EventRef::Log(log) => Some(log),
        _ => None,
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of vectordotdev/vector@bdb87aeaa4 (2026-09-16). Data as JSON: /api/errors/55bbfae57aef315d. Report an issue: GitHub.

Appendix: source

Thrown at lib/vector-core/src/event/ref.rs:28

pub enum EventRef<'a> {
    /// Reference to a `LogEvent`
    Log(&'a LogEvent),
    /// Reference to a `Metric`
    Metric(&'a Metric),
    /// Reference to a `TraceEvent`
    Trace(&'a TraceEvent),
}

impl<'a> EventRef<'a> {
    /// Extract the `LogEvent` reference in this.
    ///
    /// # Panics
    ///
    /// This will panic if this is not a `LogEvent` reference.
    pub fn as_log(self) -> &'a LogEvent {
        match self {
            Self::Log(log) => log,
            _ => panic!("Failed type coercion, {self:?} is not a log reference"),
        }
    }

    /// Convert this reference into a new `LogEvent` by cloning.
    ///
    /// # Panics
    ///
    /// This will panic if this is not a `LogEvent` reference.
    pub fn into_log(self) -> LogEvent {
        match self {
            Self::Log(log) => log.clone(),
            _ => panic!("Failed type coercion, {self:?} is not a log reference"),
        }
    }

    /// Extract the `Metric` reference in this.
    ///
    /// # Panics

View on GitHub (pinned to bdb87aeaa4)