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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// Copyright 2025-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0

use crate::constants::pattern::NO_RULE;
use crate::glob_matcher::GlobMatcher;
use crate::rate_sampler::RateSampler;
use crate::sampling_rule_config::SamplingRuleConfig;
use crate::types::{AttributeLike, SpanProperties, TraceIdLike, ValueLike};
use std::collections::HashMap;

// HTTP status code attribute constants
const HTTP_RESPONSE_STATUS_CODE: &str = "http.response.status_code";
const HTTP_STATUS_CODE: &str = "http.status_code";

fn matcher_from_rule(rule: &str) -> Option<GlobMatcher> {
    (rule != NO_RULE).then(|| GlobMatcher::new(rule))
}

/// Represents a sampling rule with criteria for matching spans
#[derive(Clone, Debug)]
pub struct SamplingRule {
    /// The sample rate to apply when this rule matches (0.0-1.0)
    pub(crate) sample_rate: f64,

    /// Where this rule comes from (customer, dynamic, default)
    pub(crate) provenance: String,

    /// Internal rate sampler used when this rule matches
    rate_sampler: RateSampler,

    /// Glob matchers for pattern matching
    pub(crate) name_matcher: Option<GlobMatcher>,
    pub(crate) service_matcher: Option<GlobMatcher>,
    pub(crate) resource_matcher: Option<GlobMatcher>,
    pub(crate) tag_matchers: HashMap<String, GlobMatcher>,
}

impl SamplingRule {
    /// Converts a vector of SamplingRuleConfig into SamplingRule objects
    /// Centralizes the conversion logic
    pub fn from_configs(configs: Vec<SamplingRuleConfig>) -> Vec<Self> {
        configs
            .into_iter()
            .map(|config| {
                Self::new(
                    config.sample_rate,
                    config.service,
                    config.name,
                    config.resource,
                    Some(config.tags),
                    Some(config.provenance),
                )
            })
            .collect()
    }

    /// Creates a new sampling rule
    pub fn new(
        sample_rate: f64,
        service: Option<String>,
        name: Option<String>,
        resource: Option<String>,
        tags: Option<HashMap<String, String>>,
        provenance: Option<String>,
    ) -> Self {
        // Create glob matchers for the patterns
        let name_matcher = name.as_deref().and_then(matcher_from_rule);
        let service_matcher = service.as_deref().and_then(matcher_from_rule);
        let resource_matcher = resource.as_deref().and_then(matcher_from_rule);

        // Create matchers for tag values. `tags` is consumed here so no clone is needed.
        let tag_map = tags.unwrap_or_default();
        let mut tag_matchers = HashMap::with_capacity(tag_map.len());
        for (key, value) in tag_map {
            if let Some(matcher) = matcher_from_rule(&value) {
                tag_matchers.insert(key, matcher);
            }
        }

        SamplingRule {
            sample_rate,
            provenance: provenance.unwrap_or_else(|| "default".to_string()),
            rate_sampler: RateSampler::new(sample_rate),
            name_matcher,
            service_matcher,
            resource_matcher,
            tag_matchers,
        }
    }

    /// Checks if this rule matches the given span's attributes and name
    /// The name is derived from the attributes and span kind
    pub(crate) fn matches(&self, span: &impl SpanProperties) -> bool {
        // Get the operation name from the span
        let name = span.operation_name();

        // Check name using glob matcher if specified
        if let Some(ref matcher) = self.name_matcher {
            if !matcher.matches(name.as_ref()) {
                return false;
            }
        }

        // Check service if specified using glob matcher
        if let Some(ref matcher) = self.service_matcher {
            // Get service from the span
            let service = span.service();

            // Match against the service
            if !matcher.matches(&service) {
                return false;
            }
        }

        // Get the resource string for matching
        let resource_str = span.resource();

        // Check resource if specified using glob matcher
        if let Some(ref matcher) = self.resource_matcher {
            // Use the resource from the span
            if !matcher.matches(resource_str.as_ref()) {
                return false;
            }
        }

        // Check all tags using glob matchers
        for (key, matcher) in &self.tag_matchers {
            let rule_tag_key_str = key.as_str();

            // Special handling for rules defined with "http.status_code" or
            // "http.response.status_code"
            if rule_tag_key_str == HTTP_STATUS_CODE || rule_tag_key_str == HTTP_RESPONSE_STATUS_CODE
            {
                match self.match_http_status_code_rule(matcher, span) {
                    Some(true) => continue,             // Status code matched
                    Some(false) | None => return false, // Status code didn't match or wasn't found
                }
            } else {
                // Logic for other tags:
                // First, try to match directly with the provided tag key
                let direct_match = span
                    .attributes()
                    .find(|attr| attr.key() == rule_tag_key_str)
                    .and_then(|attr| self.match_attribute_value(attr.value(), matcher));

                if direct_match.unwrap_or(false) {
                    continue;
                }

                // If no direct match, try to find the corresponding OpenTelemetry attribute that
                // maps to the Datadog tag key This handles cases where the rule key
                // is a Datadog key (e.g., "http.method") and the attribute is an
                // OTel key (e.g., "http.request.method")
                if rule_tag_key_str.starts_with("http.") {
                    let tag_match = span.attributes().any(|attr| {
                        if let Some(alternate_key) = span.get_alternate_key(attr.key()) {
                            if alternate_key == rule_tag_key_str {
                                return self
                                    .match_attribute_value(attr.value(), matcher)
                                    .unwrap_or(false);
                            }
                        }
                        false
                    });

                    if !tag_match {
                        return false; // Mapped attribute not found or did not match
                    }
                    // If tag_match is true, loop continues to next rule_tag_key.
                } else {
                    // For non-HTTP attributes, if we don't have a direct match, the rule doesn't
                    // match
                    return false;
                }
            }
        }

        true
    }

    /// Helper method to specifically match a rule against an HTTP status code extracted from
    /// attributes. Returns Some(true) if status code found and matches, Some(false) if found
    /// but not matched, None if not found.
    fn match_http_status_code_rule(
        &self,
        matcher: &GlobMatcher,
        span: &impl SpanProperties,
    ) -> Option<bool> {
        span.status_code().and_then(|status_code| {
            let status_value = ValueI64(i64::from(status_code));
            self.match_attribute_value(&status_value, matcher)
        })
    }

    // Helper method to match attribute values considering different value types
    fn match_attribute_value(&self, value: &impl ValueLike, matcher: &GlobMatcher) -> Option<bool> {
        // Floating point values are handled with special rules
        if let Some(float_val) = value.as_float() {
            // A float is treated as an integer iff it has no fractional part *and* it is
            // finite. `fract()` is robust for values outside the i64 range, unlike
            // `(float_val as i64) as f64` which silently saturates / produces garbage.
            let is_integer = float_val.is_finite() && float_val.fract() == 0.0;

            // For non-integer floats, only match if it's a wildcard pattern
            if !is_integer {
                // All '*' pattern returns true, any other pattern returns false
                return Some(matcher.pattern().chars().all(|c| c == '*'));
            }

            // For integer floats, convert to string for matching
            return Some(matcher.matches(&float_val.to_string()));
        }

        // For non-float values, use normal matching
        value
            .as_str()
            .map(|string_value| matcher.matches(&string_value))
    }

    /// Samples a trace ID using this rule's sample rate
    pub fn sample(&self, trace_id: &impl TraceIdLike) -> bool {
        // Delegate to the internal rate sampler's new sample method
        self.rate_sampler.sample(trace_id)
    }
}

/// Represents a priority for sampling rules.
///
/// TODO: this enum is currently only exercised by tests and `From<&str>`. It will become
/// actively used once Remote Configuration drives `SamplingRule::provenance` (which today
/// is a `String`). The `dead_code` allow can go away once that wiring lands.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum RuleProvenance {
    Customer = 0,
    Dynamic = 1,
    Default = 2,
}

impl From<&str> for RuleProvenance {
    fn from(s: &str) -> Self {
        match s {
            "customer" => RuleProvenance::Customer,
            "dynamic" => RuleProvenance::Dynamic,
            _ => RuleProvenance::Default,
        }
    }
}

/// Helper struct for representing i64 values as ValueLike
struct ValueI64(i64);

impl ValueLike for ValueI64 {
    fn as_float(&self) -> Option<f64> {
        Some(self.0 as f64)
    }

    fn as_str(&self) -> Option<std::borrow::Cow<'_, str>> {
        Some(std::borrow::Cow::Owned(self.0.to_string()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sampling_rule_config::SamplingRuleConfig;
    use std::borrow::Cow;

    // Minimal SpanProperties impl for unit testing sampling_rule logic.
    struct TestSpan {
        name: &'static str,
        service: &'static str,
        resource: &'static str,
        status_code: Option<u32>,
        // (key, value_str, is_metric) — is_metric=true gives a float value
        attrs: Vec<TestAttr>,
        // alternate key mapping: (stored_key, alternate_dd_key)
        alternates: Vec<(&'static str, &'static str)>,
    }

    struct TestAttr {
        key: &'static str,
        value: TestValue,
    }

    struct TestValue {
        value: &'static str,
        is_metric: bool,
    }

    impl crate::types::ValueLike for TestValue {
        fn as_float(&self) -> Option<f64> {
            if self.is_metric {
                self.value.parse().ok()
            } else {
                None
            }
        }
        fn as_str(&self) -> Option<Cow<'_, str>> {
            Some(Cow::Borrowed(self.value))
        }
    }

    impl crate::types::AttributeLike for TestAttr {
        type Value = TestValue;
        fn key(&self) -> &str {
            self.key
        }
        fn value(&self) -> &TestValue {
            &self.value
        }
    }

    impl crate::types::SpanProperties for TestSpan {
        type Attribute<'a>
            = &'a TestAttr
        where
            Self: 'a;

        fn operation_name(&self) -> Cow<'_, str> {
            Cow::Borrowed(self.name)
        }
        fn service(&self) -> Cow<'_, str> {
            Cow::Borrowed(self.service)
        }
        fn env(&self) -> Cow<'_, str> {
            Cow::Borrowed("")
        }
        fn resource(&self) -> Cow<'_, str> {
            Cow::Borrowed(self.resource)
        }
        fn status_code(&self) -> Option<u32> {
            self.status_code
        }
        fn attributes(&self) -> impl Iterator<Item = &TestAttr> + '_ {
            self.attrs.iter()
        }
        fn get_alternate_key<'b>(&self, key: &'b str) -> Option<Cow<'b, str>> {
            self.alternates
                .iter()
                .find(|(k, _)| *k == key)
                .map(|(_, alt)| Cow::Borrowed(*alt))
        }
    }

    fn make_span(name: &'static str, service: &'static str, resource: &'static str) -> TestSpan {
        TestSpan {
            name,
            service,
            resource,
            status_code: None,
            attrs: vec![],
            alternates: vec![],
        }
    }

    // --- from_configs ---

    #[test]
    fn test_from_configs_empty() {
        let rules = SamplingRule::from_configs(vec![]);
        assert!(rules.is_empty());
    }

    #[test]
    fn test_from_configs_single() {
        let config = SamplingRuleConfig {
            sample_rate: 0.5,
            service: Some("svc".into()),
            name: Some("op.*".into()),
            resource: None,
            tags: HashMap::new(),
            provenance: "customer".into(),
        };
        let rules = SamplingRule::from_configs(vec![config]);
        assert_eq!(rules.len(), 1);
        assert_eq!(rules[0].sample_rate, 0.5);
        assert_eq!(rules[0].provenance, "customer");
    }

    #[test]
    fn test_from_configs_preserves_provenance() {
        let configs = vec![
            SamplingRuleConfig {
                sample_rate: 1.0,
                provenance: "customer".into(),
                ..Default::default()
            },
            SamplingRuleConfig {
                sample_rate: 0.5,
                provenance: "dynamic".into(),
                ..Default::default()
            },
            SamplingRuleConfig {
                sample_rate: 0.1,
                provenance: "default".into(),
                ..Default::default()
            },
        ];
        let rules = SamplingRule::from_configs(configs);
        assert_eq!(rules[0].provenance, "customer");
        assert_eq!(rules[1].provenance, "dynamic");
        assert_eq!(rules[2].provenance, "default");
    }

    // --- HTTP status code matching ---

    #[test]
    fn test_matches_http_status_code_rule_matching() {
        let rule = SamplingRule::new(
            1.0,
            None,
            None,
            None,
            Some(HashMap::from([("http.status_code".into(), "200".into())])),
            None,
        );
        let mut span = make_span("op", "svc", "res");
        span.status_code = Some(200);
        assert!(rule.matches(&span));
    }

    #[test]
    fn test_matches_http_status_code_rule_not_matching() {
        let rule = SamplingRule::new(
            1.0,
            None,
            None,
            None,
            Some(HashMap::from([("http.status_code".into(), "200".into())])),
            None,
        );
        let mut span = make_span("op", "svc", "res");
        span.status_code = Some(404);
        assert!(!rule.matches(&span));
    }

    #[test]
    fn test_matches_http_status_code_absent_returns_false() {
        let rule = SamplingRule::new(
            1.0,
            None,
            None,
            None,
            Some(HashMap::from([("http.status_code".into(), "200".into())])),
            None,
        );
        let span = make_span("op", "svc", "res"); // no status_code
        assert!(!rule.matches(&span));
    }

    #[test]
    fn test_matches_http_response_status_code_key() {
        let rule = SamplingRule::new(
            1.0,
            None,
            None,
            None,
            Some(HashMap::from([(
                "http.response.status_code".into(),
                "404".into(),
            )])),
            None,
        );
        let mut span = make_span("op", "svc", "res");
        span.status_code = Some(404);
        assert!(rule.matches(&span));
    }

    #[test]
    fn test_matches_http_status_code_wildcard() {
        let rule = SamplingRule::new(
            1.0,
            None,
            None,
            None,
            Some(HashMap::from([("http.status_code".into(), "2*".into())])),
            None,
        );
        let mut span = make_span("op", "svc", "res");
        span.status_code = Some(201);
        assert!(rule.matches(&span));
    }

    // --- Alternate key (OTel → DD) matching ---

    #[test]
    fn test_matches_alternate_key_found() {
        // Rule uses DD key "http.method"; span stores OTel key "http.request.method"
        // with alternate mapping back to "http.method"
        let rule = SamplingRule::new(
            1.0,
            None,
            None,
            None,
            Some(HashMap::from([("http.method".into(), "POST".into())])),
            None,
        );
        let mut span = make_span("op", "svc", "res");
        span.attrs = vec![TestAttr {
            key: "http.request.method",
            value: TestValue {
                value: "POST",
                is_metric: false,
            },
        }];
        span.alternates = vec![("http.request.method", "http.method")];
        assert!(rule.matches(&span));
    }

    #[test]
    fn test_matches_alternate_key_value_mismatch() {
        let rule = SamplingRule::new(
            1.0,
            None,
            None,
            None,
            Some(HashMap::from([("http.method".into(), "POST".into())])),
            None,
        );
        let mut span = make_span("op", "svc", "res");
        span.attrs = vec![TestAttr {
            key: "http.request.method",
            value: TestValue {
                value: "GET",
                is_metric: false,
            },
        }];
        span.alternates = vec![("http.request.method", "http.method")];
        assert!(!rule.matches(&span));
    }

    #[test]
    fn test_matches_non_http_tag_no_alternate_fallback() {
        // Non-http. keys do NOT fall through to alternate-key scan
        let rule = SamplingRule::new(
            1.0,
            None,
            None,
            None,
            Some(HashMap::from([("custom.tag".into(), "value".into())])),
            None,
        );
        let mut span = make_span("op", "svc", "res");
        span.attrs = vec![TestAttr {
            key: "some.other.key",
            value: TestValue {
                value: "value",
                is_metric: false,
            },
        }];
        span.alternates = vec![("some.other.key", "custom.tag")];
        assert!(!rule.matches(&span));
    }

    // --- Float attribute matching ---

    #[test]
    fn test_match_attribute_value_non_integer_float_wildcard_matches() {
        let rule = SamplingRule::new(
            1.0,
            None,
            None,
            None,
            Some(HashMap::from([("score".into(), "*".into())])),
            None,
        );
        let mut span = make_span("op", "svc", "res");
        span.attrs = vec![TestAttr {
            key: "score",
            value: TestValue {
                value: "3.14",
                is_metric: true,
            },
        }];
        assert!(rule.matches(&span));
    }

    #[test]
    fn test_match_attribute_value_non_integer_float_non_wildcard_no_match() {
        let rule = SamplingRule::new(
            1.0,
            None,
            None,
            None,
            Some(HashMap::from([("score".into(), "3.14".into())])),
            None,
        );
        let mut span = make_span("op", "svc", "res");
        span.attrs = vec![TestAttr {
            key: "score",
            value: TestValue {
                value: "3.14",
                is_metric: true,
            },
        }];
        assert!(!rule.matches(&span));
    }

    #[test]
    fn test_resource_mismatch_returns_false() {
        let rule = SamplingRule::new(
            1.0,
            None,
            Some("specific-resource".into()),
            None,
            None,
            None,
        );
        let span = make_span("op", "svc", "other-resource");
        assert!(!rule.matches(&span));
    }

    // --- RuleProvenance ---

    #[test]
    fn test_rule_provenance_from_str() {
        assert_eq!(RuleProvenance::from("customer"), RuleProvenance::Customer);
        assert_eq!(RuleProvenance::from("dynamic"), RuleProvenance::Dynamic);
        assert_eq!(RuleProvenance::from("default"), RuleProvenance::Default);
        assert_eq!(RuleProvenance::from("unknown"), RuleProvenance::Default);
        assert_eq!(RuleProvenance::from(""), RuleProvenance::Default);
    }

    #[test]
    fn test_rule_provenance_ordering() {
        assert!(RuleProvenance::Customer < RuleProvenance::Dynamic);
        assert!(RuleProvenance::Dynamic < RuleProvenance::Default);
    }
}