Skip to content

Commit

Permalink
int-only, inclusive range
Browse files Browse the repository at this point in the history
Signed-off-by: Brian L. Troutwine <[email protected]>
  • Loading branch information
blt committed Sep 12, 2023
1 parent 22f547a commit 18f2dea
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## [0.18.1-rc3]
### Changed
- DogStatsD NumValue is now int-only for experimental purposes, range is also made inclusive. The inclusivity will remain.

## [0.18.1-rc2]
### Fixed
- DogStatsD value min/max is now 2**63 symmetric. This avoids a range issue in the `rand` crate.
Expand Down
16 changes: 8 additions & 8 deletions lading_payload/src/dogstatsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ fn contexts_maximum() -> u16 {
10_000
}

fn value_minimum() -> f64 {
-2_f64.powi(63)
fn value_minimum() -> i64 {
i64::MIN
}

fn value_maximum() -> f64 {
2_f64.powi(63)
fn value_maximum() -> i64 {
i64::MAX
}

// https://docs.datadoghq.com/developers/guide/what-best-practices-are-recommended-for-naming-metrics-and-tags/#rules-and-best-practices-for-naming-metrics
Expand Down Expand Up @@ -199,11 +199,11 @@ pub struct Config {

/// The minimum value to appear in metrics.
#[serde(default = "value_minimum")]
pub value_minimum: f64,
pub value_minimum: i64,

/// The maximum value to appear in metrics.
#[serde(default = "value_maximum")]
pub value_maximum: f64,
pub value_maximum: i64,
}

fn choose_or_not_ref<'a, R, T>(mut rng: &mut R, pool: &'a [T]) -> Option<&'a T>
Expand Down Expand Up @@ -289,7 +289,7 @@ impl MemberGenerator {
multivalue_pack_probability: f32,
kind_weights: KindWeights,
metric_weights: MetricWeights,
num_value_range: Range<f64>,
num_value_range: Range<i64>,
mut rng: &mut R,
) -> Self
where
Expand Down Expand Up @@ -474,7 +474,7 @@ impl DogStatsD {
multivalue_pack_probability: f32,
kind_weights: KindWeights,
metric_weights: MetricWeights,
num_value_range: Range<f64>,
num_value_range: Range<i64>,
rng: &mut R,
) -> Self
where
Expand Down
14 changes: 3 additions & 11 deletions lading_payload/src/dogstatsd/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,19 @@ pub(crate) mod tags;

#[derive(Clone, Debug)]
pub(crate) enum NumValue {
Float(f64),
Int(i64),
}

#[derive(Clone, Debug)]
pub(crate) struct NumValueGenerator {
float_distr: Uniform<f64>,
int_distr: Uniform<i64>,
}

impl NumValueGenerator {
#[allow(clippy::cast_possible_truncation)]
pub(crate) fn new(range: Range<f64>) -> Self {
pub(crate) fn new(range: Range<i64>) -> Self {
Self {
float_distr: Uniform::new(range.start, range.end),
int_distr: Uniform::new(range.start as i64, range.end as i64),
int_distr: Uniform::new_inclusive(range.start, range.end),
}
}
}
Expand All @@ -39,18 +36,13 @@ impl<'a> Generator<'a> for NumValueGenerator {
where
R: rand::Rng + ?Sized,
{
match rng.gen_range(0..=1) {
0 => NumValue::Float(self.float_distr.sample(rng)),
1 => NumValue::Int(self.int_distr.sample(rng)),
_ => unreachable!(),
}
NumValue::Int(self.int_distr.sample(rng))
}
}

impl fmt::Display for NumValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Float(val) => write!(f, "{val}"),
Self::Int(val) => write!(f, "{val}"),
}
}
Expand Down
2 changes: 1 addition & 1 deletion lading_payload/src/dogstatsd/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl MetricGenerator {
container_ids: Vec<String>,
tagsets: common::tags::Tagsets,
str_pool: &strings::Pool,
num_value_range: Range<f64>,
num_value_range: Range<i64>,
mut rng: &mut R,
) -> Self
where
Expand Down

0 comments on commit 18f2dea

Please sign in to comment.