generated from okp4/template-go
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
9 changed files
with
107 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters