Skip to content

Commit 808b9d1

Browse files
committed
Fix dogstatsd property test
This commit fixes our prefix length calculation, triggered only when max_bytes is low. We were removing the size of a `usize` when `u32` was appropriate, as that is the proper length of the payload prefix. Signed-off-by: Brian L. Troutwine <brian.troutwine@datadoghq.com>
1 parent 144fec6 commit 808b9d1

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

lading_payload/src/dogstatsd.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub mod service_check;
2929

3030
const MAX_CONTEXTS: u32 = 1_000_000;
3131
const MAX_NAME_LENGTH: u16 = 4_096;
32+
const LENGTH_PREFIX_SIZE: usize = std::mem::size_of::<u32>();
3233

3334
/// Weights for `DogStatsD` kinds: metrics, events, service checks
3435
///
@@ -566,9 +567,15 @@ impl DogStatsD {
566567
R: Rng + Sized,
567568
W: Write,
568569
{
569-
let mut bytes_remaining = max_bytes.saturating_sub(std::mem::size_of::<usize>());
570+
// If max_bytes is less than the length prefix size, we can't write anything
571+
if max_bytes < LENGTH_PREFIX_SIZE {
572+
return Ok(());
573+
}
574+
575+
let mut bytes_remaining = max_bytes.saturating_sub(LENGTH_PREFIX_SIZE);
570576
let mut members = Vec::new();
571-
// generate as many messages as we can fit
577+
// Generate as many messages as we can fit, If we couldn't fit any
578+
// members, don't write anything.
572579
loop {
573580
let member: Member = self.member_generator.generate(&mut rng)?;
574581
let encoding = format!("{member}");
@@ -581,7 +588,7 @@ impl DogStatsD {
581588
None => break,
582589
}
583590
}
584-
if bytes_remaining == max_bytes {
591+
if members.is_empty() {
585592
return Ok(());
586593
}
587594

0 commit comments

Comments
 (0)