From b440bc27dfd067337b473a9d281c766f6f45bfe1 Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Mon, 8 Jul 2024 13:57:38 +0530 Subject: [PATCH] fixup! fixup! fix: add `readyz` endpoint --- pkg/app/server.go | 3 +-- pkg/util/utils.go | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/pkg/app/server.go b/pkg/app/server.go index a9f1e7c2e6..920c219840 100644 --- a/pkg/app/server.go +++ b/pkg/app/server.go @@ -42,7 +42,6 @@ import ( versionCollector "github.com/prometheus/client_golang/prometheus/collectors/version" "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promhttp" - "github.com/prometheus/client_golang/prometheus/testutil" "github.com/prometheus/common/version" "github.com/prometheus/exporter-toolkit/web" @@ -380,7 +379,7 @@ func buildTelemetryServer(registry prometheus.Gatherer) *http.ServeMux { // Add readyzPath mux.Handle(readyzPath, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - count, err := testutil.GatherAndCount(registry) + count, err := util.GatherAndCount(registry) if err != nil || count == 0 { w.WriteHeader(http.StatusServiceUnavailable) w.Write([]byte(http.StatusText(http.StatusServiceUnavailable))) diff --git a/pkg/util/utils.go b/pkg/util/utils.go index 63e5aba650..14b108f37e 100644 --- a/pkg/util/utils.go +++ b/pkg/util/utils.go @@ -21,7 +21,6 @@ import ( "runtime" "strings" - "github.com/prometheus/common/version" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/discovery" @@ -32,6 +31,9 @@ import ( "k8s.io/klog/v2" testUnstructuredMock "k8s.io/sample-controller/pkg/apis/samplecontroller/v1alpha1" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/common/version" + "k8s.io/kube-state-metrics/v2/pkg/customresource" ) @@ -154,3 +156,19 @@ func GVRFromType(resourceName string, expectedType interface{}) *schema.GroupVer Resource: r, } } + +// GatherAndCount gathers all metrics from the provided Gatherer and counts +// them. It returns the number of metric children in all gathered metric +// families together. +func GatherAndCount(g prometheus.Gatherer) (int, error) { + got, err := g.Gather() + if err != nil { + return 0, fmt.Errorf("gathering metrics failed: %w", err) + } + + result := 0 + for _, mf := range got { + result += len(mf.GetMetric()) + } + return result, nil +}