Skip to content

Commit 86a4df6

Browse files
fix(loki sink): use json size of unencoded event (#17572)
Currently the `loki` sink is encoding the event and then taking the estimated json size of that encoded event. This is wrong. All sinks should take the estimated json size of the unencoded event. There is an open question around whether we should be taking the json size before or after the `only_fields` and `except_fields` are applied. I'm currently trying to get an answer to that. Currently everything is before. The `loki` sink works a little bit different to the other stream based sinks. Most sinks pass the event unencoded to the request builder. It is at this point that the json size of the metrics are calculated. However, `loki` encodes the event and passes the encoded value to the request builder. This PR changes it so it also passes the json size to the request builder so it can use that value to calculate the metrics. Signed-off-by: Stephen Wakely <fungus.humungus@gmail.com>
1 parent 6e45477 commit 86a4df6

File tree

2 files changed

+5
-17
lines changed

2 files changed

+5
-17
lines changed

src/sinks/loki/event.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,6 @@ impl ByteSizeOf for LokiEvent {
139139
}
140140
}
141141

142-
/// This implementation approximates the `Serialize` implementation below, without any allocations.
143-
impl EstimatedJsonEncodedSizeOf for LokiEvent {
144-
fn estimated_json_encoded_size_of(&self) -> JsonSize {
145-
static BRACKETS_SIZE: JsonSize = JsonSize::new(2);
146-
static COLON_SIZE: JsonSize = JsonSize::new(1);
147-
static QUOTES_SIZE: JsonSize = JsonSize::new(2);
148-
149-
BRACKETS_SIZE
150-
+ QUOTES_SIZE
151-
+ self.timestamp.estimated_json_encoded_size_of()
152-
+ COLON_SIZE
153-
+ self.event.estimated_json_encoded_size_of()
154-
}
155-
}
156-
157142
impl Serialize for LokiEvent {
158143
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
159144
where
@@ -172,6 +157,7 @@ pub struct LokiRecord {
172157
pub partition: PartitionKey,
173158
pub labels: Labels,
174159
pub event: LokiEvent,
160+
pub json_byte_size: JsonSize,
175161
pub finalizers: EventFinalizers,
176162
}
177163

@@ -187,7 +173,7 @@ impl ByteSizeOf for LokiRecord {
187173

188174
impl EstimatedJsonEncodedSizeOf for LokiRecord {
189175
fn estimated_json_encoded_size_of(&self) -> JsonSize {
190-
self.event.estimated_json_encoded_size_of()
176+
self.json_byte_size
191177
}
192178
}
193179

src/sinks/loki/sink.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use vector_core::{
1212
partition::Partitioner,
1313
sink::StreamSink,
1414
stream::BatcherSettings,
15-
ByteSizeOf,
15+
ByteSizeOf, EstimatedJsonEncodedSizeOf,
1616
};
1717

1818
use super::{
@@ -268,6 +268,7 @@ impl EventEncoder {
268268
pub(super) fn encode_event(&mut self, mut event: Event) -> Option<LokiRecord> {
269269
let tenant_id = self.key_partitioner.partition(&event);
270270
let finalizers = event.take_finalizers();
271+
let json_byte_size = event.estimated_json_encoded_size_of();
271272
let mut labels = self.build_labels(&event);
272273
self.remove_label_fields(&mut event);
273274

@@ -302,6 +303,7 @@ impl EventEncoder {
302303
},
303304
partition,
304305
finalizers,
306+
json_byte_size,
305307
})
306308
}
307309
}

0 commit comments

Comments
 (0)