Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement(kubernetes source): Kubernetes partial message merge #2134

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .meta/sources/kubernetes.toml.bak
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ of `kube-system`. Which is by default excluded, \
unless there are any non empty `include` options.\
"""

[sources.kubernetes.options.auto_partial_merge]
type = "bool"
common = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Majority of the logs won't be separated, so I wouldn't consider it common.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how it is in docker source, but I don't mind changing this.
@binarylogic what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree that this should be false. I don't think this is a common option that users should be changing.

default = true
description = """\
Setting this to `false` will disable the automatic merging of partial events.\
"""

[sources.kubernetes.output.log.fields.container_name]
type = "string"
examples = ["vector"]
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ sources-docker = ["shiplift"]
sources-file = ["bytesize"]
sources-journald = []
sources-kafka = ["owning_ref"]
sources-kubernetes = ["sources-file", "transforms-json_parser", "transforms-regex_parser"]
sources-kubernetes = ["sources-file", "transforms-json_parser", "transforms-regex_parser", "transforms-merge"]
sources-logplex = ["warp", "sources-tls"]
sources-prometheus = []
sources-http = ["warp", "sources-tls"]
Expand Down
1 change: 1 addition & 0 deletions src/sources/kubernetes/file_source_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ mod tests {
include_pod_uids: vec!["a027f09d8f18234519fa930f8fa71234".to_owned()],
include_container_names: vec!["busybox".to_owned()],
include_namespaces: vec!["telemetry".to_owned(), "app".to_owned()],
..KubernetesConfig::default()
};

assert_eq!(
Expand Down
119 changes: 102 additions & 17 deletions src/sources/kubernetes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
sources::Source,
topology::config::{DataType, GlobalOptions, SourceConfig},
transforms::{
merge::{Merge, TrailingNewlineNormalizer},
regex_parser::{RegexParser, RegexParserConfig},
Transform,
},
Expand Down Expand Up @@ -39,12 +40,15 @@ enum BuildError {
IllegalCharacterInUid { uid: String },
}

#[derive(Deserialize, Serialize, Debug, Clone, Default)]
#[derive(Deserialize, Serialize, Debug, Clone, Derivative)]
#[derivative(Default)]
#[serde(deny_unknown_fields, default)]
pub struct KubernetesConfig {
include_container_names: Vec<String>,
include_pod_uids: Vec<String>,
include_namespaces: Vec<String>,
#[derivative(Default(value = "true"))]
auto_partial_merge: bool,
}

#[typetag::serde(name = "kubernetes")]
Expand Down Expand Up @@ -73,12 +77,23 @@ impl SourceConfig for KubernetesConfig {
let mut parse_message = message_parser::build_message_parser()?;

// Kubernetes source
let source = file_recv
let stream = file_recv
.filter_map(move |event| transform_file.transform(event))
.filter_map(move |event| parse_message.transform(event))
.filter_map(move |event| now.filter(event))
.map(remove_ending_newline)
.filter_map(move |event| transform_pod_uid.transform(event))
.filter_map(move |event| now.filter(event));

let stream: Box<dyn Stream<Item = Event, Error = _> + Send> = if self.auto_partial_merge {
let mut transform_merge_partial_events = transform_merge_partial_events();
Copy link
Contributor

@ktff ktff Apr 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach to merging won't work with CRI runtimes. Implementations of it usually leave newline from the stdin stdout, and don't put it if the log was broken in multiple parts, instead they add a tag which we extract here to a field multiline_tag.

This is evident if you test it against Kubernetes cluster with CRI runtime, for example microk8s.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Thanks for pointing this out. Indeed, we can't merge before this is corrected.

Box::new(
stream.filter_map(move |event| transform_merge_partial_events.transform(event)),
)
} else {
Box::new(stream)
};

let stream = stream.filter_map(move |event| transform_pod_uid.transform(event));

let source = stream
.forward(out.sink_map_err(drop))
.map(drop)
.join(file_source)
Expand Down Expand Up @@ -120,18 +135,6 @@ impl TimeFilter {
}
}

fn remove_ending_newline(mut event: Event) -> Event {
if let Some(Value::Bytes(msg)) = event
.as_mut_log()
.get_mut(&event::log_schema().message_key())
{
if msg.ends_with(&['\n' as u8]) {
msg.truncate(msg.len() - 1);
}
}
event
}

fn transform_file() -> crate::Result<Box<dyn Transform>> {
let mut config = RegexParserConfig::default();

Expand Down Expand Up @@ -210,6 +213,17 @@ fn transform_pod_uid() -> crate::Result<ApplicableTransform> {
Ok(ApplicableTransform::Candidates(transforms))
}

fn transform_merge_partial_events() -> Merge<TrailingNewlineNormalizer> {
let message_key = event::log_schema().message_key();
Merge::new(
TrailingNewlineNormalizer {
probe_field: message_key.clone(),
},
vec![message_key.clone()],
vec!["pod_uid".into()],
)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -271,4 +285,75 @@ mod tests {

has(&event, "object_uid", "306cd636-0c6d-11ea-9079-1c1b0de4d755");
}

#[test]
fn partial_events_merge() {
let message_key = event::log_schema().message_key();
let sample_pod_uid = "qwerty";

let part1 = {
let mut event = Event::new_empty_log();
event
.as_mut_log()
.insert("pod_uid", sample_pod_uid.to_owned());
event.as_mut_log().insert(message_key, "hello".to_owned());
event
};
let part2 = {
let mut event = Event::new_empty_log();
event
.as_mut_log()
.insert("pod_uid", sample_pod_uid.to_owned());
event
.as_mut_log()
.insert(message_key, " world!\n".to_owned());
event
};

let mut transform = transform_merge_partial_events();

assert!(transform.transform(part1).is_none());
let event = transform.transform(part2).unwrap();

has(&event, "pod_uid", sample_pod_uid);
has(&event, message_key, "hello world!");
}

#[test]
fn partial_events_merge_separate_streams() {
let message_key = event::log_schema().message_key();

let part1 = {
let mut event = Event::new_empty_log();
event.as_mut_log().insert("pod_uid", "qwerty1".to_owned());
event.as_mut_log().insert(message_key, "par".to_owned());
event
};
let part2 = {
let mut event = Event::new_empty_log();
event.as_mut_log().insert("pod_uid", "qwerty2".to_owned());
event
.as_mut_log()
.insert(message_key, "non-partial!\n".to_owned());
event
};
let part3 = {
let mut event = Event::new_empty_log();
event.as_mut_log().insert("pod_uid", "qwerty1".to_owned());
event.as_mut_log().insert(message_key, "tial!\n".to_owned());
event
};

let mut transform = transform_merge_partial_events();

assert!(transform.transform(part1).is_none());

let event = transform.transform(part2).unwrap();
has(&event, "pod_uid", "qwerty2");
has(&event, message_key, "non-partial!");

let event = transform.transform(part3).unwrap();
has(&event, "pod_uid", "qwerty1");
has(&event, message_key, "partial!");
}
}
38 changes: 38 additions & 0 deletions src/sources/kubernetes/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use kube::{
};
use serde::de::DeserializeOwned;
use serde_json::Value;
use std::fmt::Write;
use uuid::Uuid;

static NAMESPACE_MARKER: &'static str = "$(TEST_NAMESPACE)";
Expand Down Expand Up @@ -671,3 +672,40 @@ fn kube_diff_pod_uid() {
false
});
}

#[test]
fn kube_partial() {
let namespace = format!("partial-{}", Uuid::new_v4());
let message = {
let mut s = String::new();
for i in 0..8 {
write!(s, "{}", i.to_string().repeat(8 * 1024)).unwrap();
}
s
};
assert_eq!(message.len(), 64 * 1024); // 64 kb
let user_namespace = user_namespace(&namespace);

let kube = Kube::new(&namespace);
let user = Kube::new(&user_namespace);

// Start vector
let vector = start_vector(&kube, user_namespace.as_str(), None);

// Start echo
let _echo = echo(&user, "echo", &message);
// Verify logs
wait_for(|| {
// If any daemon logged message, done.
for line in logs(&kube, &vector) {
if line["message"].as_str().unwrap() == message {
// Very long message arrived as a single (merged) message.
// DONE
return true;
} else {
debug!(namespace=%namespace,log=%line);
}
}
false
});
}
Loading