1- use std:: { fmt, iter:: IntoIterator , pin:: Pin } ;
1+ use std:: { fmt, iter:: IntoIterator , ops :: Deref , pin:: Pin } ;
22
33use futures:: {
44 Sink , SinkExt , Stream , StreamExt , stream,
@@ -7,20 +7,71 @@ use futures::{
77
88use crate :: event:: { Event , EventArray , EventContainer , into_event_stream} ;
99
10+ /// Error returned by a running sink.
11+ #[ repr( transparent) ]
12+ #[ derive( Debug ) ]
13+ pub struct SinkError ( vector_common:: Error ) ;
14+
15+ impl SinkError {
16+ pub fn new ( error : impl Into < vector_common:: Error > ) -> Self {
17+ Self ( error. into ( ) )
18+ }
19+
20+ pub fn into_inner ( self ) -> vector_common:: Error {
21+ self . 0
22+ }
23+ }
24+
25+ impl Default for SinkError {
26+ fn default ( ) -> Self {
27+ Self ( "the sink task completed with an error" . into ( ) )
28+ }
29+ }
30+
31+ impl From < vector_common:: Error > for SinkError {
32+ fn from ( error : vector_common:: Error ) -> Self {
33+ Self ( error)
34+ }
35+ }
36+
37+ impl fmt:: Display for SinkError {
38+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
39+ self . 0 . fmt ( f)
40+ }
41+ }
42+
43+ impl std:: error:: Error for SinkError {
44+ fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
45+ Some ( self . 0 . as_ref ( ) )
46+ }
47+ }
48+
49+ impl Deref for SinkError {
50+ type Target = vector_common:: Error ;
51+
52+ fn deref ( & self ) -> & Self :: Target {
53+ & self . 0
54+ }
55+ }
56+
1057pub enum VectorSink {
1158 Sink ( Box < dyn Sink < EventArray , Error = ( ) > + Send + Unpin > ) ,
12- Stream ( Box < dyn StreamSink < EventArray > + Send > ) ,
59+ Stream ( Box < dyn StreamSink < EventArray , SinkError > + Send > ) ,
1360}
1461
1562impl VectorSink {
1663 /// Run the `VectorSink`
1764 ///
1865 /// # Errors
1966 ///
20- /// It is unclear under what conditions this function will error .
21- pub async fn run ( self , input : impl Stream < Item = EventArray > + Send ) -> Result < ( ) , ( ) > {
67+ /// Returns an error if the sink reports a failure .
68+ pub async fn run ( self , input : impl Stream < Item = EventArray > + Send ) -> Result < ( ) , SinkError > {
2269 match self {
23- Self :: Sink ( sink) => input. map ( Ok ) . forward ( sink) . await ,
70+ Self :: Sink ( sink) => input
71+ . map ( Ok )
72+ . forward ( sink)
73+ . await
74+ . map_err ( |( ) | SinkError :: default ( ) ) ,
2475 Self :: Stream ( s) => s. run ( Box :: pin ( input) ) . await ,
2576 }
2677 }
@@ -30,7 +81,7 @@ impl VectorSink {
3081 /// # Errors
3182 ///
3283 /// See `VectorSink::run` for errors.
33- pub async fn run_events < I > ( self , input : I ) -> Result < ( ) , ( ) >
84+ pub async fn run_events < I > ( self , input : I ) -> Result < ( ) , SinkError >
3485 where
3586 I : IntoIterator < Item = Event > + Send ,
3687 I :: IntoIter : Send ,
@@ -55,7 +106,7 @@ impl VectorSink {
55106 /// # Panics
56107 ///
57108 /// This function will panic if the self instance is not `VectorSink::Stream`.
58- pub fn into_stream ( self ) -> Box < dyn StreamSink < EventArray > + Send > {
109+ pub fn into_stream ( self ) -> Box < dyn StreamSink < EventArray , SinkError > + Send > {
59110 match self {
60111 Self :: Stream ( stream) => stream,
61112 _ => panic ! ( "Failed type coercion, {self:?} is not a Stream" ) ,
@@ -75,6 +126,20 @@ impl VectorSink {
75126
76127 /// Converts an event stream into a `VectorSink`
77128 pub fn from_event_streamsink ( sink : impl StreamSink < Event > + Send + ' static ) -> Self {
129+ Self :: from_streamsink ( EventStream {
130+ sink : Box :: new ( sink) ,
131+ } )
132+ }
133+
134+ /// Converts a stream sink with opaque runtime errors into a `VectorSink`.
135+ pub fn from_streamsink ( sink : impl StreamSink < EventArray > + Send + ' static ) -> Self {
136+ Self :: Stream ( Box :: new ( OpaqueStream ( Box :: new ( sink) ) ) )
137+ }
138+
139+ /// Converts a sink that retains its runtime errors into a `VectorSink`.
140+ pub fn from_detailed_event_streamsink (
141+ sink : impl StreamSink < Event , SinkError > + Send + ' static ,
142+ ) -> Self {
78143 let sink = Box :: new ( sink) ;
79144 VectorSink :: Stream ( Box :: new ( EventStream { sink } ) )
80145 }
@@ -89,8 +154,23 @@ impl fmt::Debug for VectorSink {
89154// === StreamSink ===
90155
91156#[ async_trait:: async_trait]
92- pub trait StreamSink < T > {
93- async fn run ( self : Box < Self > , input : stream:: BoxStream < ' _ , T > ) -> Result < ( ) , ( ) > ;
157+ pub trait StreamSink < T , E = ( ) > {
158+ async fn run ( self : Box < Self > , input : stream:: BoxStream < ' _ , T > ) -> Result < ( ) , E > ;
159+ }
160+
161+ struct OpaqueStream < T > ( Box < T > ) ;
162+
163+ #[ async_trait:: async_trait]
164+ impl < T > StreamSink < EventArray , SinkError > for OpaqueStream < T >
165+ where
166+ T : StreamSink < EventArray > + Send ,
167+ {
168+ async fn run (
169+ self : Box < Self > ,
170+ input : stream:: BoxStream < ' _ , EventArray > ,
171+ ) -> Result < ( ) , SinkError > {
172+ self . 0 . run ( input) . await . map_err ( |( ) | SinkError :: default ( ) )
173+ }
94174}
95175
96176/// Wrapper for sinks implementing `Sink<Event>` to implement
@@ -175,8 +255,12 @@ struct EventStream<T> {
175255}
176256
177257#[ async_trait:: async_trait]
178- impl < T : StreamSink < Event > + Send > StreamSink < EventArray > for EventStream < T > {
179- async fn run ( self : Box < Self > , input : stream:: BoxStream < ' _ , EventArray > ) -> Result < ( ) , ( ) > {
258+ impl < T , E > StreamSink < EventArray , E > for EventStream < T >
259+ where
260+ T : StreamSink < Event , E > + Send ,
261+ E : Send ,
262+ {
263+ async fn run ( self : Box < Self > , input : stream:: BoxStream < ' _ , EventArray > ) -> Result < ( ) , E > {
180264 let input = Box :: pin ( input. flat_map ( into_event_stream) ) ;
181265 self . sink . run ( input) . await
182266 }
0 commit comments