libdd-sampling 2.0.0

Core sampling logic for Datadog tracing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright 2025-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0

//! Sampling types and mechanisms for Datadog distributed tracing.
//!
//! This module provides types for representing sampling decisions, priorities,
//! and mechanisms used in Datadog's trace sampling system.

use std::{borrow::Cow, fmt, str::FromStr};

/// Represents a sampling decision for a trace.
///
/// Contains the priority level and the mechanism that made the decision.
#[derive(Clone, Copy, Debug)]
pub struct SamplingDecision {
    /// The sampling priority indicating whether the trace should be kept or rejected.
    pub priority: Option<SamplingPriority>,
    /// The mechanism that made the sampling decision.
    pub mechanism: Option<SamplingMechanism>,
}

/// Represents the sampling priority of a trace.
///
/// Positive values indicate the trace should be kept, while zero or negative
/// values indicate rejection. Use the constants in the [`priority`] module
/// for standard priority values.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SamplingPriority {
    value: i8,
}

impl SamplingPriority {
    pub const fn from_i8(value: i8) -> Self {
        Self { value }
    }

    pub fn into_i8(self) -> i8 {
        self.value
    }

    /// Returns whether this sampling priority indicates the trace should be kept.
    ///
    /// # Returns
    ///
    /// `true` if the priority value is positive (indicating the trace should be kept),
    /// `false` otherwise (indicating the trace should be dropped).
    ///
    /// # Examples
    ///
    /// ```
    /// use libdd_sampling::priority;
    ///
    /// assert!(priority::AUTO_KEEP.is_keep());
    /// assert!(priority::USER_KEEP.is_keep());
    /// assert!(!priority::AUTO_REJECT.is_keep());
    /// assert!(!priority::USER_REJECT.is_keep());
    /// ```
    #[inline(always)]
    pub fn is_keep(&self) -> bool {
        self.value > 0
    }
}

/// Sampling priority constants.
///
/// These values indicate whether a trace should be kept or rejected,
/// and whether the decision was made automatically or by the user.
pub mod priority {
    use super::SamplingPriority;

    /// User explicitly rejected this trace (priority -1).
    pub const USER_REJECT: SamplingPriority = SamplingPriority::from_i8(-1);
    /// User explicitly requested to keep this trace (priority 2).
    pub const USER_KEEP: SamplingPriority = SamplingPriority::from_i8(2);
    /// Automatically rejected by the sampler (priority 0).
    pub const AUTO_REJECT: SamplingPriority = SamplingPriority::from_i8(0);
    /// Automatically kept by the sampler (priority 1).
    pub const AUTO_KEEP: SamplingPriority = SamplingPriority::from_i8(1);
}

impl fmt::Display for SamplingPriority {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.value)
    }
}

impl FromStr for SamplingPriority {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.parse::<i8>() {
            Ok(value) => Ok(SamplingPriority::from_i8(value)),
            Err(_) => Err(()),
        }
    }
}

/// Represents the mechanism that made a sampling decision.
///
/// The sampling mechanism identifies which component or rule determined
/// whether a trace should be sampled (kept or rejected).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct SamplingMechanism {
    value: u8,
}

impl SamplingMechanism {
    pub const fn from_u8(value: u8) -> Self {
        Self { value }
    }

    pub fn into_u8(self) -> u8 {
        self.value
    }

    pub fn to_priority(self, is_keep: bool) -> SamplingPriority {
        const AUTO_PAIR: PriorityPair = PriorityPair {
            keep: priority::AUTO_KEEP,
            reject: priority::AUTO_REJECT,
        };
        const USER_PAIR: PriorityPair = PriorityPair {
            keep: priority::USER_KEEP,
            reject: priority::USER_REJECT,
        };
        let pair = match self {
            mechanism::AGENT_RATE_BY_SERVICE | mechanism::DEFAULT => AUTO_PAIR,
            mechanism::MANUAL
            | mechanism::LOCAL_USER_TRACE_SAMPLING_RULE
            | mechanism::REMOTE_USER_TRACE_SAMPLING_RULE
            | mechanism::REMOTE_DYNAMIC_TRACE_SAMPLING_RULE
            | mechanism::SPAN_SAMPLING_RULE
            | mechanism::DATA_JOBS_MONITORING => USER_PAIR,
            mechanism::APPSEC => AUTO_PAIR,

            _ => AUTO_PAIR,
        };
        if is_keep {
            pair.keep
        } else {
            pair.reject
        }
    }

    /// Returns the string representation of the sampling mechanism.
    ///
    /// The format is `"-N"` (e.g. `"-4"` for manual sampling). The leading `-` comes from the
    /// propagation tags RFC, which initially had a prefix component before the `-`; that prefix
    /// was dropped, but the `-` was retained as-is for backwards compatibility with existing
    /// existing tracers.
    pub fn to_cow(self) -> Cow<'static, str> {
        match self {
            mechanism::DEFAULT => Cow::Borrowed("-0"),
            mechanism::AGENT_RATE_BY_SERVICE => Cow::Borrowed("-1"),
            mechanism::REMOTE_RATE => Cow::Borrowed("-2"),
            mechanism::LOCAL_USER_TRACE_SAMPLING_RULE => Cow::Borrowed("-3"),
            mechanism::MANUAL => Cow::Borrowed("-4"),
            mechanism::APPSEC => Cow::Borrowed("-5"),
            mechanism::REMOTE_RATE_USER => Cow::Borrowed("-6"),
            mechanism::REMOTE_RATE_DATADOG => Cow::Borrowed("-7"),
            mechanism::SPAN_SAMPLING_RULE => Cow::Borrowed("-8"),
            mechanism::OTLP_INGEST_PROBABILISTIC_SAMPLING => Cow::Borrowed("-9"),
            mechanism::DATA_JOBS_MONITORING => Cow::Borrowed("-10"),
            mechanism::REMOTE_USER_TRACE_SAMPLING_RULE => Cow::Borrowed("-11"),
            mechanism::REMOTE_DYNAMIC_TRACE_SAMPLING_RULE => Cow::Borrowed("-12"),
            _ => Cow::Owned(self.to_string()),
        }
    }
}

/// Sampling mechanism constants.
///
/// These constants identify which component or rule made a sampling decision.
pub mod mechanism {
    use super::SamplingMechanism;

    /// Default sampling mechanism.
    pub const DEFAULT: SamplingMechanism = SamplingMechanism::from_u8(0);
    /// Agent-based rate sampling by service.
    pub const AGENT_RATE_BY_SERVICE: SamplingMechanism = SamplingMechanism::from_u8(1);
    /// Remote configuration rate sampling.
    pub const REMOTE_RATE: SamplingMechanism = SamplingMechanism::from_u8(2);
    /// Local user-defined trace sampling rule.
    pub const LOCAL_USER_TRACE_SAMPLING_RULE: SamplingMechanism = SamplingMechanism::from_u8(3);
    /// Manual sampling decision via API.
    pub const MANUAL: SamplingMechanism = SamplingMechanism::from_u8(4);
    /// Application Security (AppSec) sampling.
    pub const APPSEC: SamplingMechanism = SamplingMechanism::from_u8(5);
    /// Remote user rate sampling.
    pub const REMOTE_RATE_USER: SamplingMechanism = SamplingMechanism::from_u8(6);
    /// Remote Datadog rate sampling.
    pub const REMOTE_RATE_DATADOG: SamplingMechanism = SamplingMechanism::from_u8(7);
    /// Span-level sampling rule.
    pub const SPAN_SAMPLING_RULE: SamplingMechanism = SamplingMechanism::from_u8(8);
    /// OTLP ingest probabilistic sampling.
    pub const OTLP_INGEST_PROBABILISTIC_SAMPLING: SamplingMechanism = SamplingMechanism::from_u8(9);
    /// Data Jobs Monitoring sampling.
    pub const DATA_JOBS_MONITORING: SamplingMechanism = SamplingMechanism::from_u8(10);
    /// Remote user-defined trace sampling rule.
    pub const REMOTE_USER_TRACE_SAMPLING_RULE: SamplingMechanism = SamplingMechanism::from_u8(11);
    /// Remote dynamic trace sampling rule.
    pub const REMOTE_DYNAMIC_TRACE_SAMPLING_RULE: SamplingMechanism =
        SamplingMechanism::from_u8(12);
}

impl fmt::Display for SamplingMechanism {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "-{}", self.into_u8())
    }
}

impl FromStr for SamplingMechanism {
    type Err = ();

    /// Gets the sampling mechanism from it's string representation.
    fn from_str(s: &str) -> Result<Self, ()> {
        let val: i16 = s.parse().map_err(drop)?;
        if val > 0 {
            return Err(());
        }
        let val = -val;
        if val > u8::MAX as i16 {
            return Err(());
        }
        Ok(SamplingMechanism::from_u8(val as u8))
    }
}

struct PriorityPair {
    keep: SamplingPriority,
    reject: SamplingPriority,
}

#[cfg(test)]
mod tests {
    use super::*;

    // --- SamplingPriority ---

    #[test]
    fn test_priority_into_i8() {
        assert_eq!(priority::AUTO_KEEP.into_i8(), 1);
        assert_eq!(priority::AUTO_REJECT.into_i8(), 0);
        assert_eq!(priority::USER_KEEP.into_i8(), 2);
        assert_eq!(priority::USER_REJECT.into_i8(), -1);
    }

    #[test]
    fn test_priority_is_keep() {
        assert!(priority::AUTO_KEEP.is_keep());
        assert!(priority::USER_KEEP.is_keep());
        assert!(!priority::AUTO_REJECT.is_keep());
        assert!(!priority::USER_REJECT.is_keep());
    }

    #[test]
    fn test_priority_display() {
        assert_eq!(priority::AUTO_KEEP.to_string(), "1");
        assert_eq!(priority::AUTO_REJECT.to_string(), "0");
        assert_eq!(priority::USER_KEEP.to_string(), "2");
        assert_eq!(priority::USER_REJECT.to_string(), "-1");
    }

    #[test]
    fn test_priority_from_str() {
        assert_eq!(
            "1".parse::<SamplingPriority>().unwrap(),
            priority::AUTO_KEEP
        );
        assert_eq!(
            "0".parse::<SamplingPriority>().unwrap(),
            priority::AUTO_REJECT
        );
        assert_eq!(
            "2".parse::<SamplingPriority>().unwrap(),
            priority::USER_KEEP
        );
        assert_eq!(
            "-1".parse::<SamplingPriority>().unwrap(),
            priority::USER_REJECT
        );
        assert!("not_a_number".parse::<SamplingPriority>().is_err());
        assert!("999".parse::<SamplingPriority>().is_err()); // overflows i8
    }

    // --- SamplingMechanism ---

    #[test]
    fn test_mechanism_into_u8() {
        assert_eq!(mechanism::DEFAULT.into_u8(), 0);
        assert_eq!(mechanism::AGENT_RATE_BY_SERVICE.into_u8(), 1);
        assert_eq!(mechanism::LOCAL_USER_TRACE_SAMPLING_RULE.into_u8(), 3);
        assert_eq!(mechanism::REMOTE_USER_TRACE_SAMPLING_RULE.into_u8(), 11);
        assert_eq!(mechanism::REMOTE_DYNAMIC_TRACE_SAMPLING_RULE.into_u8(), 12);
    }

    #[test]
    fn test_mechanism_to_priority_auto_pair() {
        // DEFAULT and AGENT_RATE_BY_SERVICE use AUTO priority
        assert_eq!(mechanism::DEFAULT.to_priority(true), priority::AUTO_KEEP);
        assert_eq!(mechanism::DEFAULT.to_priority(false), priority::AUTO_REJECT);
        assert_eq!(
            mechanism::AGENT_RATE_BY_SERVICE.to_priority(true),
            priority::AUTO_KEEP
        );
        assert_eq!(
            mechanism::AGENT_RATE_BY_SERVICE.to_priority(false),
            priority::AUTO_REJECT
        );
        assert_eq!(mechanism::APPSEC.to_priority(true), priority::AUTO_KEEP);
        assert_eq!(mechanism::APPSEC.to_priority(false), priority::AUTO_REJECT);
    }

    #[test]
    fn test_mechanism_to_priority_user_pair() {
        // Rule-based mechanisms use USER priority
        assert_eq!(
            mechanism::LOCAL_USER_TRACE_SAMPLING_RULE.to_priority(true),
            priority::USER_KEEP
        );
        assert_eq!(
            mechanism::LOCAL_USER_TRACE_SAMPLING_RULE.to_priority(false),
            priority::USER_REJECT
        );
        assert_eq!(
            mechanism::REMOTE_USER_TRACE_SAMPLING_RULE.to_priority(true),
            priority::USER_KEEP
        );
        assert_eq!(
            mechanism::REMOTE_DYNAMIC_TRACE_SAMPLING_RULE.to_priority(true),
            priority::USER_KEEP
        );
        assert_eq!(mechanism::MANUAL.to_priority(true), priority::USER_KEEP);
        assert_eq!(
            mechanism::SPAN_SAMPLING_RULE.to_priority(false),
            priority::USER_REJECT
        );
        assert_eq!(
            mechanism::DATA_JOBS_MONITORING.to_priority(true),
            priority::USER_KEEP
        );
    }

    #[test]
    fn test_mechanism_to_priority_unknown_falls_back_to_auto() {
        let unknown = SamplingMechanism::from_u8(99);
        assert_eq!(unknown.to_priority(true), priority::AUTO_KEEP);
        assert_eq!(unknown.to_priority(false), priority::AUTO_REJECT);
    }

    #[test]
    fn test_mechanism_to_cow() {
        assert_eq!(mechanism::DEFAULT.to_cow(), "-0");
        assert_eq!(mechanism::AGENT_RATE_BY_SERVICE.to_cow(), "-1");
        assert_eq!(mechanism::REMOTE_RATE.to_cow(), "-2");
        assert_eq!(mechanism::LOCAL_USER_TRACE_SAMPLING_RULE.to_cow(), "-3");
        assert_eq!(mechanism::MANUAL.to_cow(), "-4");
        assert_eq!(mechanism::APPSEC.to_cow(), "-5");
        assert_eq!(mechanism::REMOTE_RATE_USER.to_cow(), "-6");
        assert_eq!(mechanism::REMOTE_RATE_DATADOG.to_cow(), "-7");
        assert_eq!(mechanism::SPAN_SAMPLING_RULE.to_cow(), "-8");
        assert_eq!(mechanism::OTLP_INGEST_PROBABILISTIC_SAMPLING.to_cow(), "-9");
        assert_eq!(mechanism::DATA_JOBS_MONITORING.to_cow(), "-10");
        assert_eq!(mechanism::REMOTE_USER_TRACE_SAMPLING_RULE.to_cow(), "-11");
        assert_eq!(
            mechanism::REMOTE_DYNAMIC_TRACE_SAMPLING_RULE.to_cow(),
            "-12"
        );
        // Unknown mechanism falls back to Display
        assert_eq!(SamplingMechanism::from_u8(99).to_cow(), "-99");
    }

    #[test]
    fn test_mechanism_display() {
        assert_eq!(mechanism::DEFAULT.to_string(), "-0");
        assert_eq!(mechanism::LOCAL_USER_TRACE_SAMPLING_RULE.to_string(), "-3");
        assert_eq!(
            mechanism::REMOTE_DYNAMIC_TRACE_SAMPLING_RULE.to_string(),
            "-12"
        );
    }

    #[test]
    fn test_mechanism_from_str() {
        assert_eq!(
            "-0".parse::<SamplingMechanism>().unwrap(),
            mechanism::DEFAULT
        );
        assert_eq!(
            "-1".parse::<SamplingMechanism>().unwrap(),
            mechanism::AGENT_RATE_BY_SERVICE
        );
        assert_eq!(
            "-3".parse::<SamplingMechanism>().unwrap(),
            mechanism::LOCAL_USER_TRACE_SAMPLING_RULE
        );
        assert_eq!(
            "-12".parse::<SamplingMechanism>().unwrap(),
            mechanism::REMOTE_DYNAMIC_TRACE_SAMPLING_RULE
        );
    }

    #[test]
    fn test_mechanism_from_str_errors() {
        assert!("not_a_number".parse::<SamplingMechanism>().is_err());
        assert!("1".parse::<SamplingMechanism>().is_err()); // positive not allowed
        assert!("-99999".parse::<SamplingMechanism>().is_err()); // overflows u8
    }
}