Skip to content

Commit a142a0e

Browse files
committed
chore(sampling): hook in the libdd-sampling crate
1 parent 4db525d commit a142a0e

7 files changed

Lines changed: 576 additions & 7 deletions

File tree

Cargo.lock

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ members = [
5353
"libdd-http-client",
5454
"libdd-log",
5555
"libdd-log-ffi",
56+
"libdd-sampling",
5657
]
5758

5859
# https://doc.rust-lang.org/cargo/reference/resolver.html

LICENSE-3rdparty.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ linux-raw-sys,https://github.com/sunfishcode/linux-raw-sys,Apache-2.0 WITH LLVM-
229229
litemap,https://github.com/unicode-org/icu4x,Unicode-3.0,The ICU4X Project Developers
230230
lock_api,https://github.com/Amanieu/parking_lot,MIT OR Apache-2.0,Amanieu d'Antras <amanieu@gmail.com>
231231
log,https://github.com/rust-lang/log,MIT OR Apache-2.0,The Rust Project Developers
232+
lru,https://github.com/jeromefroe/lru-rs,MIT,Jerome Froelich <jeromefroelic@hotmail.com>
232233
manual_future,https://github.com/dmarcuse/manual_future,MIT,Dominic Marcuse <dominic@marcuse.us>
233234
matchers,https://github.com/hawkw/matchers,MIT,Eliza Weisman <eliza@buoyant.io>
234235
matchit,https://github.com/ibraheemdev/matchit,MIT AND BSD-3-Clause,Ibraheem Ahmed <ibraheem@ibraheem.ca>

libdd-sampling/Cargo.toml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,17 @@
33

44
[package]
55
name = "libdd-sampling"
6-
version.workspace = true
6+
version = "0.1.0"
77
edition.workspace = true
88
rust-version.workspace = true
99
license.workspace = true
10-
repository.workspace = true
11-
readme.workspace = true
10+
repository = "https://github.com/DataDog/libdatadog/tree/main/libdd-sampling"
1211
description = "Core sampling logic for Datadog tracing"
1312
authors.workspace = true
1413

1514
[dependencies]
16-
serde = { workspace = true, features = ["derive"] }
17-
serde_json = { workspace = true }
18-
hashbrown = { workspace = true }
19-
foldhash = { workspace = true }
15+
serde = { version = "1.0", features = ["derive"] }
16+
serde_json = "1.0"
2017
lru = "0.16.3"
2118

2219
[dev-dependencies]

libdd-sampling/src/dd_sampling.rs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,126 @@ struct PriorityPair {
224224
keep: SamplingPriority,
225225
reject: SamplingPriority,
226226
}
227+
228+
#[cfg(test)]
229+
mod tests {
230+
use super::*;
231+
232+
// --- SamplingPriority ---
233+
234+
#[test]
235+
fn test_priority_into_i8() {
236+
assert_eq!(priority::AUTO_KEEP.into_i8(), 1);
237+
assert_eq!(priority::AUTO_REJECT.into_i8(), 0);
238+
assert_eq!(priority::USER_KEEP.into_i8(), 2);
239+
assert_eq!(priority::USER_REJECT.into_i8(), -1);
240+
}
241+
242+
#[test]
243+
fn test_priority_is_keep() {
244+
assert!(priority::AUTO_KEEP.is_keep());
245+
assert!(priority::USER_KEEP.is_keep());
246+
assert!(!priority::AUTO_REJECT.is_keep());
247+
assert!(!priority::USER_REJECT.is_keep());
248+
}
249+
250+
#[test]
251+
fn test_priority_display() {
252+
assert_eq!(priority::AUTO_KEEP.to_string(), "1");
253+
assert_eq!(priority::AUTO_REJECT.to_string(), "0");
254+
assert_eq!(priority::USER_KEEP.to_string(), "2");
255+
assert_eq!(priority::USER_REJECT.to_string(), "-1");
256+
}
257+
258+
#[test]
259+
fn test_priority_from_str() {
260+
assert_eq!("1".parse::<SamplingPriority>().unwrap(), priority::AUTO_KEEP);
261+
assert_eq!("0".parse::<SamplingPriority>().unwrap(), priority::AUTO_REJECT);
262+
assert_eq!("2".parse::<SamplingPriority>().unwrap(), priority::USER_KEEP);
263+
assert_eq!("-1".parse::<SamplingPriority>().unwrap(), priority::USER_REJECT);
264+
assert!("not_a_number".parse::<SamplingPriority>().is_err());
265+
assert!("999".parse::<SamplingPriority>().is_err()); // overflows i8
266+
}
267+
268+
// --- SamplingMechanism ---
269+
270+
#[test]
271+
fn test_mechanism_into_u8() {
272+
assert_eq!(mechanism::DEFAULT.into_u8(), 0);
273+
assert_eq!(mechanism::AGENT_RATE_BY_SERVICE.into_u8(), 1);
274+
assert_eq!(mechanism::LOCAL_USER_TRACE_SAMPLING_RULE.into_u8(), 3);
275+
assert_eq!(mechanism::REMOTE_USER_TRACE_SAMPLING_RULE.into_u8(), 11);
276+
assert_eq!(mechanism::REMOTE_DYNAMIC_TRACE_SAMPLING_RULE.into_u8(), 12);
277+
}
278+
279+
#[test]
280+
fn test_mechanism_to_priority_auto_pair() {
281+
// DEFAULT and AGENT_RATE_BY_SERVICE use AUTO priority
282+
assert_eq!(mechanism::DEFAULT.to_priority(true), priority::AUTO_KEEP);
283+
assert_eq!(mechanism::DEFAULT.to_priority(false), priority::AUTO_REJECT);
284+
assert_eq!(mechanism::AGENT_RATE_BY_SERVICE.to_priority(true), priority::AUTO_KEEP);
285+
assert_eq!(mechanism::AGENT_RATE_BY_SERVICE.to_priority(false), priority::AUTO_REJECT);
286+
assert_eq!(mechanism::APPSEC.to_priority(true), priority::AUTO_KEEP);
287+
assert_eq!(mechanism::APPSEC.to_priority(false), priority::AUTO_REJECT);
288+
}
289+
290+
#[test]
291+
fn test_mechanism_to_priority_user_pair() {
292+
// Rule-based mechanisms use USER priority
293+
assert_eq!(mechanism::LOCAL_USER_TRACE_SAMPLING_RULE.to_priority(true), priority::USER_KEEP);
294+
assert_eq!(mechanism::LOCAL_USER_TRACE_SAMPLING_RULE.to_priority(false), priority::USER_REJECT);
295+
assert_eq!(mechanism::REMOTE_USER_TRACE_SAMPLING_RULE.to_priority(true), priority::USER_KEEP);
296+
assert_eq!(mechanism::REMOTE_DYNAMIC_TRACE_SAMPLING_RULE.to_priority(true), priority::USER_KEEP);
297+
assert_eq!(mechanism::MANUAL.to_priority(true), priority::USER_KEEP);
298+
assert_eq!(mechanism::SPAN_SAMPLING_RULE.to_priority(false), priority::USER_REJECT);
299+
assert_eq!(mechanism::DATA_JOBS_MONITORING.to_priority(true), priority::USER_KEEP);
300+
}
301+
302+
#[test]
303+
fn test_mechanism_to_priority_unknown_falls_back_to_auto() {
304+
let unknown = SamplingMechanism::from_u8(99);
305+
assert_eq!(unknown.to_priority(true), priority::AUTO_KEEP);
306+
assert_eq!(unknown.to_priority(false), priority::AUTO_REJECT);
307+
}
308+
309+
#[test]
310+
fn test_mechanism_to_cow() {
311+
assert_eq!(mechanism::DEFAULT.to_cow(), "-0");
312+
assert_eq!(mechanism::AGENT_RATE_BY_SERVICE.to_cow(), "-1");
313+
assert_eq!(mechanism::REMOTE_RATE.to_cow(), "-2");
314+
assert_eq!(mechanism::LOCAL_USER_TRACE_SAMPLING_RULE.to_cow(), "-3");
315+
assert_eq!(mechanism::MANUAL.to_cow(), "-4");
316+
assert_eq!(mechanism::APPSEC.to_cow(), "-5");
317+
assert_eq!(mechanism::REMOTE_RATE_USER.to_cow(), "-6");
318+
assert_eq!(mechanism::REMOTE_RATE_DATADOG.to_cow(), "-7");
319+
assert_eq!(mechanism::SPAN_SAMPLING_RULE.to_cow(), "-8");
320+
assert_eq!(mechanism::OTLP_INGEST_PROBABILISTIC_SAMPLING.to_cow(), "-9");
321+
assert_eq!(mechanism::DATA_JOBS_MONITORING.to_cow(), "-10");
322+
assert_eq!(mechanism::REMOTE_USER_TRACE_SAMPLING_RULE.to_cow(), "-11");
323+
assert_eq!(mechanism::REMOTE_DYNAMIC_TRACE_SAMPLING_RULE.to_cow(), "-12");
324+
// Unknown mechanism falls back to Display
325+
assert_eq!(SamplingMechanism::from_u8(99).to_cow(), "-99");
326+
}
327+
328+
#[test]
329+
fn test_mechanism_display() {
330+
assert_eq!(mechanism::DEFAULT.to_string(), "-0");
331+
assert_eq!(mechanism::LOCAL_USER_TRACE_SAMPLING_RULE.to_string(), "-3");
332+
assert_eq!(mechanism::REMOTE_DYNAMIC_TRACE_SAMPLING_RULE.to_string(), "-12");
333+
}
334+
335+
#[test]
336+
fn test_mechanism_from_str() {
337+
assert_eq!("-0".parse::<SamplingMechanism>().unwrap(), mechanism::DEFAULT);
338+
assert_eq!("-1".parse::<SamplingMechanism>().unwrap(), mechanism::AGENT_RATE_BY_SERVICE);
339+
assert_eq!("-3".parse::<SamplingMechanism>().unwrap(), mechanism::LOCAL_USER_TRACE_SAMPLING_RULE);
340+
assert_eq!("-12".parse::<SamplingMechanism>().unwrap(), mechanism::REMOTE_DYNAMIC_TRACE_SAMPLING_RULE);
341+
}
342+
343+
#[test]
344+
fn test_mechanism_from_str_errors() {
345+
assert!("not_a_number".parse::<SamplingMechanism>().is_err());
346+
assert!("1".parse::<SamplingMechanism>().is_err()); // positive not allowed
347+
assert!("-99999".parse::<SamplingMechanism>().is_err()); // overflows u8
348+
}
349+
}

0 commit comments

Comments
 (0)