Skip to content

Commit

Permalink
fix: Collect more metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
NoUseFreak committed Aug 2, 2023
1 parent a9a861b commit e794465
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"k8s.io/client-go/tools/clientcmd"
"k8s.io/component-base/logs"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

Expand All @@ -33,6 +35,21 @@ var svcReps []SvcRep
var interval int
var showDegraded bool

var (
opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
Name: "k8status_health_checks_total",
Help: "The total number of health checks performed",
})
servicesReady = promauto.NewGauge(prometheus.GaugeOpts{
Name: "k8status_services_ready",
Help: "Current number of service ready",
})
servicesNotReady = promauto.NewGauge(prometheus.GaugeOpts{
Name: "k8status_services_not_ready",
Help: "Current number of service not ready",
})
)

func init() {
if home := homedir.HomeDir(); home != "" {
flag.StringVar(&kubeconfig, "kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
Expand Down Expand Up @@ -117,6 +134,7 @@ func main() {
}

func loadServiceInfo(clientset *kubernetes.Clientset, ns string) []SvcRep {
var readyCnt, notReadyCnt int = 0, 0
var result []SvcRep
svcs, err := clientset.CoreV1().Services(ns).List(context.Background(), v1.ListOptions{})
if err != nil {
Expand All @@ -132,11 +150,11 @@ func loadServiceInfo(clientset *kubernetes.Clientset, ns string) []SvcRep {
if err != nil {
log.Fatal(err)
}
healty := 0
healthy := 0
for _, pod := range pods.Items {
for _, container := range pod.Status.ContainerStatuses {
if container.Ready {
healty++
healthy++
}
}
}
Expand All @@ -149,7 +167,7 @@ func loadServiceInfo(clientset *kubernetes.Clientset, ns string) []SvcRep {
description = val
}
var status string
switch healty {
switch healthy {
case 0:
status = "down"
case len(pods.Items):
Expand All @@ -164,11 +182,21 @@ func loadServiceInfo(clientset *kubernetes.Clientset, ns string) []SvcRep {
result = append(result, SvcRep{
Name: name,
Description: description,
Ready: healty > 0,
Ready: healthy > 0,
Status: status,
})

if healthy > 0 {
readyCnt++
} else {
notReadyCnt++
}
}

opsProcessed.Inc()
servicesReady.Set(float64(readyCnt))
servicesNotReady.Set(float64(notReadyCnt))

return result
}

Expand Down

0 comments on commit e794465

Please sign in to comment.