Repository navigation
Feat/dashboard prometheus data sources #271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jhjaggars
merged 2 commits into
openshift-online:main
from
kdoberst:feat/dashboard-prometheus-data-sources
Sep 11, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
components/api-server/plugins/managedClusters/inventory.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package managedClusters | ||
|
|
||
| import ( | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| const inventoryLookback = 30 * 24 * time.Hour | ||
|
|
||
| // ClusterInventoryRow is one labeled bucket in the managed cluster inventory | ||
| // aggregate exposed to Prometheus. | ||
| type ClusterInventoryRow struct { | ||
| Status string | ||
| Provider string | ||
| Region string | ||
| Count int64 | ||
| } | ||
|
|
||
| // ClusterInventorySnapshot is the fleet-wide managed cluster inventory computed | ||
| // on each metrics scrape. | ||
| type ClusterInventorySnapshot struct { | ||
| CreatedLast30Days int64 | ||
| Rows []ClusterInventoryRow | ||
| } | ||
|
|
||
| func inventoryBucketString(value string) string { | ||
| trimmed := strings.TrimSpace(value) | ||
| if trimmed == "" { | ||
| return "unknown" | ||
| } | ||
| return trimmed | ||
| } | ||
|
|
||
| func inventoryBucketOptional(value *string) string { | ||
| if value == nil { | ||
| return "unknown" | ||
| } | ||
| return inventoryBucketString(*value) | ||
| } | ||
|
|
||
| func inventoryLookbackStart(evaluationTime time.Time) time.Time { | ||
| return evaluationTime.UTC().Add(-inventoryLookback) | ||
| } | ||
|
|
||
| func buildClusterInventorySnapshot( | ||
| clusters ManagedClusterList, | ||
| evaluationTime time.Time, | ||
| ) *ClusterInventorySnapshot { | ||
| windowStart := inventoryLookbackStart(evaluationTime) | ||
| type inventoryKey struct { | ||
| status string | ||
| provider string | ||
| region string | ||
| } | ||
| counts := make(map[inventoryKey]int64) | ||
| var createdLast30Days int64 | ||
|
|
||
| for _, cluster := range clusters { | ||
| key := inventoryKey{ | ||
| status: inventoryBucketOptional(cluster.Status), | ||
| provider: inventoryBucketString(cluster.Provider), | ||
| region: inventoryBucketOptional(cluster.Region), | ||
| } | ||
| counts[key]++ | ||
|
|
||
| if !cluster.CreatedAt.IsZero() && !cluster.CreatedAt.Before(windowStart) { | ||
| createdLast30Days++ | ||
| } | ||
| } | ||
|
|
||
| rows := make([]ClusterInventoryRow, 0, len(counts)) | ||
| for key, count := range counts { | ||
| rows = append(rows, ClusterInventoryRow{ | ||
| Status: key.status, | ||
| Provider: key.provider, | ||
| Region: key.region, | ||
| Count: count, | ||
| }) | ||
| } | ||
|
|
||
| return &ClusterInventorySnapshot{ | ||
| CreatedLast30Days: createdLast30Days, | ||
| Rows: rows, | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
components/api-server/plugins/managedClusters/inventory_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package managedClusters | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/openshift-online/rh-trex-ai/pkg/api" | ||
| ) | ||
|
|
||
| func TestBuildClusterInventorySnapshot(t *testing.T) { | ||
| evaluationTime := time.Date(2026, 9, 10, 12, 0, 0, 0, time.UTC) | ||
| recentCreatedAt := evaluationTime.Add(-10 * 24 * time.Hour) | ||
| oldCreatedAt := evaluationTime.Add(-60 * 24 * time.Hour) | ||
|
|
||
| clusters := ManagedClusterList{ | ||
| { | ||
| Meta: api.Meta{CreatedAt: recentCreatedAt}, | ||
| Provider: "aws", | ||
| Region: strPtr("us-east-1"), | ||
| Status: strPtr("Ready"), | ||
| }, | ||
| { | ||
| Meta: api.Meta{CreatedAt: oldCreatedAt}, | ||
| Provider: "openshift", | ||
| Region: nil, | ||
| Status: strPtr("Failed"), | ||
| }, | ||
| { | ||
| Meta: api.Meta{CreatedAt: recentCreatedAt}, | ||
| Provider: "aws", | ||
| Region: strPtr(""), | ||
| Status: nil, | ||
| }, | ||
| } | ||
|
|
||
| snapshot := buildClusterInventorySnapshot(clusters, evaluationTime) | ||
|
|
||
| if snapshot.CreatedLast30Days != 2 { | ||
| t.Fatalf("expected 2 clusters created in the last 30 days, got %d", snapshot.CreatedLast30Days) | ||
| } | ||
|
|
||
| if len(snapshot.Rows) != 3 { | ||
| t.Fatalf("expected 3 inventory rows, got %d", len(snapshot.Rows)) | ||
| } | ||
| } | ||
|
|
||
| func strPtr(value string) *string { | ||
| return &value | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package managedClusters | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| ) | ||
|
|
||
| const ( | ||
| metricsNamespace = "hypershell" | ||
| metricsSubsystem = "managed_clusters" | ||
| ) | ||
|
|
||
| var ( | ||
| managedClusterMetricsOnce sync.Once | ||
| ) | ||
|
|
||
| // RegisterManagedClusterMetrics registers Prometheus gauges for fleet-wide | ||
| // managed cluster inventory. Safe to call multiple times. | ||
| func RegisterManagedClusterMetrics(dao ManagedClusterDao) { | ||
| managedClusterMetricsOnce.Do(func() { | ||
| prometheus.MustRegister(newManagedClusterInventoryCollector(dao)) | ||
| }) | ||
| } | ||
|
|
||
| type managedClusterInventoryCollector struct { | ||
| dao ManagedClusterDao | ||
| totalDesc *prometheus.Desc | ||
| createdDesc *prometheus.Desc | ||
| dimensionedDesc *prometheus.Desc | ||
| } | ||
|
|
||
| func newManagedClusterInventoryCollector(dao ManagedClusterDao) *managedClusterInventoryCollector { | ||
| return &managedClusterInventoryCollector{ | ||
| dao: dao, | ||
| totalDesc: prometheus.NewDesc( | ||
| prometheus.BuildFQName(metricsNamespace, metricsSubsystem, "total"), | ||
| "Total registered managed clusters.", | ||
| nil, | ||
| nil, | ||
| ), | ||
| createdDesc: prometheus.NewDesc( | ||
| prometheus.BuildFQName(metricsNamespace, metricsSubsystem, "created_last_30_days_total"), | ||
| "Managed clusters created in the last 30 days.", | ||
| nil, | ||
| nil, | ||
| ), | ||
| dimensionedDesc: prometheus.NewDesc( | ||
| prometheus.BuildFQName(metricsNamespace, metricsSubsystem, "inventory_total"), | ||
| "Managed clusters by inventory status, provider, and region.", | ||
| []string{"status", "provider", "region"}, | ||
|
kdoberst marked this conversation as resolved.
|
||
| nil, | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| func (c *managedClusterInventoryCollector) Describe(ch chan<- *prometheus.Desc) { | ||
| ch <- c.totalDesc | ||
| ch <- c.createdDesc | ||
| ch <- c.dimensionedDesc | ||
| } | ||
|
|
||
| func (c *managedClusterInventoryCollector) Collect(ch chan<- prometheus.Metric) { | ||
| snapshot, err := c.dao.InventorySnapshot(context.Background(), time.Now().UTC()) | ||
| if err != nil { | ||
| ch <- prometheus.NewInvalidMetric(c.totalDesc, err) | ||
| return | ||
| } | ||
|
|
||
| var total int64 | ||
| for _, row := range snapshot.Rows { | ||
| total += row.Count | ||
| ch <- prometheus.MustNewConstMetric( | ||
| c.dimensionedDesc, | ||
| prometheus.GaugeValue, | ||
| float64(row.Count), | ||
| row.Status, | ||
| row.Provider, | ||
| row.Region, | ||
| ) | ||
| } | ||
|
|
||
| ch <- prometheus.MustNewConstMetric(c.totalDesc, prometheus.GaugeValue, float64(total)) | ||
| ch <- prometheus.MustNewConstMetric( | ||
| c.createdDesc, | ||
| prometheus.GaugeValue, | ||
| float64(snapshot.CreatedLast30Days), | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
InventorySnapshot calls
d.All(ctx)and buckets in Go on every Prometheus scrape. The gateway collector in this same PR aggregates in SQL (CountByPhase); doing the same here (GROUP BY status, provider, regionplus a windowed created-count) would avoid loading the full table each scrape and keep the collectors consistent. Not blocking at current scale.