Skip to content

feat: Add crossplane_managed_resource_* state metrics#308

Open
lkiii wants to merge 6 commits intocrossplane-contrib:masterfrom
lkiii:add-state-metrics
Open

feat: Add crossplane_managed_resource_* state metrics#308
lkiii wants to merge 6 commits intocrossplane-contrib:masterfrom
lkiii:add-state-metrics

Conversation

@lkiii
Copy link

@lkiii lkiii commented Jan 28, 2026

Description of your changes

Add Prometheus state metrics for all managed resource types following the Crossplane observability guide.

Metrics exposed:

  • crossplane_managed_resource_exists - Count of existing managed resources
  • crossplane_managed_resource_ready - Count of ready managed resources
  • crossplane_managed_resource_synced - Count of synced managed resources

Each metric is labeled with GVK (GroupVersionKind). Polls every 30 seconds.

Implementation:

  • Added pkg/controller/statemetrics.go with SetupStateMetrics() function
  • Registers MRStateRecorder for all 22 managed resource types (PostgreSQL, MySQL, MSSQL - both cluster and namespaced)
  • Added unit tests in pkg/controller/statemetrics_test.go

Fixes #307

I have:

  • Read and followed Crossplane's contribution process.
  • Run make reviewable to ensure this PR is ready for review.

How has this code been tested

  • All unit tests pass (make reviewable)
  • Manual end-to-end testing verified metrics are exposed correctly at :8080/metrics
  • Created a test Database resource and confirmed crossplane_managed_resource_exists incremented from 0 to 1 after the 30-second poll interval

lkiii and others added 2 commits January 28, 2026 20:34
Add Prometheus state metrics for all managed resource types following
the Crossplane contribution guide for provider observability.

Metrics exposed:
- crossplane_managed_resource_exists
- crossplane_managed_resource_ready
- crossplane_managed_resource_synced

Each metric is labeled with GVK. Polls every 30 seconds.

Signed-off-by: Lukas Ignatavičius <lukas@cashcoach.ai>
@chlunde chlunde self-assigned this Mar 3, 2026
Copy link
Collaborator

@chlunde chlunde left a comment

Choose a reason for hiding this comment

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

Could you look at provider-kubernetes and do setup automatic per-controller via MetricOptions? Poll interval should be a CLI option - same as provider-kubernetes

We should avoid having to list each resource in a separate file here

@lkiii lkiii force-pushed the add-state-metrics branch from 005152b to 602d416 Compare March 4, 2026 11:10
@lkiii
Copy link
Author

lkiii commented Mar 4, 2026

Could you look at provider-kubernetes and do setup automatic per-controller via MetricOptions? Poll interval should be a CLI option - same as provider-kubernetes

We should avoid having to list each resource in a separate file here

I have refactored the approach to be closer to provider-kubernetes

@lkiii lkiii requested a review from chlunde March 4, 2026 11:19
@lkiii lkiii force-pushed the add-state-metrics branch 3 times, most recently from aca79fa to 5a551ea Compare March 6, 2026 10:23
lkiii added 2 commits March 9, 2026 10:32
Replace the centralized SetupStateMetrics function and its explicit
resource type list with per-controller registration, matching the
pattern used in provider-kubernetes.

- Add --poll-state-metric CLI flag (default 5s) instead of hardcoding
- Build MetricOptions and pass it through controller.Options so each
  controller's Setup registers its own MRStateRecorder automatically
- Add --metrics-bind-address flag to allow configuring the metrics port
- Delete pkg/controller/statemetrics.go (centralized list no longer needed)
- Keep Prometheus plumbing tests in metrics_test.go; drop config-list tests

As a side effect, DefaultPrivileges is now correctly covered — it was
missing from the old centralized list.

Signed-off-by: Lukas Ignatavičius <lukas@cashcoach.ai>
Signed-off-by: Lukas Ignatavičius <lukas@cashcoach.ai>
@lkiii lkiii force-pushed the add-state-metrics branch from 94221a5 to 33962ae Compare March 9, 2026 08:33
Signed-off-by: Lukas Ignatavičius <lukas@cashcoach.ai>
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.

feat: Add crossplane_managed_resource_* state metrics

2 participants