Repository: GoogleCloudPlatform/cloud-sql-proxy-operator
Pull request #224 (2023-02-24 15:42:13)

feat: Configure prometheus flags on the proxy.
Enable user to configure --prometheus and --prometheus-namespace 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 23df9d2..df30678 100644
--- a/config/crd/bases/cloudsql.cloud.google.com_authproxyworkloads.yaml
+++ b/config/crd/bases/cloudsql.cloud.google.com_authproxyworkloads.yaml
@@ -935,6 +935,12 @@ 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
+                        prometheus:
+                          description: Prometheus Enables Prometheus HTTP endpoint /metrics on localhost This sets the proxy container's CLI argument `--prometheus`
+                          type: boolean
+                        prometheusNamespace:
+                          description: PrometheusNamespace is used the provided Prometheus namespace for metrics This sets the proxy container's CLI argument `--prometheus-namespace`
+                          type: string
                         telemetryPrefix:
                           description: TelemetryPrefix is the prefix for Cloud Monitoring metrics. This sets the proxy container's CLI argument `--telemetry-prefix`
                           type: string
diff --git a/docs/api.md b/docs/api.md
index 4153fce..b96f464 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -120,6 +120,8 @@ _Appears in:_
 
 | Field | Description |
 | --- | --- |
+| `prometheus` _boolean_ | Prometheus Enables Prometheus HTTP endpoint /metrics on localhost This sets the proxy container's CLI argument `--prometheus` |
+| `prometheusNamespace` _string_ | PrometheusNamespace is used the provided Prometheus namespace for metrics This sets the proxy container's CLI argument `--prometheus-namespace` |
 | `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` |
diff --git a/installer/cloud-sql-proxy-operator.yaml b/installer/cloud-sql-proxy-operator.yaml
index fdb5d3e..faedd04 100644
--- a/installer/cloud-sql-proxy-operator.yaml
+++ b/installer/cloud-sql-proxy-operator.yaml
@@ -953,6 +953,12 @@ 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
+                        prometheus:
+                          description: Prometheus Enables Prometheus HTTP endpoint /metrics on localhost This sets the proxy container's CLI argument `--prometheus`
+                          type: boolean
+                        prometheusNamespace:
+                          description: PrometheusNamespace is used the provided Prometheus namespace for metrics This sets the proxy container's CLI argument `--prometheus-namespace`
+                          type: string
                         telemetryPrefix:
                           description: TelemetryPrefix is the prefix for Cloud Monitoring metrics. This sets the proxy container's CLI argument `--telemetry-prefix`
                           type: string
diff --git a/internal/api/v1alpha1/authproxyworkload_types.go b/internal/api/v1alpha1/authproxyworkload_types.go
index 408b342..ee56706 100644
--- a/internal/api/v1alpha1/authproxyworkload_types.go
+++ b/internal/api/v1alpha1/authproxyworkload_types.go
@@ -220,6 +220,16 @@ type AdminServerSpec struct {
 
 // TelemetrySpec specifies how the proxy container will expose telemetry.
 type TelemetrySpec struct {
+	// Prometheus Enables Prometheus HTTP endpoint /metrics on localhost
+	// This sets the proxy container's CLI argument `--prometheus`
+	//+kubebuilder:validation:Optional
+	Prometheus *bool `json:"prometheus,omitempty"`
+
+	// PrometheusNamespace is used the provided Prometheus namespace for metrics
+	// This sets the proxy container's CLI argument `--prometheus-namespace`
+	//+kubebuilder:validation:Optional
+	PrometheusNamespace *string `json:"prometheusNamespace,omitempty"`
+
 	// 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
diff --git a/internal/api/v1alpha1/zz_generated.deepcopy.go b/internal/api/v1alpha1/zz_generated.deepcopy.go
index 6568955..b719553 100644
--- a/internal/api/v1alpha1/zz_generated.deepcopy.go
+++ b/internal/api/v1alpha1/zz_generated.deepcopy.go
@@ -247,6 +247,16 @@ 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.Prometheus != nil {
+		in, out := &in.Prometheus, &out.Prometheus
+		*out = new(bool)
+		**out = **in
+	}
+	if in.PrometheusNamespace != nil {
+		in, out := &in.PrometheusNamespace, &out.PrometheusNamespace
+		*out = new(string)
+		**out = **in
+	}
 	if in.TelemetryProject != nil {
 		in, out := &in.TelemetryProject, &out.TelemetryProject
 		*out = new(string)
diff --git a/internal/workload/podspec_updates.go b/internal/workload/podspec_updates.go
index e5c5ee4..978fa8a 100644
--- a/internal/workload/podspec_updates.go
+++ b/internal/workload/podspec_updates.go
@@ -656,6 +656,12 @@ func (s *updateState) applyTelemetrySpec(p *cloudsqlapi.AuthProxyWorkload) {
 	if tel.DisableMetrics != nil && *tel.DisableMetrics {
 		s.addProxyContainerEnvVar(p, "CSQL_PROXY_DISABLE_METRICS", "true")
 	}
+	if tel.PrometheusNamespace != nil || (tel.Prometheus != nil && *tel.Prometheus) {
+		s.addProxyContainerEnvVar(p, "CSQL_PROXY_PROMETHEUS", "true")
+	}
+	if tel.PrometheusNamespace != nil {
+		s.addProxyContainerEnvVar(p, "CSQL_PROXY_PROMETHEUS_NAMESPACE", *tel.PrometheusNamespace)
+	}
 	if tel.TelemetryProject != nil {
 		s.addProxyContainerEnvVar(p, "CSQL_PROXY_TELEMETRY_PROJECT", *tel.TelemetryProject)
 	}


Test patch:
diff --git a/internal/workload/podspec_updates_test.go b/internal/workload/podspec_updates_test.go
index 56d36bc..c905809 100644
--- a/internal/workload/podspec_updates_test.go
+++ b/internal/workload/podspec_updates_test.go
@@ -628,12 +628,14 @@ func TestProxyCLIArgs(t *testing.T) {
 				AuthProxyContainer: &v1alpha1.AuthProxyContainerSpec{
 					SQLAdminAPIEndpoint: "https://example.com",
 					Telemetry: &v1alpha1.TelemetrySpec{
-						HTTPPort:            ptr(int32(9092)),
-						DisableTraces:       &wantTrue,
-						DisableMetrics:      &wantTrue,
 						TelemetryPrefix:     ptr("telprefix"),
 						TelemetryProject:    ptr("telproject"),
 						TelemetrySampleRate: ptr(200),
+						HTTPPort:            ptr(int32(9092)),
+						DisableTraces:       &wantTrue,
+						DisableMetrics:      &wantTrue,
+						Prometheus:          &wantTrue,
+						PrometheusNamespace: ptr("hello"),
 					},
 					AdminServer: &v1alpha1.AdminServerSpec{
 						EnableAPIs: []string{"Debug", "QuitQuitQuit"},
@@ -653,6 +655,7 @@ 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_PROMETHEUS_NAMESPACE":  "hello",
 				"CSQL_PROXY_TELEMETRY_PROJECT":     "telproject",
 				"CSQL_PROXY_TELEMETRY_PREFIX":      "telprefix",
 				"CSQL_PROXY_HTTP_PORT":             "9092",
@@ -662,6 +665,7 @@ func TestProxyCLIArgs(t *testing.T) {
 				"CSQL_PROXY_HEALTH_CHECK":          "true",
 				"CSQL_PROXY_DISABLE_TRACES":        "true",
 				"CSQL_PROXY_DISABLE_METRICS":       "true",
+				"CSQL_PROXY_PROMETHEUS":            "true",
 				"CSQL_PROXY_MAX_CONNECTIONS":       "10",
 				"CSQL_PROXY_MAX_SIGTERM_DELAY":     "20",
 			},