Skip to content

Commit

Permalink
fixup! fixup! fix: add readyz endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rexagod committed Jul 8, 2024
1 parent b980579 commit b440bc2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
3 changes: 1 addition & 2 deletions pkg/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)))
Expand Down
20 changes: 19 additions & 1 deletion pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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
}

0 comments on commit b440bc2

Please sign in to comment.