# Add a ServiceMonitor to a Helm Chart

Yes. An agent adds an optional ServiceMonitor template to your Helm chart, guarded by the Prometheus Operator CRD, with values, a CI values file and checks.

[Run this on Rush](https://cloud.getrush.ai/run/add-service-monitors/helm/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the chart as it renders today. In a repository laid out like helm/examples, `charts/hello-world/Chart.yaml` is an `application` chart at version `0.1.0` with `appVersion: "1.16.0"`, and `values.yaml` sets `replicaCount: 1`, an `nginx` image whose tag defaults to the app version, a `serviceAccount` block and a `ClusterIP` service on port 80. `templates/deployment.yaml` runs one container with a port named `http` on 80 and liveness and readiness probes on `/`; `templates/service.yaml` exposes port `{{ .Values.service.port }}` with `targetPort: http` and the port name `http`; `_helpers.tpl` defines `hello-world.name`, `fullname`, `chart`, `labels` and `selectorLabels`, the last two being `app.kubernetes.io/name` and `app.kubernetes.io/instance`. There are no `ingress`, `hpa` or monitoring templates, and no `ci/` directory. `ct.yaml` points chart-testing at `charts`, `.github/workflows/lint-test.yaml` runs `ct lint` and, when a chart changed, `ct install` on a kind cluster, and `release.yaml` publishes with chart-releaser on every push to `main`.

The template the agent adds, `templates/servicemonitor.yaml`, is wrapped in two conditions: `.Values.serviceMonitor.enabled`, which defaults to `false`, and `.Capabilities.APIVersions.Has "monitoring.coreos.com/v1"`, so a cluster without the Prometheus Operator CRDs renders nothing and `helm install` cannot fail on an unknown kind. The object is `monitoring.coreos.com/v1` `ServiceMonitor`, named with `hello-world.fullname`, carrying the chart's `labels` plus any `serviceMonitor.labels` from values, which is how a Prometheus whose `serviceMonitorSelector` matches a `release` label picks it up. Its `spec.selector.matchLabels` reuses `hello-world.selectorLabels`, the same pair the Service's selector uses, so the monitor finds exactly that Service; `spec.endpoints` has one entry with `port: http`, the name the Service already gives its port, which a ServiceMonitor needs because it scrapes by port name, plus `path`, `interval` and `scrapeTimeout` from values with `/metrics`, `30s` and `10s` as defaults. `namespaceSelector` is left out so the monitor watches its own namespace, and `serviceMonitor.endpoints.extra` lets a reader add a second endpoint without editing the template.

`values.yaml` gains the `serviceMonitor` block with a comment per key, and because the default image is `nginx` with no exporter, the agent says in the pull request what the values are for: a chart adapted to an application that serves Prometheus metrics sets `enabled: true` and the right `path`, and `nginx` as shipped answers `/metrics` with a 404, which is a scrape error rather than an install error. `README.md` gets a table row per new value. A new `ci/servicemonitor-values.yaml` with `serviceMonitor.enabled: true` means `ct install` also installs the chart with the monitor on; on the kind cluster in `lint-test.yaml` the CRD is absent, so the capability guard renders nothing and the install stays green, and the pull request shows how to install the Prometheus Operator CRDs on that cluster to make the test exercise the object.

Checks run in the sandbox with no cluster. `helm lint charts/hello-world` with and without the CI values file; `helm template --api-versions monitoring.coreos.com/v1 --set serviceMonitor.enabled=true`, which makes `Capabilities.APIVersions.Has` true and prints the rendered ServiceMonitor; the same command without `--api-versions` to show that nothing renders; and `kubeconform` with the Prometheus Operator CRD schema against the rendered output, so a misspelt field in `spec.endpoints` fails before any cluster sees it. The agent bumps `Chart.yaml` to `0.2.0`, because `ct lint` checks that a changed chart's version increased and `release.yaml` packages a release per chart version.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add an optional Prometheus Operator ServiceMonitor to the Helm chart in this repository. Read Chart.yaml, values.yaml, every file under templates including _helpers.tpl, README.md, ct.yaml and the workflows under .github/workflows first. Add templates/servicemonitor.yaml rendered only when serviceMonitor.enabled is true and .Capabilities.APIVersions.Has reports monitoring.coreos.com/v1, using the chart's fullname, labels and selectorLabels helpers, selecting the Service by its selector labels, with one endpoint on the Service's named port and path, interval and scrapeTimeout from values, extra labels from values, and a list for extra endpoints. Add the values block with comments, defaults of enabled false, path /metrics, interval 30s and scrapeTimeout 10s, document the values in the README, add ci/servicemonitor-values.yaml enabling it for chart-testing, and bump the chart version. Run helm lint with and without the CI values, helm template with and without --api-versions monitoring.coreos.com/v1 to show both outcomes, and kubeconform with the ServiceMonitor CRD schema. Open a pull request quoting those outputs and noting that the default nginx image serves no metrics.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request adds `charts/hello-world/templates/servicemonitor.yaml` and `charts/hello-world/ci/servicemonitor-values.yaml`, and changes `values.yaml`, `README.md` and `Chart.yaml`. Its description quotes the two `helm lint` runs, the rendered ServiceMonitor from `helm template` with `--api-versions`, the empty result without it, and the `kubeconform` summary against the CRD schema; it also names the `release` label a Prometheus Operator installation usually selects on and notes that the chart's default image answers no metrics.

A reviewer checks the selector against their Service labels, the port name against their application, and whether `path` and `interval` defaults suit the exporter they run; they add `serviceMonitor.labels` matching their Prometheus's `serviceMonitorSelector`. If they want `ct install` to create the object, they add the CRD install step the pull request shows to `lint-test.yaml`.

The Deployment, Service, ServiceAccount and NOTES templates are untouched, and a release that keeps `serviceMonitor.enabled` at `false` renders exactly what it rendered before.

## Run it on every chart without monitoring templates

A platform team that owns many charts can send this body once per chart from a short script on its own server: list the repositories the GitHub App reaches with `GET /o/{org}/github`, keep those whose default branch has a `Chart.yaml` and no `templates/servicemonitor.yaml`, and give each request a `request_id` that is a UUID v5 of the repository's full name so a rerun returns the runs it already started. Each run opens one pull request fitted to that chart's helpers and port names, and the key's `webhook_url` receives `run.completed` for each.

Once merged, an automation on the Project with a `github` trigger on `pull_request.opened` and `pull_request.synchronize` can run read-only with a prompt that renders the chart with `--api-versions monitoring.coreos.com/v1` and comments when a change to `service.yaml` renames the port the monitor scrapes. The trigger adds the event after the prompt; it fills in no placeholder.

## Why run it on Rush?

A ServiceMonitor is twenty lines, and the mistakes are in what surrounds them: a selector that does not match the Service, a port given by number where the Operator wants a name, an object that breaks `helm install` on a cluster without the CRD, a chart-testing run that never exercises the new value, a version that `ct lint` refuses. The agent reads the chart's helpers and port names, guards the template with the capability check, renders both outcomes in its sandbox, and validates the object against the CRD schema, with every command and its output kept in the run's record.

It also says what it could not prove: without a cluster running the Operator, a scrape was not observed, so the pull request shows the rendered object and the schema check and leaves the first scrape to the reviewer's cluster.

## Frequently asked questions

### Will the chart still install where the Prometheus Operator is absent?

Yes. The template checks `.Capabilities.APIVersions.Has "monitoring.coreos.com/v1"` as well as the `enabled` value, so a cluster without the CRD renders nothing even when the value is on. `helm template` reproduces that with and without `--api-versions`, and the pull request quotes both.

### Why select by port name?

A ServiceMonitor's endpoint names the Service port rather than giving a number, which is why the chart's Service already names its port `http`. The template uses that name, and a chart whose Service has unnamed ports needs them named before the monitor can scrape.

### Which labels does Prometheus match on?

That depends on the `serviceMonitorSelector` of the Prometheus resource in your cluster; a common installation selects on a `release` label. `serviceMonitor.labels` in values is merged into the object's labels for exactly that, and the README row says so.

### Does ct install test the monitor?

It installs the chart once more with `ci/servicemonitor-values.yaml`, so the value is exercised, but the kind cluster in `lint-test.yaml` has no CRD and the guard renders nothing. The pull request shows the step that applies the Prometheus Operator CRDs before `ct install` for a team that wants the object created in CI.

### Why did the chart version change?

`ct lint` fails when a changed chart keeps its version, and `release.yaml` packages a release per version, so `Chart.yaml` moves from `0.1.0` to `0.2.0`. `appVersion` stays, because the application image did not change.

## More Helm jobs

- [Add a values.schema.json to a Helm Chart](https://getrush.ai/run/add-values-schema/helm/)
- [Fix "failed to fetch https://kubernetes-charts.storage.googleapis.com/index.yaml : 403 Forbidden" in a Helm Chart's Dependencies](https://getrush.ai/run/fix-kubernetes-charts-storage-googleapis-com-403-forbidden/helm/)
- [Fix Helm Errors from Sentry Alerts](https://getrush.ai/run/fix-sentry-errors/helm/)

## The same job on other stacks

- [Add Prometheus ServiceMonitors to a Kustomize App](https://getrush.ai/run/add-service-monitors/kustomize/)

## More jobs like this: Hand off a long task

- [Add Shell Completion to a Click CLI](https://getrush.ai/run/add-shell-completion/click/)
- [Add Shell Completions to a Rust CLI](https://getrush.ai/run/add-shell-completion/rust-cli/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
