Skip to content

Commit 7f576ee

Browse files
committed
Migrate OTel/dogstatsd common functions to src/common
This commit is in preparation for more extensive changes to the OTel metrics payload. I have scooted common configuration and string selection functions up into the crate common sub-tree. REF SMPTNG-659 Signed-off-by: Brian L. Troutwine <brian.troutwine@datadoghq.com>
1 parent 6ffbd3d commit 7f576ee

File tree

7 files changed

+152
-144
lines changed

7 files changed

+152
-144
lines changed

lading_payload/src/common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
pub(crate) mod config;
12
pub(crate) mod strings;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use rand::distr::uniform::SampleUniform;
2+
use serde::{Deserialize, Serialize as SerdeSerialize};
3+
use std::cmp;
4+
5+
/// Range expression for configuration
6+
#[derive(Debug, Deserialize, SerdeSerialize, Clone, PartialEq, Copy)]
7+
#[serde(deny_unknown_fields)]
8+
#[serde(rename_all = "snake_case")]
9+
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
10+
11+
pub enum ConfRange<T>
12+
where
13+
T: PartialEq + cmp::PartialOrd + Clone + Copy,
14+
{
15+
/// A constant T
16+
Constant(T),
17+
/// In which a T is chosen between `min` and `max`, inclusive of `max`.
18+
Inclusive {
19+
/// The minimum of the range.
20+
min: T,
21+
/// The maximum of the range.
22+
max: T,
23+
},
24+
}
25+
26+
impl<T> ConfRange<T>
27+
where
28+
T: PartialEq + cmp::PartialOrd + Clone + Copy,
29+
{
30+
/// Returns true if the range provided by the user is valid, false
31+
/// otherwise.
32+
pub(crate) fn valid(&self) -> (bool, &'static str) {
33+
match self {
34+
Self::Constant(_) => (true, ""),
35+
Self::Inclusive { min, max } => (min < max, "min must be less than max"),
36+
}
37+
}
38+
39+
pub(crate) fn start(&self) -> T {
40+
match self {
41+
ConfRange::Constant(c) => *c,
42+
ConfRange::Inclusive { min, .. } => *min,
43+
}
44+
}
45+
46+
pub(crate) fn end(&self) -> T {
47+
match self {
48+
ConfRange::Constant(c) => *c,
49+
ConfRange::Inclusive { max, .. } => *max,
50+
}
51+
}
52+
}
53+
54+
impl<T> ConfRange<T>
55+
where
56+
T: PartialEq + cmp::PartialOrd + Clone + Copy + SampleUniform,
57+
{
58+
pub(crate) fn sample<R>(&self, rng: &mut R) -> T
59+
where
60+
R: rand::Rng + ?Sized,
61+
{
62+
match self {
63+
ConfRange::Constant(c) => *c,
64+
ConfRange::Inclusive { min, max } => rng.random_range(*min..=*max),
65+
}
66+
}
67+
}

lading_payload/src/common/strings.rs

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! Code for the quick creation of randomize strings
22
3+
use crate::common::config::ConfRange;
4+
use rand::{Rng, distr::uniform::SampleUniform, seq::IndexedRandom};
35
use std::ops::Range;
46

5-
use rand::distr::uniform::SampleUniform;
6-
77
const ALPHANUM: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
88

99
/// A pool of strings
@@ -131,6 +131,70 @@ impl Pool {
131131
}
132132
}
133133

134+
pub(crate) fn choose_or_not_ref<'a, R, T>(mut rng: &mut R, pool: &'a [T]) -> Option<&'a T>
135+
where
136+
R: rand::Rng + ?Sized,
137+
{
138+
if rng.random() {
139+
pool.choose(&mut rng)
140+
} else {
141+
None
142+
}
143+
}
144+
145+
pub(crate) fn choose_or_not_fn<R, T, F>(rng: &mut R, func: F) -> Option<T>
146+
where
147+
T: Clone,
148+
R: rand::Rng + ?Sized,
149+
F: FnOnce(&mut R) -> Option<T>,
150+
{
151+
if rng.random() { func(rng) } else { None }
152+
}
153+
154+
#[inline]
155+
/// Generate a total number of strings randomly chosen from the range `min_max` with a maximum length
156+
/// per string of `max_length`.
157+
pub(crate) fn random_strings_with_length<R>(
158+
pool: &Pool,
159+
min_max: Range<usize>,
160+
max_length: u16,
161+
rng: &mut R,
162+
) -> Vec<String>
163+
where
164+
R: Rng + ?Sized,
165+
{
166+
let total = rng.random_range(min_max);
167+
let length_range = ConfRange::Inclusive {
168+
min: 1,
169+
max: max_length,
170+
};
171+
172+
random_strings_with_length_range(pool, total, length_range, rng)
173+
}
174+
175+
#[inline]
176+
/// Generate a `total` number of strings with a maximum length per string of
177+
/// `max_length`.
178+
pub(crate) fn random_strings_with_length_range<R>(
179+
pool: &Pool,
180+
total: usize,
181+
length_range: ConfRange<u16>,
182+
mut rng: &mut R,
183+
) -> Vec<String>
184+
where
185+
R: Rng + ?Sized,
186+
{
187+
let mut buf = Vec::with_capacity(total);
188+
for _ in 0..total {
189+
let sz = length_range.sample(&mut rng) as usize;
190+
buf.push(String::from(
191+
pool.of_size(&mut rng, sz)
192+
.expect("failed to generate string"),
193+
));
194+
}
195+
buf
196+
}
197+
134198
#[cfg(test)]
135199
mod test {
136200
use proptest::prelude::*;

lading_payload/src/dogstatsd.rs

Lines changed: 10 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
//! `DogStatsD` payload.
22
3-
use std::{cmp, fmt, io::Write, ops::Range, rc::Rc};
3+
use std::{fmt, io::Write, rc::Rc};
44

5-
use rand::{
6-
Rng,
7-
distr::{uniform::SampleUniform, weighted::WeightedIndex},
8-
prelude::Distribution,
9-
seq::IndexedRandom,
10-
};
5+
use rand::{Rng, distr::weighted::WeightedIndex, prelude::Distribution};
116
use serde::{Deserialize, Serialize as SerdeSerialize};
127
use tracing::{debug, warn};
138

14-
use crate::{Serialize, common::strings};
9+
use crate::{
10+
Serialize,
11+
common::{
12+
config::ConfRange,
13+
strings,
14+
strings::{random_strings_with_length, random_strings_with_length_range},
15+
},
16+
};
1517

1618
use self::{
1719
common::tags, event::EventGenerator, metric::MetricGenerator,
@@ -144,69 +146,6 @@ impl Default for ValueConf {
144146
}
145147
}
146148
}
147-
/// Range expression for configuration
148-
#[derive(Debug, Deserialize, SerdeSerialize, Clone, PartialEq, Copy)]
149-
#[serde(deny_unknown_fields)]
150-
#[serde(rename_all = "snake_case")]
151-
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
152-
153-
pub enum ConfRange<T>
154-
where
155-
T: PartialEq + cmp::PartialOrd + Clone + Copy,
156-
{
157-
/// A constant T
158-
Constant(T),
159-
/// In which a T is chosen between `min` and `max`, inclusive of `max`.
160-
Inclusive {
161-
/// The minimum of the range.
162-
min: T,
163-
/// The maximum of the range.
164-
max: T,
165-
},
166-
}
167-
168-
impl<T> ConfRange<T>
169-
where
170-
T: PartialEq + cmp::PartialOrd + Clone + Copy,
171-
{
172-
/// Returns true if the range provided by the user is valid, false
173-
/// otherwise.
174-
fn valid(&self) -> (bool, &'static str) {
175-
match self {
176-
Self::Constant(_) => (true, ""),
177-
Self::Inclusive { min, max } => (min < max, "min must be less than max"),
178-
}
179-
}
180-
181-
fn start(&self) -> T {
182-
match self {
183-
ConfRange::Constant(c) => *c,
184-
ConfRange::Inclusive { min, .. } => *min,
185-
}
186-
}
187-
188-
fn end(&self) -> T {
189-
match self {
190-
ConfRange::Constant(c) => *c,
191-
ConfRange::Inclusive { max, .. } => *max,
192-
}
193-
}
194-
}
195-
196-
impl<T> ConfRange<T>
197-
where
198-
T: PartialEq + cmp::PartialOrd + Clone + Copy + SampleUniform,
199-
{
200-
fn sample<R>(&self, rng: &mut R) -> T
201-
where
202-
R: rand::Rng + ?Sized,
203-
{
204-
match self {
205-
ConfRange::Constant(c) => *c,
206-
ConfRange::Inclusive { min, max } => rng.random_range(*min..=*max),
207-
}
208-
}
209-
}
210149

211150
/// Configure the `DogStatsD` payload.
212151
#[derive(Debug, Deserialize, SerdeSerialize, Clone, PartialEq, Copy)]
@@ -359,70 +298,6 @@ impl Config {
359298
}
360299
}
361300

362-
fn choose_or_not_ref<'a, R, T>(mut rng: &mut R, pool: &'a [T]) -> Option<&'a T>
363-
where
364-
R: rand::Rng + ?Sized,
365-
{
366-
if rng.random() {
367-
pool.choose(&mut rng)
368-
} else {
369-
None
370-
}
371-
}
372-
373-
fn choose_or_not_fn<R, T, F>(rng: &mut R, func: F) -> Option<T>
374-
where
375-
T: Clone,
376-
R: rand::Rng + ?Sized,
377-
F: FnOnce(&mut R) -> Option<T>,
378-
{
379-
if rng.random() { func(rng) } else { None }
380-
}
381-
382-
#[inline]
383-
/// Generate a total number of strings randomly chosen from the range `min_max` with a maximum length
384-
/// per string of `max_length`.
385-
fn random_strings_with_length<R>(
386-
pool: &strings::Pool,
387-
min_max: Range<usize>,
388-
max_length: u16,
389-
rng: &mut R,
390-
) -> Vec<String>
391-
where
392-
R: Rng + ?Sized,
393-
{
394-
let total = rng.random_range(min_max);
395-
let length_range = ConfRange::Inclusive {
396-
min: 1,
397-
max: max_length,
398-
};
399-
400-
random_strings_with_length_range(pool, total, length_range, rng)
401-
}
402-
403-
#[inline]
404-
/// Generate a `total` number of strings with a maximum length per string of
405-
/// `max_length`.
406-
fn random_strings_with_length_range<R>(
407-
pool: &strings::Pool,
408-
total: usize,
409-
length_range: ConfRange<u16>,
410-
mut rng: &mut R,
411-
) -> Vec<String>
412-
where
413-
R: Rng + ?Sized,
414-
{
415-
let mut buf = Vec::with_capacity(total);
416-
for _ in 0..total {
417-
let sz = length_range.sample(&mut rng) as usize;
418-
buf.push(String::from(
419-
pool.of_size(&mut rng, sz)
420-
.expect("failed to generate string"),
421-
));
422-
}
423-
buf
424-
}
425-
426301
#[derive(Debug, Clone)]
427302
struct MemberGenerator {
428303
kind_weights: WeightedIndex<u16>,

lading_payload/src/dogstatsd/event.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use rand::{Rng, distr::StandardUniform, prelude::Distribution};
55

66
use crate::{Error, Generator, common::strings};
77

8-
use super::{ConfRange, choose_or_not_fn, common};
8+
use self::strings::choose_or_not_fn;
9+
10+
use super::{ConfRange, common};
911

1012
#[derive(Debug, Clone)]
1113
pub(crate) struct EventGenerator {

lading_payload/src/dogstatsd/metric.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ use rand::{
1111
use crate::{Error, Generator, common::strings, dogstatsd::metric::template::Template};
1212
use tracing::debug;
1313

14+
use self::strings::choose_or_not_ref;
15+
1416
use super::{
15-
ConfRange, ValueConf, choose_or_not_ref,
17+
ConfRange, ValueConf,
1618
common::{self, NumValueGenerator},
1719
};
1820

lading_payload/src/dogstatsd/service_check.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ use std::fmt;
33

44
use rand::{Rng, distr::StandardUniform, prelude::Distribution, seq::IndexedRandom};
55

6-
use crate::{Error, Generator};
6+
use crate::{Error, Generator, common::strings::choose_or_not_ref};
77

8-
use super::{
9-
choose_or_not_ref,
10-
common::{self, tags::Tagset},
11-
};
8+
use super::common::{self, tags::Tagset};
129

1310
#[derive(Debug, Clone)]
1411
pub(crate) struct ServiceCheckGenerator {

0 commit comments

Comments
 (0)