vector/sinks/elasticsearch/
request_builder.rs1use bytes::Bytes;
2use vector_lib::EstimatedJsonEncodedSizeOf;
3use vector_lib::{json_size::JsonSize, request_metadata::RequestMetadata};
4
5use crate::{
6 event::{EventFinalizers, Finalizable},
7 sinks::{
8 elasticsearch::{
9 encoder::{ElasticsearchEncoder, ProcessedEvent},
10 service::ElasticsearchRequest,
11 },
12 util::{
13 metadata::RequestMetadataBuilder, request_builder::EncodeResult, Compression,
14 RequestBuilder,
15 },
16 },
17};
18
19#[derive(Debug, Clone)]
20pub struct ElasticsearchRequestBuilder {
21 pub compression: Compression,
22 pub encoder: ElasticsearchEncoder,
23}
24
25pub struct Metadata {
26 finalizers: EventFinalizers,
27 batch_size: usize,
28 events_byte_size: JsonSize,
29 original_events: Vec<ProcessedEvent>,
30}
31
32impl RequestBuilder<Vec<ProcessedEvent>> for ElasticsearchRequestBuilder {
33 type Metadata = Metadata;
34 type Events = Vec<ProcessedEvent>;
35 type Encoder = ElasticsearchEncoder;
36 type Payload = Bytes;
37 type Request = ElasticsearchRequest;
38 type Error = std::io::Error;
39
40 fn compression(&self) -> Compression {
41 self.compression
42 }
43
44 fn encoder(&self) -> &Self::Encoder {
45 &self.encoder
46 }
47
48 fn split_input(
49 &self,
50 mut events: Vec<ProcessedEvent>,
51 ) -> (Self::Metadata, RequestMetadataBuilder, Self::Events) {
52 let events_byte_size = events
53 .iter()
54 .map(|x| x.log.estimated_json_encoded_size_of())
55 .reduce(|a, b| a + b)
56 .unwrap_or(JsonSize::zero());
57
58 let metadata_builder = RequestMetadataBuilder::from_events(&events);
59
60 let es_metadata = Metadata {
61 finalizers: events.take_finalizers(),
62 batch_size: events.len(),
63 events_byte_size,
64 original_events: events.clone(),
65 };
66 (es_metadata, metadata_builder, events)
67 }
68
69 fn build_request(
70 &self,
71 es_metadata: Self::Metadata,
72 metadata: RequestMetadata,
73 payload: EncodeResult<Self::Payload>,
74 ) -> Self::Request {
75 ElasticsearchRequest {
76 payload: payload.into_payload(),
77 finalizers: es_metadata.finalizers,
78 batch_size: es_metadata.batch_size,
79 events_byte_size: es_metadata.events_byte_size,
80 metadata,
81 original_events: es_metadata.original_events,
82 elasticsearch_request_builder: self.clone(),
83 }
84 }
85}