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: add runtime version to user agent if present #1542

Merged
merged 13 commits into from
Nov 17, 2022
12 changes: 11 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,17 @@ var (

func init() {
versionString = strings.TrimSpace(versionString)
userAgent = "cloud-sql-proxy/" + versionString
userAgent = userAgentString()
}

func userAgentString() string {
ua := "cloud-sql-proxy/" + versionString
runtime, ok := os.LookupEnv("CLOUD_SQL_PROXY_RUNTIME")
Copy link
Member

Choose a reason for hiding this comment

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

@kurtisvg pointed out that some runtimes (e.g., Cloud run where they'll use this programatically) might not be able to set an environment variable.

Let's make this a CLI flag proper (which will also support an environment variable).

I'm in favor of a single flag rather than a repeatable flag.

if !ok {
return ua
}

return fmt.Sprintf("%v %v", ua, runtime)
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down
13 changes: 13 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -81,6 +82,18 @@ func invokeProxyCommand(args []string) (*Command, error) {
return c, err
}

func TestUserAgentWithOperatorVersion(t *testing.T) {
os.Setenv("CLOUD_SQL_PROXY_RUNTIME", "cloud-sql-proxy-operator/0.0.1")
defer os.Unsetenv("CLOUD_SQL_PROXY_RUNTIME")

want := "cloud-sql-proxy-operator/0.0.1"
got := userAgentString()
if !strings.Contains(got, want) {
enocom marked this conversation as resolved.
Show resolved Hide resolved
t.Errorf("expected userAgent to contain: %v; got: %v", want, got)
}

}

func TestNewCommandArguments(t *testing.T) {
tcs := []struct {
desc string
Expand Down