Skip to content

Conversation

@shubha-rajan
Copy link
Contributor

@shubha-rajan shubha-rajan commented Nov 11, 2022

Add proxy operator version to user agent string if CSQL_PROXY_RUNTIME environment variable is set

Fixes GoogleCloudPlatform/cloud-sql-proxy-operator#67

@shubha-rajan shubha-rajan marked this pull request as ready for review November 11, 2022 21:03
@shubha-rajan shubha-rajan requested a review from a team November 11, 2022 21:03
Copy link
Collaborator

@hessjcg hessjcg left a 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 {
Copy link
Collaborator

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 {
Copy link
Member

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
Copy link
Member

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")
Copy link
Member

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")
Copy link
Member

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"
Copy link
Member

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()
Copy link
Member

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.

@enocom
Copy link
Member

enocom commented Nov 14, 2022

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.

@shubha-rajan shubha-rajan requested a review from enocom November 16, 2022 19:07
cmd/root.go Outdated
if !ok {
return ua
}
rv, ok := os.LookupEnv("CLOUD_SQL_PROXY_RUNTIME_VERSION")
Copy link
Member

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) {
Copy link
Member

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.

@shubha-rajan shubha-rajan changed the title fix: add k8s operator version to user agent if present fix: add runtime version to user agent if present Nov 16, 2022
@enocom enocom self-requested a review November 16, 2022 21:38
cmd/root.go Outdated

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.

@shubha-rajan shubha-rajan force-pushed the operator-version-user-agent branch from e483e4f to 52a15ec Compare November 17, 2022 01:10
@shubha-rajan shubha-rajan force-pushed the operator-version-user-agent branch from 52a15ec to 72b62eb Compare November 17, 2022 01:11
cmd/root.go Outdated

// Global-only flags
pflags.StringVar(&c.runtime, "runtime", "",
"Runtime and version, e.g. cloud-sql-proxy-operator/0.0.1")
Copy link
Member

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)".

@shubha-rajan shubha-rajan requested a review from enocom November 17, 2022 01:33
@shubha-rajan shubha-rajan merged commit a6b689b into main Nov 17, 2022
@shubha-rajan shubha-rajan deleted the operator-version-user-agent branch November 17, 2022 22:15
enocom added a commit to GoogleCloudPlatform/alloydb-auth-proxy that referenced this pull request Jan 15, 2023
enocom added a commit to GoogleCloudPlatform/alloydb-auth-proxy that referenced this pull request Jan 18, 2023
enocom added a commit to GoogleCloudPlatform/alloydb-auth-proxy that referenced this pull request Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add cloudsql-proxy user agent env variable for tracking

3 participants