Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
issue #31 : emit more stats from proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Seth Hoenig committed Sep 14, 2018
1 parent 81dd0d3 commit 2c3f963
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 27 deletions.
7 changes: 6 additions & 1 deletion proxy/internal/service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ func initHeartbeatSender(p *Proxy) error {
looper := heartbeat.NewLooper(
10*time.Second,
p.index,
p.statter,
sender,
)

Expand All @@ -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{
Expand Down
6 changes: 6 additions & 0 deletions proxy/internal/status/heartbeat/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"),
}
}
Expand All @@ -30,6 +33,7 @@ type looper struct {
interval time.Duration
index store.Index
sender Sender
statter statsd.Statter
log loggy.Logger
}

Expand All @@ -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
}
12 changes: 11 additions & 1 deletion proxy/internal/status/startup/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"),
}
}
Expand Down Expand Up @@ -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
}
17 changes: 12 additions & 5 deletions proxy/internal/web/mod_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,43 @@ 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"),
}
}

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)

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)
}
17 changes: 12 additions & 5 deletions proxy/internal/web/mod_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}

Expand All @@ -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
}

Expand All @@ -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)
}
16 changes: 11 additions & 5 deletions proxy/internal/web/mod_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}

Expand All @@ -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 {
Expand Down
18 changes: 12 additions & 6 deletions proxy/internal/web/mod_zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}

Expand All @@ -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
}

Expand All @@ -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)
}
8 changes: 4 additions & 4 deletions proxy/internal/web/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2c3f963

Please sign in to comment.