Skip to content

Commit 5139bc7

Browse files
committed
Truncate events to the actual length limit, rather than a constant amount
1 parent 19808b4 commit 5139bc7

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/logging/k8s_events.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ fn error_to_event<E: ReconcilerError>(err: &E) -> Event {
2828
}
2929
}
3030
};
31+
dbg!(full_msg.len());
3132
message::truncate_with_ellipsis(&mut full_msg, 1024);
33+
dbg!(full_msg.len());
34+
assert!(full_msg.len() <= 1024);
3235
Event {
3336
type_: EventType::Warning,
3437
reason: err.category().to_string(),
@@ -87,11 +90,13 @@ mod message {
8790
const ELLIPSIS_LEN: usize = ELLIPSIS.len_utf8();
8891
let len = msg.len();
8992
if len > max_len {
90-
msg.truncate(find_start_of_char(msg, len.saturating_sub(ELLIPSIS_LEN)));
93+
let start_of_trunc_char = find_start_of_char(msg, max_len.saturating_sub(ELLIPSIS_LEN));
94+
msg.truncate(start_of_trunc_char);
9195
if ELLIPSIS_LEN <= max_len {
9296
msg.push(ELLIPSIS);
9397
}
9498
}
99+
debug_assert!(msg.len() <= max_len);
95100
}
96101

97102
fn find_start_of_char(s: &str, mut pos: usize) -> usize {
@@ -120,7 +125,10 @@ mod message {
120125
fn truncate_should_ellipsize_large_string() {
121126
let mut x = "hello".to_string();
122127
truncate_with_ellipsis(&mut x, 4);
123-
assert_eq!(&x, "he…");
128+
assert_eq!(&x, "h…");
129+
x = "hello, this is a much larger string".to_string();
130+
truncate_with_ellipsis(&mut x, 4);
131+
assert_eq!(&x, "h…");
124132
}
125133

126134
#[test]

0 commit comments

Comments
 (0)