Skip to content

Commit

Permalink
chore(tracing): adds 128 bit trace id support for b3 and w3c trace he…
Browse files Browse the repository at this point in the history
…aders (#5434)

Currently the trace id component of b3 and w3c headers are truncated
from 128bits to 64bits. This was done to make b3 and w3c headers
compatible with datadog tracers. Moving forward this is not an
acceptable solution.

When ``DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED=True`` the tracer
should NOT truncate the trace_id in b3 and w3c headers. The full trace
id should be used.


## Checklist

- [x] Change(s) are motivated and described in the PR description.
- [x] Testing strategy is described if automated tests are not included
in the PR.
- [x] Risk is outlined (performance impact, potential for breakage,
maintainability, etc).
- [x] Change is maintainable (easy to change, telemetry, documentation).
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/contributing.html#Release-Note-Guidelines)
are followed.
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/)).
- [x] Author is aware of the performance implications of this PR as
reported in the benchmarks PR comment.

## Reviewer Checklist

- [ ] Title is accurate.
- [ ] No unnecessary changes are introduced.
- [ ] Description motivates each change.
- [ ] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes unless absolutely necessary.
- [ ] Testing strategy adequately addresses listed risk(s).
- [ ] Change is maintainable (easy to change, telemetry, documentation).
- [ ] Release note makes sense to a user of the library.
- [ ] Reviewer is aware of, and discussed the performance implications
of this PR as reported in the benchmarks PR comment.
  • Loading branch information
mabdinur authored Apr 4, 2023
1 parent b6ad2aa commit a1e27bb
Show file tree
Hide file tree
Showing 3 changed files with 274 additions and 116 deletions.
13 changes: 9 additions & 4 deletions ddtrace/propagation/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ def _extract_header_value(possible_header_names, headers, default=None):
def _hex_id_to_dd_id(hex_id):
# type: (str) -> int
"""Helper to convert hex ids into Datadog compatible ints
If the id is > 64 bit then truncate the trailing 64 bit.
If the id is > 64 bit and 128bit trace_id generation is disabled then truncate the trailing 64 bit.
"463ac35c9f6413ad48485a3953bb6124" -> "48485a3953bb6124" -> 5208512171318403364
Otherwise convert the full trace id into lower case hex values.
"""
if config._128_bit_trace_id_enabled:
return int(hex_id, 16)
return int(hex_id[-16:], 16)


Expand All @@ -118,9 +121,11 @@ def _hex_id_to_dd_id(hex_id):

def _dd_id_to_b3_id(dd_id):
# type: (int) -> str
"""Helper to convert Datadog trace/span int ids into hex ids"""
# DEV: `hex(dd_id)` will give us `0xDEADBEEF`
# DEV: this gives us lowercase hex, which is what we want
"""Helper to convert Datadog trace/span int ids into lower case hex values"""
if dd_id > _MAX_UINT_64BITS:
# b3 trace ids can have the length of 16 or 32 characters:
# https://github.com/openzipkin/b3-propagation#traceid
return "{:032x}".format(dd_id)
return "{:016x}".format(dd_id)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
features:
- |
tracing: Adds support for 128 bit trace ids for b3 and w3c distributing tracing headers.
Loading

0 comments on commit a1e27bb

Please sign in to comment.