Skip to content

Commit

Permalink
Adapt code for KSM stable metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
CatherineF-dev committed Jan 2, 2024
1 parent 648a0eb commit 44ee7e2
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 908 deletions.
3 changes: 2 additions & 1 deletion internal/store/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,11 +882,12 @@ func createPodInitContainerStatusReadyFamilyGenerator() generator.FamilyGenerato
}

func createPodInitContainerStatusRestartsTotalFamilyGenerator() generator.FamilyGenerator {
return *generator.NewFamilyGeneratorWithStability(
return *generator.NewFamilyGeneratorWithStabilityV2(
"kube_pod_init_container_status_restarts_total",
"The number of restarts for the init container.",
metric.Counter, basemetrics.STABLE,
"",
[]string{"container"},
wrapPodFunc(func(p *v1.Pod) *metric.Family {
ms := make([]*metric.Metric, len(p.Status.InitContainerStatuses))

Expand Down
31 changes: 31 additions & 0 deletions pkg/metric_generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ type FamilyGenerator struct {
GenerateFunc func(obj interface{}) *metric.Family
}

// NewFamilyGeneratorWithStabilityV2 creates new FamilyGenerator instances with metric
// stabilityLevel and explicit labels. These explicit labels are used for verifying stable metrics.
func NewFamilyGeneratorWithStabilityV2(name string, help string, metricType metric.Type, stabilityLevel basemetrics.StabilityLevel, deprecatedVersion string, labels []string, generateFunc func(obj interface{}) *metric.Family) *FamilyGenerator {
f := &FamilyGenerator{
Name: name,
Type: metricType,
Help: help,
OptIn: false,
StabilityLevel: stabilityLevel,
DeprecatedVersion: deprecatedVersion,
GenerateFunc: WrapLabels(generateFunc, labels),
}
if deprecatedVersion != "" {
f.Help = fmt.Sprintf("(Deprecated since %s) %s", deprecatedVersion, help)
}
return f
}

// NewFamilyGeneratorWithStability creates new FamilyGenerator instances with metric
// stabilityLevel.
func NewFamilyGeneratorWithStability(name string, help string, metricType metric.Type, stabilityLevel basemetrics.StabilityLevel, deprecatedVersion string, generateFunc func(obj interface{}) *metric.Family) *FamilyGenerator {
Expand All @@ -57,6 +75,19 @@ func NewFamilyGeneratorWithStability(name string, help string, metricType metric
return f
}

func WrapLabels(generateFunc func(obj interface{}) *metric.Family, labels []string) func(obj interface{}) *metric.Family {
return func(obj interface{}) *metric.Family {

metricFamily := generateFunc(obj)

for _, m := range metricFamily.Metrics {
m.LabelKeys = append(labels, m.LabelKeys...)
}

return metricFamily
}
}

// NewOptInFamilyGenerator creates new FamilyGenerator instances for opt-in metric families.
func NewOptInFamilyGenerator(name string, help string, metricType metric.Type, stabilityLevel basemetrics.StabilityLevel, deprecatedVersion string, generateFunc func(obj interface{}) *metric.Family) *FamilyGenerator {
f := NewFamilyGeneratorWithStability(name, help, metricType, stabilityLevel,
Expand Down
Loading

0 comments on commit 44ee7e2

Please sign in to comment.