saluki_core/components/encoders/mod.rs
1//! Encoder component basics.
2
3use async_trait::async_trait;
4use saluki_error::GenericError;
5use snafu::Snafu;
6
7use crate::{data_model::event::Event, topology::PayloadsDispatcher};
8
9mod builder;
10pub use self::builder::{EncoderBuilder, IncrementalEncoderBuilder};
11
12mod context;
13pub use self::context::EncoderContext;
14
15/// Encoder process result.
16#[allow(clippy::large_enum_variant)]
17pub enum ProcessResult {
18 /// The encoder processed the event successfully and is ready to process more events.
19 Continue,
20
21 /// The encoder can't process the event without flushing first.
22 ///
23 /// The caller should flush the encoder and try again to process the event.
24 FlushRequired(Event),
25}
26
27/// An error that interrupted flushing an incremental encoder.
28///
29/// A flush can dispatch several payloads before one of them fails, so the error reports how many events had already
30/// been dispatched, letting the caller account for them even though the flush failed.
31#[derive(Debug, Snafu)]
32#[snafu(display("Failed to flush encoder after dispatching {dispatched_events} events."))]
33pub struct FlushError {
34 dispatched_events: usize,
35 source: GenericError,
36}
37
38impl FlushError {
39 /// Creates a new `FlushError` for an error that occurred after `dispatched_events` events had been dispatched.
40 pub fn new(dispatched_events: usize, source: GenericError) -> Self {
41 Self {
42 dispatched_events,
43 source,
44 }
45 }
46
47 /// Returns the number of events that were dispatched before the error occurred.
48 pub fn dispatched_events(&self) -> usize {
49 self.dispatched_events
50 }
51
52 /// Consumes the error, returning the underlying error.
53 pub fn into_source(self) -> GenericError {
54 self.source
55 }
56}
57
58/// An encoder.
59///
60/// Encoders are the bridge between forwarders and the rest of the topology. They're responsible for encoding
61/// telemetry events into output payloads that can then be forwarded. Most encoders are specific to a particular system
62/// and not simply equivalent to a certain encoding or serialization format: while two encoders may both produce JSON,
63/// they may produce different JSON formats such that one format only works for product A and the other only works for
64/// product B. In essence, the process of taking telemetry events and sending them to product A ends up becoming the sum
65/// of a specific encoder and forwarder combination.
66///
67/// Examples of typical encoders include Datadog Metrics, Events, and Service Checks.
68#[async_trait]
69pub trait Encoder {
70 /// Runs the encoder.
71 ///
72 /// The encoder context provides access primarily to the event stream, used to receive events sent to the encoder,
73 /// and the dispatcher, used to send payloads to the downstream forwarder in the topology, as well as other
74 /// information such as the component context.
75 ///
76 /// Encoders are expected to run indefinitely until their event stream is terminated, or an error occurs.
77 ///
78 /// # Errors
79 ///
80 /// If an unrecoverable error occurs while running, an error is returned.
81 async fn run(self: Box<Self>, context: EncoderContext) -> Result<(), GenericError>;
82}
83
84/// An incremental encoder.
85///
86/// Incremental encoders represent the essential operations of a encoder: adding events and flushing the resulting
87/// payloads. Generally, encoders shouldn't need to concern themselves with the high-level details of being a
88/// component within a topology, such as responding to health probes, handling graceful shutdown, and so on. Through
89/// separating out the core encoder functionality from the component functionality, we can make it easier to implement
90/// various encoder implementations while only maintaining a few common encoder components that actually _drive_ the
91/// underlying encoder logic.
92#[async_trait]
93pub trait IncrementalEncoder {
94 /// Process a single event.
95 ///
96 /// The encoder will process the event, attempting to add it to the current payload. If the encoder is unable to
97 /// process the event without flushing first, `Ok(ProcessResult::FlushRequired(event))` is returned, containing the
98 /// event that couldn't be processed. Otherwise, `Ok(ProcessResult::Continue)` is returned.
99 ///
100 /// # Errors
101 ///
102 /// If the encoder can't process the event due to an unrecoverable error, an error is returned.
103 async fn process_event(&mut self, event: Event) -> Result<ProcessResult, GenericError>;
104
105 /// Flush the encoder, finalizing all current payloads and sending them to the dispatcher.
106 ///
107 /// Returns the number of events contained in the payloads that were dispatched. An encoder can discard events it
108 /// has accepted but can't send, such as when a payload exceeds its size limit and can't be split, and those aren't
109 /// included in the count.
110 ///
111 /// # Errors
112 ///
113 /// If the encoder can't dispatch a payload, an error is returned. It reports the number of events that were
114 /// dispatched earlier in the same flush, so they can still be accounted for.
115 async fn flush(&mut self, dispatcher: &PayloadsDispatcher) -> Result<usize, FlushError>;
116}