Skip to content
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 lading_payload/proptest-regressions/dogstatsd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc 97158e48115dce6b53c08d3f3744ebd92cb711273b19aee225daa774f71f6e23 # shrinks to seed = 0, max_bytes = 0
cc 077dc27bd44ddd568537d3742556e1a422619e126a2fbf86c7e7f54374780baf # shrinks to seed = 11798065272331789525, max_bytes = 3627
4 changes: 2 additions & 2 deletions lading_payload/src/apache_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ impl<'a> Generator<'a> for ApacheCommon {
}

impl crate::Serialize for ApacheCommon {
fn to_bytes<W, R>(&self, mut rng: R, max_bytes: usize, writer: &mut W) -> Result<(), Error>
fn to_bytes<W, R>(&mut self, mut rng: R, max_bytes: usize, writer: &mut W) -> Result<(), Error>
where
R: Rng + Sized,
W: Write,
Expand Down Expand Up @@ -377,7 +377,7 @@ mod test {
fn payload_not_exceed_max_bytes(seed: u64, max_bytes: u16) {
let max_bytes = max_bytes as usize;
let mut rng = SmallRng::seed_from_u64(seed);
let apache = ApacheCommon::new(&mut rng);
let mut apache = ApacheCommon::new(&mut rng);

let mut bytes = Vec::with_capacity(max_bytes);
apache.to_bytes(rng, max_bytes, &mut bytes).expect("failed to convert to bytes");
Expand Down
4 changes: 2 additions & 2 deletions lading_payload/src/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Ascii {
}

impl crate::Serialize for Ascii {
fn to_bytes<W, R>(&self, mut rng: R, max_bytes: usize, writer: &mut W) -> Result<(), Error>
fn to_bytes<W, R>(&mut self, mut rng: R, max_bytes: usize, writer: &mut W) -> Result<(), Error>
where
R: Rng + Sized,
W: Write,
Expand Down Expand Up @@ -69,7 +69,7 @@ mod test {
fn payload_not_exceed_max_bytes(seed: u64, max_bytes: u16) {
let max_bytes = max_bytes as usize;
let mut rng = SmallRng::seed_from_u64(seed);
let ascii = Ascii::new(&mut rng);
let mut ascii = Ascii::new(&mut rng);

let mut bytes = Vec::with_capacity(max_bytes);
ascii.to_bytes(rng, max_bytes, &mut bytes)?;
Expand Down
57 changes: 33 additions & 24 deletions lading_payload/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,23 +192,29 @@ impl Cache {

let blocks = match payload {
crate::Config::TraceAgent(enc) => {
let ta = match enc {
let mut ta = match enc {
crate::Encoding::Json => crate::TraceAgent::json(&mut rng),
crate::Encoding::MsgPack => crate::TraceAgent::msg_pack(&mut rng),
};

let span = span!(Level::INFO, "fixed", payload = "trace-agent");
let _guard = span.enter();

construct_block_cache_inner(&mut rng, &ta, maximum_block_bytes, total_bytes.get())?
construct_block_cache_inner(
&mut rng,
&mut ta,
maximum_block_bytes,
total_bytes.get(),
)?
}
crate::Config::Syslog5424 => {
let span = span!(Level::INFO, "fixed", payload = "syslog5424");
let _guard = span.enter();

let mut syslog = crate::Syslog5424::default();
construct_block_cache_inner(
&mut rng,
&crate::Syslog5424::default(),
&mut syslog,
maximum_block_bytes,
total_bytes.get(),
)?
Expand All @@ -221,109 +227,112 @@ impl Cache {
return Err(Error::InvalidConfig(e));
}
}
let serializer = crate::DogStatsD::new(*conf, &mut rng)?;
let mut serializer = crate::DogStatsD::new(*conf, &mut rng)?;

let span = span!(Level::INFO, "fixed", payload = "dogstatsd");
let _guard = span.enter();

construct_block_cache_inner(
&mut rng,
&serializer,
&mut serializer,
maximum_block_bytes,
total_bytes.get(),
)?
}
crate::Config::Fluent => {
let pyld = crate::Fluent::new(&mut rng);
let mut pyld = crate::Fluent::new(&mut rng);
let span = span!(Level::INFO, "fixed", payload = "fluent");
let _guard = span.enter();
construct_block_cache_inner(
&mut rng,
&pyld,
&mut pyld,
maximum_block_bytes,
total_bytes.get(),
)?
}
crate::Config::SplunkHec { encoding } => {
let span = span!(Level::INFO, "fixed", payload = "splunkHec");
let _guard = span.enter();
let mut splunk_hec = crate::SplunkHec::new(*encoding);
construct_block_cache_inner(
&mut rng,
&crate::SplunkHec::new(*encoding),
&mut splunk_hec,
maximum_block_bytes,
total_bytes.get(),
)?
}
crate::Config::ApacheCommon => {
let pyld = crate::ApacheCommon::new(&mut rng);
let mut pyld = crate::ApacheCommon::new(&mut rng);
let span = span!(Level::INFO, "fixed", payload = "apache-common");
let _guard = span.enter();
construct_block_cache_inner(
&mut rng,
&pyld,
&mut pyld,
maximum_block_bytes,
total_bytes.get(),
)?
}
crate::Config::Ascii => {
let pyld = crate::Ascii::new(&mut rng);
let mut pyld = crate::Ascii::new(&mut rng);
let span = span!(Level::INFO, "fixed", payload = "ascii");
let _guard = span.enter();
construct_block_cache_inner(
&mut rng,
&pyld,
&mut pyld,
maximum_block_bytes,
total_bytes.get(),
)?
}
crate::Config::DatadogLog => {
let serializer = crate::DatadogLog::new(&mut rng);
let mut serializer = crate::DatadogLog::new(&mut rng);
let span = span!(Level::INFO, "fixed", payload = "datadog-log");
let _guard = span.enter();
construct_block_cache_inner(
&mut rng,
&serializer,
&mut serializer,
maximum_block_bytes,
total_bytes.get(),
)?
}
crate::Config::Json => {
let span = span!(Level::INFO, "fixed", payload = "json");
let _guard = span.enter();
let mut json = crate::Json;
construct_block_cache_inner(
&mut rng,
&crate::Json,
&mut json,
maximum_block_bytes,
total_bytes.get(),
)?
}
crate::Config::Static { static_path } => {
let span = span!(Level::INFO, "fixed", payload = "static");
let _guard = span.enter();
let mut static_serializer = crate::Static::new(static_path)?;
construct_block_cache_inner(
&mut rng,
&crate::Static::new(static_path)?,
&mut static_serializer,
maximum_block_bytes,
total_bytes.get(),
)?
}
crate::Config::OpentelemetryTraces => {
let pyld = crate::OpentelemetryTraces::new(&mut rng);
let mut pyld = crate::OpentelemetryTraces::new(&mut rng);
let span = span!(Level::INFO, "fixed", payload = "otel-traces");
let _guard = span.enter();
construct_block_cache_inner(rng, &pyld, maximum_block_bytes, total_bytes.get())?
construct_block_cache_inner(rng, &mut pyld, maximum_block_bytes, total_bytes.get())?
}
crate::Config::OpentelemetryLogs => {
let pyld = crate::OpentelemetryLogs::new(&mut rng);
let mut pyld = crate::OpentelemetryLogs::new(&mut rng);
let span = span!(Level::INFO, "fixed", payload = "otel-logs");
let _guard = span.enter();
construct_block_cache_inner(rng, &pyld, maximum_block_bytes, total_bytes.get())?
construct_block_cache_inner(rng, &mut pyld, maximum_block_bytes, total_bytes.get())?
}
crate::Config::OpentelemetryMetrics => {
let pyld = crate::OpentelemetryMetrics::new(&mut rng);
let mut pyld = crate::OpentelemetryMetrics::new(&mut rng);
let span = span!(Level::INFO, "fixed", payload = "otel-metrics");
let _guard = span.enter();
construct_block_cache_inner(rng, &pyld, maximum_block_bytes, total_bytes.get())?
construct_block_cache_inner(rng, &mut pyld, maximum_block_bytes, total_bytes.get())?
}
};

Expand Down Expand Up @@ -467,7 +476,7 @@ impl Cache {
#[allow(clippy::cast_sign_loss)]
fn construct_block_cache_inner<R, S>(
mut rng: &mut R,
serializer: &S,
serializer: &mut S,
max_block_size: u32,
total_bytes: u32,
) -> Result<Vec<Block>, SpinError>
Expand Down Expand Up @@ -573,7 +582,7 @@ where
#[inline]
fn construct_block<R, S>(
mut rng: &mut R,
serializer: &S,
serializer: &mut S,
chunk_size: u32,
) -> Result<Block, SpinError>
where
Expand Down
6 changes: 3 additions & 3 deletions lading_payload/src/datadog_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<'a> Generator<'a> for DatadogLog {
}

impl crate::Serialize for DatadogLog {
fn to_bytes<W, R>(&self, mut rng: R, max_bytes: usize, writer: &mut W) -> Result<(), Error>
fn to_bytes<W, R>(&mut self, mut rng: R, max_bytes: usize, writer: &mut W) -> Result<(), Error>
where
W: Write,
R: Rng + Sized,
Expand Down Expand Up @@ -178,7 +178,7 @@ mod test {
fn payload_not_exceed_max_bytes(seed: u64, max_bytes: u16) {
let max_bytes = max_bytes as usize;
let mut rng = SmallRng::seed_from_u64(seed);
let ddlogs = DatadogLog::new(&mut rng);
let mut ddlogs = DatadogLog::new(&mut rng);

let mut bytes = Vec::with_capacity(max_bytes);
ddlogs.to_bytes(rng, max_bytes, &mut bytes).expect("failed to convert to bytes");
Expand All @@ -197,7 +197,7 @@ mod test {
fn every_payload_deserializes(seed: u64, max_bytes: u16) {
let max_bytes = max_bytes as usize;
let mut rng = SmallRng::seed_from_u64(seed);
let ddlogs = DatadogLog::new(&mut rng);
let mut ddlogs = DatadogLog::new(&mut rng);

let mut bytes: Vec<u8> = Vec::with_capacity(max_bytes);
ddlogs.to_bytes(rng, max_bytes, &mut bytes).expect("failed to convert to bytes");
Expand Down
29 changes: 18 additions & 11 deletions lading_payload/src/dogstatsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,15 +663,20 @@ impl DogStatsD {
}

impl Serialize for DogStatsD {
fn to_bytes<W, R>(&self, rng: R, max_bytes: usize, writer: &mut W) -> Result<(), crate::Error>
fn to_bytes<W, R>(
&mut self,
rng: R,
max_bytes: usize,
writer: &mut W,
) -> Result<(), crate::Error>
where
R: Rng + Sized,
W: Write,
{
if self.length_prefix_framed {
self.to_bytes_length_prefix_framed(rng, max_bytes, writer)
} else {
self.to_bytes(rng, max_bytes, writer)
self.to_bytes_unframed(rng, max_bytes, writer)
}
}
}
Expand All @@ -686,7 +691,7 @@ impl DogStatsD {
R: Rng + Sized,
W: Write,
{
let mut bytes_remaining = max_bytes;
let mut bytes_remaining = max_bytes.saturating_sub(std::mem::size_of::<usize>());
let mut members = Vec::new();
// generate as many messages as we can fit
loop {
Expand Down Expand Up @@ -733,7 +738,8 @@ impl DogStatsD {

Ok(())
}
fn to_bytes<W, R>(

fn to_bytes_unframed<W, R>(
&self,
mut rng: R,
max_bytes: usize,
Expand Down Expand Up @@ -775,7 +781,7 @@ mod test {
use proptest::prelude::*;
use rand::{SeedableRng, rngs::SmallRng};

use crate::DogStatsD;
use crate::{DogStatsD, Serialize};

// We want to be sure that the serialized size of the payload does not
// exceed `max_bytes`.
Expand All @@ -786,11 +792,11 @@ mod test {
let mut rng = SmallRng::seed_from_u64(seed);

let dogstatsd_config = Config::default();
let dogstatsd = DogStatsD::new(dogstatsd_config, &mut rng)?;
let mut dogstatsd = DogStatsD::new(dogstatsd_config, &mut rng)?;

let mut bytes = Vec::with_capacity(max_bytes);
dogstatsd.to_bytes(rng, max_bytes, &mut bytes)?;
debug_assert!(
prop_assert!(
bytes.len() <= max_bytes,
"{:?}",
std::str::from_utf8(&bytes).expect("failed to convert from utf-8 to str")
Expand All @@ -807,14 +813,15 @@ mod test {
let mut rng = SmallRng::seed_from_u64(seed);

let dogstatsd_config = Config { length_prefix_framed: true, ..Default::default() };
let dogstatsd = DogStatsD::new(dogstatsd_config, &mut rng).expect("failed to create DogStatsD");
let mut dogstatsd = DogStatsD::new(dogstatsd_config, &mut rng).expect("failed to create DogStatsD");

let mut bytes = Vec::with_capacity(max_bytes);
dogstatsd.to_bytes(rng, max_bytes, &mut bytes).expect("failed to convert to bytes");
debug_assert!(
prop_assert!(
bytes.len() <= max_bytes,
"{:?}",
std::str::from_utf8(&bytes).expect("failed to convert from utf-8 to str")
"{l} <= {max_bytes}, {pyld:?}",
l = bytes.len(),
pyld = std::str::from_utf8(&bytes).expect("failed to convert from utf-8 to str")
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lading_payload/src/fluent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ struct Entry<'a> {
}

impl crate::Serialize for Fluent {
fn to_bytes<W, R>(&self, mut rng: R, max_bytes: usize, writer: &mut W) -> Result<(), Error>
fn to_bytes<W, R>(&mut self, mut rng: R, max_bytes: usize, writer: &mut W) -> Result<(), Error>
where
W: Write,
R: Rng + Sized,
Expand Down Expand Up @@ -218,7 +218,7 @@ mod test {
fn payload_not_exceed_max_bytes(seed: u64, max_bytes: u16) {
let max_bytes = max_bytes as usize;
let mut rng = SmallRng::seed_from_u64(seed);
let fluent = Fluent::new(&mut rng);
let mut fluent = Fluent::new(&mut rng);

let mut bytes = Vec::with_capacity(max_bytes);
fluent.to_bytes(rng, max_bytes, &mut bytes).expect("failed to convert to bytes");
Expand Down
6 changes: 3 additions & 3 deletions lading_payload/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<'a> Generator<'a> for Json {
}

impl crate::Serialize for Json {
fn to_bytes<W, R>(&self, mut rng: R, max_bytes: usize, writer: &mut W) -> Result<(), Error>
fn to_bytes<W, R>(&mut self, mut rng: R, max_bytes: usize, writer: &mut W) -> Result<(), Error>
where
R: Rng + Sized,
W: Write,
Expand Down Expand Up @@ -96,7 +96,7 @@ mod test {
fn payload_not_exceed_max_bytes(seed: u64, max_bytes: u16) {
let max_bytes = max_bytes as usize;
let rng = SmallRng::seed_from_u64(seed);
let json = Json;
let mut json = Json;

let mut bytes = Vec::with_capacity(max_bytes);
json.to_bytes(rng, max_bytes, &mut bytes).expect("failed to convert to bytes");
Expand All @@ -111,7 +111,7 @@ mod test {
fn every_payload_deserializes(seed: u64, max_bytes: u16) {
let max_bytes = max_bytes as usize;
let rng = SmallRng::seed_from_u64(seed);
let json = Json;
let mut json = Json;

let mut bytes: Vec<u8> = Vec::with_capacity(max_bytes);
json.to_bytes(rng, max_bytes, &mut bytes).expect("failed to convert to bytes");
Expand Down
4 changes: 2 additions & 2 deletions lading_payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub trait Serialize {
///
/// Most implementations are serializing data in some way. The errors that
/// result come from serialization crackups.
fn to_bytes<W, R>(&self, rng: R, max_bytes: usize, writer: &mut W) -> Result<(), Error>
fn to_bytes<W, R>(&mut self, rng: R, max_bytes: usize, writer: &mut W) -> Result<(), Error>
where
R: Rng + Sized,
W: Write;
Expand Down Expand Up @@ -171,7 +171,7 @@ pub(crate) enum Payload {
}

impl Serialize for Payload {
fn to_bytes<W, R>(&self, rng: R, max_bytes: usize, writer: &mut W) -> Result<(), Error>
fn to_bytes<W, R>(&mut self, rng: R, max_bytes: usize, writer: &mut W) -> Result<(), Error>
where
W: Write,
R: Rng + Sized,
Expand Down
Loading
Loading