Skip to content

Metrics

Every controller serves a Prometheus metrics endpoint. The generated Deployment, Service, and ServiceMonitor all assume it’s listening in plain HTTP on :8080 — driven by two env vars in the generated controller-default-env ConfigMap:

Env varDefaultFlag
METRICS_BIND_ADDRESS:8080--metrics-bind-address
METRICS_SECUREfalse--metrics-secure

Set METRICS_SECURE: "true" (Helm: global.metricsSecure) to serve over HTTPS with authn/authz instead — the generated ServiceMonitor still scrapes plain HTTP by default, so switching this on also means configuring scheme: https and the matching TLS/bearer-token settings on the ServiceMonitor yourself.

This is the HTTP endpoint itself, separate from whether a given resource’s business metrics are recorded — that’s the per-resource switch:

Env varDefault
<PLURAL>_METRICS_ENABLEDTRUE

Disable it per resource with --no-metrics on genop create api, or by editing .genop.yaml. The Go code is generated unconditionally and reads the switch at runtime, so flipping it never rewrites a .go file.

Prometheus Operator labels

Neither packaging’s ServiceMonitor ships a default Prometheus Operator release label or metricRelabelings — every install’s Prometheus Operator selects by a different label, so there’s no one correct default to guess. Add what your install needs:

Helm — set it in your own values file:

# my-values.yaml
metrics:
  serviceMonitor:
    labels:
      release: my-prometheus-operator
    metricRelabelings:
      - regex: pod
        action: labeldrop

Kustomize — layer a patch on top, the same way you’d customize any other managed manifest under config/kustomize/:

# config/kustomize/overlay/servicemonitor-labels.yaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: postgresql-database
  labels:
    release: my-prometheus-operator

Custom metrics

Two gauges are wired up automatically for every resource: phase (a 0–5 numeric encoding) and ready (0 or 1, from the Ready condition). Metric names follow {group|dots_replaced}_{kind}_{suffix} — e.g. infrastructure_acme_io_vpc_phase — and every metric carries the common labels cluster, resource_namespace, name, api_version.

Add your own in the generated, write-once internal/controllers/<version>/<kindLower>/metrics.go (Setup<Kind>MetricsServiceWithManager — the same escape hatch pattern as operator_config.go, never touched again by genop apply):

func SetupVPCMetricsServiceWithManager(clusterName string, mgr ctrl.Manager) error {
    recorder, err := operators.NewResourcesMetricsRecorder[*infrastructurev1alpha1.VPCStatus, *infrastructurev1alpha1.VPC](
        clusterName, mgr.GetScheme(), mgr.GetConfig(), mgr.GetRESTMapper(), metrics.Registry,
    )
    if err != nil {
        return fmt.Errorf("unable to build VPC metrics recorder: %w", err)
    }

    // Add your custom metrics here

    if err := mgr.Add(recorder); err != nil {
        return fmt.Errorf("unable to register VPC metrics recorder: %w", err)
    }
    return nil
}

A single-value gauge — one series per resource:

recorder.AddGauge(operators.SimpleGaugeRequest[*VPCStatus, *VPC]{
    NameSuffix:  "subnet_count",
    Description: "Number of subnets in the VPC",
    ValueMapper: func(r *VPC) float64 { return float64(len(r.Spec.Subnets)) },
})

A multi-value gauge (a “metric set”) — one series per item, each with its own extra labels:

recorder.AddGaugeMetricSet(operators.GaugeRequestSet[*VPCStatus, *VPC]{
    NameSuffix:     "subnet_info",
    Description:    "Info per subnet",
    ExtraLabelKeys: []string{"subnet_id", "availability_zone"},
    Metrics: func(r *VPC) []*operators.GaugeMetric {
        var result []*operators.GaugeMetric
        for _, s := range r.Status.Subnets {
            result = append(result, &operators.GaugeMetric{
                Value:  1,
                Labels: map[string]string{"subnet_id": s.ID, "availability_zone": s.AZ},
            })
        }
        return result
    },
})

ExtraLabelKeys must list every key any Labels map you return can use — AddGaugeMetricSet registers the Prometheus GaugeVec once, up front, with a fixed label set. Both AddGauge and AddGaugeMetricSet must be called before mgr.Add(recorder); the recorder is a leader-only manager runnable, so it starts after the cache has synced, runs only on the elected leader and stops with the manager. It recomputes every registered gauge on each add/update/delete event it observes, and restarts its watch automatically on failure.