Skip to content

feat: add Prometheus metrics for operator reconciliation#793

Merged
jamieplu merged 2 commits into
masterfrom
claude/meshery-operator-pr-780-3307c7
Jun 30, 2026
Merged

feat: add Prometheus metrics for operator reconciliation#793
jamieplu merged 2 commits into
masterfrom
claude/meshery-operator-pr-780-3307c7

Conversation

@leecalcote

@leecalcote leecalcote commented Jun 30, 2026

Copy link
Copy Markdown
Member

fDescription

This PR fixes #779.

Adds reconciliation observability to the operator and continues the work started in #780 by @Chaithanya5gif, rebased onto the post-Kubebuilder-go/v4 (WS-1) layout per the request on that PR.

A new pkg/metrics package registers three metrics with the controller-runtime registry, so they are exposed automatically on the manager's existing metrics endpoint (default :8080/metrics) with no extra wiring:

  • meshery_operator_reconcile_total{controller}
  • meshery_operator_reconcile_errors_total{controller}
  • meshery_operator_reconcile_duration_seconds{controller}

The BrokerReconciler and MeshSyncReconciler are instrumented via a deferred closure that records the count and duration on every reconcile and increments the error counter only when a non-nil error is returned.

Notes for Reviewers

Carried over from #780 (metric naming and the defer-based timing pattern), with the following review changes applied:

  • Dropped the Makefile and CONTRIBUTING.md changes from feat: add Prometheus metrics for operator reconciliation #780 — WS-1 already provides the envtest-aware make test target and contributor docs (docs/testing.md), so those edits are now obsolete and would conflict.
  • Named return values (result, reconcileErr) keep the instrumentation in a single defer at the top of Reconcile instead of rewriting every return site, minimizing churn in the control flow.
  • Lint-clean: extracted controller-name and label constants to satisfy goconst, grouped imports for gci/goimports, added the Apache license headers, and renamed an inner result to avoid shadowing under govet's shadow analyzer.
  • Tests assert all three metrics are registered with the controller-runtime registry (the acceptance criterion) and that the counters and histogram record values.

Verified locally: gofmt, go vet, go build ./..., golangci-lint v2.12.2 run ./... (0 issues), and the full unit/envtest suite (make test) all pass.

Signed commits

  • Yes, I signed my commits.

Add a pkg/metrics package that registers reconciliation metrics with the
controller-runtime registry, so they are exposed automatically on the
manager's existing metrics endpoint (default :8080/metrics):

- meshery_operator_reconcile_total{controller}
- meshery_operator_reconcile_errors_total{controller}
- meshery_operator_reconcile_duration_seconds{controller}

Instrument the Broker and MeshSync reconcilers via a deferred closure that
records count, duration, and (on a non-nil error) the error counter. Named
return values keep the instrumentation out of the reconciliation control
flow without rewriting each return site.

Tests assert that all three metrics are registered with the
controller-runtime registry and that counters and the histogram record
values as expected.

This rebases the instrumentation onto the post-Kubebuilder-go/v4 (WS-1)
layout: the test tooling and contributor docs are now provided by the
Makefile test target and docs/testing.md, so no Makefile or CONTRIBUTING
changes are needed here.

Signed-off-by: Lee Calcote <lee.calcote@layer5.io>
@pontusringblom

Copy link
Copy Markdown
Member

/gemini review

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds first-class Prometheus reconciliation observability to Meshery Operator by defining and registering controller-runtime metrics and instrumenting the Broker and MeshSync controllers to record reconcile counts, errors, and latency.

Changes:

  • Introduces a new pkg/metrics package that registers reconcile metrics with the controller-runtime metrics registry (auto-exposed on the manager metrics endpoint).
  • Instruments BrokerReconciler and MeshSyncReconciler reconcile loops using a single top-level defer to consistently record total reconciles, duration, and error counts.
  • Adds unit tests validating metric registration and basic counter/histogram behavior; updates go.mod to add a direct Prometheus client dependency.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
pkg/metrics/metrics.go Defines and registers reconcile counters/histogram with controller-runtime’s metrics registry.
pkg/metrics/metrics_test.go Verifies metrics are registered and that counters/histogram accept observations.
controllers/broker_controller.go Adds deferred reconcile instrumentation (total/errors/duration) to Broker controller.
controllers/meshsync_controller.go Adds deferred reconcile instrumentation (total/errors/duration) to MeshSync controller.
go.mod Adds prometheus/client_golang as a direct dependency and updates indirect deps accordingly.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Add docs/metrics.md describing the three Prometheus reconciliation
metrics added in this PR (meshery_operator_reconcile_total,
_errors_total, _duration_seconds), how they register with the
controller-runtime registry and surface on the manager's :8080/metrics
endpoint, the defer-based named-return instrumentation pattern in both
controllers, how to add a new metric, and the test conventions. Note the
WS-5 follow-ups (metrics TLS, ServiceMonitor). Link it from the docs
README index.

Signed-off-by: Lee Calcote <lee.calcote@layer5.io>
@jamieplu jamieplu merged commit c0f9a66 into master Jun 30, 2026
6 of 7 checks passed
@jamieplu jamieplu deleted the claude/meshery-operator-pr-780-3307c7 branch June 30, 2026 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Add Prometheus metrics for operator reconciliation

4 participants