refactoring of code
This commit is contained in:
@@ -19,6 +19,7 @@ import (
|
|||||||
"github.com/jkaninda/goma-gateway/pkg/logger"
|
"github.com/jkaninda/goma-gateway/pkg/logger"
|
||||||
"github.com/jkaninda/goma-gateway/util"
|
"github.com/jkaninda/goma-gateway/util"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -72,7 +73,12 @@ func (oauth *OauthRulerMiddleware) getUserInfo(token *oauth2.Token) (UserInfo, e
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return UserInfo{}, err
|
return UserInfo{}, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer func(Body io.ReadCloser) {
|
||||||
|
err := Body.Close()
|
||||||
|
if err != nil {
|
||||||
|
|
||||||
|
}
|
||||||
|
}(resp.Body)
|
||||||
|
|
||||||
// Parse the user info
|
// Parse the user info
|
||||||
var userInfo UserInfo
|
var userInfo UserInfo
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package pkg
|
package metrics
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
@@ -26,11 +26,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type PrometheusRoute struct {
|
type PrometheusRoute struct {
|
||||||
name string
|
Name string
|
||||||
path string
|
Path string
|
||||||
}
|
}
|
||||||
|
|
||||||
var totalRequests = prometheus.NewCounterVec(
|
var TotalRequests = prometheus.NewCounterVec(
|
||||||
prometheus.CounterOpts{
|
prometheus.CounterOpts{
|
||||||
Name: "http_requests_total",
|
Name: "http_requests_total",
|
||||||
Help: "Number of get requests.",
|
Help: "Number of get requests.",
|
||||||
@@ -38,7 +38,7 @@ var totalRequests = prometheus.NewCounterVec(
|
|||||||
[]string{"name", "path"},
|
[]string{"name", "path"},
|
||||||
)
|
)
|
||||||
|
|
||||||
var responseStatus = prometheus.NewCounterVec(
|
var ResponseStatus = prometheus.NewCounterVec(
|
||||||
prometheus.CounterOpts{
|
prometheus.CounterOpts{
|
||||||
Name: "response_status",
|
Name: "response_status",
|
||||||
Help: "Status of HTTP response",
|
Help: "Status of HTTP response",
|
||||||
@@ -46,22 +46,22 @@ var responseStatus = prometheus.NewCounterVec(
|
|||||||
[]string{"status"},
|
[]string{"status"},
|
||||||
)
|
)
|
||||||
|
|
||||||
var httpDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
|
var HttpDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
|
||||||
Name: "http_response_time_seconds",
|
Name: "http_response_time_seconds",
|
||||||
Help: "Duration of HTTP requests.",
|
Help: "Duration of HTTP requests.",
|
||||||
}, []string{"name", "path"})
|
}, []string{"name", "path"})
|
||||||
|
|
||||||
func (pr PrometheusRoute) prometheusMiddleware(next http.Handler) http.Handler {
|
func (pr PrometheusRoute) PrometheusMiddleware(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
path := pr.path
|
path := pr.Path
|
||||||
if len(path) == 0 {
|
if len(path) == 0 {
|
||||||
route := mux.CurrentRoute(r)
|
route := mux.CurrentRoute(r)
|
||||||
path, _ = route.GetPathTemplate()
|
path, _ = route.GetPathTemplate()
|
||||||
}
|
}
|
||||||
timer := prometheus.NewTimer(httpDuration.WithLabelValues(pr.name, path))
|
timer := prometheus.NewTimer(HttpDuration.WithLabelValues(pr.Name, path))
|
||||||
|
|
||||||
responseStatus.WithLabelValues(strconv.Itoa(http.StatusOK)).Inc()
|
ResponseStatus.WithLabelValues(strconv.Itoa(http.StatusOK)).Inc()
|
||||||
totalRequests.WithLabelValues(pr.name, path).Inc()
|
TotalRequests.WithLabelValues(pr.Name, path).Inc()
|
||||||
|
|
||||||
timer.ObserveDuration()
|
timer.ObserveDuration()
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
@@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
*/
|
*/
|
||||||
import (
|
import (
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/jkaninda/goma-gateway/internal/metrics"
|
||||||
"github.com/jkaninda/goma-gateway/internal/middlewares"
|
"github.com/jkaninda/goma-gateway/internal/middlewares"
|
||||||
"github.com/jkaninda/goma-gateway/pkg/logger"
|
"github.com/jkaninda/goma-gateway/pkg/logger"
|
||||||
"github.com/jkaninda/goma-gateway/util"
|
"github.com/jkaninda/goma-gateway/util"
|
||||||
@@ -26,9 +27,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
_ = prometheus.Register(totalRequests)
|
_ = prometheus.Register(metrics.TotalRequests)
|
||||||
_ = prometheus.Register(responseStatus)
|
_ = prometheus.Register(metrics.ResponseStatus)
|
||||||
_ = prometheus.Register(httpDuration)
|
_ = prometheus.Register(metrics.HttpDuration)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize the routes
|
// Initialize the routes
|
||||||
@@ -265,12 +266,12 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router {
|
|||||||
router.PathPrefix("").Handler(proxyRoute.ProxyHandler())
|
router.PathPrefix("").Handler(proxyRoute.ProxyHandler())
|
||||||
}
|
}
|
||||||
if gateway.EnableMetrics {
|
if gateway.EnableMetrics {
|
||||||
pr := PrometheusRoute{
|
pr := metrics.PrometheusRoute{
|
||||||
name: route.Name,
|
Name: route.Name,
|
||||||
path: route.Path,
|
Path: route.Path,
|
||||||
}
|
}
|
||||||
// Prometheus endpoint
|
// Prometheus endpoint
|
||||||
router.Use(pr.prometheusMiddleware)
|
router.Use(pr.PrometheusMiddleware)
|
||||||
}
|
}
|
||||||
// Apply route Error interceptor middlewares
|
// Apply route Error interceptor middlewares
|
||||||
if len(route.InterceptErrors) != 0 {
|
if len(route.InterceptErrors) != 0 {
|
||||||
|
|||||||
Reference in New Issue
Block a user