Skip to content

Commit

Permalink
chore: Update the build_info metric label naming and tests (#5275)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis authored Dec 8, 2023
1 parent 8f500c2 commit 99cfbaf
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 126 deletions.
2 changes: 1 addition & 1 deletion charts/karpenter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ helm upgrade --install --namespace karpenter --create-namespace \
| controller.envFrom | list | `[]` | |
| controller.extraVolumeMounts | list | `[]` | Additional volumeMounts for the controller pod. |
| controller.healthProbe.port | int | `8081` | The container port to use for http health probe. |
| controller.image.digest | string | `"sha256:afa0d0fd5ac375859dc3d239ec992f197cdf01f6c8e3413e3845a43c2434621e"` | SHA256 digest of the controller image. |
| controller.image.digest | string | `"sha256:5e5f59f74d86ff7f13d7d80b89afff8c661cb4e3265f2fdda95b76dd9c838cc1"` | SHA256 digest of the controller image. |
| controller.image.repository | string | `"public.ecr.aws/karpenter/controller"` | Repository path to the controller image. |
| controller.image.tag | string | `"v0.33.0"` | Tag of the controller image. |
| controller.metrics.port | int | `8000` | The container port to use for metrics. |
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pelletier/go-toml/v2 v2.1.0
github.com/prometheus/client_golang v1.17.0
github.com/prometheus/client_model v0.5.0
github.com/samber/lo v1.39.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.26.0
Expand Down Expand Up @@ -79,7 +80,6 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/prometheus/statsd_exporter v0.24.0 // indirect
Expand Down
46 changes: 27 additions & 19 deletions hack/docs/metrics_gen_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func main() {
packages := getPackages(flag.Arg(i))
allMetrics = append(allMetrics, getMetricsFromPackages(packages...)...)
}
// Controller Runtime naming is different in that they don't specify a namespace or subsystem
// Getting the metrics requires special parsing logic
for i := range allMetrics {
if allMetrics[i].subsystem == "" && strings.HasPrefix(allMetrics[i].name, "controller_runtime_") {
allMetrics[i].subsystem = "controller_runtime"
allMetrics[i].name = strings.TrimPrefix(allMetrics[i].name, "controller_runtime_")
}
}
sort.Slice(allMetrics, bySubsystem(allMetrics))

outputFileName := flag.Arg(flag.NArg() - 1)
Expand All @@ -80,19 +88,15 @@ description: >
previousSubsystem := ""

for _, metric := range allMetrics {
// Controller Runtime naming is different in that they don't specify a namespace or subsystem
// Getting the metrics requires special parsing logic
if metric.subsystem == "" && strings.HasPrefix(metric.name, "controller_runtime_") {
metric.subsystem = "controller_runtime"
metric.name = strings.TrimPrefix(metric.name, "controller_runtime_")
}
if metric.subsystem != previousSubsystem {
subsystemTitle := strings.Join(lo.Map(strings.Split(metric.subsystem, "_"), func(s string, _ int) string {
return fmt.Sprintf("%s%s", strings.ToTitle(s[0:1]), s[1:])
}), " ")
fmt.Fprintf(f, "## %s Metrics\n", subsystemTitle)
if metric.subsystem != "" {
subsystemTitle := strings.Join(lo.Map(strings.Split(metric.subsystem, "_"), func(s string, _ int) string {
return fmt.Sprintf("%s%s", strings.ToTitle(s[0:1]), s[1:])
}), " ")
fmt.Fprintf(f, "## %s Metrics\n", subsystemTitle)
fmt.Fprintln(f)
}
previousSubsystem = metric.subsystem
fmt.Fprintln(f)
}
fmt.Fprintf(f, "### `%s`\n", metric.qualifiedName())
fmt.Fprintf(f, "%s\n", metric.help)
Expand Down Expand Up @@ -155,19 +159,23 @@ func getMetricsFromPackages(packages ...*ast.Package) []metricInfo {
}

func bySubsystem(metrics []metricInfo) func(i int, j int) bool {
subSystemSortOrder := map[string]int{}
subSystemSortOrder["nodepool"] = 1
subSystemSortOrder["nodes"] = 2
subSystemSortOrder["pods"] = 3
subSystemSortOrder["cloudprovider"] = 4
subSystemSortOrder["cloudprovider_batcher"] = 5
// Higher ordering comes first. If a value isn't designated here then the subsystem will be given a default of 0.
// Metrics without a subsystem come first since there is no designation for the bucket they fall under
subSystemSortOrder := map[string]int{
"": 100,
"nodepool": 10,
"nodeclaim": 9,
"nodes": 8,
"pods": 7,
}

return func(i, j int) bool {
lhs := metrics[i]
rhs := metrics[j]
if subSystemSortOrder[lhs.subsystem] != subSystemSortOrder[rhs.subsystem] {
return subSystemSortOrder[lhs.subsystem] < subSystemSortOrder[rhs.subsystem]
return subSystemSortOrder[lhs.subsystem] > subSystemSortOrder[rhs.subsystem]
}
return lhs.qualifiedName() < rhs.qualifiedName()
return lhs.qualifiedName() > rhs.qualifiedName()
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var BuildInfo = prometheus.NewGaugeVec(
Name: "build_info",
Help: "A metric with a constant '1' value labeled by version from which karpenter was built.",
},
[]string{"version", "goversion", "sha"})
[]string{"version", "goversion", "commit"})

func init() {
lo.Must0(apis.AddToScheme(scheme.Scheme))
Expand Down
16 changes: 13 additions & 3 deletions pkg/operator/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ import (
"github.com/aws/aws-sdk-go/service/eks"
"github.com/samber/lo"

prometheusmodel "github.com/prometheus/client_model/go"
"sigs.k8s.io/karpenter/pkg/operator/scheme"
coretest "sigs.k8s.io/karpenter/pkg/test"

"github.com/aws/karpenter-provider-aws/pkg/apis"
"github.com/aws/karpenter-provider-aws/pkg/fake"
awscontext "github.com/aws/karpenter-provider-aws/pkg/operator"
"github.com/aws/karpenter-provider-aws/pkg/operator/options"
"github.com/aws/karpenter-provider-aws/pkg/test"

"sigs.k8s.io/karpenter/pkg/operator/scheme"
coretest "sigs.k8s.io/karpenter/pkg/test"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "knative.dev/pkg/logging/testing"
Expand Down Expand Up @@ -102,4 +103,13 @@ var _ = Describe("Operator", func() {
_, err := awscontext.ResolveClusterEndpoint(ctx, fakeEKSAPI)
Expect(err).To(HaveOccurred())
})
It("should fire a metric with the build_info", func() {
m, found := FindMetricWithLabelValues("karpenter_build_info", map[string]string{})
Expect(found).To(BeTrue())

for _, label := range []string{"version", "goversion", "commit"} {
_, ok := lo.Find(m.GetLabel(), func(l *prometheusmodel.LabelPair) bool { return lo.FromPtr(l.Name) == label })
Expect(ok).To(BeTrue())
}
})
})
Loading

0 comments on commit 99cfbaf

Please sign in to comment.