Better logging of worker info
This commit is contained in:
parent
d880f7e7f0
commit
d3071146da
@ -89,7 +89,6 @@ func buildWebService(flamenco api.ServerInterface, persist api_impl.PersistenceS
|
|||||||
|
|
||||||
// Ensure panics when serving a web request won't bring down the server.
|
// Ensure panics when serving a web request won't bring down the server.
|
||||||
e.Use(middleware.Recover())
|
e.Use(middleware.Recover())
|
||||||
e.Use(api_impl.MiddleWareRequestLogger)
|
|
||||||
|
|
||||||
// Load the API definition and enable validation & authentication checks.
|
// Load the API definition and enable validation & authentication checks.
|
||||||
swagger, err := api.GetSwagger()
|
swagger, err := api.GetSwagger()
|
||||||
|
@ -21,8 +21,6 @@ package api_impl
|
|||||||
* ***** END GPL LICENSE BLOCK ***** */
|
* ***** END GPL LICENSE BLOCK ***** */
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
@ -34,28 +32,15 @@ const (
|
|||||||
loggerKey = loggerContextKey("logger")
|
loggerKey = loggerContextKey("logger")
|
||||||
)
|
)
|
||||||
|
|
||||||
// MiddleWareRequestLogger is Echo middleware that puts a Zerolog logger in the request context, for endpoints to use.
|
|
||||||
func MiddleWareRequestLogger(next echo.HandlerFunc) echo.HandlerFunc {
|
|
||||||
return func(c echo.Context) error {
|
|
||||||
remoteIP := c.RealIP()
|
|
||||||
logger := log.With().Str("remoteAddr", remoteIP).Logger()
|
|
||||||
ctx := context.WithValue(c.Request().Context(), loggerKey, logger)
|
|
||||||
c.SetRequest(c.Request().WithContext(ctx))
|
|
||||||
|
|
||||||
if err := next(c); err != nil {
|
|
||||||
c.Error(err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func requestLogger(e echo.Context) zerolog.Logger {
|
func requestLogger(e echo.Context) zerolog.Logger {
|
||||||
ctx := e.Request().Context()
|
logCtx := log.With().Str("remoteAddr", e.RealIP())
|
||||||
logger, ok := ctx.Value(loggerKey).(zerolog.Logger)
|
|
||||||
if ok {
|
worker := requestWorker(e)
|
||||||
return logger
|
if worker != nil {
|
||||||
|
logCtx = logCtx.
|
||||||
|
Str("wUUID", worker.UUID).
|
||||||
|
Str("wName", worker.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Error().Msg("no logger found in request context, returning default logger")
|
return logCtx.Logger()
|
||||||
return log.With().Logger()
|
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@ import (
|
|||||||
oapi_middle "github.com/deepmap/oapi-codegen/pkg/middleware"
|
oapi_middle "github.com/deepmap/oapi-codegen/pkg/middleware"
|
||||||
"github.com/getkin/kin-openapi/openapi3filter"
|
"github.com/getkin/kin-openapi/openapi3filter"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
|
"gitlab.com/blender/flamenco-ng-poc/internal/manager/persistence"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -77,3 +78,13 @@ func WorkerAuth(ctx context.Context, authInfo *openapi3filter.AuthenticationInpu
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// requestWorker returns the Worker associated with this HTTP request.
|
||||||
|
func requestWorker(e echo.Context) *persistence.Worker {
|
||||||
|
ctx := e.Request().Context()
|
||||||
|
worker, ok := ctx.Value(workerKey).(*persistence.Worker)
|
||||||
|
if ok {
|
||||||
|
return worker
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -88,7 +88,7 @@ func (f *Flamenco) SignOn(e echo.Context) error {
|
|||||||
return sendAPIError(e, http.StatusBadRequest, "invalid format")
|
return sendAPIError(e, http.StatusBadRequest, "invalid format")
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Info().Str("nickname", req.Nickname).Msg("worker signing on")
|
logger.Info().Msg("worker signing on")
|
||||||
|
|
||||||
return e.JSON(http.StatusOK, &api.WorkerStateChange{
|
return e.JSON(http.StatusOK, &api.WorkerStateChange{
|
||||||
// TODO: look up proper status in DB.
|
// TODO: look up proper status in DB.
|
||||||
@ -106,7 +106,7 @@ func (f *Flamenco) SignOff(e echo.Context) error {
|
|||||||
return sendAPIError(e, http.StatusBadRequest, "invalid format")
|
return sendAPIError(e, http.StatusBadRequest, "invalid format")
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Info().Str("nickname", req.Nickname).Msg("worker signing off")
|
logger.Info().Msg("worker signing off")
|
||||||
|
|
||||||
// TODO: store status in DB.
|
// TODO: store status in DB.
|
||||||
return e.String(http.StatusNoContent, "")
|
return e.String(http.StatusNoContent, "")
|
||||||
@ -135,6 +135,9 @@ func (f *Flamenco) WorkerStateChanged(e echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *Flamenco) ScheduleTask(e echo.Context) error {
|
func (f *Flamenco) ScheduleTask(e echo.Context) error {
|
||||||
|
logger := requestLogger(e)
|
||||||
|
logger.Info().Msg("worker requesting task")
|
||||||
|
|
||||||
return e.JSON(http.StatusOK, &api.AssignedTask{
|
return e.JSON(http.StatusOK, &api.AssignedTask{
|
||||||
Uuid: uuid.New().String(),
|
Uuid: uuid.New().String(),
|
||||||
Commands: []api.Command{
|
Commands: []api.Command{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user