Repository navigation
Preserve and re-propagate the B3 debug flag #5870
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
ocelotl
wants to merge
17
commits into
open-telemetry:main
Choose a base branch
from
ocelotl:b3-propagator-preserve-debug-flag
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+329
−14
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7bda01a
Store the received B3 debug flag in the extracted context
ocelotl c5b0343
Re-propagate the B3 debug flag on inject
ocelotl 6d221ba
Add changelog entry for the B3 debug flag fix
ocelotl cb95d81
Make the extracted debug flag depend on the carrier alone
ocelotl d2ff3dd
Cover the attached context and the flags against sampled case
ocelotl ceaefa6
Keep asserting that x-b3-flags turns on the sampled trace flag
ocelotl ab11a1b
Keep a debug flag that arrives with no trace or span ids
ocelotl 13b67dd
Pin the header written for a debug flag received with no ids
ocelotl 69ed1a8
Rename the changelog fragment to the pull request number
ocelotl 9c823b9
Pin that the debug check compares against an exact value
ocelotl 4866385
Drop the unread debug initialiser in extract
ocelotl 681f73e
Name the changelog fragment after the pull request
ocelotl ed1364e
Merge branch 'main' into b3-propagator-preserve-debug-flag
herin049 8710f4d
Read an empty b3 identifier as present rather than absent
ocelotl 40400c1
Clear a stale debug flag for a sampling state sent alone
ocelotl dfc8e6d
Omit x-b3-sampled when the debug flag is injected
ocelotl 5b2e290
Merge remote-tracking branch 'upstream/main' into b3-propagator-prese…
ocelotl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| `opentelemetry-propagator-b3`: preserve and re-propagate a received debug flag |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ | |
| from typing_extensions import deprecated | ||
|
|
||
| from opentelemetry import trace | ||
| from opentelemetry.context import Context | ||
| from opentelemetry.context import Context, create_key, get_value, set_value | ||
| from opentelemetry.propagators.textmap import ( | ||
| CarrierT, | ||
| Getter, | ||
|
|
@@ -18,6 +18,8 @@ | |
| ) | ||
| from opentelemetry.trace import format_span_id, format_trace_id | ||
|
|
||
| _DEBUG_FLAG_KEY = create_key("b3-debug-flag") | ||
|
|
||
|
|
||
| class B3MultiFormat(TextMapPropagator): | ||
| """Propagator for the B3 HTTP multi-header format. | ||
|
|
@@ -32,6 +34,8 @@ class B3MultiFormat(TextMapPropagator): | |
| SAMPLED_KEY = "x-b3-sampled" | ||
| FLAGS_KEY = "x-b3-flags" | ||
| _SAMPLE_PROPAGATE_VALUES = {"1", "True", "true", "d"} | ||
| _DEBUG_SAMPLED_VALUE = "d" | ||
| _DEBUG_FLAGS_VALUE = "1" | ||
| _trace_id_regex = re_compile(r"[\da-fA-F]{16}|[\da-fA-F]{32}") | ||
| _span_id_regex = re_compile(r"[\da-fA-F]{16}") | ||
|
|
||
|
|
@@ -64,18 +68,30 @@ def extract( | |
| trace_id, span_id, sampled = fields | ||
| elif len(fields) == 4: | ||
| trace_id, span_id, sampled, _ = fields | ||
|
|
||
| debug = sampled == self._DEBUG_SAMPLED_VALUE | ||
| sampling_state_alone = len(fields) == 1 | ||
| else: | ||
| trace_id = _extract_first_element(getter.get(carrier, self.TRACE_ID_KEY)) or trace_id | ||
| span_id = _extract_first_element(getter.get(carrier, self.SPAN_ID_KEY)) or span_id | ||
| sampled = _extract_first_element(getter.get(carrier, self.SAMPLED_KEY)) or sampled | ||
| flags = _extract_first_element(getter.get(carrier, self.FLAGS_KEY)) or flags | ||
| trace_id = _extract_first_element(getter.get(carrier, self.TRACE_ID_KEY)) | ||
| span_id = _extract_first_element(getter.get(carrier, self.SPAN_ID_KEY)) | ||
| sampled = _extract_first_element(getter.get(carrier, self.SAMPLED_KEY)) | ||
| flags = _extract_first_element(getter.get(carrier, self.FLAGS_KEY)) | ||
|
|
||
| debug = flags == self._DEBUG_FLAGS_VALUE | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case, we should ensure we're not setting the debug key unless the flags equal
|
||
| sampling_state_alone = trace_id is None and span_id is None and bool(sampled or flags) | ||
|
|
||
| trace_id = trace.INVALID_TRACE_ID if trace_id is None else trace_id | ||
| span_id = trace.INVALID_SPAN_ID if span_id is None else span_id | ||
| sampled = "0" if sampled is None else sampled | ||
|
|
||
| if ( | ||
| trace_id == trace.INVALID_TRACE_ID | ||
| or span_id == trace.INVALID_SPAN_ID | ||
| or self._trace_id_regex.fullmatch(trace_id) is None | ||
| or self._span_id_regex.fullmatch(span_id) is None | ||
| ): | ||
| if sampling_state_alone: | ||
| context = set_value(_DEBUG_FLAG_KEY, debug, context) | ||
| return context | ||
|
|
||
| trace_id = int(trace_id, 16) | ||
|
|
@@ -88,6 +104,8 @@ def extract( | |
| if sampled in self._SAMPLE_PROPAGATE_VALUES or flags == "1": | ||
| options |= trace.TraceFlags.SAMPLED | ||
|
|
||
| context = set_value(_DEBUG_FLAG_KEY, debug, context) | ||
|
|
||
| return trace.set_span_in_context( | ||
| trace.NonRecordingSpan( | ||
| trace.SpanContext( | ||
|
|
@@ -121,14 +139,18 @@ def inject( | |
| format_trace_id(span_context.trace_id), | ||
| ) | ||
| setter.set(carrier, self.SPAN_ID_KEY, format_span_id(span_context.span_id)) | ||
| setter.set(carrier, self.SAMPLED_KEY, "1" if sampled else "0") | ||
| if get_value(_DEBUG_FLAG_KEY, context) is True: | ||
| setter.set(carrier, self.FLAGS_KEY, self._DEBUG_FLAGS_VALUE) | ||
| else: | ||
| setter.set(carrier, self.SAMPLED_KEY, "1" if sampled else "0") | ||
|
|
||
| @property | ||
| def fields(self) -> set[str]: | ||
| return { | ||
| self.TRACE_ID_KEY, | ||
| self.SPAN_ID_KEY, | ||
| self.SAMPLED_KEY, | ||
| self.FLAGS_KEY, | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -153,10 +175,15 @@ def inject( | |
|
|
||
| sampled = (trace.TraceFlags.SAMPLED & span_context.trace_flags) != 0 | ||
|
|
||
| if get_value(_DEBUG_FLAG_KEY, context) is True: | ||
| sampling_state = self._DEBUG_SAMPLED_VALUE | ||
| else: | ||
| sampling_state = "1" if sampled else "0" | ||
|
|
||
| fields = [ | ||
| format_trace_id(span_context.trace_id), | ||
| format_span_id(span_context.span_id), | ||
| "1" if sampled else "0", | ||
| sampling_state, | ||
| ] | ||
|
|
||
| setter.set(carrier, self.SINGLE_HEADER_KEY, "-".join(fields)) | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should validate that
sampledis valid here, otherwise an invalidsampledflag could result in the debug key getting set toFalsewhen it shouldn't.