Skip to content

Commit 040260c

Browse files
authored
refactor(sampling): move the sampling logic from dd-trace-rs [APMSP-2946] (#1927)
# What does this PR do? Moves the sampling logic from `dd-trace-rs` so that it can be reused. # Motivation Reuse all the things. # Additional Notes Has been tested and benchmarked with the code in `dd-trace-rs`. # How to test the change? Unit tests and benchmarks are here. Co-authored-by: bjorn.antonsson <bjorn.antonsson@datadoghq.com>
1 parent 37d17ee commit 040260c

25 files changed

Lines changed: 5186 additions & 2 deletions

.codecov.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ component_management:
5555
name: libdd-profiling-ffi # this is a display name, and can be changed freely
5656
paths:
5757
- libdd-profiling-ffi
58+
- component_id: sampling # this is an identifier that should not be changed
59+
name: libdd-sampling # this is a display name, and can be changed freely
60+
paths:
61+
- libdd-sampling
5862
- component_id: sidecar # this is an identifier that should not be changed
5963
name: datadog-sidecar # this is a display name, and can be changed freely
6064
paths:

.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

.github/labeler.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,9 @@ crashtracker:
6262
'libdd-crashtracker/**/*',
6363
'libdd-crashtracker-ffi/**/*',
6464
]
65+
66+
sampling:
67+
- changed-files:
68+
- any-glob-to-any-file: [
69+
'libdd-sampling/**/*',
70+
]

Cargo.lock

Lines changed: 26 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>

benchmark/run_benchmarks_ci.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pushd "${PROJECT_DIR}" > /dev/null
2222

2323
# Run benchmarks
2424
message "Running benchmarks"
25-
cargo bench --workspace --features libdd-crashtracker/benchmarking -- --warm-up-time 1 --measurement-time 5 --sample-size=200
25+
cargo bench --workspace --features libdd-crashtracker/benchmarking,libdd-sampling/v04_span -- --warm-up-time 1 --measurement-time 5 --sample-size=200
2626
message "Finished running benchmarks"
2727

2828
# Copy the benchmark results to the output directory

libdd-common/src/lib.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use anyhow::Context;
1010
use http::uri;
1111
use serde::de::Error;
1212
use serde::{Deserialize, Deserializer, Serialize, Serializer};
13-
use std::sync::{Mutex, MutexGuard};
13+
use std::sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
1414
use std::{borrow::Cow, ops::Deref, path::PathBuf, str::FromStr};
1515

1616
pub mod azure_app_services;
@@ -90,6 +90,51 @@ impl<T> MutexExt<T> for Mutex<T> {
9090
}
9191
}
9292

93+
/// Extension trait for `RwLock` to provide methods that acquire read/write locks, panicking if
94+
/// the lock is poisoned.
95+
///
96+
/// Mirrors [`MutexExt`] for `RwLock` so callers avoid `#[allow(clippy::unwrap_used)]` at each
97+
/// lock site.
98+
///
99+
/// # Examples
100+
///
101+
/// ```
102+
/// use libdd_common::RwLockExt;
103+
/// use std::sync::{Arc, RwLock};
104+
///
105+
/// let data = Arc::new(RwLock::new(5));
106+
/// let data_clone = Arc::clone(&data);
107+
///
108+
/// std::thread::spawn(move || {
109+
/// let mut num = data_clone.write_or_panic();
110+
/// *num += 1;
111+
/// })
112+
/// .join()
113+
/// .expect("Thread panicked");
114+
///
115+
/// assert_eq!(*data.read_or_panic(), 6);
116+
/// ```
117+
pub trait RwLockExt<T> {
118+
fn read_or_panic(&self) -> RwLockReadGuard<'_, T>;
119+
fn write_or_panic(&self) -> RwLockWriteGuard<'_, T>;
120+
}
121+
122+
impl<T> RwLockExt<T> for RwLock<T> {
123+
#[inline(always)]
124+
#[track_caller]
125+
fn read_or_panic(&self) -> RwLockReadGuard<'_, T> {
126+
#[allow(clippy::unwrap_used)]
127+
self.read().unwrap()
128+
}
129+
130+
#[inline(always)]
131+
#[track_caller]
132+
fn write_or_panic(&self) -> RwLockWriteGuard<'_, T> {
133+
#[allow(clippy::unwrap_used)]
134+
self.write().unwrap()
135+
}
136+
}
137+
93138
pub mod header {
94139
#![allow(clippy::declare_interior_mutable_const)]
95140
use http::{header::HeaderName, HeaderValue};

libdd-sampling/Cargo.toml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 = "0.1.0"
7+
edition.workspace = true
8+
rust-version.workspace = true
9+
license.workspace = true
10+
homepage = "https://github.com/DataDog/libdatadog/tree/main/libdd-sampling"
11+
repository = "https://github.com/DataDog/libdatadog/tree/main/libdd-sampling"
12+
description = "Core sampling logic for Datadog tracing"
13+
authors.workspace = true
14+
autobenches = false
15+
16+
[lib]
17+
bench = false
18+
19+
[[bench]]
20+
name = "sampling_bench"
21+
harness = false
22+
path = "benches/sampling_bench.rs"
23+
required-features = ["v04_span"]
24+
25+
[dependencies]
26+
serde = { version = "1.0", features = ["derive"] }
27+
serde_json = "1.0"
28+
lru = "0.16.3"
29+
libdd-common = { path = "../libdd-common", version = "4.0.0" }
30+
libdd-trace-utils = { path = "../libdd-trace-utils", version = "3.0.1", optional = true }
31+
32+
[features]
33+
v04_span = ["dep:libdd-trace-utils"]
34+
35+
[dev-dependencies]
36+
criterion = "0.5"
37+
libdd-common = { path = "../libdd-common", version = "4.0.0", features = ["bench-utils"] }

0 commit comments

Comments
 (0)