[New feature] Added metrics

This commit is contained in:
AquoDev 2020-11-09 09:06:51 +01:00
parent 41f3706774
commit 40d1204906
No known key found for this signature in database
GPG Key ID: 27C4524C2511DF26
7 changed files with 101 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"github.com/zevenet/kube-nftlb/pkg/auth"
"github.com/zevenet/kube-nftlb/pkg/controller"
"github.com/zevenet/kube-nftlb/pkg/metrics"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
)
@ -19,6 +20,9 @@ func main() {
// TODO Enable NetworkPolicyController after nftlb fully supports policies
}
// Start metrics server
go metrics.StartServer()
// Run controllers as background processes
for _, controller := range controllers {
go controller.Run(wait.NeverStop)

View File

@ -6,6 +6,7 @@ import (
"github.com/zevenet/kube-nftlb/pkg/http"
"github.com/zevenet/kube-nftlb/pkg/log"
"github.com/zevenet/kube-nftlb/pkg/metrics"
"github.com/zevenet/kube-nftlb/pkg/parser"
"github.com/zevenet/kube-nftlb/pkg/types"
"github.com/zevenet/kube-nftlb/pkg/watcher"
@ -60,12 +61,16 @@ func AddNftlbBackends(obj interface{}) {
}
log.WriteLog(types.StandardLog, fmt.Sprintf("AddNftlbBackends: Endpoints name: %s\n%s", ep.Name, nftlbJSON))
metrics.EndpointsChangesPending.Inc()
metrics.EndpointsChangesTotal.Inc()
// Get the response from that request
response, err := http.Send(&types.RequestData{
Method: "POST",
Path: "farms",
Body: strings.NewReader(nftlbJSON),
})
metrics.EndpointsChangesPending.Dec()
if err != nil {
log.WriteLog(types.ErrorLog, fmt.Sprintf("AddNftlbBackends: Endpoints name: %s\n%s", ep.Name, err.Error()))
return
@ -76,6 +81,7 @@ func AddNftlbBackends(obj interface{}) {
// DeleteNftlbBackends
func DeleteNftlbBackends(obj interface{}) {
metrics.EndpointsChangesTotal.Inc()
ep := obj.(*corev1.Endpoints)
pathsChan := make(chan string)

View File

@ -6,6 +6,7 @@ import (
"github.com/zevenet/kube-nftlb/pkg/http"
"github.com/zevenet/kube-nftlb/pkg/log"
"github.com/zevenet/kube-nftlb/pkg/metrics"
"github.com/zevenet/kube-nftlb/pkg/parser"
"github.com/zevenet/kube-nftlb/pkg/types"
"github.com/zevenet/kube-nftlb/pkg/watcher"
@ -56,12 +57,16 @@ func AddNftlbFarm(obj interface{}) {
return
}
metrics.ServicesChangesPending.Inc()
metrics.ServicesChangesTotal.Inc()
// Send that JSON data to nftlb
response, err := http.Send(&types.RequestData{
Method: "POST",
Path: "farms",
Body: strings.NewReader(nftlbJSON),
})
metrics.ServicesChangesPending.Dec()
if err != nil {
log.WriteLog(types.ErrorLog, fmt.Sprintf("AddNftlbFarms: Service name: %s\n%s", svc.Name, err.Error()))
return
@ -73,6 +78,7 @@ func AddNftlbFarm(obj interface{}) {
// DeleteNftlbFarm takes in a Service object (k8s) and deletes the farm related to the service and its addresses (nftlb).
func DeleteNftlbFarm(obj interface{}) {
metrics.ServicesChangesTotal.Inc()
svc := obj.(*corev1.Service)
// Make channel where paths will come through

19
pkg/metrics/endpoints.go Normal file
View File

@ -0,0 +1,19 @@
package metrics
import (
"github.com/prometheus/client_golang/prometheus"
)
var (
EndpointsChangesPending = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "kube_nftlb",
Name: "rules_endpoints_changes_pending",
Help: "How many Endpoints are pending to apply",
})
EndpointsChangesTotal = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "kube_nftlb",
Name: "rules_endpoints_changes_total",
Help: "How many Endpoints changes have happened",
})
)

45
pkg/metrics/server.go Normal file
View File

@ -0,0 +1,45 @@
package metrics
import (
"flag"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
defaultAddress = ":9195"
address = flag.String("listen-address", defaultAddress, "The address to listen on for HTTP requests.")
collectors = []prometheus.Collector{
EndpointsChangesPending,
EndpointsChangesTotal,
ServicesChangesPending,
ServicesChangesTotal,
}
)
func StartServer() {
flag.Parse()
if *address == "" {
*address = defaultAddress
}
for _, collector := range collectors {
prometheus.MustRegister(collector)
}
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>kube-nftlb Exporter</title></head>
<body>
<h1>kube-nftlb Exporter</h1>
<p><a href='/metrics'>Metrics</a></p>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(*address, nil))
}

19
pkg/metrics/services.go Normal file
View File

@ -0,0 +1,19 @@
package metrics
import (
"github.com/prometheus/client_golang/prometheus"
)
var (
ServicesChangesPending = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "kube_nftlb",
Name: "rules_services_changes_pending",
Help: "How many Services are pending to apply",
})
ServicesChangesTotal = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "kube_nftlb",
Name: "rules_services_changes_total",
Help: "How many Services changes have happened",
})
)

View File

@ -25,6 +25,8 @@ spec:
- name: kube-nftlb
image: zevenet/kube-nftlb
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9195
resources:
limits:
memory: 200Mi