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

chore(storage): Support a cookie header in calls. #11614

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions storage/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"io"
"net"
"net/url"
"os"
"strings"
"sync"
"time"

"cloud.google.com/go/internal"
Expand All @@ -39,8 +41,16 @@ var defaultRetry *retryConfig = &retryConfig{}
var xGoogDefaultHeader = fmt.Sprintf("gl-go/%s gccl/%s", version.Go(), sinternal.Version)

const (
xGoogHeaderKey = "x-goog-api-client"
idempotencyHeaderKey = "x-goog-gcs-idempotency-token"
xGoogHeaderKey = "x-goog-api-client"
idempotencyHeaderKey = "x-goog-gcs-idempotency-token"
cookieHeaderKey = "cookie"
directpathCookieHeaderKey = "x-directpath-tracing-cookie"
)

var (
cookieHeader = sync.OnceValue(func() string {
return os.Getenv("STORAGE_TRACING_COOKIE")
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's do GOOGLE_SDK_GO_TRACING_COOKIE (matches prefix of some other env vars in the library).

})
)

// run determines whether a retry is necessary based on the config and
Expand Down Expand Up @@ -113,6 +123,12 @@ func setInvocationHeaders(ctx context.Context, invocationID string, attempts int

ctx = callctx.SetHeaders(ctx, xGoogHeaderKey, xGoogHeader)
ctx = callctx.SetHeaders(ctx, idempotencyHeaderKey, invocationID)

if c := cookieHeader(); c != "" {
ctx = callctx.SetHeaders(ctx, cookieHeaderKey, c)
ctx = callctx.SetHeaders(ctx, directpathCookieHeaderKey, c)
}

return ctx
}

Expand Down
28 changes: 28 additions & 0 deletions storage/invoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,31 @@ func TestShouldRetry(t *testing.T) {
})
}
}

func TestInvokeWithCookie(t *testing.T) {
expectedCookie := "C=a_test_cookie"
oldCookieHeader := cookieHeader
cookieHeader = func() string { return expectedCookie }
defer func() {
cookieHeader = oldCookieHeader
}()

ctx := context.Background()
var gotCookie, gotDirectpathCookie string
if err := run(ctx, func(ctx context.Context) error {
headers := callctx.HeadersFromContext(ctx)
gotCookie = headers["cookie"][0]
gotDirectpathCookie = headers["x-directpath-tracing-cookie"][0]
return nil
}, nil, false); err != nil {
t.Errorf("error during run; got %v, want nil", err)
}

if gotCookie != expectedCookie {
t.Errorf("incorrect value for cookie header; got %v, want %v", gotCookie, expectedCookie)
}

if gotDirectpathCookie != expectedCookie {
t.Errorf("incorrect value for x-directpath-tracing-cookie header; got %v, want %v", gotDirectpathCookie, expectedCookie)
}
}
Loading