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

fix(influxdb_logs): encode influx line when no tags present #17029

Merged
merged 4 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 10 additions & 6 deletions src/sinks/influxdb/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,23 +592,27 @@ mod tests {
event.as_mut_log().insert("value", 100);
event.as_mut_log().insert("timestamp", ts());

let sink = create_sink(
let mut sink = create_sink(
"http://localhost:9999",
"my-token",
ProtocolVersion::V2,
"vector",
["metric_type"].to_vec(),
[].to_vec(),
);
sink.transformer
.set_except_fields(Some(vec!["metric_type".into()]))
.unwrap();
spencergilbert marked this conversation as resolved.
Show resolved Hide resolved
let mut encoder = sink.build_encoder();

let bytes = encoder.encode_event(event).unwrap();
let string = std::str::from_utf8(&bytes).unwrap();
let line = std::str::from_utf8(&bytes).unwrap();
assert!(line.starts_with("vector "));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should have no comma after measurement.


let line_protocol = split_line_protocol(string);
let line_protocol = split_line_protocol(line);
assert_eq!("vector", line_protocol.0);
assert_eq!("metric_type=logs", line_protocol.1);
assert_eq!("", line_protocol.1);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No tags present

assert_fields(
line_protocol.2.to_string(),
line_protocol.2,
["value=100i", "message=\"hello\""].to_vec(),
);

Expand Down
20 changes: 12 additions & 8 deletions src/sinks/influxdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,16 @@ pub(in crate::sinks) fn influx_line_protocol(
}

encode_string(measurement, line_protocol);
line_protocol.put_u8(b',');

// Tags
let unwrapped_tags = tags.unwrap_or_default();
encode_tags(unwrapped_tags, line_protocol);
line_protocol.put_u8(b' ');
if unwrapped_tags.is_empty() {
line_protocol.put_u8(b' ');
} else {
line_protocol.put_u8(b',');
encode_tags(unwrapped_tags, line_protocol);
line_protocol.put_u8(b' ');
}
Copy link
Member

Choose a reason for hiding this comment

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

nit: Could technically de-duplicate this trailing put:

Suggested change
if unwrapped_tags.is_empty() {
line_protocol.put_u8(b' ');
} else {
line_protocol.put_u8(b',');
encode_tags(unwrapped_tags, line_protocol);
line_protocol.put_u8(b' ');
}
if !unwrapped_tags.is_empty() {
line_protocol.put_u8(b',');
encode_tags(unwrapped_tags, line_protocol);
}
line_protocol.put_u8(b' ');


// Fields
encode_fields(protocol_version, unwrapped_fields, line_protocol);
Expand Down Expand Up @@ -442,12 +446,12 @@ pub mod test_util {
// 1542182950000000011
//
pub(crate) fn split_line_protocol(line_protocol: &str) -> (&str, &str, String, &str) {
let mut split = line_protocol.splitn(2, ',').collect::<Vec<&str>>();
let measurement = split[0];

split = split[1].splitn(3, ' ').collect::<Vec<&str>>();
// tags and ts may not be present
let parts: Vec<&str> = line_protocol.splitn(2, ' ').collect();
let (measurement, tags) = parts[0].split_once(',').unwrap_or((parts[0], ""));
let (fields, ts) = parts[1].split_once(' ').unwrap_or((parts[1], ""));
Copy link
Member

Choose a reason for hiding this comment

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

Would be better to use split_once instead of splitn(2) here, as it doesn't need to allocate a new Vec for the results. However, as a test it doesn't really matter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @bruceg , they are all nice suggestions, and I'm learning from it. I'll update them soon.


(measurement, split[0], split[1].to_string(), split[2])
(measurement, tags, fields.to_string(), ts)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Both tags and timestamp are optional.

}

fn client() -> reqwest::Client {
Expand Down