From 574ced6e367d93ca3583c1b5897a4e4ba2fa39cd Mon Sep 17 00:00:00 2001 From: "Stephen McQuay (smcquay)" Date: Thu, 8 Mar 2018 13:45:07 -0800 Subject: [PATCH] Add prometheus metrics --- main.go | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index c7f1aed..170f9de 100644 --- a/main.go +++ b/main.go @@ -4,11 +4,15 @@ import ( "encoding/json" "fmt" "log" + "math/rand" "net/http" "os" + + "github.com/prometheus/client_golang/prometheus/promhttp" + "mcquay.me/metrics" ) -const version = "v0.0.11" +const version = "v0.1.0" type v struct { Hostname string `json:"hostname"` @@ -16,11 +20,15 @@ type v struct { } func main() { + m, err := metrics.New("hw") + if err != nil { + log.Fatalf("metrics: %v", err) + } hn, err := os.Hostname() if err != nil { log.Fatalf("hostname: %+v", err) } - http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { + http.HandleFunc("/", m.WrapFunc("/", func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") r := v{ Hostname: hn, @@ -29,14 +37,25 @@ func main() { if err := json.NewEncoder(w).Encode(r); err != nil { log.Printf("json: %+v", err) } - }) - http.HandleFunc("/env", func(w http.ResponseWriter, req *http.Request) { + })) + http.HandleFunc("/env", m.WrapFunc("/env", func(w http.ResponseWriter, req *http.Request) { for _, line := range os.Environ() { fmt.Fprintf(w, "%v\n", line) } - }) - http.HandleFunc("/live", ok) - http.HandleFunc("/ready", ok) + })) + + codes := []int{ + http.StatusBadGateway, + http.StatusBadRequest, + http.StatusUnauthorized, + } + http.HandleFunc("/bad", m.WrapFunc("/bad", func(w http.ResponseWriter, req *http.Request) { + code := codes[rand.Intn(len(codes))] + w.WriteHeader(code) + })) + http.HandleFunc("/live", m.WrapFunc("/live", ok)) + http.HandleFunc("/ready", m.WrapFunc("/ready", ok)) + http.Handle("/metrics", promhttp.Handler()) if err := http.ListenAndServe(":8080", nil); err != nil { log.Fatalf("listen and serve: %v", err) }