Skip to content
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

fix: 16-bit hexadecimal formatting for XCTC span IDs #946

Merged
merged 5 commits into from
Oct 15, 2024
Merged

Conversation

gkevinzheng
Copy link
Contributor

Fixes #942

Also updated link to comment for XCTC formatting, as that seems to have been moved prior.

@gkevinzheng gkevinzheng requested review from a team as code owners October 11, 2024 16:44
@product-auto-label product-auto-label bot added the size: m Pull request size is medium. label Oct 11, 2024
@product-auto-label product-auto-label bot added the api: logging Issues related to the googleapis/python-logging API. label Oct 11, 2024
if span_id_int > 0 and span_id_int < 2**64:
span_id = f"{span_id_int:016x}"
else:
span_id = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe raise an exception here, since we already have the try/catch?

try:
    span_id_int = int(span_id)
    if span_id_int < 0 or span_id_int > 2**64:
        raise ValueError("span_id outside valid range")
    span_id = f"{span_id_int:016x}"
except ValueError:
    span_id = None


# Convert the span ID to 16-bit hexadecimal instead of decimal
if span_id is not None:
try:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a lot of layers of nesting here... consider splitting this up into multiple smaller helper functions. For example:

   xctc_trace_id, xctc_span_id, xctc_options = _parse_xct_header(header)
   trace_id = _validate_and_normalize_xctc_trace_id(xctc_trace_id)
   span_id = _validate_and_normalize_xctc_span_id(xctc_span_id)
   sampled = _extract_should_sample_from_xctc_option(xctc_options)

if span_id_int > 0 and span_id_int < 2**64:
span_id = f"{span_id_int:016x}"
else:
span_id = None

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If parts of the header fail to validate, you may want to consider making it more obvious that this has happened. Consider some form of debug logging or other tracking if not propagated an error to the caller.

for input_span in input_spans:
expected_trace = "12345"
input_span = "67890"
expected_span = "10932".zfill(16)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this the expected span when the input span is invalid? Shouldn't the expected span be None?

@gkevinzheng gkevinzheng merged commit 1f2b190 into main Oct 15, 2024
15 of 16 checks passed
@gkevinzheng gkevinzheng deleted the xctc-hex branch October 15, 2024 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: logging Issues related to the googleapis/python-logging API. size: m Pull request size is medium.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Span ID reported when "X-Cloud-Trace-Context" is used is incorrect
4 participants