Skip to content

Commit

Permalink
adjust config to allow float, be more ergonomic
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 16, 2023
1 parent 18f2dea commit e7ba21f
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 37 deletions.
10 changes: 4 additions & 6 deletions lading/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ impl Cache {
multivalue_count_maximum,
kind_weights,
metric_weights,
value_minimum,
value_maximum,
value,
}) => {
let context_range = *contexts_minimum..*contexts_maximum;
let tags_per_msg_range = *tags_per_msg_minimum..*tags_per_msg_maximum;
Expand All @@ -175,7 +174,7 @@ impl Cache {
*multivalue_pack_probability,
*kind_weights,
*metric_weights,
*value_minimum..*value_maximum,
*value,
&mut rng,
);

Expand Down Expand Up @@ -289,8 +288,7 @@ fn stream_inner(
multivalue_count_maximum,
kind_weights,
metric_weights,
value_minimum,
value_maximum,
value,
}) => {
let context_range = *contexts_minimum..*contexts_maximum;
let tags_per_msg_range = *tags_per_msg_minimum..*tags_per_msg_maximum;
Expand All @@ -309,7 +307,7 @@ fn stream_inner(
*multivalue_pack_probability,
*kind_weights,
*metric_weights,
*value_minimum..*value_maximum,
*value,
&mut rng,
);

Expand Down
65 changes: 43 additions & 22 deletions lading_payload/src/dogstatsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ fn contexts_maximum() -> u16 {
10_000
}

fn value_minimum() -> i64 {
i64::MIN
}

fn value_maximum() -> i64 {
i64::MAX
fn value_config() -> ValueConf {
ValueConf {
float_weight: 128, // 50%
range: ValueRange::Inclusive {
min: i64::MIN,
max: 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 @@ -126,6 +128,29 @@ impl Default for MetricWeights {
}
}

/// Configuration for the values of a metric.
#[derive(Debug, Deserialize, Clone, PartialEq, Copy)]
pub struct ValueConf {
/// Odds out of 256 that the value will be a float and not an integer.
float_weight: u8,
range: ValueRange,
}

/// Configuration for the values range of a metric.
#[derive(Debug, Deserialize, Clone, PartialEq, Copy)]
pub enum ValueRange {
/// Metric values are always constant.
Constant(i64),
/// Metric values are uniformly distributed between min and max, inclusive
/// of max.
Inclusive {
/// The minimum of the range.
min: i64,
/// The maximum of the range.
max: i64,
},
}

/// Configure the `DogStatsD` payload.
#[derive(Debug, Deserialize, Clone, PartialEq, Copy)]
pub struct Config {
Expand Down Expand Up @@ -197,13 +222,9 @@ pub struct Config {
#[serde(default)]
pub metric_weights: MetricWeights,

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

/// The maximum value to appear in metrics.
#[serde(default = "value_maximum")]
pub value_maximum: i64,
/// The configuration of values that appear in all metrics.
#[serde(default = "value_config")]
pub value: ValueConf,
}

fn choose_or_not_ref<'a, R, T>(mut rng: &mut R, pool: &'a [T]) -> Option<&'a T>
Expand Down Expand Up @@ -289,7 +310,7 @@ impl MemberGenerator {
multivalue_pack_probability: f32,
kind_weights: KindWeights,
metric_weights: MetricWeights,
num_value_range: Range<i64>,
value_conf: ValueConf,
mut rng: &mut R,
) -> Self
where
Expand Down Expand Up @@ -361,7 +382,7 @@ impl MemberGenerator {
small_strings,
tagsets.clone(),
pool.as_ref(),
num_value_range,
value_conf,
&mut rng,
);

Expand Down Expand Up @@ -444,7 +465,7 @@ impl DogStatsD {
multivalue_pack_probability(),
KindWeights::default(),
MetricWeights::default(),
value_minimum()..value_maximum(),
value_config(),
rng,
)
}
Expand Down Expand Up @@ -474,7 +495,7 @@ impl DogStatsD {
multivalue_pack_probability: f32,
kind_weights: KindWeights,
metric_weights: MetricWeights,
num_value_range: Range<i64>,
value_conf: ValueConf,
rng: &mut R,
) -> Self
where
Expand All @@ -490,7 +511,7 @@ impl DogStatsD {
multivalue_pack_probability,
kind_weights,
metric_weights,
num_value_range,
value_conf,
rng,
);

Expand Down Expand Up @@ -531,8 +552,8 @@ mod test {
contexts_maximum, contexts_minimum, multivalue_count_maximum, multivalue_count_minimum,
multivalue_pack_probability, name_length_maximum, name_length_minimum,
tag_key_length_maximum, tag_key_length_minimum, tag_value_length_maximum,
tag_value_length_minimum, tags_per_msg_maximum, tags_per_msg_minimum, value_maximum,
value_minimum, KindWeights, MetricWeights,
tag_value_length_minimum, tags_per_msg_maximum, tags_per_msg_minimum, value_config,
KindWeights, MetricWeights,
},
DogStatsD, Serialize,
};
Expand All @@ -551,14 +572,14 @@ mod test {
let tags_per_msg_range = tags_per_msg_minimum()..tags_per_msg_maximum();
let multivalue_count_range = multivalue_count_minimum()..multivalue_count_maximum();
let multivalue_pack_probability = multivalue_pack_probability();
let num_value_range = value_minimum()..value_maximum();
let value_conf = value_config();

let kind_weights = KindWeights::default();
let metric_weights = MetricWeights::default();
let dogstatsd = DogStatsD::new(context_range, name_length_range, tag_key_length_range,
tag_value_length_range, tags_per_msg_range,
multivalue_count_range, multivalue_pack_probability, kind_weights,
metric_weights, num_value_range, &mut rng);
metric_weights, value_conf, &mut rng);

let mut bytes = Vec::with_capacity(max_bytes);
dogstatsd.to_bytes(rng, max_bytes, &mut bytes).unwrap();
Expand Down
59 changes: 52 additions & 7 deletions lading_payload/src/dogstatsd/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt, ops::Range};
use std::fmt;

use rand::{
distributions::{Standard, Uniform},
Expand All @@ -8,23 +8,44 @@ use rand::{

use crate::Generator;

use super::{ValueConf, ValueRange};

pub(crate) mod tags;

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

#[derive(Clone, Debug)]
pub(crate) struct NumValueGenerator {
int_distr: Uniform<i64>,
pub(crate) enum NumValueGenerator {
Constant {
float_weight: u8,
int: i64,
float: f64,
},
Uniform {
float_weight: u8,
int_distr: Uniform<i64>,
float_distr: Uniform<f64>,
},
}

impl NumValueGenerator {
#[allow(clippy::cast_possible_truncation)]
pub(crate) fn new(range: Range<i64>) -> Self {
Self {
int_distr: Uniform::new_inclusive(range.start, range.end),
pub(crate) fn new(conf: ValueConf) -> Self {
match conf.range {
ValueRange::Constant(c) => Self::Constant {
float_weight: conf.float_weight,
int: c,
float: c as f64,
},
ValueRange::Inclusive { min, max } => Self::Uniform {
float_weight: conf.float_weight,
int_distr: Uniform::new_inclusive(min, max),
float_distr: Uniform::new_inclusive(min as f64, max as f64),
},
}
}
}
Expand All @@ -36,14 +57,38 @@ impl<'a> Generator<'a> for NumValueGenerator {
where
R: rand::Rng + ?Sized,
{
NumValue::Int(self.int_distr.sample(rng))
match self {
Self::Constant {
float_weight,
int,
float,
} => {
if rng.gen_ratio(u32::from(*float_weight), 256) {
NumValue::Float(*float)
} else {
NumValue::Int(*int)
}
}
Self::Uniform {
float_weight,
int_distr,
float_distr,
} => {
if rng.gen_ratio(u32::from(*float_weight), 256) {
NumValue::Float(float_distr.sample(rng))
} else {
NumValue::Int(int_distr.sample(rng))
}
}
}
}
}

impl fmt::Display for NumValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Int(val) => write!(f, "{val}"),
Self::Float(val) => write!(f, "{val}"),
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions lading_payload/src/dogstatsd/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use tracing::debug;
use super::{
choose_or_not_ref,
common::{self, NumValueGenerator},
ValueConf,
};

mod template;
Expand All @@ -36,7 +37,7 @@ impl MetricGenerator {
container_ids: Vec<String>,
tagsets: common::tags::Tagsets,
str_pool: &strings::Pool,
num_value_range: Range<i64>,
value_conf: ValueConf,
mut rng: &mut R,
) -> Self
where
Expand Down Expand Up @@ -70,7 +71,7 @@ impl MetricGenerator {
templates,
multivalue_count_range,
multivalue_pack_probability,
num_value_generator: NumValueGenerator::new(num_value_range),
num_value_generator: NumValueGenerator::new(value_conf),
}
}
}
Expand Down

0 comments on commit e7ba21f

Please sign in to comment.