Skip to content
This repository was archived by the owner on Jan 16, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/logicmonitor/k8s-argus/pkg"
"github.com/logicmonitor/k8s-argus/pkg/constants"
"github.com/logicmonitor/k8s-argus/pkg/healthz"
"github.com/logicmonitor/k8s-argus/pkg/metrics"

"github.com/logicmonitor/k8s-argus/pkg/config"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -52,9 +51,8 @@ var watchCmd = &cobra.Command{
// Invoke the watcher.
argus.Watch()

// Monitoring metrics.
// Health check.
http.HandleFunc("/healthz", healthz.HandleFunc)
http.HandleFunc("/metrics", metrics.HandleFunc)

log.Fatal(http.ListenAndServe(":8080", nil))
},
Expand Down
37 changes: 11 additions & 26 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,35 @@
package metrics

import (
"encoding/json"
"net/http"
"expvar"
"runtime"
"sync"

log "github.com/sirupsen/logrus"
)

var (
m *metrics
m *expvar.Map
once sync.Once
)

type metrics struct {
APIErrors uint
RESTErrors uint
}

func init() {
once.Do(func() {
m = &metrics{
APIErrors: 0,
RESTErrors: 0,
}
m = expvar.NewMap("errors")
m.Add("APIErrors", 0)
m.Add("RESTErrors", 0)
})
expvar.Publish("goroutines", expvar.Func(goroutines))
}

// APIError increments the API error count by 1.
func APIError() {
m.APIErrors++
m.Add("APIErrors", 1)
}

// RESTError increments the REST error count by 1.
func RESTError() {
m.RESTErrors++
m.Add("RESTErrors", 1)
}

// HandleFunc is an http handler function to expose Argus metrics.
func HandleFunc(w http.ResponseWriter, req *http.Request) {
resp, err := json.Marshal(m)
if err != nil {
log.Println(err)
}
_, err = w.Write(resp)
if err != nil {
log.Errorf("Failed to write metrics: %v", err)
}
func goroutines() interface{} {
return runtime.NumGoroutine()
}