Skip to content

Commit

Permalink
allow for empty X-Parent (#820)
Browse files Browse the repository at this point in the history
The `X-Parent` header could be empty, and will be for the root span. This PR allows for that.
  • Loading branch information
carldunham authored Jan 3, 2024
1 parent 39a8c2e commit 1d9d09f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
7 changes: 2 additions & 5 deletions baseplate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ def new(cls) -> "TraceInfo":
@classmethod
def from_upstream(
cls,
trace_id: Optional[str],
trace_id: str,
parent_id: Optional[str],
span_id: Optional[str],
span_id: str,
sampled: Optional[bool],
flags: Optional[int],
) -> "TraceInfo":
Expand All @@ -153,9 +153,6 @@ def from_upstream(
if span_id is None:
raise ValueError("invalid span_id")

if parent_id is None:
raise ValueError("invalid parent_id")

if sampled is not None and not isinstance(sampled, bool):
raise ValueError("invalid sampled value")

Expand Down
2 changes: 1 addition & 1 deletion baseplate/frameworks/pyramid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def _get_trace_info(self, headers: Mapping[str, str]) -> TraceInfo:
flags = headers.get("X-Flags", None)
return TraceInfo.from_upstream(
headers["X-Trace"],
headers["X-Parent"],
headers.get("X-Parent", None),
headers["X-Span"],
sampled,
int(flags) if flags is not None else None,
Expand Down
2 changes: 1 addition & 1 deletion baseplate/frameworks/thrift/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def call_processor_with_span_context(
flags = headers.get(b"Flags", None)
trace_info = TraceInfo.from_upstream(
headers[b"Trace"].decode(),
headers[b"Parent"].decode(),
headers.get(b"Parent", b"").decode(),
headers[b"Span"].decode(),
sampled,
int(flags) if flags is not None else None,
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ def test_new_does_not_have_parent_id(self):
new_trace_info = TraceInfo.new()
self.assertIsNone(new_trace_info.parent_id)

def test_new_sets_trace_and_span_ids(self):
new_trace_info = TraceInfo.new()
self.assertIsNotNone(new_trace_info.trace_id)
self.assertIsNotNone(new_trace_info.span_id)

def test_new_does_not_set_flags(self):
new_trace_info = TraceInfo.new()
self.assertIsNone(new_trace_info.flags)
Expand All @@ -341,3 +346,10 @@ def test_from_upstream_handles_no_sampled_or_flags(self):
span = TraceInfo.from_upstream(1, 2, 3, None, None)
self.assertIsNone(span.sampled)
self.assertIsNone(span.flags)

def test_from_upstream_handles_no_parent(self):
"""Verify that `parent_id` can be left out without raising an exception."""
span = TraceInfo.from_upstream(1, None, 3, None, None)
self.assertIsNone(span.parent_id)
self.assertIsNotNone(span.trace_id)
self.assertIsNotNone(span.span_id)

0 comments on commit 1d9d09f

Please sign in to comment.