Skip to content

feat: Add a go plugin to include a custom error page with the traceid#282

Open
mateustd-ciandt wants to merge 7 commits intoGoogleCloudPlatform:mainfrom
mateustd-ciandt:go-plugin-error-traceid
Open

feat: Add a go plugin to include a custom error page with the traceid#282
mateustd-ciandt wants to merge 7 commits intoGoogleCloudPlatform:mainfrom
mateustd-ciandt:go-plugin-error-traceid

Conversation

@mateustd-ciandt
Copy link
Contributor

Adding a plugin in GO 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.

Using the one in C++ as the model.

@mateustd-ciandt mateustd-ciandt marked this pull request as ready for review October 21, 2025 14:40
@mateustd-ciandt mateustd-ciandt requested a review from a team as a code owner October 21, 2025 14:40
@snippet-bot
Copy link

snippet-bot bot commented Oct 21, 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


func getErrorTemplate(statusCode, traceID string) []byte {
templateOnce.Do(func() {
errorTemplateBytes = []byte(errorTemplate)
Copy link
Collaborator

Choose a reason for hiding this comment

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

is errorTemplateBytes needed? Since []byte(errorTemplate) already performs a copy of errorTemplate, how about just doing:

result = []byte(errorTemplate)
result = bytes.ReplaceAll(result, ...)
result = bytes.ReplaceAll(result, ...)
return result

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done! ty!

}

// Check for error status codes (4xx or 5xx)
if status[0] != '4' && status[0] != '5' {
Copy link
Collaborator

Choose a reason for hiding this comment

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

These numeric checks expressed as character checks doesn't feel very clean to me. This method already parses status to an int later on, suggest moving that conversion earlier and performing the error status code checks on the int value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

true, made it cleaner.

{"Content-Type", "text/html; charset=utf-8"},
}

statusCode, err := strconv.ParseUint(status, 10, 32)
Copy link
Collaborator

Choose a reason for hiding this comment

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

shorten to strconv.Atoi?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done! ty!

func extractTraceID() string {
// Try Google Cloud trace header first
if traceHeader, err := proxywasm.GetHttpRequestHeader("x-cloud-trace-context"); err == nil {
for i := 0; i < len(traceHeader); i++ {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Include comment that states the expected format of the trace ID, like the C++ example

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

// Try Google Cloud trace header first
if traceHeader, err := proxywasm.GetHttpRequestHeader("x-cloud-trace-context"); err == nil {
for i := 0; i < len(traceHeader); i++ {
if traceHeader[i] == '/' {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you use traceHeader.Index("/") or strings.Cut(traceHeader, "/") to do this, rather than hand-coding a for loop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

using Cut now


// Try W3C Trace Context
if w3cTrace, err := proxywasm.GetHttpRequestHeader("traceparent"); err == nil {
if len(w3cTrace) >= 55 && w3cTrace[2] == '-' {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use a regex to do this, similar to the C++ example

Copy link
Contributor Author

Choose a reason for hiding this comment

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

using pre-compiled regex matching C++ implementation

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