Skip to content

Commit

Permalink
Fix Prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
darland committed Jul 10, 2024
1 parent 4c3ac41 commit c2a6d0f
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/webdevelop-pro/go-common/server/route"
)

const component = "http-server"
const component = "http_server"

type HTTPServer struct {
Echo *echo.Echo
Expand All @@ -30,8 +30,10 @@ type HTTPServer struct {
func InitAndRun() fx.Option {
return fx.Module(component,
// Init http server
fx.Provide(NewServerWithMiddlewares),
fx.Provide(NewServer),
fx.Invoke(
//
AddDefaultMiddlewares,
// Registration routes and handlers for http server
InitHandlerGroups,
// Run HTTP server
Expand All @@ -40,9 +42,9 @@ func InitAndRun() fx.Option {
)
}
func (s *HTTPServer) InitRoutes(rg route.Configurator) {
for _, route := range rg.GetRoutes() {
for _, r := range rg.GetRoutes() {
//nolint:gosec,scopelint
s.AddRoute(&route)
s.AddRoute(&r)
}
}

Expand Down Expand Up @@ -98,36 +100,25 @@ func NewServer() *HTTPServer {
}
}

// NewServerWithMiddlewares returns new API instance with default middlewares
func NewServerWithMiddlewares() *HTTPServer {
server := NewServer()
AddDefaultMiddlewares(server.Echo)
AddPrometheus(server.Echo)
return server
func AddPrometheus(srv *HTTPServer) {
srv.Echo.Use(echoprometheus.NewMiddleware(component))
srv.Echo.GET("/metrics", echoprometheus.NewHandler())
}

func AddPrometheus(e *echo.Echo) *echo.Echo {
// Add prometheus metrics
e.Use(echoprometheus.NewMiddleware(component))
e.GET("/metrics", echoprometheus.NewHandler())
return e
}

func AddDefaultMiddlewares(e *echo.Echo) *echo.Echo {
func AddDefaultMiddlewares(srv *HTTPServer) {
// Set context logger
e.Use(middleware.SetIPAddress)
e.Use(middleware.SetRequestTime)
e.Use(middleware.LogRequests)
srv.Echo.Use(middleware.SetIPAddress)
srv.Echo.Use(middleware.SetRequestTime)
srv.Echo.Use(middleware.LogRequests)
// Trace ID middleware generates a unique id for a request.
e.Use(echoMW.RequestIDWithConfig(echoMW.RequestIDConfig{
srv.Echo.Use(echoMW.RequestIDWithConfig(echoMW.RequestIDConfig{
RequestIDHandler: func(c echo.Context, requestID string) {
c.Set(echo.HeaderXRequestID, requestID)

ctx := context.WithValue(c.Request().Context(), keys.RequestID, requestID)
c.SetRequest(c.Request().WithContext(ctx))
},
}))
return e
}

// StartServer is function that registers start of http server in lifecycle
Expand Down

0 comments on commit c2a6d0f

Please sign in to comment.