-
Notifications
You must be signed in to change notification settings - Fork 349
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
Changes from 6 commits
b61b903
7b516ec
fa17d88
6f6605f
88be7aa
cdc1c4d
c7706e9
e01e62c
c01511e
4a0a707
c3da678
72b62eb
16e6fc1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,20 @@ 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") | ||
if !ok { | ||
return ua | ||
} | ||
rv, ok := os.LookupEnv("CLOUD_SQL_PROXY_RUNTIME_VERSION") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we just use |
||
if !ok { | ||
return ua | ||
} | ||
return fmt.Sprintf("%v %v/%v", ua, runtime, rv) | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ import ( | |
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
@@ -81,6 +82,20 @@ func invokeProxyCommand(args []string) (*Command, error) { | |
return c, err | ||
} | ||
|
||
func Test_UserAgentWithOperatorVersion(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We've been omitting underscores in test names. Let's remove the underscore here. |
||
os.Setenv("CLOUD_SQL_PROXY_RUNTIME", "cloud-sql-proxy-operator") | ||
defer os.Unsetenv("CLOUD_SQL_PROXY_RUNTIME") | ||
os.Setenv("CLOUD_SQL_PROXY_RUNTIME_VERSION", "0.0.1") | ||
defer os.Unsetenv("CLOUD_SQL_PROXY_OPERATOR_VERSION") | ||
|
||
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 | ||
|
There was a problem hiding this comment.
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.