{"record":{"id":"0d09a3e86e1867c5","repo":"vectordotdev/vector","slug":"failed-type-coercion-self-is-not-a-trace-even","errorCode":null,"errorMessage":"Failed type coercion, {self:?} is not a trace event","messagePattern":"Failed type coercion, (.+?) is not a trace event","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/vector-core/src/event/mod.rs","lineNumber":226,"sourceCode":"    /// Fallibly coerces self into a `Metric`\n    ///\n    /// If the event is a `Metric`, then `Some(metric)` is returned, otherwise `None`.\n    pub fn try_into_metric(self) -> Option<Metric> {\n        match self {\n            Event::Metric(metric) => Some(metric),\n            _ => None,\n        }\n    }\n\n    /// Return self as a `TraceEvent`\n    ///\n    /// # Panics\n    ///\n    /// This function panics if self is anything other than an `Event::Trace`.\n    pub fn as_trace(&self) -> &TraceEvent {\n        match self {\n            Event::Trace(trace) => trace,\n            _ => panic!(\"Failed type coercion, {self:?} is not a trace event\"),\n        }\n    }\n\n    /// Return self as a mutable `TraceEvent`\n    ///\n    /// # Panics\n    ///\n    /// This function panics if self is anything other than an `Event::Trace`.\n    pub fn as_mut_trace(&mut self) -> &mut TraceEvent {\n        match self {\n            Event::Trace(trace) => trace,\n            _ => panic!(\"Failed type coercion, {self:?} is not a trace event\"),\n        }\n    }\n\n    /// Coerces self into a `TraceEvent`\n    ///\n    /// # Panics","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/vectordotdev/vector/blob/3708c39b12a93212ed8b8d7510b4cc7769cb5864/lib/vector-core/src/event/mod.rs#L208-L244","documentation":"`Event::as_trace` (lib/vector-core/src/event/mod.rs:218-228) borrows the event as `&TraceEvent` and panics with a debug dump when it is `Event::Log` or `Event::Metric`. Trace-specific components (OTLP trace sinks, span processors) assume this precondition; the `# Panics` doc on the method states it, and there is no fallible borrow twin in this impl block.","triggerScenarios":"Calling `event.as_trace()` on a log event from a file/http source or a metric from a prometheus source — e.g. a trace-export sink attached to a pipeline that also carries logs, or generic event-loop code reused across pipelines.","commonSituations":"Enabling the trace (`opentelemetry`) feature and wiring OTLP sources into shared pipelines; custom telemetry exporters that process every event through trace-specific code; fixtures with log events run against trace tests.","solutions":["Check the variant first: `if matches!(event, Event::Trace(_))` before `as_trace`.","Match on the enum and dispatch each variant to its own handler, emitting an internal error for unexpected ones.","Keep trace pipelines separate (or route with a filter transform) so trace sinks only receive `Event::Trace`."],"exampleFix":"// before\nlet trace = event.as_trace(); // panics on Log/Metric\n\n// after\nmatch &event {\n    Event::Trace(trace) => export_spans(trace),\n    other => emit!(UnexpectedEventForTraceSink { event: other }),\n}","handlingStrategy":"type-guard","validationCode":"if matches!(event, Event::Trace(_)) {\n    let trace = event.as_trace();\n}","typeGuard":"fn is_trace_event(e: &Event) -> bool { matches!(e, Event::Trace(_)) }","tryCatchPattern":null,"preventionTips":["Guard as_trace() with a variant check in shared event loops.","Wire trace sinks only to trace-carrying branches (route/filter transforms).","Test trace components with log and metric fixtures."],"tags":["rust","vector","event","traces","opentelemetry","panic"],"backgroundTag":"event-type-coercion-failure","analyzedSha":"3708c39b12a93212ed8b8d7510b4cc7769cb5864","analyzedAt":"2026-08-20T07:02:18.786Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}