Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions metrics/cluster_info_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ var clusterFactsDesc = prometheus.NewDesc(
nil,
)

// commodore build info has dynamic labels
var clusterDynFactsDesc = prometheus.NewDesc(
"syn_lieutenant_cluster_dynamic_facts",
"Lieutenant cluster dynamic facts.",
[]string{"cluster", "tenant", "display_name"},
nil,
)

// cluster facts has dynamic labels
func newClusterFactsDesc(lbls ...string) *prometheus.Desc {
return prometheus.NewDesc(
"syn_lieutenant_cluster_facts",
Expand All @@ -41,6 +48,16 @@ func newClusterFactsDesc(lbls ...string) *prometheus.Desc {
)
}

// cluster dynamic facts has dynamic labels
func newClusterDynFactsDesc(lbls ...string) *prometheus.Desc {
return prometheus.NewDesc(
"syn_lieutenant_cluster_dynamic_facts",
"Lieutenant cluster dynamic facts. Keys are normalized to be valid Prometheus labels.",
lbls,
nil,
)
}

// ClusterInfoCollector is a Prometheus collector that collects cluster info metrics.
type ClusterInfoCollector struct {
Client client.Client
Expand Down Expand Up @@ -73,9 +90,13 @@ func (m *ClusterInfoCollector) Collect(ch chan<- prometheus.Metric) {
cl.Name, cl.Spec.TenantRef.Name, cl.Spec.DisplayName,
)

if err := clusterFacts(cl, ch); err != nil {
if err := clusterFacts(newClusterFactsDesc, cl, cl.Spec.Facts, ch); err != nil {
log.Log.Info("failed to collect cluster facts", "error", err)
}

if err := clusterFacts(newClusterDynFactsDesc, cl, cl.Status.Facts, ch); err != nil {
log.Log.Info("failed to collect cluster dynamic facts", "error", err)
}
}
}

Expand All @@ -85,8 +106,8 @@ func (m *ClusterInfoCollector) Collect(ch chan<- prometheus.Metric) {
// If a key is empty it is replaced with "_empty".
// If a key is in the protected list after normalizing it is prefixed with "orig_".
// If a key is a duplicate after normalizing it is suffixed with "_<n>" where n is the number of duplicates.
func clusterFacts(cl synv1alpha1.Cluster, ch chan<- prometheus.Metric) error {
rks, vs := pairs(cl.Spec.Facts)
func clusterFacts(descfn func(lbls ...string) *prometheus.Desc, cl synv1alpha1.Cluster, facts synv1alpha1.Facts, ch chan<- prometheus.Metric) error {
rks, vs := pairs(facts)
ks := make([]string, len(rks))
for i, k := range rks {
ks[i] = normalizeLabelKey(k, []string{"cluster", "tenant"}, "fact_")
Expand All @@ -100,7 +121,7 @@ func clusterFacts(cl synv1alpha1.Cluster, ch chan<- prometheus.Metric) error {
}

m, err := prometheus.NewConstMetric(
newClusterFactsDesc(append([]string{"cluster", "tenant"}, ks...)...),
descfn(append([]string{"cluster", "tenant"}, ks...)...),
prometheus.GaugeValue,
1,
append([]string{cl.Name, cl.Spec.TenantRef.Name}, vs...)...,
Expand Down
12 changes: 11 additions & 1 deletion metrics/cluster_info_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func Test_ClusterInfoCollector(t *testing.T) {
expectedMetricNames := []string{
"syn_lieutenant_cluster_info",
"syn_lieutenant_cluster_facts",
"syn_lieutenant_cluster_dynamic_facts",
}

c := prepareClient(t,
Expand Down Expand Up @@ -49,6 +50,11 @@ func Test_ClusterInfoCollector(t *testing.T) {
"tenant": "value",
},
},
Status: synv1alpha1.ClusterStatus{
Facts: map[string]string{
"test": "value",
},
},
},
)

Expand All @@ -58,7 +64,11 @@ func Test_ClusterInfoCollector(t *testing.T) {
Namespace: namespace,
}

metrics := `# HELP syn_lieutenant_cluster_facts Lieutenant cluster facts. Keys are normalized to be valid Prometheus labels.
metrics := `# HELP syn_lieutenant_cluster_dynamic_facts Lieutenant cluster dynamic facts. Keys are normalized to be valid Prometheus labels.
# TYPE syn_lieutenant_cluster_dynamic_facts gauge
syn_lieutenant_cluster_dynamic_facts{cluster="c-empty",tenant=""} 1
syn_lieutenant_cluster_dynamic_facts{cluster="c2",tenant="t2",test="value"} 1
# HELP syn_lieutenant_cluster_facts Lieutenant cluster facts. Keys are normalized to be valid Prometheus labels.
# TYPE syn_lieutenant_cluster_facts gauge
syn_lieutenant_cluster_facts{cluster="c-empty",tenant=""} 1
syn_lieutenant_cluster_facts{cluster="c2",fact__key="value",fact__key_duplicate_after_normalize="value",fact__key_duplicate_after_normalize_1="value",fact__key_duplicate_after_normalize_2="value",key="value",key_with847_____invalid_chars="value",orig_cluster="value",orig_tenant="value",tenant="t2"} 1
Expand Down
Loading