Skip to content

Commit 09a8c5e

Browse files
authored
Fix dogstatsd property test (#1413)
* 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> * clippy Signed-off-by: Brian L. Troutwine <brian.troutwine@datadoghq.com> * lifetime ding Signed-off-by: Brian L. Troutwine <brian.troutwine@datadoghq.com> --------- Signed-off-by: Brian L. Troutwine <brian.troutwine@datadoghq.com>
1 parent 742f93c commit 09a8c5e

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

lading_payload/src/dogstatsd.rs

Lines changed: 11 additions & 4 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
///
@@ -490,7 +491,7 @@ impl DogStatsD {
490491
/// # Errors
491492
///
492493
/// Function will error if a member could not be generated
493-
pub fn generate<R>(&self, rng: &mut R) -> Result<Member, crate::Error>
494+
pub fn generate<R>(&self, rng: &mut R) -> Result<Member<'_>, crate::Error>
494495
where
495496
R: rand::Rng + ?Sized,
496497
{
@@ -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)