From 2c3f963beb1654b7201b8358572aaa3b6db15112 Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Fri, 14 Sep 2018 13:34:58 -0500 Subject: [PATCH] issue #31 : emit more stats from proxy --- proxy/internal/service/init.go | 7 ++++++- proxy/internal/status/heartbeat/loop.go | 6 ++++++ proxy/internal/status/startup/sender.go | 12 +++++++++++- proxy/internal/web/mod_file.go | 17 ++++++++++++----- proxy/internal/web/mod_info.go | 17 ++++++++++++----- proxy/internal/web/mod_list.go | 16 +++++++++++----- proxy/internal/web/mod_zip.go | 18 ++++++++++++------ proxy/internal/web/router.go | 8 ++++---- 8 files changed, 74 insertions(+), 27 deletions(-) diff --git a/proxy/internal/service/init.go b/proxy/internal/service/init.go index b529219..97191cc 100644 --- a/proxy/internal/service/init.go +++ b/proxy/internal/service/init.go @@ -186,6 +186,7 @@ func initHeartbeatSender(p *Proxy) error { looper := heartbeat.NewLooper( 10*time.Second, p.index, + p.statter, sender, ) @@ -195,7 +196,11 @@ func initHeartbeatSender(p *Proxy) error { } func initStartupConfigSender(p *Proxy) error { - sender := startup.NewSender(p.registryClient, 30*time.Second) + sender := startup.NewSender( + p.registryClient, + 30*time.Second, + p.statter, + ) go sender.Send( payloads.Configuration{ Self: netservice.Instance{ diff --git a/proxy/internal/status/heartbeat/loop.go b/proxy/internal/status/heartbeat/loop.go index f62a9ae..38bd1cd 100644 --- a/proxy/internal/status/heartbeat/loop.go +++ b/proxy/internal/status/heartbeat/loop.go @@ -3,6 +3,7 @@ package heartbeat import ( "time" + "github.com/cactus/go-statsd-client/statsd" "github.com/shoenig/toolkit" "github.com/modprox/mp/pkg/loggy" @@ -16,12 +17,14 @@ type PokeLooper interface { func NewLooper( interval time.Duration, index store.Index, + statter statsd.Statter, sender Sender, ) PokeLooper { return &looper{ interval: interval, index: index, sender: sender, + statter: statter, log: loggy.New("heartbeat-looper"), } } @@ -30,6 +33,7 @@ type looper struct { interval time.Duration index store.Index sender Sender + statter statsd.Statter log loggy.Logger } @@ -51,9 +55,11 @@ func (l *looper) loop() error { numPackages, numModules, ); err != nil { + l.statter.Inc("heartbeat-send-failure", 1, 1) l.log.Warnf("could not send heartbeat, will try again later: %v", err) return nil // always nil, never stop } + l.statter.Inc("heartbeat-send-ok", 1, 1) return nil } diff --git a/proxy/internal/status/startup/sender.go b/proxy/internal/status/startup/sender.go index c570a66..de017e9 100644 --- a/proxy/internal/status/startup/sender.go +++ b/proxy/internal/status/startup/sender.go @@ -5,6 +5,8 @@ import ( "encoding/json" "time" + "github.com/cactus/go-statsd-client/statsd" + "github.com/modprox/mp/pkg/clients/payloads" "github.com/modprox/mp/pkg/clients/registry" "github.com/modprox/mp/pkg/loggy" @@ -22,13 +24,19 @@ type Sender interface { type sender struct { registryClient registry.Client retryInterval time.Duration + statter statsd.Statter log loggy.Logger } -func NewSender(registryClient registry.Client, retryInterval time.Duration) Sender { +func NewSender( + registryClient registry.Client, + retryInterval time.Duration, + statter statsd.Statter, +) Sender { return &sender{ registryClient: registryClient, retryInterval: retryInterval, + statter: statter, log: loggy.New("startup-config-sender"), } } @@ -63,9 +71,11 @@ func (s *sender) trySend(configuration payloads.Configuration) error { reader := bytes.NewReader(bs) response := bytes.NewBuffer(nil) if err := s.registryClient.Post(configurationPath, reader, response); err != nil { + s.statter.Inc("startup-config-send-failure", 1, 1) return err } s.log.Infof("startup configuration successfully sent!") + s.statter.Inc("startup-config-send-ok", 1, 1) return nil } diff --git a/proxy/internal/web/mod_file.go b/proxy/internal/web/mod_file.go index 7a1fd37..48879b4 100644 --- a/proxy/internal/web/mod_file.go +++ b/proxy/internal/web/mod_file.go @@ -3,20 +3,24 @@ package web import ( "net/http" + "github.com/cactus/go-statsd-client/statsd" + "github.com/modprox/mp/pkg/loggy" "github.com/modprox/mp/proxy/internal/modules/store" "github.com/modprox/mp/proxy/internal/web/output" ) type moduleFile struct { - index store.Index - log loggy.Logger + index store.Index + statter statsd.Statter + log loggy.Logger } -func modFile(index store.Index) http.Handler { +func modFile(index store.Index, statter statsd.Statter) http.Handler { return &moduleFile{ - index: index, - log: loggy.New("mod-file"), + index: index, + statter: statter, + log: loggy.New("mod-file"), } } @@ -24,6 +28,7 @@ func (h *moduleFile) ServeHTTP(w http.ResponseWriter, r *http.Request) { mod, err := modInfoFromPath(r.URL.Path) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) + h.statter.Inc("mod-file-bad-request", 1, 1) return } h.log.Infof("serving request for go.mod file of %s", mod) @@ -31,8 +36,10 @@ func (h *moduleFile) ServeHTTP(w http.ResponseWriter, r *http.Request) { modFile, err := h.index.Mod(mod) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) + h.statter.Inc("mod-file-not-found", 1, 1) return } output.Write(w, output.Text, modFile) + h.statter.Inc("mod-file-ok", 1, 1) } diff --git a/proxy/internal/web/mod_info.go b/proxy/internal/web/mod_info.go index 9dfb737..4bad98c 100644 --- a/proxy/internal/web/mod_info.go +++ b/proxy/internal/web/mod_info.go @@ -3,20 +3,24 @@ package web import ( "net/http" + "github.com/cactus/go-statsd-client/statsd" + "github.com/modprox/mp/pkg/loggy" "github.com/modprox/mp/proxy/internal/modules/store" "github.com/modprox/mp/proxy/internal/web/output" ) type moduleInfo struct { - log loggy.Logger - index store.Index + log loggy.Logger + statter statsd.Statter + index store.Index } -func modInfo(index store.Index) http.Handler { +func modInfo(index store.Index, statter statsd.Statter) http.Handler { return &moduleInfo{ - index: index, - log: loggy.New("mod-info"), + index: index, + statter: statter, + log: loggy.New("mod-info"), } } @@ -27,6 +31,7 @@ func (h *moduleInfo) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err != nil { h.log.Warnf("bad request for info: %v", err) http.Error(w, err.Error(), http.StatusBadRequest) + h.statter.Inc("mod-info-bad-request", 1, 1) return } @@ -35,9 +40,11 @@ func (h *moduleInfo) ServeHTTP(w http.ResponseWriter, r *http.Request) { revInfo, err := h.index.Info(mod) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) + h.statter.Inc("mod-info-not-found", 1, 1) return } content := revInfo.String() output.Write(w, output.JSON, content) + h.statter.Inc("mod-info-ok", 1, 1) } diff --git a/proxy/internal/web/mod_list.go b/proxy/internal/web/mod_list.go index 285d08d..f0b05e7 100644 --- a/proxy/internal/web/mod_list.go +++ b/proxy/internal/web/mod_list.go @@ -4,20 +4,24 @@ import ( "net/http" "strings" + "github.com/cactus/go-statsd-client/statsd" + "github.com/modprox/mp/pkg/loggy" "github.com/modprox/mp/proxy/internal/modules/store" "github.com/modprox/mp/proxy/internal/web/output" ) type moduleList struct { - index store.Index - log loggy.Logger + index store.Index + statter statsd.Statter + log loggy.Logger } -func modList(index store.Index) http.Handler { +func modList(index store.Index, statter statsd.Statter) http.Handler { return &moduleList{ - index: index, - log: loggy.New("mod-list"), + index: index, + statter: statter, + log: loggy.New("mod-list"), } } @@ -34,10 +38,12 @@ func (h *moduleList) ServeHTTP(w http.ResponseWriter, r *http.Request) { listing, err := h.index.Versions(module) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) + h.statter.Inc("mod-list-not-found", 1, 1) return } output.Write(w, output.Text, formatList(listing)) + h.statter.Inc("mod-list-ok", 1, 1) } func formatList(list []string) string { diff --git a/proxy/internal/web/mod_zip.go b/proxy/internal/web/mod_zip.go index d2c39f8..1ed8eef 100644 --- a/proxy/internal/web/mod_zip.go +++ b/proxy/internal/web/mod_zip.go @@ -3,20 +3,24 @@ package web import ( "net/http" + "github.com/cactus/go-statsd-client/statsd" + "github.com/modprox/mp/pkg/loggy" "github.com/modprox/mp/proxy/internal/modules/store" "github.com/modprox/mp/proxy/internal/web/output" ) type moduleZip struct { - store store.ZipStore - log loggy.Logger + store store.ZipStore + statter statsd.Statter + log loggy.Logger } -func modZip(store store.ZipStore) http.Handler { +func modZip(store store.ZipStore, statter statsd.Statter) http.Handler { return &moduleZip{ - store: store, - log: loggy.New("mod-zip"), + store: store, + statter: statter, + log: loggy.New("mod-zip"), } } @@ -26,6 +30,7 @@ func (h *moduleZip) ServeHTTP(w http.ResponseWriter, r *http.Request) { mod, err := modInfoFromPath(r.URL.Path) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) + h.statter.Inc("mod-zip-bad-request", 1, 1) return } @@ -35,10 +40,11 @@ func (h *moduleZip) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err != nil { h.log.Warnf("failed to get zip file of %s, %v", mod, err) http.Error(w, err.Error(), http.StatusNotFound) + h.statter.Inc("mod-zip-not-found", 1, 1) return } h.log.Infof("sending zip which is %d bytes", len(zipBlob)) - output.WriteZip(w, zipBlob) + h.statter.Inc("mod-zip-ok", 1, 1) } diff --git a/proxy/internal/web/router.go b/proxy/internal/web/router.go index d443721..8444f9b 100644 --- a/proxy/internal/web/router.go +++ b/proxy/internal/web/router.go @@ -26,10 +26,10 @@ func NewRouter( // e.g. GET http://localhost:9000/github.com/shoenig/toolkit/@v/v1.0.0.info - router.PathPrefix("/").Handler(modList(index)).MatcherFunc(suffix("list")).Methods(get) - router.PathPrefix("/").Handler(modInfo(index)).MatcherFunc(suffix(".info")).Methods(get) - router.PathPrefix("/").Handler(modFile(index)).MatcherFunc(suffix(".mod")).Methods(get) - router.PathPrefix("/").Handler(modZip(store)).MatcherFunc(suffix(".zip")).Methods(get) + router.PathPrefix("/").Handler(modList(index, statter)).MatcherFunc(suffix("list")).Methods(get) + router.PathPrefix("/").Handler(modInfo(index, statter)).MatcherFunc(suffix(".info")).Methods(get) + router.PathPrefix("/").Handler(modFile(index, statter)).MatcherFunc(suffix(".mod")).Methods(get) + router.PathPrefix("/").Handler(modZip(store, statter)).MatcherFunc(suffix(".zip")).Methods(get) router.PathPrefix("/").HandlerFunc(notFound(statter)) return router