Skip to content

Commit

Permalink
Use traceparent instead of X-Cloud-Trace-Context (#16)
Browse files Browse the repository at this point in the history
According to
https://cloud.google.com/trace/docs/trace-context#http-requests,
`X-Cloud-Trace-Context` is a legacy header that precedes the W3C
standard `traceparent` header. Google Cloud services that support trace
context propagation typically support both the `traceparent` and the
legacy `X-Cloud-Trace-Context` header.

Switching to use this so that we don't have to use the legacy header
`X-Cloud-Trace-Context` when restoring trace context header for pubsub
eventing, but only using `traceparent` every where.
  • Loading branch information
tcnghia authored Jun 19, 2024
1 parent f300b26 commit 6962f14
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
16 changes: 12 additions & 4 deletions gcp/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ func WithCloudTraceContext(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if projectID != "" {
var trace string
traceHeader := r.Header.Get("X-Cloud-Trace-Context")
traceParts := strings.Split(traceHeader, "/")
if len(traceParts) > 0 && len(traceParts[0]) > 0 {
trace = fmt.Sprintf("projects/%s/traces/%s", projectID, traceParts[0])
traceHeader := r.Header.Get("traceparent")
traceID := parseTraceFromW3CHeader(traceHeader)
if traceID != "" {
trace = fmt.Sprintf("projects/%s/traces/%s", projectID, traceID)
}
r = r.WithContext(context.WithValue(r.Context(), "trace", trace))
}
Expand All @@ -114,3 +114,11 @@ func traceFromContext(ctx context.Context) string {
}
return trace.(string)
}

func parseTraceFromW3CHeader(traceparent string) string {
traceParts := strings.Split(traceparent, "-")
if len(traceParts) > 1 {
return traceParts[1]
}
return ""
}
25 changes: 13 additions & 12 deletions gcp/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"log/slog"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/chainguard-dev/clog"
Expand All @@ -22,10 +21,10 @@ func TestTrace(t *testing.T) {
for _, c := range []struct {
name string
env string
wantTrace bool
wantTrace string
}{
{"no env set", "", false},
{"env set", "my-project", true},
{"no env set", "", ""},
{"env set", "my-project", "projects/my-project/traces/traceid"},
} {
t.Run(c.name, func(t *testing.T) {
t.Setenv("GOOGLE_CLOUD_PROJECT", c.env)
Expand All @@ -39,16 +38,18 @@ func TestTrace(t *testing.T) {
// TODO: This doesn't propagate the trace context to the logger.
//clog.FromContext(ctx).Info("hello world")

if r.Header.Get("X-Cloud-Trace-Context") == "" {
if r.Header.Get("traceparent") == "" {
t.Error("got empty trace context header, want non-empty")
}

if found := ctx.Value("trace") != nil; found != c.wantTrace {
t.Fatalf("got trace context %t, want %t", found, c.wantTrace)
if c.wantTrace {
if trace := ctx.Value("trace"); !strings.Contains(trace.(string), "/"+c.env+"/") {
t.Errorf("got trace context %q, want %q", trace, c.env)
}
traceCtx := ctx.Value("trace")
if traceCtx == nil {
if c.wantTrace != "" {
t.Fatalf("want %s, not found", c.wantTrace)
}
} else {
if traceCtx != c.wantTrace {
t.Fatalf("got %s, want %s", traceCtx, c.wantTrace)
}
}
}))
Expand All @@ -60,7 +61,7 @@ func TestTrace(t *testing.T) {
if err != nil {
t.Fatal(err)
}
req.Header.Set("X-Cloud-Trace-Context", "trace/id/yay")
req.Header.Set("traceparent", "00-traceid-spanid-01")
if _, err := http.DefaultClient.Do(req); err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 6962f14

Please sign in to comment.