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

UTF-8 decoding and handling #50

Merged
merged 6 commits into from
Oct 24, 2023
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ anyhow = "1"
async-trait = "0"
aws-config = { version = "0" }
aws-sdk-kinesis = { version = "0" }
base64 = "0"
chrono = { version = "0", features = ["clock", "std"] }
clap = { version = "4", features = ["derive"] }
colored = "2"
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ The [release page](https://github.com/grumlimited/kinesis-tailr/releases) provid
-o, --output-file <OUTPUT_FILE> Output file to write to
-c, --concurrent <CONCURRENT> Concurrent number of shards to tail
-v, --verbose Display additional information
-n, --no-base64 Do not base64 encode the payload upon invalid UTF-8 payloads. Print it raw instead
-h, --help Print help
-V, --version Print version

Expand All @@ -65,6 +66,14 @@ kinesis-tailr \
--max-messages 2
```

### UTF-8

`kinesis-tailr` expects payloads to be UTF-8 encoded. If a payload is not UTF-8 encoded, it will be base64 encoded and printed as such.

It might be useful to print the raw payload instead though. This can be achieved with the `--no-base64` flag.

Properly UTF-8 encoded payloads will be printed as such and never base64 encoded.

### Logging

General logging level for debugging can be turned on with:
Expand Down
4 changes: 4 additions & 0 deletions src/cli_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ pub struct Opt {
/// Display additional information
#[structopt(short, long)]
pub verbose: bool,

/// Do not base64 encode the payload upon invalid UTF-8 payloads. Print it raw instead.
#[structopt(short, long)]
pub no_base64: bool,
}

pub(crate) fn selected_shards(
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ async fn main() -> Result<()> {
opt.print_shard_id,
opt.print_timestamp,
opt.print_delimiter,
opt.no_base64,
shard_count,
file,
)
Expand All @@ -82,6 +83,7 @@ async fn main() -> Result<()> {
opt.print_shard_id,
opt.print_timestamp,
opt.print_delimiter,
opt.no_base64,
shard_count,
)
.run(tx_records, rx_records)
Expand Down
19 changes: 14 additions & 5 deletions src/sink.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::io;
use std::io::{BufWriter, Write};

use anyhow::Error;
use anyhow::Result;
use async_trait::async_trait;
use chrono::TimeZone;
use log::{debug, error, warn};
use std::io;
use std::io::{BufWriter, Write};
use tokio::sync::mpsc::{Receiver, Sender};

use crate::kinesis::models::{ProcessError, RecordResult, ShardProcessorADT};
Expand All @@ -22,6 +23,7 @@ pub struct SinkConfig {
print_timestamp: bool,
print_delimiter: bool,
exit_after_termination: bool,
no_base64: bool,
}

pub trait Configurable {
Expand Down Expand Up @@ -225,9 +227,16 @@ where
}

fn format_record(&self, record_result: &RecordResult) -> String {
let data = std::str::from_utf8(record_result.data.as_slice())
.unwrap()
.to_string();
let data = match std::str::from_utf8(record_result.data.as_slice()) {
Ok(payload) => payload.to_string(),
Err(_) if self.get_config().no_base64 => {
String::from_utf8_lossy(record_result.data.as_slice()).to_string()
}
Err(_) => {
use base64::{engine::general_purpose, Engine as _};
general_purpose::STANDARD.encode(record_result.data.as_slice())
}
};

let data = if self.get_config().print_key {
let key = record_result.partition_key.to_string();
Expand Down
2 changes: 2 additions & 0 deletions src/sink/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl ConsoleSink {
print_shard_id: bool,
print_timestamp: bool,
print_delimiter: bool,
no_base64: bool,
shard_count: usize,
) -> Self {
ConsoleSink {
Expand All @@ -31,6 +32,7 @@ impl ConsoleSink {
print_shard_id,
print_timestamp,
print_delimiter,
no_base64,
exit_after_termination: true,
},
shard_count,
Expand Down
57 changes: 47 additions & 10 deletions src/sink/console_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@ use tokio::sync::mpsc;
#[test]
fn format_nb_messages_ok() {
let console = ConsoleSink {
config: SinkConfig {
max_messages: None,
no_color: false,
print_key: false,
print_sequence_number: false,
print_shard_id: false,
print_timestamp: false,
print_delimiter: false,
exit_after_termination: false,
},
config: SinkConfig::default(),
shard_count: 1,
};

Expand Down Expand Up @@ -54,6 +45,52 @@ fn format_outputs() {
assert_eq!(bw_console.write_delimiter("data"), "data");
}

#[test]
fn format_outputs_base64() {
let console = ConsoleSink {
config: SinkConfig {
no_color: true,
..Default::default()
},
shard_count: 1,
};

let input = b"Hello \xF0\x90\x80World";

let record = RecordResult {
shard_id: "shard_id".to_string(),
sequence_id: "sequence_id".to_string(),
partition_key: "partition_key".to_string(),
datetime: DateTime::from_secs(1_000_000_i64),
data: input.to_vec(),
};

assert_eq!(console.format_record(&record), "SGVsbG8g8JCAV29ybGQ=");
}

#[test]
fn format_outputs_no_base64() {
let console = ConsoleSink {
config: SinkConfig {
no_base64: true,
..Default::default()
},
shard_count: 1,
};

let input = b"Hello \xF0\x90\x80World";

let record = RecordResult {
shard_id: "shard_id".to_string(),
sequence_id: "sequence_id".to_string(),
partition_key: "partition_key".to_string(),
datetime: DateTime::from_secs(1_000_000_i64),
data: input.to_vec(),
};

assert_eq!(console.format_record(&record), "Hello �World");
}

#[tokio::test]
async fn expect_zero_messages_processed() {
let (tx_records, rx_records) = mpsc::channel::<Result<ShardProcessorADT, ProcessError>>(1);
Expand Down
2 changes: 2 additions & 0 deletions src/sink/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl FileSink {
print_shard_id: bool,
print_timestamp: bool,
print_delimiter: bool,
no_base64: bool,
shard_count: usize,
file: P,
) -> Self {
Expand All @@ -35,6 +36,7 @@ impl FileSink {
print_shard_id,
print_timestamp,
print_delimiter,
no_base64,
exit_after_termination: true,
},
file: file.into(),
Expand Down