Skip to content

refactor(sampling): move the sampling logic from dd-trace-rs [APMSP-2946]#1927

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 6 commits intomainfrom
ban/sample-all-the-things
May 8, 2026
Merged

refactor(sampling): move the sampling logic from dd-trace-rs [APMSP-2946]#1927
gh-worker-dd-mergequeue-cf854d[bot] merged 6 commits intomainfrom
ban/sample-all-the-things

Conversation

@bantonsson
Copy link
Copy Markdown
Contributor

@bantonsson bantonsson commented Apr 27, 2026

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.

@bantonsson bantonsson requested review from a team as code owners April 27, 2026 12:02
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 27, 2026

Clippy Allow Annotation Report

Comparing clippy allow annotations between branches:

  • Base Branch: origin/main
  • PR Branch: origin/ban/sample-all-the-things

Summary by Rule

Rule Base Branch PR Branch Change
unwrap_used 3 6 ⚠️ +3 (+100.0%)
Total 3 6 ⚠️ +3 (+100.0%)

Annotation Counts by File

File Base Branch PR Branch Change
libdd-common/src/lib.rs 3 6 ⚠️ +3 (+100.0%)

Annotation Stats by Crate

Crate Base Branch PR Branch Change
clippy-annotation-reporter 5 5 No change (0%)
datadog-ffe-ffi 1 1 No change (0%)
datadog-ipc 21 21 No change (0%)
datadog-live-debugger 6 6 No change (0%)
datadog-live-debugger-ffi 10 10 No change (0%)
datadog-profiling-replayer 4 4 No change (0%)
datadog-remote-config 3 3 No change (0%)
datadog-sidecar 57 57 No change (0%)
libdd-common 10 13 ⚠️ +3 (+30.0%)
libdd-common-ffi 12 12 No change (0%)
libdd-data-pipeline 5 5 No change (0%)
libdd-ddsketch 2 2 No change (0%)
libdd-dogstatsd-client 1 1 No change (0%)
libdd-profiling 13 13 No change (0%)
libdd-telemetry 20 20 No change (0%)
libdd-tinybytes 4 4 No change (0%)
libdd-trace-normalization 2 2 No change (0%)
libdd-trace-obfuscation 8 8 No change (0%)
libdd-trace-stats 1 1 No change (0%)
libdd-trace-utils 15 15 No change (0%)
Total 200 203 ⚠️ +3 (+1.5%)

About This Report

This report tracks Clippy allow annotations for specific rules, showing how they've changed in this PR. Decreasing the number of these annotations generally improves code quality.

@bantonsson bantonsson force-pushed the ban/sample-all-the-things branch from b244f7c to c710521 Compare April 27, 2026 12:06
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b244f7c989

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread libdd-sampling/src/agent_service_sampler.rs Outdated
Comment thread libdd-sampling/src/rate_limiter.rs Outdated
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 27, 2026

Codecov Report

❌ Patch coverage is 97.27047% with 66 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.65%. Comparing base (37d17ee) to head (89db307).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1927      +/-   ##
==========================================
+ Coverage   71.88%   72.65%   +0.76%     
==========================================
  Files         437      448      +11     
  Lines       71152    73570    +2418     
==========================================
+ Hits        51150    53451    +2301     
- Misses      20002    20119     +117     
Components Coverage Δ
libdd-crashtracker 65.21% <ø> (-0.05%) ⬇️
libdd-crashtracker-ffi 38.04% <ø> (-0.37%) ⬇️
libdd-alloc 98.77% <ø> (ø)
libdd-data-pipeline 85.90% <ø> (-0.40%) ⬇️
libdd-data-pipeline-ffi 71.79% <ø> (-2.25%) ⬇️
libdd-common 79.81% <100.00%> (+0.07%) ⬆️
libdd-common-ffi 74.41% <ø> (ø)
libdd-telemetry 69.26% <ø> (-0.03%) ⬇️
libdd-telemetry-ffi 19.37% <ø> (ø)
libdd-dogstatsd-client 82.64% <ø> (ø)
datadog-ipc 76.22% <ø> (ø)
libdd-profiling 81.56% <ø> (-0.02%) ⬇️
libdd-profiling-ffi 64.51% <ø> (ø)
libdd-sampling 97.25% <97.25%> (∅)
datadog-sidecar 30.56% <ø> (-0.11%) ⬇️
datdog-sidecar-ffi 16.83% <ø> (-0.30%) ⬇️
spawn-worker 54.69% <ø> (ø)
libdd-tinybytes 93.16% <ø> (ø)
libdd-trace-normalization 81.71% <ø> (ø)
libdd-trace-obfuscation 87.26% <ø> (ø)
libdd-trace-protobuf 68.25% <ø> (ø)
libdd-trace-utils 89.27% <ø> (ø)
libdd-tracer-flare 86.88% <ø> (ø)
libdd-log 74.83% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@datadog-prod-us1-4
Copy link
Copy Markdown

datadog-prod-us1-4 Bot commented Apr 27, 2026

Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage (details)
Patch Coverage: 97.27%
Overall Coverage: 72.65% (+0.76%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 89db307 | Docs | Datadog PR Page | Give us feedback!

@dd-octo-sts
Copy link
Copy Markdown
Contributor

dd-octo-sts Bot commented Apr 27, 2026

Artifact Size Benchmark Report

aarch64-alpine-linux-musl
Artifact Baseline Commit Change
/aarch64-alpine-linux-musl/lib/libdatadog_profiling.so 7.51 MB 7.51 MB 0% (0 B) 👌
/aarch64-alpine-linux-musl/lib/libdatadog_profiling.a 81.65 MB 81.65 MB +0% (+40 B) 👌
aarch64-unknown-linux-gnu
Artifact Baseline Commit Change
/aarch64-unknown-linux-gnu/lib/libdatadog_profiling.so 10.00 MB 10.00 MB 0% (0 B) 👌
/aarch64-unknown-linux-gnu/lib/libdatadog_profiling.a 97.81 MB 97.81 MB +0% (+40 B) 👌
libdatadog-x64-windows
Artifact Baseline Commit Change
/libdatadog-x64-windows/debug/dynamic/datadog_profiling_ffi.dll 24.41 MB 24.41 MB 0% (0 B) 👌
/libdatadog-x64-windows/debug/dynamic/datadog_profiling_ffi.lib 79.87 KB 79.87 KB 0% (0 B) 👌
/libdatadog-x64-windows/debug/dynamic/datadog_profiling_ffi.pdb 179.67 MB 179.66 MB -0% (-8.00 KB) 👌
/libdatadog-x64-windows/debug/static/datadog_profiling_ffi.lib 910.54 MB 910.54 MB 0% (0 B) 👌
/libdatadog-x64-windows/release/dynamic/datadog_profiling_ffi.dll 7.71 MB 7.71 MB 0% (0 B) 👌
/libdatadog-x64-windows/release/dynamic/datadog_profiling_ffi.lib 79.87 KB 79.87 KB 0% (0 B) 👌
/libdatadog-x64-windows/release/dynamic/datadog_profiling_ffi.pdb 23.09 MB 23.09 MB 0% (0 B) 👌
/libdatadog-x64-windows/release/static/datadog_profiling_ffi.lib 45.22 MB 45.22 MB 0% (0 B) 👌
libdatadog-x86-windows
Artifact Baseline Commit Change
/libdatadog-x86-windows/debug/dynamic/datadog_profiling_ffi.dll 21.02 MB 21.02 MB 0% (0 B) 👌
/libdatadog-x86-windows/debug/dynamic/datadog_profiling_ffi.lib 81.11 KB 81.11 KB 0% (0 B) 👌
/libdatadog-x86-windows/debug/dynamic/datadog_profiling_ffi.pdb 183.79 MB 183.79 MB 0% (0 B) 👌
/libdatadog-x86-windows/debug/static/datadog_profiling_ffi.lib 896.63 MB 896.63 MB 0% (0 B) 👌
/libdatadog-x86-windows/release/dynamic/datadog_profiling_ffi.dll 5.98 MB 5.98 MB 0% (0 B) 👌
/libdatadog-x86-windows/release/dynamic/datadog_profiling_ffi.lib 81.11 KB 81.11 KB 0% (0 B) 👌
/libdatadog-x86-windows/release/dynamic/datadog_profiling_ffi.pdb 24.73 MB 24.73 MB 0% (0 B) 👌
/libdatadog-x86-windows/release/static/datadog_profiling_ffi.lib 42.73 MB 42.73 MB 0% (0 B) 👌
x86_64-alpine-linux-musl
Artifact Baseline Commit Change
/x86_64-alpine-linux-musl/lib/libdatadog_profiling.a 72.76 MB 72.76 MB +0% (+32 B) 👌
/x86_64-alpine-linux-musl/lib/libdatadog_profiling.so 8.40 MB 8.40 MB 0% (0 B) 👌
x86_64-unknown-linux-gnu
Artifact Baseline Commit Change
/x86_64-unknown-linux-gnu/lib/libdatadog_profiling.a 90.50 MB 90.50 MB +0% (+40 B) 👌
/x86_64-unknown-linux-gnu/lib/libdatadog_profiling.so 10.03 MB 10.03 MB 0% (0 B) 👌

gh-worker-dd-mergequeue-cf854d Bot pushed a commit to DataDog/dd-trace-rs that referenced this pull request Apr 29, 2026
…ken loss (#215)

# What does this PR do?

Fixes for issues found in this [PR](DataDog/libdatadog#1927) while moving the sampling code to `libdatadog`.

# Motivation

Fix all the bugs


Co-authored-by: bjorn.antonsson <bjorn.antonsson@datadoghq.com>
@bantonsson bantonsson force-pushed the ban/sample-all-the-things branch 3 times, most recently from 06ce6dd to 0b83000 Compare April 29, 2026 15:14
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know that there's already a rate limiter in libdd-common/src/rate_limiter.rs?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope I didn't. Thanks. I'll look at it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's by the way the limiter PHP uses, and is a sliding window rather than discrete windows.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a dig through the specs for _dd.limit_psr and how it should be reported back to the agent, which is what this rate limiter is implementing, and I can't see how you could do that with the rate limiter in libdd-common.

Copy link
Copy Markdown
Contributor

@bwoebi bwoebi May 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But, that's what the rate() function is for on the Limiter trait?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are not the same, and the RFC mandates tokenized buckets and not a sliding window.

@bantonsson bantonsson force-pushed the ban/sample-all-the-things branch from 0b83000 to f108550 Compare April 29, 2026 15:36
Comment thread libdd-sampling/src/sampling_rule_config.rs
@bantonsson bantonsson force-pushed the ban/sample-all-the-things branch 3 times, most recently from 0bb5943 to d30a3f2 Compare April 30, 2026 13:23
@bantonsson bantonsson force-pushed the ban/sample-all-the-things branch from d30a3f2 to ce8315c Compare May 4, 2026 12:09
/// Checks if the given subject matches the glob pattern
/// The match is case insensitive.
pub fn matches(&self, subject: &str) -> bool {
let subject_lower = subject.to_lowercase();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to_lowercase() is going to allocate a new string on every match. Would it be more performant to use eq_ignore_ascii_case() when checking for a match below?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see there is nothing in the RFC that says that it should be ASCII. I'll save this optimization for a follow up PR.

Comment thread libdd-sampling/src/glob_matcher.rs Outdated
Comment thread libdd-sampling/src/lib.rs
Comment thread libdd-sampling/src/rate_limiter.rs Outdated
Comment thread libdd-sampling/src/rate_limiter.rs Outdated
Comment thread libdd-sampling/src/constants.rs Outdated
Comment thread libdd-sampling/src/datadog_sampler.rs Outdated
Comment thread libdd-sampling/src/sampling_rule.rs Outdated
Copy link
Copy Markdown
Contributor

@ekump ekump left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a ticket to Jira for this?

Left some minor comments and performance concern trolling. Nothing blocking. LGTM.

Comment thread libdd-sampling/src/agent_service_sampler.rs
Comment thread libdd-sampling/src/agent_service_sampler.rs Outdated
Comment thread libdd-sampling/src/agent_service_sampler.rs Outdated
Comment thread libdd-sampling/src/datadog_sampler.rs Outdated
Comment thread libdd-sampling/src/datadog_sampler.rs Outdated
}

/// Represents a priority for sampling rules
#[allow(dead_code)]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to allow dead_code for a pub enum?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only pub(crate), so it's needed (for now).

Comment thread libdd-sampling/src/sampling_rule_config.rs
Comment thread libdd-sampling/src/types.rs Outdated
Comment thread libdd-sampling/src/types.rs Outdated
.get("http.status_code")
.and_then(|f| {
let v = *f as u64;
(v > 0 && v <= u32::MAX as u64).then_some(v as u32)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(v > 0 && v <= u32::MAX as u64).then_some(v as u32)
u32::try_from(v).ok()?

@yannham
Copy link
Copy Markdown
Contributor

yannham commented May 6, 2026

(PS: Given the size of the PR, I went fast on tests and benches)

Comment thread libdd-sampling/src/datadog_sampler.rs Outdated
Comment thread libdd-sampling/src/glob_matcher.rs Outdated
Comment thread libdd-sampling/src/datadog_sampler.rs
Copy link
Copy Markdown
Contributor

@hoolioh hoolioh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add the crate to the .codecov file.

Comment thread libdd-sampling/Cargo.toml Outdated
@bantonsson bantonsson changed the title refactor(sampling): move the sampling logic from dd-trace-rs refactor(sampling): move the sampling logic from dd-trace-rs [APMSP-2946] May 6, 2026
@bantonsson bantonsson force-pushed the ban/sample-all-the-things branch 2 times, most recently from 8a9a8ab to c56620e Compare May 7, 2026 12:16
@github-actions github-actions Bot added the common label May 7, 2026
@bantonsson bantonsson force-pushed the ban/sample-all-the-things branch 2 times, most recently from 64c8403 to 493fd9e Compare May 7, 2026 15:26
@bantonsson bantonsson force-pushed the ban/sample-all-the-things branch from 493fd9e to 89db307 Compare May 8, 2026 11:06
@bantonsson
Copy link
Copy Markdown
Contributor Author

/merge

@gh-worker-devflow-routing-ef8351
Copy link
Copy Markdown

gh-worker-devflow-routing-ef8351 Bot commented May 8, 2026

View all feedbacks in Devflow UI.

2026-05-08 11:07:12 UTC ℹ️ Start processing command /merge


2026-05-08 11:07:19 UTC ℹ️ MergeQueue: waiting for PR to be ready

This pull request is not mergeable according to GitHub. Common reasons include pending required checks, missing approvals, or merge conflicts — but it could also be blocked by other repository rules or settings.
It will be added to the queue as soon as checks pass and/or get approvals. View in MergeQueue UI.
Note: if you pushed new commits since the last approval, you may need additional approval.
You can remove it from the waiting list with /remove command.


2026-05-08 12:59:08 UTC ℹ️ MergeQueue: merge request added to the queue

The expected merge time in main is approximately 49m (p90).


2026-05-08 13:40:11 UTC ℹ️ MergeQueue: This merge request was merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants