Skip to content

Conversation

yuandesu
Copy link
Contributor

@yuandesu yuandesu commented Jul 1, 2025

What does this PR do?

  • Check tracing.sampling.default_rate as fallback for sample_rate
  • Resolve null sampling_rules in diagnostic logs when sampler is unavailable by check tracing.sampling.rules when no sampler is configured

Motivation:

The sample_rate and sampling_rules are null even we configured via env var or Datadog.configure.

  • | `tracing.sampling.default_rate` | `DD_TRACE_SAMPLE_RATE` | `Float` | `nil` | Sets the trace sampling rate between `0.0` (0%) and `1.0` (100%). |
  • | `tracing.sampling.rules` | `DD_TRACE_SAMPLING_RULES` | `String` | `nil` | Sets trace-level sampling rules, matching against the local root span. The format is a `String` with JSON, containing an Array of Objects. Each Object must have a float attribute `sample_rate` (between 0.0 and 1.0, inclusive), and optionally `name`, `service`, `resource`, and `tags` string attributes. `name`, `service`, `resource`, and `tags` control to which traces this sampling rule applies; if they are all absent, then this rule applies to all traces. Rules are evaluated in order of declaration in the array; only the first to match is applied. If none apply, then `tracing.sampling.default_rate` is applied. |

Before:

app-1  | I, [2025-07-01T08:14:36.550791 #1]  INFO -- datadog: [datadog] (/usr/local/bundle/gems/datadog-2.17.0/lib/datadog/core/diagnostics/environment_logger.rb:15:in 'Datadog::Core::Diagnostics::EnvironmentLogging#log_configuration!') DATADOG CONFIGURATION - TRACING - {"enabled":true,"agent_url":"http://agent:8126?timeout=30","analytics_enabled":false,"sample_rate":null,"sampling_rules":null,"integrations_loaded":null,"partial_flushing_enabled":false}

After:

app-1  | I, [2025-07-01T08:16:06.774422 #1]  INFO -- datadog: [datadog] (/usr/local/bundle/gems/datadog-2.17.0/lib/datadog/core/diagnostics/environment_logger.rb:15:in 'Datadog::Core::Diagnostics::EnvironmentLogging#log_configuration!') DATADOG CONFIGURATION - TRACING - {"enabled":true,"agent_url":"http://agent:8126?timeout=30","analytics_enabled":false,"sample_rate":0.33,"sampling_rules":"[{\"service\":\"my-ruby-app\",\"sample_rate\":0.22}]","integrations_loaded":null,"partial_flushing_enabled":false}

Change log entry

Yes. Fix sampling rules and sample rate reporting in environment logger.

Additional Notes:

How to test the change?

You can reproduce this issue with the following files.

app.rb

#!/usr/bin/env ruby

require 'datadog'

Datadog.configure do |c|
  c.service = 'my-ruby-app'
  c.tracing.sampling.default_rate = 0.33
  c.tracing.sampling.rules = [
    {
      service: "my-ruby-app",
      sample_rate: 0.22
    }
].to_json
end

def main
  Datadog::Tracing.trace('do_something') do |root_span|
    puts "Do Something"
  end
end

begin
  main
rescue => e
  puts "Error: #{e.message}"
  puts e.backtrace
  exit 1
end 

Gemfile

source 'https://rubygems.org'

gem 'datadog', '~> 2.17'

Dockerfile

FROM ruby:3
WORKDIR /app
COPY Gemfile Gemfile.lock .
RUN bundle install
COPY app.rb .
CMD ["ruby", "app.rb"]

docker-compose.yaml

services:
  agent:
    image: datadog/agent:7
    volumes:
      - /sys/fs/cgroup/:/host/sys/fs/cgroup:ro
      - /proc/:/host/proc/:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
    pid: host
    cgroup: host
    environment:
      - DD_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
      - DD_APM_ENABLED=true
      - DD_APM_NON_LOCAL_TRAFFIC=true
  app:
    build:
      context: .
    volumes: 
     # To test updated code.
      - ./environment_logger.rb:/usr/local/bundle/gems/datadog-2.17.0/lib/datadog/tracing/diagnostics/environment_logger.rb
    depends_on:
      - agent
    environment:
      - DD_AGENT_HOST=agent
      - DD_TRACE_DEBUG=true

@yuandesu yuandesu requested a review from a team as a code owner July 1, 2025 08:23
Copy link

github-actions bot commented Jul 1, 2025

Thank you for updating Change log entry section 👏

Visited at: 2025-07-03 02:14:55 UTC

@codecov-commenter
Copy link

codecov-commenter commented Jul 1, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.52%. Comparing base (fd3e808) to head (2fd1aaf).
Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4772      +/-   ##
==========================================
- Coverage   97.54%   97.52%   -0.02%     
==========================================
  Files        1484     1483       -1     
  Lines       88499    88548      +49     
  Branches     4588     4594       +6     
==========================================
+ Hits        86322    86356      +34     
- Misses       2177     2192      +15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 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.

@marcotc
Copy link
Member

marcotc commented Jul 2, 2025

@yuandesu great work! Can you add a changelog entry to the PR description, like it's mentioned in #4772 (comment)?

@yuandesu
Copy link
Contributor Author

yuandesu commented Jul 3, 2025

@yuandesu great work! Can you add a changelog entry to the PR description, like it's mentioned in #4772 (comment)?

@marcotc Thank you for the review!
I filled in the change log entry and added test as well. Please have a look when you have time.

@pr-commenter
Copy link

pr-commenter bot commented Jul 3, 2025

Benchmarks

Benchmark execution time: 2025-07-04 04:40:32

Comparing candidate commit 2fd1aaf in PR branch yuandesu/fix_null_sampling_rules_environment_logger with baseline commit fd3e808 in branch master.

Found 1 performance improvements and 1 performance regressions! Performance is the same for 45 metrics, 2 unstable metrics.

scenario:profiling - intern_all 1000 repeated strings

  • 🟥 throughput [-1955.080op/s; -1885.825op/s] or [-8.125%; -7.837%]

scenario:tracing - Tracing.log_correlation

  • 🟩 throughput [+10067.425op/s; +10375.011op/s] or [+10.077%; +10.385%]

@yuandesu
Copy link
Contributor Author

yuandesu commented Jul 4, 2025

/merge

@dd-devflow
Copy link

dd-devflow bot commented Jul 4, 2025

View all feedbacks in Devflow UI.

2025-07-04 04:45:50 UTC ℹ️ Start processing command /merge


2025-07-04 04:45:59 UTC ℹ️ MergeQueue: waiting for PR to be ready

This merge request is not mergeable yet, because of pending checks/missing approvals. It will be added to the queue as soon as checks pass and/or get approvals.
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.


2025-07-04 08:46:09 UTC ⚠️ MergeQueue: This merge request was unqueued

devflow unqueued this merge request: It did not become mergeable within the expected time

@p-datadog p-datadog merged commit 74a83a1 into master Jul 7, 2025
753 of 764 checks passed
@p-datadog p-datadog deleted the yuandesu/fix_null_sampling_rules_environment_logger branch July 7, 2025 16:00
@github-actions github-actions bot added this to the 2.19.0 milestone Jul 7, 2025
@lloeki lloeki mentioned this pull request Jul 24, 2025
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.

4 participants