Skip to content

Commit

Permalink
feat: prometheus metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
ingvaar committed May 10, 2022
1 parent 3810d8e commit c6e3f84
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 14 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/gorilla/mux v1.8.0
github.com/rs/zerolog v1.26.1
github.com/smartystreets/goconvey v1.7.2
github.com/prometheus/client_golang v1.12.1
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.11.0
Expand Down Expand Up @@ -77,7 +78,6 @@ require (
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
Expand Down
11 changes: 11 additions & 0 deletions pkg/server/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package server

import "net/http"

// NewHealthRequestHandlerFunc returns a REST handler func to check app status
func NewHealthRequestHandlerFunc() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(""))
}
}
92 changes: 80 additions & 12 deletions pkg/server/prometheus.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,90 @@
package server

import (
"context"
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
"strconv"

"okp4/cosmos-faucet/pkg/client"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

// NewHealthRequestHandlerFunc returns a REST handler func to check app status
func NewHealthRequestHandlerFunc() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(""))
}
type responseWriter struct {
http.ResponseWriter
statusCode int
}

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

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

type prometheusHandler struct {
totalRequests *prometheus.CounterVec
responseStatus *prometheus.CounterVec
httpDuration *prometheus.HistogramVec
}

func (p prometheusHandler) init() {
_ = prometheus.Register(p.totalRequests)
_ = prometheus.Register(p.responseStatus)
_ = prometheus.Register(p.httpDuration)
}

func newPrometheusHandler() prometheusHandler {
handler := prometheusHandler{
totalRequests: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Number of get requests.",
}, []string{"path"},
),
responseStatus: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "response_status",
Help: "Status of HTTP response",
},
[]string{"status"},
),
httpDuration: promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "http_response_time_seconds",
Help: "Duration of HTTP requests.",
}, []string{"path"}),
}
handler.init()
return handler
}

func prometheusMiddleware(handler http.Handler) http.Handler {
promHandler := newPrometheusHandler()

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
route := mux.CurrentRoute(r)
path, _ := route.GetPathTemplate()

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

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

statusCode := rw.statusCode

if !(path == "/metrics" || path == "/health") {
promHandler.responseStatus.WithLabelValues(strconv.Itoa(statusCode)).Inc()
promHandler.totalRequests.WithLabelValues(path).Inc()
timer.ObserveDuration()
}
})
}

// NewMetricsRequestHandlerFunc returns a REST handler func returning useful prometheus metrics
func NewMetricsRequestHandlerFunc(ctx context.Context, faucet *client.Faucet) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
}
func NewMetricsRequestHandlerFunc() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
promhttp.Handler()
}
}
3 changes: 2 additions & 1 deletion pkg/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

func (s httpServer) createRoutes(faucet *client.Faucet) {
s.router.Use(prometheusMiddleware)
s.router.Path("/").
Queries("address", "{address}").
HandlerFunc(NewSendRequestHandlerFn(context.Background(), faucet)).
Expand All @@ -15,6 +16,6 @@ func (s httpServer) createRoutes(faucet *client.Faucet) {
HandlerFunc(NewHealthRequestHandlerFunc()).
Methods("GET")
s.router.Path("/metrics").
HandlerFunc(NewMetricsRequestHandlerFunc(context.Background(), faucet)).
HandlerFunc(NewMetricsRequestHandlerFunc()).
Methods("GET")
}

0 comments on commit c6e3f84

Please sign in to comment.