Skip to content

Commit 4db525d

Browse files
committed
chore(sampling): move the libdd-sampling crate from dd-trace-rs
1 parent 53c8c16 commit 4db525d

15 files changed

Lines changed: 3177 additions & 0 deletions

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ libdd-library-config*/ @DataDog/apm-sdk-capabilities-rust
4949
libdd-log*/ @DataDog/apm-common-components-core
5050
libdd-otel-thread-ctx/ @DataDog/apm-common-components-core
5151
libdd-profiling*/ @DataDog/libdatadog-profiling
52+
libdd-sampling/ @DataDog/apm-common-components-core
5253
libdd-shared-runtime*/ @DataDog/apm-common-components-core
5354
libdd-telemetry*/ @DataDog/apm-common-components-core
5455
libdd-tinybytes @DataDog/apm-common-components-core

libdd-sampling/Cargo.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2025-Present Datadog, Inc. https://www.datadoghq.com/
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
[package]
5+
name = "libdd-sampling"
6+
version.workspace = true
7+
edition.workspace = true
8+
rust-version.workspace = true
9+
license.workspace = true
10+
repository.workspace = true
11+
readme.workspace = true
12+
description = "Core sampling logic for Datadog tracing"
13+
authors.workspace = true
14+
15+
[dependencies]
16+
serde = { workspace = true, features = ["derive"] }
17+
serde_json = { workspace = true }
18+
hashbrown = { workspace = true }
19+
foldhash = { workspace = true }
20+
lru = "0.16.3"
21+
22+
[dev-dependencies]
23+
criterion = "0.5"
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2025-Present Datadog, Inc. https://www.datadoghq.com/
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use std::{
5+
collections::HashMap,
6+
sync::{Arc, RwLock},
7+
};
8+
9+
use crate::rate_sampler::RateSampler;
10+
11+
#[derive(Debug, serde::Deserialize)]
12+
pub struct AgentRates<'a> {
13+
#[serde(borrow)]
14+
pub rate_by_service: Option<HashMap<&'a str, f64>>,
15+
}
16+
17+
#[derive(Debug, Default, Clone)]
18+
pub struct ServicesSampler {
19+
inner: Arc<RwLock<HashMap<String, RateSampler>>>,
20+
}
21+
22+
impl ServicesSampler {
23+
pub fn get(&self, service: &str) -> Option<RateSampler> {
24+
self.inner.read().unwrap().get(service).cloned()
25+
}
26+
27+
pub fn update_rates<I: IntoIterator<Item = (String, f64)>>(&self, rates: I) {
28+
let new_rates: HashMap<_, _> = rates
29+
.into_iter()
30+
.map(|(s, r)| (s, RateSampler::new(r)))
31+
.collect();
32+
*self.inner.write().unwrap() = new_rates;
33+
}
34+
35+
// used for testing purposes
36+
37+
#[allow(dead_code)]
38+
pub(crate) fn is_empty(&self) -> bool {
39+
self.inner.read().unwrap().is_empty()
40+
}
41+
42+
#[allow(dead_code)]
43+
pub(crate) fn len(&self) -> usize {
44+
self.inner.read().unwrap().len()
45+
}
46+
47+
#[allow(dead_code)]
48+
pub(crate) fn contains_key(&self, service: &str) -> bool {
49+
self.inner.read().unwrap().contains_key(service)
50+
}
51+
}

libdd-sampling/src/constants.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2025-Present Datadog, Inc. https://www.datadoghq.com/
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//! Shared constants for the datadog_opentelemetry::sampling crate
5+
6+
/// Sampling rate limits
7+
pub mod rate {
8+
/// Maximum sampling rate
9+
pub const MAX_SAMPLE_RATE: f64 = 1.0;
10+
/// Minimum sampling rate
11+
pub const MIN_SAMPLE_RATE: f64 = 0.0;
12+
}
13+
14+
/// Pattern matching constants
15+
pub mod pattern {
16+
/// Marker to represent "no rule" for a field (empty string)
17+
pub const NO_RULE: &str = "";
18+
}
19+
20+
/// Numeric constants used in sampling algorithms
21+
pub mod numeric {
22+
/// Knuth's multiplicative hash factor for deterministic sampling
23+
pub const KNUTH_FACTOR: u64 = 1_111_111_111_111_111_111;
24+
/// Maximum 64-bit unsigned integer value
25+
pub const MAX_UINT_64BITS: u64 = u64::MAX;
26+
}
27+
28+
#[allow(unused)]
29+
/// Attribute keys used in tracing
30+
pub mod attr {
31+
/// Service name attribute key
32+
pub const SERVICE_TAG: &str = "service.name";
33+
/// Environment attribute key
34+
pub const ENV_TAG: &str = "env";
35+
/// Resource name attribute key
36+
pub const RESOURCE_TAG: &str = "resource.name";
37+
}
38+
39+
#[allow(unused)]
40+
/// Rule provenance categories
41+
pub mod provenance {
42+
/// Customer-defined rules
43+
pub const CUSTOMER: &str = "customer";
44+
/// Dynamically loaded rules
45+
pub const DYNAMIC: &str = "dynamic";
46+
/// Default built-in rules
47+
pub const DEFAULT: &str = "default";
48+
}

0 commit comments

Comments
 (0)