vectordotdev/vector · error
Failed type coercion
Error message
Failed type coercion, {self:?} is not a metric reference What it means
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.
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
Example fix
// before
let metric = EventRef::from(&event).as_metric();
// after
if let EventRef::Metric(metric) = EventRef::from(&event) {
// use metric
} Defensive patterns
Strategy: type-guard
Validate before calling
assert!(matches!(event_ref, EventRef::Metric(_)), "expected metric, got {:?}", event_ref); Type guard
fn as_metric_ref(r: EventRef<'_>) -> Option<&Metric> {
match r {
EventRef::Metric(m) => Some(m),
_ => None,
}
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
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
- Failed type coercion
- Failed type coercion
- Failed type coercion
- Failed type coercion
- Failed type coercion
AI-assisted analysis of vectordotdev/vector@bdb87aeaa4 (2026-09-16).
Data as JSON: /api/errors/b9ba9b942102c0fb.
Report an issue: GitHub.
Appendix: source
Thrown at lib/vector-core/src/event/ref.rs:52
/// # 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
///
/// This will panic if this is not a `Metric` reference.
pub fn as_metric(self) -> &'a Metric {
match self {
Self::Metric(metric) => metric,
_ => panic!("Failed type coercion, {self:?} is not a metric reference"),
}
}
/// Convert this reference into a new `Metric` by cloning.
///
/// # Panics
///
/// This will panic if this is not a `Metric` reference.
pub fn into_metric(self) -> Metric {
match self {
Self::Metric(metric) => metric.clone(),
_ => panic!("Failed type coercion, {self:?} is not a metric reference"),
}
}
}
impl<'a> From<&'a Event> for EventRef<'a> {
fn from(event: &'a Event) -> Self {View on GitHub (pinned to bdb87aeaa4)