Skip to content

fix(sampling): ensure agent based sampling is not reset after forking and on tracer.configure #13560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

mabdinur
Copy link
Contributor

@mabdinur mabdinur commented Jun 2, 2025

Builds on 7fbdc9f

  • Fix: Avoids reinitializing the SpanAggregator on tracer.configure(...) and when an application forks. Instead SpanAggregator._reset() is called. This operation ensures global configurations are re-applied, trace buffer is reset, and trace writer is recreated. This ensures agent based sampling rules are not reset.

  • Clean up

    • Removes writer parameter from SpanAggregator.__init__(...) with this change the intialization of the global writer is an implementation detail of the SpanAggregator. There is no longer a need to supply the SpanAggregator with a writer on the initialization of the global tracer.
    • Removes the initialization of the SpanAggregator from _default_span_processors_factory. With this change the global tracer's SpanAggregator is never re-created. It's only modified when tracer.configure(..) is used.
    • Rename DatadogSampler._service_based_samplers property to DatadogSampler._agent_sampling_rules to improve clarity. These sampling rules are no longer supplied via environment variables or a programatic api, they can only be set by the Datadog Agent.
    • Splits SpanAggregator.trace_proccessors into two properties SpanAggregator.dd_proccessors and SpanAggregator.user_processors. SpanAggregator.users_proccessors is set after application startup via Tracer.configure(..) while SpanAggregator.dd_proccessors is internal to the ddtrace library and should only be set by ddtrace components. This separation allows us to avoid recreating all trace processors when tracer.configure() is called.
    • Adds a more descriptive release note to an unreleased fix.

Checklist

  • PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

Copy link
Contributor

github-actions bot commented Jun 2, 2025

CODEOWNERS have been resolved as:

releasenotes/notes/fix-agent-based-sampling-8877694a37053e51.yaml       @DataDog/apm-python
ddtrace/_trace/processor/__init__.py                                    @DataDog/apm-sdk-api-python
ddtrace/_trace/sampler.py                                               @DataDog/apm-sdk-api-python
ddtrace/_trace/tracer.py                                                @DataDog/apm-sdk-api-python
ddtrace/internal/ci_visibility/recorder.py                              @DataDog/ci-app-libraries
ddtrace/internal/writer/writer.py                                       @DataDog/apm-core-python
ddtrace/opentracer/tracer.py                                            @DataDog/apm-sdk-api-python
tests/ci_visibility/test_ci_visibility.py                               @DataDog/ci-app-libraries
tests/integration/test_priority_sampling.py                             @DataDog/apm-sdk-api-python
tests/tracer/test_processors.py                                         @DataDog/apm-sdk-api-python
tests/tracer/test_sampler.py                                            @DataDog/apm-sdk-api-python
tests/tracer/test_writer.py                                             @DataDog/apm-sdk-api-python
tests/utils.py                                                          @DataDog/python-guild

Copy link
Contributor

github-actions bot commented Jun 2, 2025

Bootstrap import analysis

Comparison of import times between this PR and base.

Summary

The average import time from this PR is: 275 ± 4 ms.

The average import time from base is: 277 ± 4 ms.

The import time difference between this PR and base is: -1.1 ± 0.2 ms.

Import time breakdown

The following import paths have grown:

ddtrace.auto 0.213 ms (0.08%)
ddtrace 0.149 ms (0.05%)
ddtrace.trace 0.149 ms (0.05%)
ddtrace._trace.tracer 0.107 ms (0.04%)
ddtrace.internal.schema.processor 0.107 ms (0.04%)
ddtrace._trace.filters 0.042 ms (0.02%)
ddtrace._trace.processor 0.042 ms (0.02%)
ddtrace.bootstrap.sitecustomize 0.063 ms (0.02%)
ddtrace.bootstrap.preload 0.063 ms (0.02%)
ddtrace.internal.products 0.063 ms (0.02%)
importlib.metadata 0.063 ms (0.02%)
importlib.metadata._collections 0.063 ms (0.02%)

The following import paths have shrunk:

ddtrace.auto 2.078 ms (0.75%)
ddtrace.bootstrap.sitecustomize 1.334 ms (0.48%)
ddtrace.bootstrap.preload 1.334 ms (0.48%)
ddtrace.internal.remoteconfig.client 0.632 ms (0.23%)
ddtrace.internal.products 0.062 ms (0.02%)
importlib.metadata 0.062 ms (0.02%)
importlib.metadata._itertools 0.062 ms (0.02%)
ddtrace 0.745 ms (0.27%)
ddtrace.trace 0.101 ms (0.04%)
ddtrace._trace.tracer 0.101 ms (0.04%)
ddtrace.settings.peer_service 0.101 ms (0.04%)

@pr-commenter
Copy link

pr-commenter bot commented Jun 2, 2025

Benchmarks

Benchmark execution time: 2025-06-17 04:04:14

Comparing candidate commit 7101cbd in PR branch munir/cleanup-agent-based-sampling-fix with baseline commit 02341fe in branch main.

Found 0 performance improvements and 5 performance regressions! Performance is the same for 556 metrics, 3 unstable metrics.

scenario:iastaspects-upper_aspect

  • 🟥 execution_time [+205.781ns; +239.780ns] or [+9.277%; +10.809%]

scenario:iastaspectsospath-ospathjoin_aspect

  • 🟥 execution_time [+874.583ns; +968.053ns] or [+14.197%; +15.715%]

scenario:iastaspectsospath-ospathsplitext_aspect

  • 🟥 execution_time [+353.219ns; +538.090ns] or [+7.852%; +11.961%]

scenario:telemetryaddmetric-1-distribution-metric-1-times

  • 🟥 execution_time [+318.413ns; +378.706ns] or [+10.940%; +13.011%]

scenario:telemetryaddmetric-flush-1-metric

  • 🟥 execution_time [+337.574ns; +483.705ns] or [+8.169%; +11.706%]

Copy link
Member

@brettlangdon brettlangdon left a comment

Choose a reason for hiding this comment

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

would be nice to have a regression test for this, otherwise lgtm

@mabdinur mabdinur force-pushed the munir/cleanup-agent-based-sampling-fix branch from 4d50f44 to 7e14fee Compare June 15, 2025 01:56
@mabdinur mabdinur changed the title chore(sampling): clean up agent based sampling fix fix(sampling): clean up agent based sampling fix Jun 15, 2025
@mabdinur mabdinur changed the title fix(sampling): clean up agent based sampling fix fix(sampling): ensure agent service based sampling is not reset after forks and on tracer.configure Jun 15, 2025
@mabdinur mabdinur changed the title fix(sampling): ensure agent service based sampling is not reset after forks and on tracer.configure fix(sampling): ensure agent service based sampling is not reset after forking and on tracer.configure Jun 15, 2025
@mabdinur mabdinur force-pushed the munir/cleanup-agent-based-sampling-fix branch from 6afec51 to cbfe917 Compare June 15, 2025 17:38
@mabdinur mabdinur changed the title fix(sampling): ensure agent service based sampling is not reset after forking and on tracer.configure fix(sampling): ensure agent based sampling is not reset after forking and on tracer.configure Jun 15, 2025
@mabdinur mabdinur marked this pull request as ready for review June 15, 2025 22:49
@mabdinur mabdinur requested review from a team as code owners June 15, 2025 22:49
Copy link
Collaborator

@emmettbutler emmettbutler left a comment

Choose a reason for hiding this comment

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

Nice work. Is there a test for how this works in subprocesses that I'm missing?

@@ -101,7 +102,7 @@ def __init__(
else:
self.rules: List[SamplingRule] = rules or []
# Set Agent based samplers
self._by_service_samplers: Dict[str, RateSampler] = {}
self._agent_sampling_rules = agent_sampling_rules or {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

_agent_based_samplers might be better here, since the use of the term "rules" conflicts with the way it's used elsewhere in the library as opposed to "sampler".

@mabdinur mabdinur force-pushed the munir/cleanup-agent-based-sampling-fix branch from ef76a21 to 4d311a8 Compare June 17, 2025 00:17
return trace

class UserProc(TraceProcessor)
def process_trace(self, trace):
Copy link
Contributor

Choose a reason for hiding this comment

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

Code Quality Violation

class names should be PascalCase (...read more)

Class names should be PascalCase and not camelCase or snake_case.

Learn More

View in Datadog  Leave us feedback  Documentation


class UserProc(TraceProcessor)
def process_trace(self, trace):
return trace
Copy link
Contributor

Choose a reason for hiding this comment

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

Code Quality Violation

return outside of a function (...read more)

All return statements must be within a function. Putting a return statement outside of a function may have unexpected behaviors (such as exiting the program early).

View in Datadog  Leave us feedback  Documentation

@mabdinur mabdinur force-pushed the munir/cleanup-agent-based-sampling-fix branch from 376430f to 7101cbd Compare June 17, 2025 03:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants