1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use bytes::Bytes;
use vector_lib::request_metadata::{MetaDescriptive, RequestMetadata};
use vector_lib::ByteSizeOf;

use crate::codecs::EncodingConfig;
use crate::{
    codecs::{Encoder, Transformer},
    event::{Event, EventFinalizers, Finalizable},
    internal_events::TemplateRenderingError,
    sinks::util::{
        metadata::RequestMetadataBuilder, request_builder::EncodeResult, Compression,
        EncodedLength, RequestBuilder,
    },
    template::Template,
};

#[derive(Clone)]
pub(super) struct SSMetadata {
    pub(super) finalizers: EventFinalizers,
    pub(super) message_group_id: Option<String>,
    pub(super) message_deduplication_id: Option<String>,
}

#[derive(Clone)]
pub(super) struct SSRequestBuilder {
    encoder: (Transformer, Encoder<()>),
    message_group_id: Option<Template>,
    message_deduplication_id: Option<Template>,
}

impl SSRequestBuilder {
    pub(super) fn new(
        message_group_id: Option<Template>,
        message_deduplication_id: Option<Template>,
        encoding_config: EncodingConfig,
    ) -> crate::Result<Self> {
        let transformer = encoding_config.transformer();
        let serializer = encoding_config.build()?;
        let encoder = Encoder::<()>::new(serializer);

        Ok(Self {
            encoder: (transformer, encoder),
            message_group_id,
            message_deduplication_id,
        })
    }
}

impl RequestBuilder<Event> for SSRequestBuilder {
    type Metadata = SSMetadata;
    type Events = Event;
    type Encoder = (Transformer, Encoder<()>);
    type Payload = Bytes;
    type Request = SendMessageEntry;
    type Error = std::io::Error;

    fn compression(&self) -> Compression {
        Compression::None
    }

    fn encoder(&self) -> &Self::Encoder {
        &self.encoder
    }

    fn split_input(
        &self,
        mut event: Event,
    ) -> (Self::Metadata, RequestMetadataBuilder, Self::Events) {
        let message_group_id = match self.message_group_id {
            Some(ref tpl) => match tpl.render_string(&event) {
                Ok(value) => Some(value),
                Err(error) => {
                    emit!(TemplateRenderingError {
                        error,
                        field: Some("message_group_id"),
                        drop_event: true,
                    });
                    None
                }
            },
            None => None,
        };
        let message_deduplication_id = match self.message_deduplication_id {
            Some(ref tpl) => match tpl.render_string(&event) {
                Ok(value) => Some(value),
                Err(error) => {
                    emit!(TemplateRenderingError {
                        error,
                        field: Some("message_deduplication_id"),
                        drop_event: true,
                    });
                    None
                }
            },
            None => None,
        };

        let builder = RequestMetadataBuilder::from_event(&event);

        let metadata = SSMetadata {
            finalizers: event.take_finalizers(),
            message_group_id,
            message_deduplication_id,
        };
        (metadata, builder, event)
    }

    fn build_request(
        &self,
        client_metadata: Self::Metadata,
        metadata: RequestMetadata,
        payload: EncodeResult<Self::Payload>,
    ) -> Self::Request {
        let payload_bytes = payload.into_payload();
        let message_body = String::from(std::str::from_utf8(&payload_bytes).unwrap());

        SendMessageEntry {
            message_body,
            message_group_id: client_metadata.message_group_id,
            message_deduplication_id: client_metadata.message_deduplication_id,
            finalizers: client_metadata.finalizers,
            metadata,
        }
    }
}

#[derive(Debug, Clone)]
pub(super) struct SendMessageEntry {
    pub(super) message_body: String,
    pub(super) message_group_id: Option<String>,
    pub(super) message_deduplication_id: Option<String>,
    pub(super) finalizers: EventFinalizers,
    pub(super) metadata: RequestMetadata,
}

impl ByteSizeOf for SendMessageEntry {
    fn allocated_bytes(&self) -> usize {
        self.message_body.size_of()
            + self.message_group_id.size_of()
            + self.message_deduplication_id.size_of()
    }
}

impl EncodedLength for SendMessageEntry {
    fn encoded_length(&self) -> usize {
        self.message_body.len()
    }
}

impl Finalizable for SendMessageEntry {
    fn take_finalizers(&mut self) -> EventFinalizers {
        self.finalizers.take_finalizers()
    }
}

impl MetaDescriptive for SendMessageEntry {
    fn get_metadata(&self) -> &RequestMetadata {
        &self.metadata
    }

    fn metadata_mut(&mut self) -> &mut RequestMetadata {
        &mut self.metadata
    }
}