You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(tracing): activate distributed headers once per request (#12250)
Currently core will call _start_span for each django, flask, and certain
other integrations span's generated. Basically an integration using
`core.context_with_data` to generate spans. Each `_start_span` call
calls `activate_distribute_headers`, which in turn call
`HTTP_PROPAGATOR.extract`. This leads to around 7-10 unneeded calls on
average of that method per request. The logic in
[activate_distributed_headers](https://github.com/DataDog/dd-trace-py/blob/main/ddtrace/contrib/internal/trace_utils.py#L586-L594)
is what's currently saving us from not re-activating the same context
over and over again when each new span is generated.
By adding an `activate_distributed_headers` param we can make sure to
only activate the headers when necessary, reducing the calling of code
that runs for no reason and improving performance.
Something I learned while making this PR is that when
`core.context_with_data()` is called, if we do `ctx.get_item` in say `
_start_span` and the item doesn't exist on the local context we look at
the parent context, essentially going all the way up the tree of
contexts till we either find a value or return `None`. With that being
the case, we needed to use
`core.get_local_item("activate_distributed_headers")` instead of the
usual `ctx.get_item`, since the parent context will almost always have
`activate_distribute_headers` set to `True`.
## 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)
0 commit comments