Repository: GoogleCloudPlatform/cloud-sql-proxy-operator
Pull request #223 (2023-02-24 15:41:11)

feat: Configure Google Cloud Telemetry flags on the proxy.
Allow users to configure  the --telemetry-prefix --telemetry-project --telemetry-sample-rate flags 
on the proxy.

Related to #45 


Patch:
diff --git a/config/crd/bases/cloudsql.cloud.google.com_authproxyworkloads.yaml b/config/crd/bases/cloudsql.cloud.google.com_authproxyworkloads.yaml
index 5a25846..23df9d2 100644
--- a/config/crd/bases/cloudsql.cloud.google.com_authproxyworkloads.yaml
+++ b/config/crd/bases/cloudsql.cloud.google.com_authproxyworkloads.yaml
@@ -935,6 +935,15 @@ spec:
                           description: HTTPPort the port for Prometheus and health check server. This sets the proxy container's CLI argument `--http-port`
                           format: int32
                           type: integer
+                        telemetryPrefix:
+                          description: TelemetryPrefix is the prefix for Cloud Monitoring metrics. This sets the proxy container's CLI argument `--telemetry-prefix`
+                          type: string
+                        telemetryProject:
+                          description: TelemetryProject enables Cloud Monitoring and Cloud Trace with the provided project ID. This sets the proxy container's CLI argument `--telemetry-project`
+                          type: string
+                        telemetrySampleRate:
+                          description: TelemetrySampleRate is the Cloud Trace sample rate. A smaller number means more traces. This sets the proxy container's CLI argument `--telemetry-sample-rate`
+                          type: integer
                       type: object
                   type: object
                 instances:
diff --git a/docs/api.md b/docs/api.md
index 771d3ea..4153fce 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -120,6 +120,9 @@ _Appears in:_
 
 | Field | Description |
 | --- | --- |
+| `telemetryProject` _string_ | TelemetryProject enables Cloud Monitoring and Cloud Trace with the provided project ID. This sets the proxy container's CLI argument `--telemetry-project` |
+| `telemetryPrefix` _string_ | TelemetryPrefix is the prefix for Cloud Monitoring metrics. This sets the proxy container's CLI argument `--telemetry-prefix` |
+| `telemetrySampleRate` _integer_ | TelemetrySampleRate is the Cloud Trace sample rate. A smaller number means more traces. This sets the proxy container's CLI argument `--telemetry-sample-rate` |
 | `httpPort` _integer_ | HTTPPort the port for Prometheus and health check server. This sets the proxy container's CLI argument `--http-port` |
 | `disableTraces` _boolean_ | DisableTraces disables Cloud Trace testintegration (used with telemetryProject) This sets the proxy container's CLI argument `--disable-traces` |
 | `disableMetrics` _boolean_ | DisableMetrics disables Cloud Monitoring testintegration (used with telemetryProject) This sets the proxy container's CLI argument `--disable-metrics` |
diff --git a/installer/cloud-sql-proxy-operator.yaml b/installer/cloud-sql-proxy-operator.yaml
index b12d277..fdb5d3e 100644
--- a/installer/cloud-sql-proxy-operator.yaml
+++ b/installer/cloud-sql-proxy-operator.yaml
@@ -953,6 +953,15 @@ spec:
                           description: HTTPPort the port for Prometheus and health check server. This sets the proxy container's CLI argument `--http-port`
                           format: int32
                           type: integer
+                        telemetryPrefix:
+                          description: TelemetryPrefix is the prefix for Cloud Monitoring metrics. This sets the proxy container's CLI argument `--telemetry-prefix`
+                          type: string
+                        telemetryProject:
+                          description: TelemetryProject enables Cloud Monitoring and Cloud Trace with the provided project ID. This sets the proxy container's CLI argument `--telemetry-project`
+                          type: string
+                        telemetrySampleRate:
+                          description: TelemetrySampleRate is the Cloud Trace sample rate. A smaller number means more traces. This sets the proxy container's CLI argument `--telemetry-sample-rate`
+                          type: integer
                       type: object
                   type: object
                 instances:
diff --git a/internal/api/v1alpha1/authproxyworkload_types.go b/internal/api/v1alpha1/authproxyworkload_types.go
index eaeabda..408b342 100644
--- a/internal/api/v1alpha1/authproxyworkload_types.go
+++ b/internal/api/v1alpha1/authproxyworkload_types.go
@@ -220,6 +220,21 @@ type AdminServerSpec struct {
 
 // TelemetrySpec specifies how the proxy container will expose telemetry.
 type TelemetrySpec struct {
+	// TelemetryProject enables Cloud Monitoring and Cloud Trace with the provided project ID.
+	// This sets the proxy container's CLI argument `--telemetry-project`
+	//+kubebuilder:validation:Optional
+	TelemetryProject *string `json:"telemetryProject,omitempty"`
+
+	// TelemetryPrefix is the prefix for Cloud Monitoring metrics.
+	// This sets the proxy container's CLI argument `--telemetry-prefix`
+	//+kubebuilder:validation:Optional
+	TelemetryPrefix *string `json:"telemetryPrefix,omitempty"`
+
+	// TelemetrySampleRate is the Cloud Trace sample rate. A smaller number means more traces.
+	// This sets the proxy container's CLI argument `--telemetry-sample-rate`
+	//+kubebuilder:validation:Optional
+	TelemetrySampleRate *int `json:"telemetrySampleRate,omitempty"`
+
 	// HTTPPort the port for Prometheus and health check server.
 	// This sets the proxy container's CLI argument `--http-port`
 	//+kubebuilder:validation:Optional
diff --git a/internal/api/v1alpha1/zz_generated.deepcopy.go b/internal/api/v1alpha1/zz_generated.deepcopy.go
index 55666d5..6568955 100644
--- a/internal/api/v1alpha1/zz_generated.deepcopy.go
+++ b/internal/api/v1alpha1/zz_generated.deepcopy.go
@@ -247,6 +247,21 @@ func (in *InstanceSpec) DeepCopy() *InstanceSpec {
 // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
 func (in *TelemetrySpec) DeepCopyInto(out *TelemetrySpec) {
 	*out = *in
+	if in.TelemetryProject != nil {
+		in, out := &in.TelemetryProject, &out.TelemetryProject
+		*out = new(string)
+		**out = **in
+	}
+	if in.TelemetryPrefix != nil {
+		in, out := &in.TelemetryPrefix, &out.TelemetryPrefix
+		*out = new(string)
+		**out = **in
+	}
+	if in.TelemetrySampleRate != nil {
+		in, out := &in.TelemetrySampleRate, &out.TelemetrySampleRate
+		*out = new(int)
+		**out = **in
+	}
 	if in.HTTPPort != nil {
 		in, out := &in.HTTPPort, &out.HTTPPort
 		*out = new(int32)
diff --git a/internal/workload/podspec_updates.go b/internal/workload/podspec_updates.go
index 874cff2..e5c5ee4 100644
--- a/internal/workload/podspec_updates.go
+++ b/internal/workload/podspec_updates.go
@@ -647,12 +647,21 @@ func (s *updateState) applyTelemetrySpec(p *cloudsqlapi.AuthProxyWorkload) {
 	}
 	tel := p.Spec.AuthProxyContainer.Telemetry
 
+	if tel.TelemetrySampleRate != nil {
+		s.addProxyContainerEnvVar(p, "CSQL_PROXY_TELEMETRY_SAMPLE_RATE", fmt.Sprintf("%d", *tel.TelemetrySampleRate))
+	}
 	if tel.DisableTraces != nil && *tel.DisableTraces {
 		s.addProxyContainerEnvVar(p, "CSQL_PROXY_DISABLE_TRACES", "true")
 	}
 	if tel.DisableMetrics != nil && *tel.DisableMetrics {
 		s.addProxyContainerEnvVar(p, "CSQL_PROXY_DISABLE_METRICS", "true")
 	}
+	if tel.TelemetryProject != nil {
+		s.addProxyContainerEnvVar(p, "CSQL_PROXY_TELEMETRY_PROJECT", *tel.TelemetryProject)
+	}
+	if tel.TelemetryPrefix != nil {
+		s.addProxyContainerEnvVar(p, "CSQL_PROXY_TELEMETRY_PREFIX", *tel.TelemetryPrefix)
+	}
 
 	return
 }


Test patch:
diff --git a/internal/workload/podspec_updates_test.go b/internal/workload/podspec_updates_test.go
index ecea20b..56d36bc 100644
--- a/internal/workload/podspec_updates_test.go
+++ b/internal/workload/podspec_updates_test.go
@@ -628,9 +628,12 @@ func TestProxyCLIArgs(t *testing.T) {
 				AuthProxyContainer: &v1alpha1.AuthProxyContainerSpec{
 					SQLAdminAPIEndpoint: "https://example.com",
 					Telemetry: &v1alpha1.TelemetrySpec{
-						HTTPPort:       ptr(int32(9092)),
-						DisableTraces:  &wantTrue,
-						DisableMetrics: &wantTrue,
+						HTTPPort:            ptr(int32(9092)),
+						DisableTraces:       &wantTrue,
+						DisableMetrics:      &wantTrue,
+						TelemetryPrefix:     ptr("telprefix"),
+						TelemetryProject:    ptr("telproject"),
+						TelemetrySampleRate: ptr(200),
 					},
 					AdminServer: &v1alpha1.AdminServerSpec{
 						EnableAPIs: []string{"Debug", "QuitQuitQuit"},
@@ -649,6 +652,9 @@ func TestProxyCLIArgs(t *testing.T) {
 			},
 			wantWorkloadEnv: map[string]string{
 				"CSQL_PROXY_SQLADMIN_API_ENDPOINT": "https://example.com",
+				"CSQL_PROXY_TELEMETRY_SAMPLE_RATE": "200",
+				"CSQL_PROXY_TELEMETRY_PROJECT":     "telproject",
+				"CSQL_PROXY_TELEMETRY_PREFIX":      "telprefix",
 				"CSQL_PROXY_HTTP_PORT":             "9092",
 				"CSQL_PROXY_ADMIN_PORT":            "9091",
 				"CSQL_PROXY_DEBUG":                 "true",