Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce 'default tags' to the dogstatsd payload #752

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 4 additions & 0 deletions lading_payload/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ impl Cache {
metric_weights,
value,
service_check_names,
default_tags,
},
) => {
if !conf.valid() {
Expand All @@ -233,6 +234,7 @@ impl Cache {
*tag_key_length,
*tag_value_length,
*tags_per_msg,
default_tags.clone(),
*multivalue_count,
*multivalue_pack_probability,
*sampling_range,
Expand Down Expand Up @@ -357,6 +359,7 @@ fn stream_inner(
kind_weights,
metric_weights,
value,
default_tags,
},
) => {
if !conf.valid() {
Expand All @@ -369,6 +372,7 @@ fn stream_inner(
*tag_key_length,
*tag_value_length,
*tags_per_msg,
default_tags.clone(),
*multivalue_count,
*multivalue_pack_probability,
*sampling,
Expand Down
20 changes: 17 additions & 3 deletions lading_payload/src/dogstatsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ fn sampling_probability() -> f32 {
0.5
}

fn default_tags() -> Vec<String> {
Vec::new()
}

/// Weights for `DogStatsD` kinds: metrics, events, service checks
///
/// Defines the relative probability of each kind of `DogStatsD` datagram.
Expand Down Expand Up @@ -258,7 +262,7 @@ where
}

/// Configure the `DogStatsD` payload.
#[derive(Debug, Deserialize, Clone, PartialEq, Copy)]
#[derive(Debug, Deserialize, Clone, PartialEq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[serde(deny_unknown_fields)]
pub struct Config {
Expand Down Expand Up @@ -288,6 +292,11 @@ pub struct Config {
#[serde(default = "tags_per_msg")]
pub tags_per_msg: ConfRange<u8>,

/// Selection of tag pairs to always prepend to tagsets. Assumed to be
/// valid, do not count toward `tags_per_msg`.
#[serde(default = "default_tags")]
pub default_tags: Vec<String>,
Comment on lines +295 to +298
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: this could be validated using serde's deserialize_with attribute https://serde.rs/field-attrs.html#deserialize_with


/// Probability between 0 and 1 that a given dogstatsd msg
/// contains multiple values
#[serde(default = "multivalue_pack_probability")]
Expand Down Expand Up @@ -427,6 +436,7 @@ impl MemberGenerator {
tag_key_length: ConfRange<u8>,
tag_value_length: ConfRange<u8>,
tags_per_msg: ConfRange<u8>,
default_tags: Vec<String>,
multivalue_count: ConfRange<u16>,
multivalue_pack_probability: f32,
sampling: ConfRange<f32>,
Expand All @@ -448,6 +458,7 @@ impl MemberGenerator {
let mut tags_generator = tags::Generator::new(
rng.gen(),
tags_per_msg,
default_tags,
tag_key_length,
tag_value_length,
Rc::clone(&pool),
Expand Down Expand Up @@ -587,6 +598,7 @@ impl DogStatsD {
tag_key_length(),
tag_value_length(),
tags_per_msg(),
default_tags(),
multivalue_count(),
multivalue_pack_probability(),
sampling_range(),
Expand Down Expand Up @@ -622,6 +634,7 @@ impl DogStatsD {
tag_key_length: ConfRange<u8>,
tag_value_length: ConfRange<u8>,
tags_per_msg: ConfRange<u8>,
default_tags: Vec<String>,
multivalue_count: ConfRange<u16>,
multivalue_pack_probability: f32,
sampling: ConfRange<f32>,
Expand All @@ -641,6 +654,7 @@ impl DogStatsD {
tag_key_length,
tag_value_length,
tags_per_msg,
default_tags,
multivalue_count,
multivalue_pack_probability,
sampling,
Expand Down Expand Up @@ -690,7 +704,7 @@ mod test {

use crate::{
dogstatsd::{
contexts, multivalue_count, multivalue_pack_probability, name_length,
contexts, default_tags, multivalue_count, multivalue_pack_probability, name_length,
sampling_probability, sampling_range, service_check_names, tag_key_length,
tag_value_length, tags_per_msg, value_config, KindWeights, MetricWeights,
},
Expand All @@ -711,7 +725,7 @@ mod test {
let metric_weights = MetricWeights::default();
let dogstatsd = DogStatsD::new(contexts(), service_check_names(),
name_length(), tag_key_length(),
tag_value_length(), tags_per_msg(),
tag_value_length(), tags_per_msg(), default_tags(),
multivalue_count(), multivalue_pack_probability, sampling_range(), sampling_probability(), kind_weights,
metric_weights, value_conf, &mut rng).unwrap();

Expand Down
19 changes: 18 additions & 1 deletion lading_payload/src/dogstatsd/common/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
rc::Rc,
};

use rand::{rngs::SmallRng, SeedableRng};
use rand::{rngs::SmallRng, seq::SliceRandom, SeedableRng};

use crate::{common::strings, dogstatsd::ConfRange};

Expand All @@ -23,6 +23,7 @@ pub(crate) struct Generator {
internal_rng: RefCell<SmallRng>,
tagsets_produced: Cell<usize>,
num_tagsets: usize,
default_tags: Vec<String>,
tags_per_msg: ConfRange<u8>,
tag_key_length: ConfRange<u8>,
tag_value_length: ConfRange<u8>,
Expand All @@ -33,6 +34,7 @@ impl Generator {
pub(crate) fn new(
seed: u64,
tags_per_msg: ConfRange<u8>,
default_tags: Vec<String>,
tag_key_length: ConfRange<u8>,
tag_value_length: ConfRange<u8>,
str_pool: Rc<strings::Pool>,
Expand All @@ -41,6 +43,7 @@ impl Generator {
Generator {
seed: Cell::new(seed),
internal_rng: RefCell::new(SmallRng::seed_from_u64(seed)),
default_tags,
tags_per_msg,
tag_key_length,
tag_value_length,
Expand All @@ -61,6 +64,19 @@ impl<'a> crate::Generator<'a> for Generator {
{
let mut tagset = Vec::new();

if let Some(default_tag) = self
.default_tags
.choose(&mut *self.internal_rng.borrow_mut())
Comment on lines +67 to +69
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be misunderstanding how this works; this looks like it'd pick one tag to prepend. Is that correct? That behavior doesn't match the config doc string.

{
// The user is allowed to pass an empty string in as a default
// tagset. Save ourselves a little allocation and avoid putting ""
// in tagset. This also means we don't have to cope with empty
// strings when it comes time to serialize to text.
if !default_tag.is_empty() {
tagset.push(default_tag.clone());
}
}

let tags_count = self
.tags_per_msg
.sample(&mut *self.internal_rng.borrow_mut()) as usize;
Expand Down Expand Up @@ -109,6 +125,7 @@ mod test {
let generator = tags::Generator::new(
seed,
ConfRange::Inclusive{min: 0, max: tags_per_msg_max},
Vec::new(),
ConfRange::Inclusive{min: 1, max: 64},
ConfRange::Inclusive{min: 1, max: 64},
pool.clone(),
Expand Down