separate healthz from other routes

pull/2712/head
Brad Rydzewski 2019-05-21 13:29:58 -07:00
parent b52456f652
commit 346743176b
5 changed files with 33 additions and 10 deletions

View File

@ -20,6 +20,7 @@ import (
"github.com/drone/drone/cmd/drone-server/config" "github.com/drone/drone/cmd/drone-server/config"
"github.com/drone/drone/core" "github.com/drone/drone/core"
"github.com/drone/drone/handler/api" "github.com/drone/drone/handler/api"
"github.com/drone/drone/handler/health"
"github.com/drone/drone/handler/web" "github.com/drone/drone/handler/web"
"github.com/drone/drone/metric" "github.com/drone/drone/metric"
"github.com/drone/drone/operator/manager" "github.com/drone/drone/operator/manager"
@ -44,6 +45,7 @@ var serverSet = wire.NewSet(
manager.New, manager.New,
api.New, api.New,
web.New, web.New,
provideHealthz,
provideMetric, provideMetric,
provideRouter, provideRouter,
provideRPC, provideRPC,
@ -54,8 +56,9 @@ var serverSet = wire.NewSet(
// provideRouter is a Wire provider function that returns a // provideRouter is a Wire provider function that returns a
// router that is serves the provided handlers. // router that is serves the provided handlers.
func provideRouter(api api.Server, web web.Server, rpcv1 rpcHandlerV1, rpcv2 rpcHandlerV2, metrics *metric.Server) *chi.Mux { func provideRouter(api api.Server, web web.Server, rpcv1 rpcHandlerV1, rpcv2 rpcHandlerV2, healthz healthzHandler, metrics *metric.Server) *chi.Mux {
r := chi.NewRouter() r := chi.NewRouter()
r.Mount("/healthz", healthz)
r.Mount("/metrics", metrics) r.Mount("/metrics", metrics)
r.Mount("/api", api.Handler()) r.Mount("/api", api.Handler())
r.Mount("/rpc/v2", rpcv2) r.Mount("/rpc/v2", rpcv2)
@ -64,6 +67,13 @@ func provideRouter(api api.Server, web web.Server, rpcv1 rpcHandlerV1, rpcv2 rpc
return r return r
} }
// provideMetric is a Wire provider function that returns the
// healthcheck server.
func provideHealthz() healthzHandler {
v := health.New()
return healthzHandler(v)
}
// provideMetric is a Wire provider function that returns the // provideMetric is a Wire provider function that returns the
// metrics server exposing metrics in prometheus format. // metrics server exposing metrics in prometheus format.
func provideMetric(session core.Session, config config.Config) *metric.Server { func provideMetric(session core.Session, config config.Config) *metric.Server {

View File

@ -91,10 +91,11 @@ func InitializeApplication(config2 config.Config) (application, error) {
middleware := provideLogin(config2) middleware := provideLogin(config2)
options := provideServerOptions(config2) options := provideServerOptions(config2)
webServer := web.New(admissionService, buildStore, client, hookParser, coreLicense, licenseService, middleware, repositoryStore, session, syncer, triggerer, userStore, userService, webhookSender, options, system) webServer := web.New(admissionService, buildStore, client, hookParser, coreLicense, licenseService, middleware, repositoryStore, session, syncer, triggerer, userStore, userService, webhookSender, options, system)
handler := provideRPC(buildManager, config2) mainRpcHandlerV1 := provideRPC(buildManager, config2)
rpc2Server := provideRPC2(buildManager, config2) mainRpcHandlerV2 := provideRPC2(buildManager, config2)
mainHealthzHandler := provideHealthz()
metricServer := provideMetric(session, config2) metricServer := provideMetric(session, config2)
mux := provideRouter(server, webServer, handler, rpc2Server, metricServer) mux := provideRouter(server, webServer, mainRpcHandlerV1, mainRpcHandlerV2, mainHealthzHandler, metricServer)
serverServer := provideServer(mux, config2) serverServer := provideServer(mux, config2)
mainApplication := newApplication(cronScheduler, datadog, runner, serverServer, userStore) mainApplication := newApplication(cronScheduler, datadog, runner, serverServer, userStore)
return mainApplication, nil return mainApplication, nil

View File

@ -12,19 +12,32 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package web package health
import ( import (
"io" "io"
"net/http" "net/http"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
) )
// HandleHealthz creates an http.HandlerFunc that performs system // New returns a new health check router.
func New() http.Handler {
r := chi.NewRouter()
r.Use(middleware.Recoverer)
r.Use(middleware.NoCache)
r.Handle("/", Handler())
return r
}
// Handler creates an http.HandlerFunc that performs system
// healthchecks and returns 500 if the system is in an unhealthy state. // healthchecks and returns 500 if the system is in an unhealthy state.
func HandleHealthz() http.HandlerFunc { func Handler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200) w.WriteHeader(200)
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
io.WriteString(w, "OK") io.WriteString(w, "OK")
} }
} }

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by the Drone Non-Commercial License // Use of this source code is governed by the Drone Non-Commercial License
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
package web package health
import ( import (
"net/http/httptest" "net/http/httptest"
@ -13,7 +13,7 @@ func TestHandleHealthz(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/healthz", nil) r := httptest.NewRequest("GET", "/healthz", nil)
HandleHealthz().ServeHTTP(w, r) Handler().ServeHTTP(w, r)
if got, want := w.Code, 200; want != got { if got, want := w.Code, 200; want != got {
t.Errorf("Want response code %d, got %d", want, got) t.Errorf("Want response code %d, got %d", want, got)

View File

@ -102,7 +102,6 @@ func (s Server) Handler() http.Handler {
}) })
r.Get("/version", HandleVersion) r.Get("/version", HandleVersion)
r.Get("/healthz", HandleHealthz())
r.Get("/varz", HandleVarz(s.Client, s.License)) r.Get("/varz", HandleVarz(s.Client, s.License))
r.Handle("/login", r.Handle("/login",