vectordotdev/vector · error

Failed type coercion

Error message

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

What it means

`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.

Solutions

  1. Check the variant first (`matches!(event, Event::Metric(_))`) before calling `as_metric`
  2. Use the fallible accessor for generic code and handle the non-metric case
  3. Fix routing so metrics-only components receive only metric streams

Example fix

// before
let metric = event.as_metric();
// after
let metric = match &event {
    Event::Metric(m) => m,
    _ => return Ok(None),
};
Defensive patterns

Strategy: type-guard

Validate before calling

if !matches!(event, Event::Metric(_)) { return; }

Type guard

fn is_metric(e: &Event) -> bool { matches!(e, Event::Metric(_)) }

Prevention

When it happens

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

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

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/6da7d3140264f931. Report an issue: GitHub.

Appendix: source

Thrown at lib/vector-core/src/event/mod.rs:180

    /// Return self as a `LogEvent` if possible
    ///
    /// If the event is a `LogEvent`, then `Some(&log_event)` is returned, otherwise `None`.
    pub fn maybe_as_log(&self) -> Option<&LogEvent> {
        match self {
            Event::Log(log) => Some(log),
            _ => None,
        }
    }

    /// Return self as a `Metric`
    ///
    /// # Panics
    ///
    /// This function panics if self is anything other than an `Event::Metric`.
    pub fn as_metric(&self) -> &Metric {
        match self {
            Event::Metric(metric) => metric,
            _ => panic!("Failed type coercion, {self:?} is not a metric"),
        }
    }

    /// Return self as a mutable `Metric`
    ///
    /// # Panics
    ///
    /// This function panics if self is anything other than an `Event::Metric`.
    pub fn as_mut_metric(&mut self) -> &mut Metric {
        match self {
            Event::Metric(metric) => metric,
            _ => panic!("Failed type coercion, {self:?} is not a metric"),
        }
    }

    /// Coerces self into `Metric`
    ///
    /// # Panics

View on GitHub (pinned to bdb87aeaa4)