Skip to content

Conversation

@mateustd-ciandt
Copy link
Contributor

Adding a plugin in rust that surfaces traceID in custom error pages.
We're checking for both Google Cloud trace header and W3C header.
Also adding a BUILD file and a tests.textpb file.

@mateustd-ciandt mateustd-ciandt requested a review from a team as a code owner August 12, 2025 16:18
@snippet-bot
Copy link

snippet-bot bot commented Aug 12, 2025

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

Copy link
Collaborator

@mpwarres mpwarres left a comment

Choose a reason for hiding this comment

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

LGTM once comments addressed

if let Some(w3c_trace) = self.get_http_request_header("traceparent") {
// Format: version-trace_id-parent_id-flags
let parts: Vec<&str> = w3c_trace.split('-').collect();
if parts.len() >= 4 && parts[1].len() == 32 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should >= be == ? Also consider using a regex to match and extract the field, so that it can verify all chars are hex digits.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adjusted it to be ==, >= was for future-proofing, in case additional elements are added to the format, but == works too! Also, added a regex and the newlines. Thanks!

Copy link
Collaborator

@mpwarres mpwarres left a comment

Choose a reason for hiding this comment

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

LGTM mod comment. Thanks!

// Try W3C Trace Context standard
if let Some(w3c_trace) = self.get_http_request_header("traceparent") {
// Use regex to match the entire traceparent value and extract trace ID
let re = Regex::new(r"^[0-9a-f]{2}-([0-9a-f]{32})-[0-9a-f]{16}-[0-9a-f]{2}$").unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Regex::new is an expensive operation, since it compiles a regular expression for matching (see https://cloud.google.com/service-extensions/docs/plugin-best-practices#precompile). Can you move this operation to be performed once, during RootContext initialization, and then you can reuse the Regex object from HttpContexts? See https://github.com/GoogleCloudPlatform/service-extensions/blob/main/plugins/samples/regex_rewrite/plugin.rs for an example of this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm nice, good to know this. just this some changes hopefully it's fine now. thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants