vector/sinks/util/
partitioner.rs

1use vector_lib::{event::Event, partition::Partitioner};
2
3use crate::{internal_events::TemplateRenderingError, template::Template};
4
5/// Partitions items based on the generated key for the given event.
6pub struct KeyPartitioner {
7    key_prefix_template: Template,
8    dead_letter_key_prefix: Option<String>,
9}
10
11impl KeyPartitioner {
12    pub const fn new(
13        key_prefix_template: Template,
14        dead_letter_key_prefix: Option<String>,
15    ) -> Self {
16        Self {
17            key_prefix_template,
18            dead_letter_key_prefix,
19        }
20    }
21}
22
23impl Partitioner for KeyPartitioner {
24    type Item = Event;
25    type Key = Option<String>;
26
27    fn partition(&self, item: &Self::Item) -> Self::Key {
28        self.key_prefix_template
29            .render_string(item)
30            .or_else(|error| {
31                if let Some(dead_letter_key_prefix) = &self.dead_letter_key_prefix {
32                    emit!(TemplateRenderingError {
33                        error,
34                        field: Some("key_prefix"),
35                        drop_event: false,
36                    });
37                    Ok(dead_letter_key_prefix.clone())
38                } else {
39                    Err(emit!(TemplateRenderingError {
40                        error,
41                        field: Some("key_prefix"),
42                        drop_event: true,
43                    }))
44                }
45            })
46            .ok()
47    }
48}