Skip to content
All projects

Observability · 2026

Custom Operator Metrics

controller-runtime gives every operator reconcile counts and latency for free. They prove the controller is alive and say nothing about whether work is getting done. This defines metrics for the domain events across the pipeline, and ships the scrape config and dashboard in the same repo as the code they describe.

Build an operator with controller-runtime and you get a metrics endpoint without asking: reconcile totals, error totals, duration, workqueue depth. It is a good free gift, and it answers exactly one question — is the controller running and keeping up?

It cannot tell you whether the system is doing its job, because the framework has no idea what your controller is for. The useful metrics are the ones you define yourself, named after events in your own domain.

The problem it solves

“The controller is healthy” and “work is getting done” are different statements, and only one of them matters to a user.

This operator could be reconciling cleanly every five seconds, zero errors, sub-millisecond latency — a perfect set of framework metrics — while the Redis queue grows without bound because the worker image is crash-looping. Every generic signal would be green.

The tell is in the finished dashboard: not one panel queries a controller_runtime_* series. Every single one reads a metric that was defined for this pipeline.

How it works

① webapp ① worker ② operator OTel Collector prometheus exporter ③ Prometheus one store ④ Grafana webapp_subscribe_requests worker_emails_processed queueworker_queue_depth OTLP push OTLP push scrape :8889 scrape /metrics
Both orange arrows are ServiceMonitor scrapes. What differs is where the push/pull boundary sits — ephemeral components push to a collector first, and the collector is what gets scraped.
  1. Short-lived components push. The worker exists for seconds and the webapp is a Next.js route, so both emit OpenTelemetry over OTLP to a collector. The collector runs the contrib image for its Prometheus exporter, which re-publishes everything it receives on port 8889.
  2. The operator is scraped at the source. It is a long-running Deployment, so there is nothing to push through — Prometheus reads the endpoint the custom gauges were registered onto directly.
  3. Both arrows into Prometheus are ServiceMonitor scrapes. Worth being precise about: the collector is scraped by one too. The transports differ only in where the push/pull boundary sits, not in how Prometheus is told to look — and past this point nothing knows which component was ephemeral.
  4. One dashboard reads across all three, so “submissions went up, queue depth went up, workers did not” is a single glance rather than three tabs.

Registering into the endpoint that already exists

The operator’s custom metrics go into controller-runtime’s own registry rather than a second HTTP server. One endpoint, one scrape target, custom series sitting beside the framework ones:

func init() {
	metrics.Registry.MustRegister(queueDepthGauge, runningJobsGauge, jobsCreatedCounter)
}

They are then set at the moment the controller makes its decision — not sampled afterwards on a timer — so the gauge is always the number the reconcile actually acted on:

	queueDepthGauge.WithLabelValues(qw.Name, qw.Spec.Redis.Stream).Set(float64(depth))
	runningJobsGauge.WithLabelValues(qw.Name, qw.Spec.Redis.Stream).Set(float64(running))

Labels are where the thinking goes

The metric name says what happened; the labels are what make it answerable. Each one here was chosen to separate causes that need different responses:

  • {queueworker, stream} on the operator’s series — without it, every declared queue collapses into one line that says something is backed up but not which thing.
  • {outcome} on worker_emails_processed — a broken SMTP server and a malformed message are both “failures” and have nothing else in common.
  • {source} on worker_messages_claimed — distinguishes normal delivery from messages reclaimed off a dead consumer, which is the signal that workers are dying mid-flight.

A counter without those labels tells you a number went up. With them it tells you what to go and fix.

Where the dashboard lives

The ServiceMonitors and the dashboard sit in their own monitoring chart, not inside the operator chart — so the operator still installs on a cluster that has never heard of Prometheus. That chart pulls kube-prometheus-stack and opentelemetry-collector as dependencies and brings the CRDs its own templates need.

The part that matters is that it lives in the same repository as the code it describes. Keep dashboards in a central monitoring repo owned by another team and they version separately from the controller, so a metric rename silently blanks a panel. Here both land in one commit.

What I would tell you before adopting it

Every label multiplies series count, and the pressure is always to add one more. {queueworker, stream} is bounded by how many queues you declare; a message ID would not be, and would turn a metrics store into a very expensive log store.

The honest gap today is that neither ServiceMonitor is guarded by a conditional — they assume the Prometheus Operator CRDs exist, which is true only because the monitoring chart installs them.

The dashboard JSON, chart templates and instrumentation are in the repo.