diff --git a/braintrust/templates/_api-deployment.tpl b/braintrust/templates/_api-deployment.tpl index 62433ff..7dfc281 100644 --- a/braintrust/templates/_api-deployment.tpl +++ b/braintrust/templates/_api-deployment.tpl @@ -157,7 +157,8 @@ spec: ports: - containerPort: {{ $api.service.port }} {{- if dig "autoscaling" "enabled" false $api }} - - containerPort: {{ $api.healthServer.port }} + - name: api-metrics + containerPort: {{ $api.healthServer.port }} {{- end }} resources: {{- toYaml $api.resources | nindent 12 }} diff --git a/braintrust/templates/api-autoscaling-metric.yaml b/braintrust/templates/api-autoscaling-metric.yaml index f04245f..6ce6d99 100644 --- a/braintrust/templates/api-autoscaling-metric.yaml +++ b/braintrust/templates/api-autoscaling-metric.yaml @@ -38,7 +38,8 @@ spec: app: {{ $api.name }} containers: - endpoint: - port: {{ $api.healthServer.port }} + # A named port limits discovery to the API container, excluding init containers. + port: api-metrics path: {{ $api.autoscaling.metricsPath }} metrics: - gauge: diff --git a/braintrust/tests/api-autoscaling_test.yaml b/braintrust/tests/api-autoscaling_test.yaml index cb22f4c..8b7ce62 100644 --- a/braintrust/tests/api-autoscaling_test.yaml +++ b/braintrust/tests/api-autoscaling_test.yaml @@ -73,7 +73,7 @@ tests: value: braintrust-api - equal: path: spec.metrics[0].pod.containers[0].endpoint.port - value: 8001 + value: api-metrics - equal: path: spec.metrics[0].pod.containers[0].endpoint.path value: /metrics @@ -151,7 +151,7 @@ tests: path: spec.behavior.scaleUp.stabilizationWindowSeconds value: 60 - - it: should omit Deployment replicas and expose metrics port when autoscaling is enabled + - it: should expose a named metrics port with the startup gate when autoscaling is enabled template: api-deployment.yaml values: - __fixtures__/base-values.yaml @@ -171,20 +171,25 @@ tests: - contains: path: spec.template.spec.containers[0].ports content: + name: api-metrics containerPort: 8001 + - equal: + path: spec.template.spec.initContainers[0].name + value: wait-for-brainstore - contains: path: spec.template.spec.containers[0].env content: name: ENABLE_PROMETHEUS_METRICS value: "true" - - it: should set ENABLE_PROMETHEUS_METRICS after extraEnvVars so autoscaling wins + - it: should expose metrics without the startup gate and override extraEnvVars template: api-deployment.yaml values: - __fixtures__/base-values.yaml set: cloud: google api.autoscaling.enabled: true + api.brainstoreStartupGate.enabled: false api.extraEnvVars: - name: ENABLE_PROMETHEUS_METRICS value: "false" @@ -199,6 +204,11 @@ tests: - equal: path: spec.template.spec.containers[0].env[-1].value value: "true" + - contains: + path: spec.template.spec.containers[0].ports + content: + name: api-metrics + containerPort: 8001 - it: should keep Deployment replicas when autoscaling is disabled template: api-deployment.yaml @@ -217,6 +227,7 @@ tests: - notContains: path: spec.template.spec.containers[0].ports content: + name: api-metrics containerPort: 8001 - notContains: path: spec.template.spec.containers[0].env @@ -333,6 +344,9 @@ tests: - equal: path: spec.metrics[0].pod.selector.matchLabels.app value: braintrust-api-ingest + - equal: + path: spec.metrics[0].pod.containers[0].endpoint.port + value: api-metrics - it: should omit replicas on the ingest pool when autoscaling is enabled template: api-deployment.yaml @@ -356,6 +370,40 @@ tests: name: ENABLE_PROMETHEUS_METRICS value: "true" + - it: should keep a custom pool metrics port aligned with its scrape endpoint + values: + - __fixtures__/base-values.yaml + set: + cloud: google + api.autoscaling.enabled: true + api.workloadIsolation.enabled: true + api.workloadIsolation.background.name: custom-background + api.workloadIsolation.background.healthServer.port: 9091 + api.workloadIsolation.background.extraContainers: + - name: sidecar + image: example/sidecar:v1 + ports: + - containerPort: 9090 + name: sidecar-metrics + documentSelector: + path: metadata.name + value: custom-background + asserts: + - contains: + path: spec.template.spec.containers[0].ports + content: + name: api-metrics + containerPort: 9091 + template: api-deployment.yaml + - equal: + path: spec.template.spec.initContainers[0].name + value: wait-for-brainstore + template: api-deployment.yaml + - equal: + path: spec.metrics[0].pod.containers[0].endpoint.port + value: api-metrics + template: api-autoscaling-metric.yaml + - it: should not render an HPA for a pool with autoscaling disabled template: api-hpa.yaml values: