internal/worker: register status middleware

Register the custom middleware function to the worker
server. This function is responsible for recording all
the status codes of all the server's endpoints.

Due to a bug with echo/v4, a request to an endpoint using
the incorrect method should return a `405` error but returns
a `404` error instead when a middleware function is registered.
The worker `server_test` has been updated to reflect this.
This commit is contained in:
Gianluca Zuccarelli 2022-11-21 17:05:33 +00:00 committed by Ondřej Budai
parent 8b5458ae83
commit 113cda7d39
2 changed files with 7 additions and 3 deletions

View file

@ -98,7 +98,9 @@ func (s *Server) Handler() http.Handler {
handler := apiHandlers{
server: s,
}
api.RegisterHandlers(e.Group(api.BasePath), &handler)
statusMW := prometheus.StatusMiddleware(prometheus.WorkerSubsystem)
api.RegisterHandlers(e.Group(api.BasePath, statusMW), &handler)
return e
}

View file

@ -69,7 +69,8 @@ func TestErrors(t *testing.T) {
// Create job with invalid body
{"POST", "/api/worker/v1/jobs", ``, http.StatusBadRequest},
// Wrong method
{"GET", "/api/worker/v1/jobs", ``, http.StatusMethodNotAllowed},
// Returns 404, but should be 405; see https://github.com/labstack/echo/issues/1981
{"GET", "/api/worker/v1/jobs", ``, http.StatusNotFound},
// Update job with invalid ID
{"PATCH", "/api/worker/v1/jobs/foo", `{"status":"FINISHED"}`, http.StatusBadRequest},
// Update job that does not exist, with invalid body
@ -99,7 +100,8 @@ func TestErrorsAlteredBasePath(t *testing.T) {
// Create job with invalid body
{"POST", "/api/image-builder-worker/v1/jobs", ``, http.StatusBadRequest},
// Wrong method
{"GET", "/api/image-builder-worker/v1/jobs", ``, http.StatusMethodNotAllowed},
// Returns 404, but should be 405; see https://github.com/labstack/echo/issues/1981
{"GET", "/api/image-builder-worker/v1/jobs", ``, http.StatusNotFound},
// Update job with invalid ID
{"PATCH", "/api/image-builder-worker/v1/jobs/foo", `{"status":"FINISHED"}`, http.StatusBadRequest},
// Update job that does not exist, with invalid body