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(logging): Fixed input validation for X-Cloud-Trace-Context; encoded spanID from XCTC header into hex string. #10979

Merged
merged 6 commits into from
Oct 15, 2024

Conversation

gkevinzheng
Copy link
Contributor

@gkevinzheng gkevinzheng commented Oct 9, 2024

Changes:

  1. Link to format for X-Cloud-Trace-Context has moved, so that comment has been updated.
  2. Encoded spanID in the return value of deconstructXCloudTraceContext into 16-bit hexadecimal string instead of decimal string.

Fixes #10910

@gkevinzheng gkevinzheng requested review from a team as code owners October 9, 2024 19:39
@codyoss codyoss changed the title fix: Fixed input validation for X-Cloud-Trace-Context; encoded spanID from XCTC header into hex string. fix(logging): Fixed input validation for X-Cloud-Trace-Context; encoded spanID from XCTC header into hex string. Oct 9, 2024
@product-auto-label product-auto-label bot added the api: logging Issues related to the Cloud Logging API. label Oct 10, 2024
@gkevinzheng gkevinzheng added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Oct 10, 2024
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Oct 10, 2024
Copy link

@michaelsafyan michaelsafyan left a comment

Choose a reason for hiding this comment

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

Thanks for taking care of this! Per your request over Google Chat, I've left some feedback on this PR. I will defer, however, to your judgement and that of the other reviewer concerning what subset of this feedback to act on. Thanks!

// for example:
// "X-Cloud-Trace-Context: 105445aa7843bc8bf206b120001000/1;o=1"
// "X-Cloud-Trace-Context: 105445aa7843bc8bf206b12000100000/1;o=1"
//
// We expect:

Choose a reason for hiding this comment

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

It might be easier to understand if you phrase the comment as:

  // Parameters:
  //   s - ...
  //
  // Returns:
  //     ...

With respect to "We expect", it is unclear whether this is referring to the input or the output.

If it is intended to describe the input, then shouldn't the documentation for spanID say it is a decimal string?

} else {
spanID = fmt.Sprintf("%016x", intSpanID)
}
}
return

Choose a reason for hiding this comment

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

Optional readability suggestion: consider returning the values explicitly rather than relying on named return values. It was a little confusing seeing this return without the list of variables that are returned.

@@ -903,6 +903,15 @@ func deconstructXCloudTraceContext(s string) (traceID, spanID string, traceSampl
spanID = ""

Choose a reason for hiding this comment

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

You may want to consider similar validation/conversion on the trace ID (to ensure that it isn't all-zero and to ensure that it is zero-padded if smaller than the W3C spec length).

// * traceID (optional): "105445aa7843bc8bf206b120001000"
// * spanID (optional): "1"
// * traceSampled (optional): true
// * traceID (optional, 32-bit hex string): "105445aa7843bc8bf206b12000100000"

Choose a reason for hiding this comment

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

The trace ID is 128-bit, not 32-bit; it is represented as a 16-byte (32-char) array. This comment and the next one may be confusing the number of symbols in the string with the number of bits in the numeric value.

https://www.w3.org/TR/trace-context/#trace-id

TraceSampled: true,
},
}, {
name: "X-Trace-Context with Span ID too large",

Choose a reason for hiding this comment

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

Is there any logging or other debug information that can be output in such a case?

// Convert to 16 byte unsigned hex string
intSpanID, err := strconv.ParseUint(spanID, 10, 64)
if err != nil || intSpanID == 0 {
spanID = ""

Choose a reason for hiding this comment

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

Is it possible to leave some clues when this happens?

@@ -903,6 +903,15 @@ func deconstructXCloudTraceContext(s string) (traceID, spanID string, traceSampl
spanID = ""

Choose a reason for hiding this comment

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

Is it possible to leave some clues when this happens?

@gkevinzheng gkevinzheng enabled auto-merge (squash) October 15, 2024 16:31
@gkevinzheng gkevinzheng merged commit a157558 into googleapis:main Oct 15, 2024
7 checks passed
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 Cloud Logging API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

logging: incorrect span ID logged when using "X-Cloud-Trace-Context"
4 participants