-
Notifications
You must be signed in to change notification settings - Fork 352
fix: add runtime version to user agent if present #1542
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
Conversation
hessjcg
left a comment
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.
LGTM
cmd/root.go
Outdated
| func getUserAgentString() string { | ||
| userAgentString := "cloud-sql-proxy/" + versionString | ||
| operatorVersion, isSet := os.LookupEnv("CLOUD_SQL_PROXY_OPERATOR_VERSION") | ||
| if isSet { |
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.
if !isSet return?
cmd/root.go
Outdated
| userAgent = getUserAgentString() | ||
| } | ||
|
|
||
| func getUserAgentString() 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.
Go prefers to leave off get for Getters. https://go.dev/doc/effective_go#Getters
cmd/root.go
Outdated
| } | ||
|
|
||
| func getUserAgentString() string { | ||
| userAgentString := "cloud-sql-proxy/" + versionString |
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.
Since we are inside a function about user agent, this could juse be ua, or u, or something similarly short.
cmd/root.go
Outdated
|
|
||
| func getUserAgentString() string { | ||
| userAgentString := "cloud-sql-proxy/" + versionString | ||
| operatorVersion, isSet := os.LookupEnv("CLOUD_SQL_PROXY_OPERATOR_VERSION") |
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.
Same here -- a short variable is ok since it's clear what it is. Also, isSet is typically named ok.
cmd/root_test.go
Outdated
|
|
||
| func Test_UserAgentWithOperatorVersion(t *testing.T) { | ||
| os.Setenv("CLOUD_SQL_PROXY_OPERATOR_VERSION", "0.0.1") | ||
| defer os.Unsetenv("CLOUD_SQL_PROXY_OPERATOR_VERSION") |
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.
This needs to be run through go-fmt.
cmd/root_test.go
Outdated
| os.Setenv("CLOUD_SQL_PROXY_OPERATOR_VERSION", "0.0.1") | ||
| defer os.Unsetenv("CLOUD_SQL_PROXY_OPERATOR_VERSION") | ||
|
|
||
| expected := "cloud-sql-proxy-operator/0.0.1" |
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.
want is conventional here
cmd/root_test.go
Outdated
| defer os.Unsetenv("CLOUD_SQL_PROXY_OPERATOR_VERSION") | ||
|
|
||
| expected := "cloud-sql-proxy-operator/0.0.1" | ||
| userAgentString := getUserAgentString() |
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.
got is a common name for variables here.
|
We should make this generic such that other runtimes can use it too. See https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/blob/acb57cb9ce803062651a4b4764e9181237d84a23/core/src/main/java/com/google/cloud/sql/core/CoreSocketFactory.java#L439-L454 where we already do this in the Java connector. |
cmd/root.go
Outdated
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we just use CLOUD_SQL_PROXY_RUNTIME and let the runtime include version?
cmd/root_test.go
Outdated
| return c, err | ||
| } | ||
|
|
||
| func Test_UserAgentWithOperatorVersion(t *testing.T) { |
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.
We've been omitting underscores in test names. Let's remove the underscore here.
cmd/root.go
Outdated
|
|
||
| func userAgentString() string { | ||
| ua := "cloud-sql-proxy/" + versionString | ||
| runtime, ok := os.LookupEnv("CLOUD_SQL_PROXY_RUNTIME") |
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.
e483e4f to
52a15ec
Compare
52a15ec to
72b62eb
Compare
cmd/root.go
Outdated
|
|
||
| // Global-only flags | ||
| pflags.StringVar(&c.runtime, "runtime", "", | ||
| "Runtime and version, e.g. cloud-sql-proxy-operator/0.0.1") |
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.
Let's add something like "(for internal use only)".
Add proxy operator version to user agent string if
CSQL_PROXY_RUNTIMEenvironment variable is setFixes GoogleCloudPlatform/cloud-sql-proxy-operator#67