-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
89 lines (75 loc) · 2.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
/*
Main method for scrapper is handler() function, This function calling GetPodCountsByhttp() metrix.go
by using http call
Other method podCountByNamespace is calling GetPodCount() which is using rest api
*/
import (
"encoding/json"
"fmt"
"log"
"net/http"
"sync/atomic"
"github.com/mukul4u2005/matrix-service/metrix"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type PodCount struct {
Namespace string `json:"Namespace"`
PodCount int `json:"Pod Count"`
}
var (
requestsCounter uint64 = 0
podCount = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "cluster_pod_count",
Help: "Total pods in cluster",
})
hitCounter = prometheus.NewCounterFunc(prometheus.CounterOpts{
Name: "hit_counter",
Help: "Endpoint hit count",
}, func() float64 {
return float64(atomic.LoadUint64(&requestsCounter))
})
registry = prometheus.NewRegistry()
)
func init() {
// Metrics have to be registered to be exposed:
registry.Register(podCount)
registry.Register(hitCounter)
}
func podCountByNamespace(w http.ResponseWriter, r *http.Request) {
fmt.Println("Finding pod count")
var namespace string
namespaceArr := r.URL.Query()["namespace"]
if len(namespaceArr) == 0 {
namespace = ""
} else {
namespace = namespaceArr[0]
}
podCount := PodCount{
Namespace: namespace,
PodCount: metrix.GetPodCount(namespace),
}
json.NewEncoder(w).Encode(podCount)
}
func homePage(w http.ResponseWriter, r *http.Request) {
atomic.AddUint64(&requestsCounter, 1)
fmt.Fprintf(w, "Go home page")
}
func handler(w http.ResponseWriter, r *http.Request) {
atomic.AddUint64(&requestsCounter, 1)
var y float64 = float64(metrix.GetPodCountsByhttp())
podCount.Set(y)
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
func handleRequest() {
http.HandleFunc("/", homePage)
http.HandleFunc("/metrics", handler)
http.HandleFunc("/rest/metrics", podCountByNamespace)
err := http.ListenAndServe(":9000", nil)
log.Fatal(err)
}
func main() {
handleRequest()
}