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