Skip to content

Commit

Permalink
feat: reimplemntation of metrics
Browse files Browse the repository at this point in the history
remove response_status_total counter
rework http_requests_total
move server to internal package
expose pkg handlers from server sub-package
move server handlers in handlers sub-package
  • Loading branch information
ingvaar committed May 10, 2022
1 parent 6f92e73 commit cb2ee0c
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 126 deletions.
5 changes: 2 additions & 3 deletions cmd/start.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cmd

import (
"okp4/cosmos-faucet/internal/server"
"okp4/cosmos-faucet/pkg/client"
"okp4/cosmos-faucet/pkg/server"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -30,8 +30,7 @@ func NewStartCommand() *cobra.Command {
log.Info().Msg("Server stopped")
}(faucet)

httpServer := server.NewServer(faucet)
httpServer.Start(addr)
server.NewServer(faucet).Start(addr)
},
}

Expand Down
18 changes: 18 additions & 0 deletions internal/server/handlers/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package handlers

import (
"fmt"
"net/http"

"github.com/rs/zerolog/log"
)

// NewHealthRequestHandlerFunc only returns 200 OK to check app status.
func NewHealthRequestHandlerFunc() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte("")); err != nil {
log.Error().Msg(fmt.Sprintf("Error while sending health response: %v", err))
}
}
}
64 changes: 64 additions & 0 deletions internal/server/handlers/prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package handlers

import (
"net/http"
"strconv"

"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

type responseWriter struct {
http.ResponseWriter
statusCode int
}

func newResponseWriter(w http.ResponseWriter) *responseWriter {
return &responseWriter{w, http.StatusOK}
}

// PrometheusMiddleware adds prometheus metrics on every request to a router.
func PrometheusMiddleware(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
route := mux.CurrentRoute(r)
path, _ := route.GetPathTemplate()
methods, _ := route.GetMethods()

timer := prometheus.NewTimer(httpDuration.WithLabelValues(path))

rw := newResponseWriter(w)
handler.ServeHTTP(rw, r)

statusCode := rw.statusCode

totalRequests.WithLabelValues(methods[0], path, strconv.Itoa(statusCode)).Inc()
timer.ObserveDuration()
})
}

// NewMetricsRequestHandler exposes prometheus metrics.
func NewMetricsRequestHandler() http.Handler {
return promhttp.Handler()
}

func (rw *responseWriter) WriteHeader(code int) {
rw.statusCode = code
rw.ResponseWriter.WriteHeader(code)
}

var totalRequests = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "HTTP requests counter.",
}, []string{"method", "path", "code"},
)

var httpDuration = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Help: "Duration of HTTP requests.",
},
[]string{"path"},
)
21 changes: 21 additions & 0 deletions internal/server/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package server

import (
"okp4/cosmos-faucet/internal/server/handlers"
"okp4/cosmos-faucet/pkg/client"
"okp4/cosmos-faucet/pkg/server"
)

func (s *httpServer) createRoutes(faucet *client.Faucet) {
s.router.Use(handlers.PrometheusMiddleware)
s.router.Path("/").
Queries("address", "{address}").
HandlerFunc(server.NewSendRequestHandlerFn(faucet)).
Methods("GET")
s.router.Path("/health").
HandlerFunc(handlers.NewHealthRequestHandlerFunc()).
Methods("GET")
s.router.Path("/metrics").
Handler(handlers.NewMetricsRequestHandler()).
Methods("GET")
}
2 changes: 0 additions & 2 deletions pkg/server/server.go → internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package server

import (
"net/http"

"okp4/cosmos-faucet/pkg/client"

"github.com/gorilla/mux"
Expand All @@ -24,7 +23,6 @@ func NewServer(faucet *client.Faucet) HTTPServer {
router: mux.NewRouter().StrictSlash(true),
}
server.createRoutes(faucet)
initPrometheus()
return server
}

Expand Down
17 changes: 0 additions & 17 deletions pkg/server/health.go

This file was deleted.

86 changes: 0 additions & 86 deletions pkg/server/prometheus.go

This file was deleted.

17 changes: 0 additions & 17 deletions pkg/server/router.go

This file was deleted.

3 changes: 2 additions & 1 deletion pkg/server/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"github.com/rs/zerolog/log"
)

func newSendRequestHandlerFn(faucet *client.Faucet) http.HandlerFunc {
// NewSendRequestHandlerFn returns an HTTP REST handler for make transaction to a given address.
func NewSendRequestHandlerFn(faucet *client.Faucet) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bech32Addr := vars["address"]
Expand Down

0 comments on commit cb2ee0c

Please sign in to comment.