Skip to content

Conversation

@github-actions
Copy link
Contributor

Backport ca79351 from #13560 to 3.9.

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 can be 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.
    • Moves all implementation details of resetting the SpanAggregator from Tracer.configure(...) and Tracer._recreate(...) to SpanAggregator.reset(...).
    • 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_based_sampler 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

… and on tracer.configure (#13560)

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 can be 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.
- Moves all implementation details of resetting the `SpanAggregator`
from `Tracer.configure(...)` and `Tracer._recreate(...)` to
`SpanAggregator.reset(...)`.
- 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_based_sampler` 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
- [x] 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](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
- The change includes or references documentation updates if necessary
- Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

## Reviewer Checklist
- [x] 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](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
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](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)

---------

Co-authored-by: Brett Langdon <[email protected]>
(cherry picked from commit ca79351)
@erikayasuda erikayasuda reopened this Jun 23, 2025
@github-actions
Copy link
Contributor Author

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

@github-actions
Copy link
Contributor Author

github-actions bot commented Jun 23, 2025

Bootstrap import analysis

Comparison of import times between this PR and base.

Summary

The average import time from this PR is: 277 ± 3 ms.

The average import time from base is: 280 ± 3 ms.

The import time difference between this PR and base is: -2.5 ± 0.1 ms.

Import time breakdown

The following import paths have grown:

ddtrace.auto 0.238 ms (0.09%)
ddtrace 0.168 ms (0.06%)
ddtrace.trace 0.168 ms (0.06%)
ddtrace._trace.tracer 0.105 ms (0.04%)
ddtrace.internal.schema.processor 0.105 ms (0.04%)
ddtrace._trace.filters 0.063 ms (0.02%)
ddtrace._trace.processor 0.063 ms (0.02%)
ddtrace._trace.sampler 0.026 ms (0.01%)
ddtrace.bootstrap.sitecustomize 0.071 ms (0.03%)
ddtrace.bootstrap.preload 0.071 ms (0.03%)
ddtrace.internal.products 0.071 ms (0.03%)
importlib.metadata 0.071 ms (0.03%)
importlib.metadata._collections 0.071 ms (0.03%)

The following import paths have shrunk:

ddtrace.auto 2.246 ms (0.81%)
ddtrace.bootstrap.sitecustomize 1.420 ms (0.51%)
ddtrace.bootstrap.preload 1.420 ms (0.51%)
ddtrace.internal.remoteconfig.client 0.649 ms (0.23%)
ddtrace.internal.products 0.066 ms (0.02%)
importlib.metadata 0.066 ms (0.02%)
importlib.metadata._itertools 0.066 ms (0.02%)
ddtrace 0.826 ms (0.30%)
ddtrace.trace 0.146 ms (0.05%)
ddtrace._trace.tracer 0.146 ms (0.05%)
ddtrace.settings.peer_service 0.112 ms (0.04%)
ddtrace.internal._unpatched 0.031 ms (0.01%)
json 0.031 ms (0.01%)
json.decoder 0.031 ms (0.01%)
re 0.031 ms (0.01%)
enum 0.031 ms (0.01%)
types 0.031 ms (0.01%)

@pr-commenter
Copy link

pr-commenter bot commented Jun 24, 2025

Benchmarks

Benchmark execution time: 2025-06-26 19:20:20

Comparing candidate commit 2acd3ee in PR branch backport-13560-to-3.9 with baseline commit e32842b in branch 3.9.

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

scenario:iastaspectsospath-ospathbasename_aspect

  • 🟥 execution_time [+638.376ns; +844.326ns] or [+15.522%; +20.529%]

scenario:iastaspectsospath-ospathnormcase_aspect

  • 🟥 execution_time [+460.946ns; +509.351ns] or [+13.458%; +14.872%]

scenario:iastaspectsospath-ospathsplitdrive_aspect

  • 🟥 execution_time [+447.101ns; +524.650ns] or [+12.068%; +14.161%]

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

  • 🟥 execution_time [+278.765ns; +353.961ns] or [+9.305%; +11.815%]

@mabdinur mabdinur enabled auto-merge (squash) June 26, 2025 19:04
@mabdinur mabdinur merged commit 84c2cca into 3.9 Jun 27, 2025
740 of 741 checks passed
@mabdinur mabdinur deleted the backport-13560-to-3.9 branch June 27, 2025 04:09
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.

7 participants