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 metrics from the registry
Browse files Browse the repository at this point in the history
  • Loading branch information
Seth Hoenig committed Sep 14, 2018
1 parent 2c3f963 commit 34d3321
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 58 deletions.
24 changes: 15 additions & 9 deletions registry/internal/web/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"html/template"
"net/http"

"github.com/cactus/go-statsd-client/statsd"

"github.com/modprox/mp/pkg/clients/payloads"
"github.com/modprox/mp/pkg/loggy"
"github.com/modprox/mp/proxy/config"
Expand All @@ -23,38 +25,40 @@ type homePage struct {
}

type homeHandler struct {
html *template.Template
store data.Store
log loggy.Logger
html *template.Template
store data.Store
statter statsd.Statter
log loggy.Logger
}

func newHomeHandler(store data.Store) http.Handler {
func newHomeHandler(store data.Store, statter statsd.Statter) http.Handler {
html := static.MustParseTemplates(
"static/html/layout.html",
"static/html/navbar.html",
"static/html/home.html",
)
return &homeHandler{
html: html,
store: store,
log: loggy.New("home-handler"),
html: html,
store: store,
statter: statter,
log: loggy.New("home-handler"),
}
}

func (h *homeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.log.Tracef("serving up the homepage, path: %s", r.URL.Path)

configs, err := h.store.ListStartConfigs()
if err != nil {
http.Error(w, "failed to list proxy configs", http.StatusInternalServerError)
h.log.Errorf("failed to list proxy configs: %v", err)
h.statter.Inc("ui-home-error", 1, 1)
return
}

heartbeats, err := h.store.ListHeartbeats()
if err != nil {
http.Error(w, "failed to list proxy heartbeats", http.StatusInternalServerError)
h.log.Errorf("failed to list proxy heartbeats: %v", err)
h.statter.Inc("ui-home-error", 1, 1)
return
}

Expand All @@ -81,6 +85,8 @@ func (h *homeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.log.Errorf("failed to execute homepage template: %v", err)
return
}

h.statter.Inc("ui-home-ok", 1, 1)
}

func transformsText(t config.Transforms) string {
Expand Down
23 changes: 14 additions & 9 deletions registry/internal/web/mods_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"strings"

"github.com/cactus/go-statsd-client/statsd"
"github.com/gorilla/csrf"

"github.com/modprox/mp/pkg/coordinates"
Expand All @@ -22,28 +23,28 @@ type newPage struct {
}

type newHandler struct {
html *template.Template
store data.Store
log loggy.Logger
html *template.Template
store data.Store
statter statsd.Statter
log loggy.Logger
}

func newAddHandler(store data.Store) http.Handler {
func newAddHandler(store data.Store, statter statsd.Statter) http.Handler {
html := static.MustParseTemplates(
"static/html/layout.html",
"static/html/navbar.html",
"static/html/mods_add.html",
)

return &newHandler{
html: html,
store: store,
log: loggy.New("add-modules-handler"),
html: html,
store: store,
statter: statter,
log: loggy.New("add-modules-handler"),
}
}

func (h *newHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.log.Tracef("loaded page %v", r.Method)

var (
code int
err error
Expand All @@ -60,12 +61,16 @@ func (h *newHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err != nil {
h.log.Errorf("failed to serve add-module page: %v", err)
http.Error(w, err.Error(), code)
h.statter.Inc("ui-add-mod-error", 1, 1)
return
}

if err := h.html.Execute(w, page); err != nil {
h.log.Errorf("failed to execute add-module page: %v", err)
return
}

h.statter.Inc("ui-add-mod-ok", 1, 1)
}

func (h *newHandler) get(r *http.Request) (int, *newPage, error) {
Expand Down
23 changes: 14 additions & 9 deletions registry/internal/web/mods_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net/http"
"sort"

"github.com/cactus/go-statsd-client/statsd"

"github.com/modprox/mp/pkg/coordinates"
"github.com/modprox/mp/pkg/loggy"
"github.com/modprox/mp/registry/internal/data"
Expand All @@ -16,39 +18,42 @@ type modsListPage struct {
}

type modsListHandler struct {
html *template.Template
store data.Store
log loggy.Logger
html *template.Template
store data.Store
statter statsd.Statter
log loggy.Logger
}

func newModsListHandler(store data.Store) http.Handler {
func newModsListHandler(store data.Store, statter statsd.Statter) http.Handler {
html := static.MustParseTemplates(
"static/html/layout.html",
"static/html/navbar.html",
"static/html/mods_list.html",
)

return &modsListHandler{
html: html,
store: store,
log: loggy.New("list-modules-handler"),
html: html,
store: store,
statter: statter,
log: loggy.New("list-modules-handler"),
}
}

func (h *modsListHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.log.Tracef("loaded page %v", r.Method)

code, page, err := h.get(r)
if err != nil {
h.log.Errorf("failed to serve modules list page")
http.Error(w, err.Error(), code)
h.statter.Inc("ui-list-mods-error", 1, 1)
return
}

if err := h.html.Execute(w, page); err != nil {
h.log.Errorf("failed to execute modules list page")
return
}

h.statter.Inc("ui-list-mods-ok", 1, 1)
}

func (h *modsListHandler) get(r *http.Request) (int, *modsListPage, error) {
Expand Down
20 changes: 13 additions & 7 deletions registry/internal/web/mods_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"html/template"
"net/http"

"github.com/cactus/go-statsd-client/statsd"
"github.com/modprox/mp/pkg/coordinates"
"github.com/modprox/mp/pkg/loggy"
"github.com/modprox/mp/registry/internal/data"
Expand All @@ -17,22 +18,24 @@ type showPage struct {
}

type showHandler struct {
html *template.Template
store data.Store
log loggy.Logger
html *template.Template
store data.Store
statter statsd.Statter
log loggy.Logger
}

func newShowHandler(store data.Store) http.Handler {
func newShowHandler(store data.Store, statter statsd.Statter) http.Handler {
html := static.MustParseTemplates(
"static/html/layout.html",
"static/html/navbar.html",
"static/html/mods_show.html",
)

return &showHandler{
html: html,
store: store,
log: loggy.New("show-module-h"),
html: html,
store: store,
statter: statter,
log: loggy.New("show-module-h"),
}
}

Expand All @@ -42,12 +45,15 @@ func (h *showHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err != nil {
h.log.Errorf("failed to serve show modules page: %v", err)
http.Error(w, err.Error(), code)
h.statter.Inc("ui-show-mod-error", 1, 1)
return
}

if err := h.html.Execute(w, page); err != nil {
h.log.Errorf("failed to execute show modules page: %v", err)
}

h.statter.Inc("ui-show-mod-ok", 1, 1)
}

func (h *showHandler) get(r *http.Request) (int, *showPage, error) {
Expand Down
22 changes: 11 additions & 11 deletions registry/internal/web/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewRouter(
router.Handle("/v1/", routeAPI(store, statter))

// 4) a webUI handler, is CSRF protected
router.Handle("/", routeWebUI(csrfConfig, store))
router.Handle("/", routeWebUI(csrfConfig, store, statter))

return router
}
Expand All @@ -51,22 +51,22 @@ func routeStatics(files http.Handler) http.Handler {
return sub
}

func routeAPI(store data.Store, emitter statsd.Statter) http.Handler {
func routeAPI(store data.Store, stats statsd.Statter) http.Handler {
sub := mux.NewRouter()
sub.Handle("/v1/registry/sources/list", newRegistryList(store)).Methods(get, post)
sub.Handle("/v1/registry/sources/new", registryAdd(store)).Methods(post)
sub.Handle("/v1/proxy/heartbeat", newHeartbeatHandler(store, emitter)).Methods(post)
sub.Handle("/v1/proxy/configuration", newStartupHandler(store)).Methods(post)
sub.Handle("/v1/registry/sources/list", newRegistryList(store, stats)).Methods(get, post)
sub.Handle("/v1/registry/sources/new", registryAdd(store, stats)).Methods(post)
sub.Handle("/v1/proxy/heartbeat", newHeartbeatHandler(store, stats)).Methods(post)
sub.Handle("/v1/proxy/configuration", newStartupHandler(store, stats)).Methods(post)
return sub
}

func routeWebUI(csrfConfig config.CSRF, store data.Store) http.Handler {
func routeWebUI(csrfConfig config.CSRF, store data.Store, stats statsd.Statter) http.Handler {
sub := mux.NewRouter()
sub.Handle("/mods/new", newAddHandler(store)).Methods(get, post)
sub.Handle("/mods/list", newModsListHandler(store)).Methods(get)
sub.Handle("/mods/show", newShowHandler(store)).Methods(get)
sub.Handle("/mods/new", newAddHandler(store, stats)).Methods(get, post)
sub.Handle("/mods/list", newModsListHandler(store, stats)).Methods(get)
sub.Handle("/mods/show", newShowHandler(store, stats)).Methods(get)
// sub.Handle("/configure/redirects", newRedirectsHandler(store)).Methods(get)
sub.Handle("/", newHomeHandler(store)).Methods(get, post)
sub.Handle("/", newHomeHandler(store, stats)).Methods(get, post)
return chain(sub,
[]middleware{csrf.Protect(
// the key is used to generate csrf tokens to hand
Expand Down
4 changes: 2 additions & 2 deletions registry/internal/web/v1_heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ func (h *heartbeatHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err != nil {
h.log.Errorf("failed to accept heartbeat from %s, %v", from, err)
http.Error(w, response, code)
h.statter.Inc("heartbeat-unaccepted", 1, 1)
h.statter.Inc("api-heartbeat-unaccepted", 1, 1)
return
}

h.log.Tracef("accepted heartbeat from %s", from)
webutil.WriteJSON(w, response)
h.statter.Inc("heartbeat-accepted", 1, 1)
h.statter.Inc("api-heartbeat-accepted", 1, 1)
}

func (h *heartbeatHandler) post(r *http.Request) (int, string, netservice.Instance, error) {
Expand Down
7 changes: 6 additions & 1 deletion registry/internal/web/v1_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"fmt"
"net/http"

"github.com/cactus/go-statsd-client/statsd"

"github.com/modprox/mp/pkg/coordinates"
"github.com/modprox/mp/pkg/loggy"
"github.com/modprox/mp/pkg/webutil"
"github.com/modprox/mp/registry/internal/data"
)

func registryAdd(store data.Store) http.HandlerFunc {
func registryAdd(store data.Store, statter statsd.Statter) http.HandlerFunc {
log := loggy.New("registry-add-api")

return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -21,16 +23,19 @@ func registryAdd(store data.Store) http.HandlerFunc {

if err := json.NewDecoder(r.Body).Decode(&wantToAdd); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
statter.Inc("api-addmod-bad-request", 1, 1)
return
}

modulesAdded, err := store.InsertModules(wantToAdd)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
statter.Inc("api-addmod-error", 1, 1)
return
}

msg := fmt.Sprintf("added %d new modules", modulesAdded)
webutil.WriteJSON(w, msg)
statter.Inc("api-addmod-ok", 1, 1)
}
}
16 changes: 11 additions & 5 deletions registry/internal/web/v1_registry_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"net/http"

"github.com/cactus/go-statsd-client/statsd"

"github.com/modprox/mp/pkg/clients/registry"
"github.com/modprox/mp/pkg/coordinates"
"github.com/modprox/mp/pkg/loggy"
Expand All @@ -12,14 +14,16 @@ import (
)

type registryList struct {
store data.Store
log loggy.Logger
store data.Store
statter statsd.Statter
log loggy.Logger
}

func newRegistryList(store data.Store) http.Handler {
func newRegistryList(store data.Store, statter statsd.Statter) http.Handler {
return &registryList{
store: store,
log: loggy.New("registry-list-api"),
store: store,
statter: statter,
log: loggy.New("registry-list-api"),
}
}

Expand All @@ -36,6 +40,7 @@ func (h *registryList) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if send.err != nil {
h.log.Errorf("failed to serve request: %v", send.err)
http.Error(w, send.err.Error(), send.code)
h.statter.Inc("api-listmods-error", 1, 1)
return
}

Expand All @@ -44,6 +49,7 @@ func (h *registryList) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

webutil.WriteJSON(w, response)
h.statter.Inc("api-listmods-ok", 1, 1)
}

type toSend struct {
Expand Down
Loading

0 comments on commit 34d3321

Please sign in to comment.