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
use bytes::Bytes;
use vector_lib::EstimatedJsonEncodedSizeOf;
use vector_lib::{json_size::JsonSize, request_metadata::RequestMetadata};

use crate::{
    event::{EventFinalizers, Finalizable},
    sinks::{
        elasticsearch::{
            encoder::{ElasticsearchEncoder, ProcessedEvent},
            service::ElasticsearchRequest,
        },
        util::{
            metadata::RequestMetadataBuilder, request_builder::EncodeResult, Compression,
            RequestBuilder,
        },
    },
};

#[derive(Debug, Clone)]
pub struct ElasticsearchRequestBuilder {
    pub compression: Compression,
    pub encoder: ElasticsearchEncoder,
}

pub struct Metadata {
    finalizers: EventFinalizers,
    batch_size: usize,
    events_byte_size: JsonSize,
}

impl RequestBuilder<Vec<ProcessedEvent>> for ElasticsearchRequestBuilder {
    type Metadata = Metadata;
    type Events = Vec<ProcessedEvent>;
    type Encoder = ElasticsearchEncoder;
    type Payload = Bytes;
    type Request = ElasticsearchRequest;
    type Error = std::io::Error;

    fn compression(&self) -> Compression {
        self.compression
    }

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

    fn split_input(
        &self,
        mut events: Vec<ProcessedEvent>,
    ) -> (Self::Metadata, RequestMetadataBuilder, Self::Events) {
        let events_byte_size = events
            .iter()
            .map(|x| x.log.estimated_json_encoded_size_of())
            .reduce(|a, b| a + b)
            .unwrap_or(JsonSize::zero());

        let metadata_builder = RequestMetadataBuilder::from_events(&events);

        let es_metadata = Metadata {
            finalizers: events.take_finalizers(),
            batch_size: events.len(),
            events_byte_size,
        };
        (es_metadata, metadata_builder, events)
    }

    fn build_request(
        &self,
        es_metadata: Self::Metadata,
        metadata: RequestMetadata,
        payload: EncodeResult<Self::Payload>,
    ) -> Self::Request {
        ElasticsearchRequest {
            payload: payload.into_payload(),
            finalizers: es_metadata.finalizers,
            batch_size: es_metadata.batch_size,
            events_byte_size: es_metadata.events_byte_size,
            metadata,
        }
    }
}