From 7e83b40f9c4cd8f2d57274ead0cc044ad660715a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20S=C3=A1?= Date: Tue, 17 Apr 2018 12:36:47 +0200 Subject: [PATCH] POST-937 Move version to top level health check (#39) --- example.go | 22 ++---------------- ranger_http/health_check.go | 46 +++++++++++++++++++++++++++++++++---- version.json | 1 + 3 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 version.json diff --git a/example.go b/example.go index 27b38d3..042ab14 100644 --- a/example.go +++ b/example.go @@ -67,8 +67,8 @@ func main() { // /health/check/lb and /health/check // any instance of `func() ranger_http.HealthCheckService` sent as parameter of the configuration will be converted to JSON and printed // if necessary, you also can add a prefix to the endpoints with WithPrefix("/prefix") - // ex: WithHealthCheckFor(ranger_http.NewHealthCheckConfiguration(versionHealthCheck()).WithPrefix("/prefix")) - WithHealthCheckFor(ranger_http.NewHealthCheckConfiguration(versionHealthCheck(), etcdHealthCheck())) + // ex: WithHealthCheckFor(ranger_http.NewHealthCheckConfiguration(someHealthCheck()).WithPrefix("/prefix")) + WithHealthCheckFor(ranger_http.NewHealthCheckConfiguration(etcdHealthCheck()).WithVersion("version.json")) // add some endpoints. based on "github.com/julienschmidt/httprouter" s.GET("/hello", helloEndpoint()) @@ -109,24 +109,6 @@ func helloEndpoint() httprouter.Handle { } } -func versionHealthCheck() func() ranger_http.HealthCheckService { - type versionHealthCheck struct { - Tag string `json:"tag"` - Commit string `json:"commit"` - } - - return func() ranger_http.HealthCheckService { - return ranger_http.HealthCheckService{ - Name: "version", - Status: true, - Info: versionHealthCheck{ - Tag: "0.0.0", - Commit: "30ac4383d0260f517d7f171de244fa46c1879c67", - }, - } - } -} - func etcdHealthCheck() func() ranger_http.HealthCheckService { type etcdHealthCheck struct { ResponseTime int `json:"response_time"` diff --git a/ranger_http/health_check.go b/ranger_http/health_check.go index 2935801..98d41ed 100644 --- a/ranger_http/health_check.go +++ b/ranger_http/health_check.go @@ -3,10 +3,12 @@ package ranger_http import ( "encoding/json" "fmt" + "github.com/julienschmidt/httprouter" + "io/ioutil" "net/http" + "os" + "path/filepath" "time" - - "github.com/julienschmidt/httprouter" ) //HealthCheckService @@ -20,12 +22,19 @@ type HealthCheckService struct { type healthCheckConfiguration struct { Prefix string Services []func() HealthCheckService + Version healthCheckVersion +} + +type healthCheckVersion struct { + Tag string `json:"tag"` + Commit string `json:"commit"` } //NewHealthCheckConfiguration func NewHealthCheckConfiguration(services ...func() HealthCheckService) healthCheckConfiguration { return healthCheckConfiguration{ Services: services, + Version: healthCheckVersion{"n/a", "n/a"}, } } @@ -36,15 +45,39 @@ func (configuration healthCheckConfiguration) WithPrefix(prefix string) healthCh return configuration } +// WithVersion expects the path to a json file for healthCheckVersion +func (configuration healthCheckConfiguration) WithVersion(versionPath string) healthCheckConfiguration { + + version := healthCheckVersion{} + configuration.Version = version + + absPath, _ := filepath.Abs(versionPath) + fileBytes, err := ioutil.ReadFile(absPath) + + if err != nil { + logger.Warning("Unable to load file "+versionPath, nil) + } + err = json.Unmarshal(fileBytes, &version) + if err != nil { + logger.Warning("Error parsing version file "+versionPath, nil) + } + + configuration.Version = version + + return configuration +} + type healthCheckResponse struct { HTTPStatus int `json:"http-status"` Time float64 `json:"time"` Services map[string]interface{} `json:"checks"` + Version healthCheckVersion `json:"version"` + Host string `json:"host"` } //WithHealthCheckFor ... func (s Server) WithHealthCheckFor(configuration healthCheckConfiguration) Server { - s.GET(fmt.Sprintf("%s/health/check", configuration.Prefix), HealthCheckHandler(configuration.Services)) + s.GET(fmt.Sprintf("%s/health/check", configuration.Prefix), HealthCheckHandler(configuration)) s.GET(fmt.Sprintf("%s/health/check/lb", configuration.Prefix), HealthCheckHandlerLB()) return s } @@ -64,11 +97,14 @@ type healthCheckServiceResponse struct { } // HealthCheckHandler to check the service and external dependencies -func HealthCheckHandler(services []func() HealthCheckService) httprouter.Handle { +func HealthCheckHandler(configuration healthCheckConfiguration) httprouter.Handle { return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Cache-Control", "no-cache, private, max-age=0") + services := configuration.Services + hostname, _ := os.Hostname() + mapServices := make(map[string]interface{}) statusCode := http.StatusOK @@ -96,6 +132,8 @@ func HealthCheckHandler(services []func() HealthCheckService) httprouter.Handle HTTPStatus: statusCode, Time: ElapsedTimeSince(sAll), Services: mapServices, + Version: configuration.Version, + Host: hostname, }) } } diff --git a/version.json b/version.json new file mode 100644 index 0000000..b4943c9 --- /dev/null +++ b/version.json @@ -0,0 +1 @@ +{"tag": "this-is-tag", "commit": "this-is-commit"}